Revit二次开发之“加载A项目中的族到B项目中”
start
using System.Text;
using System.Windows.Forms;
using Autodesk.Revit.UI;
using Autodesk.Revit.DB;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.UI.Selection;
using RevitApp = Autodesk.Revit.ApplicationServices;
namespace RevitCodes
{
[Transaction(TransactionMode.Manual)]
[Regeneration(RegenerationOption.Manual)]
//[Journaling(JournalingMode.NoCommandData)]
public class LoadFamilyFromOtherFile : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
UIDocument uiDoc = commandData.Application.ActiveUIDocument;
Autodesk.Revit.ApplicationServices.Application app = commandData.Application.Application;
//加载一个Revit文件
//Document DOC = commandData.Application.Application.OpenDocumentFile("D:\\Backup\\桌面\\项目1.rvt");
//遍历已经打开的Revit文件
Document myFamilyDoc = null;
foreach (Document doc in commandData.Application.Application.Documents)
{
if (doc.PathName == "D:\\Backup\\桌面\\加载万能窗.rvt")
{
//遍历找到族
FilteredElementCollector collector = new FilteredElementCollector(doc);
if (collector != null)
collector.OfCategory(BuiltInCategory.OST_Windows);
IList<Element> list = collector.ToElements();
foreach (Element e in list)
{
FamilySymbol fs = e as FamilySymbol;
if (fs.Family.Name == "万能窗")
{
myFamilyDoc = doc.EditFamily(fs.Family);
break;
}
}
}
}
//加载到当前项目中
if (myFamilyDoc != null)
uiDoc.Document.LoadFamily(myFamilyDoc);//只能是族文件才可以加载。即.rfa
return Result.Succeeded;
}
}
using System.Windows.Forms;
using Autodesk.Revit.UI;
using Autodesk.Revit.DB;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.UI.Selection;
using RevitApp = Autodesk.Revit.ApplicationServices;
namespace RevitCodes
{
[Transaction(TransactionMode.Manual)]
[Regeneration(RegenerationOption.Manual)]
//[Journaling(JournalingMode.NoCommandData)]
public class LoadFamilyFromOtherFile : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
UIDocument uiDoc = commandData.Application.ActiveUIDocument;
Autodesk.Revit.ApplicationServices.Application app = commandData.Application.Application;
//加载一个Revit文件
//Document DOC = commandData.Application.Application.OpenDocumentFile("D:\\Backup\\桌面\\项目1.rvt");
//遍历已经打开的Revit文件
Document myFamilyDoc = null;
foreach (Document doc in commandData.Application.Application.Documents)
{
if (doc.PathName == "D:\\Backup\\桌面\\加载万能窗.rvt")
{
//遍历找到族
FilteredElementCollector collector = new FilteredElementCollector(doc);
if (collector != null)
collector.OfCategory(BuiltInCategory.OST_Windows);
IList<Element> list = collector.ToElements();
foreach (Element e in list)
{
FamilySymbol fs = e as FamilySymbol;
if (fs.Family.Name == "万能窗")
{
myFamilyDoc = doc.EditFamily(fs.Family);
break;
}
}
}
}
//加载到当前项目中
if (myFamilyDoc != null)
uiDoc.Document.LoadFamily(myFamilyDoc);//只能是族文件才可以加载。即.rfa
return Result.Succeeded;
}
}
一个项目A中有族myFamiy,我想加载到另一个打开的项目B中,通过手工的方式可以实现。在A中把族另存为myFamily.rfa 文件,然后在B中加载族即可。
如何通过编程在一个命令中实现?
这个是可以用编程方式来实现。
下面是主要步骤。
方法1. 模仿手工步骤
1. 在项目A中,通过对象访问函数找到myFamily族
2. Document myFamilyDoc = A.EditFamily(myFamily);
3. 保存myFamilyDoc, myFamilyDoc.SaveAs(rfaFileName),
4. B中加载该文件, B.LoadFamily(rfaFileName).
方法2. 直接加载到B中,简单高效
1. 在项目A中,通过对象访问函数找到myFamily族
2. Document myFamilyDoc = A.EditFamily(myFamily);
3. B中加载该文件, B.LoadFamily(myFamilyDoc),或用另一个重载函数
可以通过Application.Documents 访问当前REvit中打开的多个文档。
下面是方法2的例子代码:
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
[TransactionAttribute(Autodesk.Revit.Attributes.TransactionMode.Manual)]
public class RevitCommand : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string messages, ElementSet elements)
{
UIDocument uiDoc = commandData.Application.ActiveUIDocument;
Autodesk.Revit.ApplicationServices.Application app = commandData.Application.Application;
Transaction trans = new Transaction(uiDoc.Document);
trans.Start("loadfamily");
Document myFamilyDoc = null;
foreach (Document doc in commandData.Application.Application.Documents)
{
if (doc.PathName.ToLower() == @"c:\yejo\revit\adn\userproject\1269157\a.rvt")
{
FilteredElementCollector collector = new FilteredElementCollector(doc);
if (collector != null)
collector.OfClass(typeof(Family));
IList<Element> list = collector.ToElements();
foreach (Element f in list)
{
Family family = f as Family;
if (family.Name == "M_Window-Square Opening")
{
myFamilyDoc = doc.EditFamily(family);
break;
}
}
}
}
//加载到当前项目中
if (myFamilyDoc != null)
myFamilyDoc.LoadFamily(uiDoc.Document);
trans.Commit();
return Result.Succeeded;
}
}
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
[TransactionAttribute(Autodesk.Revit.Attributes.TransactionMode.Manual)]
public class RevitCommand : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string messages, ElementSet elements)
{
UIDocument uiDoc = commandData.Application.ActiveUIDocument;
Autodesk.Revit.ApplicationServices.Application app = commandData.Application.Application;
Transaction trans = new Transaction(uiDoc.Document);
trans.Start("loadfamily");
Document myFamilyDoc = null;
foreach (Document doc in commandData.Application.Application.Documents)
{
if (doc.PathName.ToLower() == @"c:\yejo\revit\adn\userproject\1269157\a.rvt")
{
FilteredElementCollector collector = new FilteredElementCollector(doc);
if (collector != null)
collector.OfClass(typeof(Family));
IList<Element> list = collector.ToElements();
foreach (Element f in list)
{
Family family = f as Family;
if (family.Name == "M_Window-Square Opening")
{
myFamilyDoc = doc.EditFamily(family);
break;
}
}
}
}
//加载到当前项目中
if (myFamilyDoc != null)
myFamilyDoc.LoadFamily(uiDoc.Document);
trans.Commit();
return Result.Succeeded;
}
}
我这个博客废弃不用了,今天想寻找外链的时候,突然想到这个博客权重很高。
有需要免费外链的,留言即可,我准备把这个博客变成免费的友情链接站点。