Revit单构件导出IFC
对revit模型的应用中,由于模型过大,不得不进行模型拆分导出。本文针对模型单构件导出ifc,写了个小功能。revit支持只仅导出视图可见的模型,窗口操作如下图:
我们要用代码实现上述功能。
revit提供API用于导出IFC,是IFCExportOption,其中的AddOption(string name,string value)方法的参数name和value,是指导出option的name,以及相应的值,代码如下:
public static Result ExportToIfc(UIDocument uidoc, Transaction transaction, string name, string Path)
{
transaction.Start();
Result r = Result.Failed;
IFCExportOptions opt = new IFCExportOptions();
opt.ExportBaseQuantities = true;
// ExporterIFC exporter;
opt.FilterViewId = uidoc.ActiveView.Id;
// exporter.GetOptions();
opt.FileVersion = IFCVersion.IFC2x3;
opt.AddOption("ExportIFCCommonPropertySets", true.ToString());
opt.AddOption("UseActiveViewGeometry",true.ToString());
// opt.AddOption("ExportVisibleElementsInView",true.ToString());
// opt.AddOption("Export only elements visible in view", true.ToString());
opt.AddOption("TessellationLevelOfDetail", "0");
opt.AddOption("ActiveViewId", uidoc.ActiveView.Id.ToString());
uidoc.Document.Export(Path, name, opt);
transaction.Commit();
r = Result.Succeeded;
return r;
}
#endregion
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
首先利用过滤器过滤项目中的全部构件(类别可能还是不全,根据需求调整):
#region 过滤构件
//获取墙
FilteredElementCollector CollectionWall = new FilteredElementCollector(doc, viewId);
// ElementFilter filterWall = new ElementCategoryFilter(BuiltInCategory.OST_Walls);
ICollection<Element> eleWalls = CollectionWall.OfClass(typeof(Wall)).ToElements().ToList();
elementList.AddRange(eleWalls);
//风管
FilteredElementCollector CollectionDuct = new FilteredElementCollector(doc, viewId);
ICollection<Element> eleDucts = CollectionDuct.OfClass(typeof(Duct)).ToElements();
elementList.AddRange(eleDucts);
//获取自建族实例
FilteredElementCollector CollectionFamIns = new FilteredElementCollector(doc, viewId);
ICollection<Element> eleFamIns = CollectionFamIns.OfClass(typeof(FamilyInstance)).ToElements();
elementList.AddRange(eleFamIns);
//屋顶 roof
FilteredElementCollector CollectionRoof = new FilteredElementCollector(doc, viewId);
ICollection<Element> eleRoof = CollectionRoof.OfClass(typeof(RoofType)).ToElements();
elementList.AddRange(eleRoof);
//天花板 ceiling
FilteredElementCollector CollectionCeil = new FilteredElementCollector(doc, viewId);
ICollection<Element> eleCeil = CollectionCeil.OfClass(typeof(Ceiling)).ToElements();
elementList.AddRange(eleCeil);
//楼板 Floor
FilteredElementCollector CollectionFloor = new FilteredElementCollector(doc, viewId);
ICollection<Element> eleFloor = CollectionFloor.OfClass(typeof(Floor)).ToElements();
elementList.AddRange(eleFloor);
//管道 pipe
FilteredElementCollector CollectionPipe = new FilteredElementCollector(doc, viewId);
ICollection<Element> elePipe = CollectionPipe.OfClass(typeof(Pipe)).ToElements();
elementList.AddRange(elePipe);
//电缆桥架 Cabletray
FilteredElementCollector CollectionCabletray = new FilteredElementCollector(doc, viewId);
List<Element> eleCable = CollectionCabletray.OfClass(typeof(CableTray)).ToElements().ToList();
elementList.AddRange(eleCable);
//线管 conduit
FilteredElementCollector CollectionCond = new FilteredElementCollector(doc, viewId);
ICollection<Element> eleCond = CollectionCond.OfClass(typeof(Conduit)).ToElements();
elementList.AddRange(eleCond);
//楼梯 stair
//FilteredElementCollector CollectionStair = new FilteredElementCollector(doc, viewId);
//ICollection<Element> eleStair = CollectionStair.OfClass(typeof(Stairs)).ToElements();
//elementList.AddRange(eleStair);
//扶手 railing
FilteredElementCollector CollectionRail = new FilteredElementCollector(doc, viewId);
ICollection<Element> eleRail = CollectionRail.OfClass(typeof(Railing)).ToElements();
elementList.AddRange(eleRail);
//扶手顶端 toprailing
FilteredElementCollector CollectionTopRail = new FilteredElementCollector(doc, viewId);
ICollection<Element> eleTopRail = CollectionTopRail.OfClass(typeof(TopRail)).ToElements();
elementList.AddRange(eleTopRail);
//梁(beam和FamilySymbol类型)
FilteredElementCollector CollectionBeam = new FilteredElementCollector(doc, viewId);
ICollection<Element> eleBeam = CollectionBeam.OfClass(typeof(BeamSystem)).ToElements();
elementList.AddRange(eleBeam);
//幕墙竖梃
FilteredElementCollector CollectionMullion = new FilteredElementCollector(doc, viewId);
ICollection<Element> eleMullion = CollectionMullion.OfClass(typeof(MullionType)).ToElements();
elementList.AddRange(eleMullion);
//// 其他 family类型
FilteredElementCollector CollectionFamily = new FilteredElementCollector(doc);
ICollection<Element> eleFamily = CollectionRail.OfClass(typeof(Family)).ToElements();
elementList.AddRange(eleFamily);
////// 其他 Element
//FilteredElementCollector CollectionElement = new FilteredElementCollector(doc);
//ICollection<Element> eleElement = CollectionElement.OfClass(typeof(ElementType)).ToElements();
//elementList.AddRange(eleElement);
//TaskDialog.Show("提示", "Element:" + eleElement.Count);
#region Category针对性分类
//其他读不到的针对性分类element类型(楼梯)
FilteredElementCollector CollectionStairs = new FilteredElementCollector(doc, viewId);
ElementFilter StairsFilter = new ElementCategoryFilter(BuiltInCategory.OST_Stairs);
ICollection<Element> eleStairs = CollectionStairs.WherePasses(StairsFilter).ToElements();
elementList.AddRange(eleStairs);
//坡道
FilteredElementCollector CollectionRamps = new FilteredElementCollector(doc, viewId);
ElementFilter RampsFilter = new ElementCategoryFilter(BuiltInCategory.OST_Ramps);
ICollection<Element> eleRamps = CollectionRamps.WherePasses(RampsFilter).ToElements();
elementList.AddRange(eleRamps);
//洞口截面
FilteredElementCollector CollectionCeilingOpening = new FilteredElementCollector(doc, viewId);
ElementFilter CeilingOpeningFilter = new ElementCategoryFilter(BuiltInCategory.OST_CeilingOpening);
ICollection<Element> eleCeilingOpening = CollectionCeilingOpening.WherePasses(CeilingOpeningFilter).ToElements();
elementList.AddRange(eleCeilingOpening);
//Group类型
FilteredElementCollector CollectionGroup = new FilteredElementCollector(doc, viewId);
ElementFilter GroupFilter = new ElementCategoryFilter(BuiltInCategory.OST_IOSModelGroups);
ICollection<Element> eleGroup = CollectionGroup.WherePasses(GroupFilter).ToElements();
elementList.AddRange(eleGroup);
////Panel类型
//FilteredElementCollector CollectionPanel = new FilteredElementCollector(doc, viewId);
//ElementFilter PanelFilter = new ElementCategoryFilter(BuiltInCategory.OST_CurtainWallPanels);
//ICollection<Element> elePanel = CollectionPanel.WherePasses(PanelFilter).ToElements();
//elementList.AddRange(elePanel);
//TaskDialog.Show("提示", "panel:" + elePanel.Count);
#endregion
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
导出时用隐藏模型,仅导出视图可见模型:
#region 隐藏构件导出模型
Transaction tr = new Transaction(doc, "tr");
Transaction tran = new Transaction(doc, "tran");
View view = uidoc.ActiveView;
foreach (Element element in elementList)
{
List<ElementId> HideShowEleIds = new List<ElementId>();
tr.Start();
ElementId e = viewId;
foreach (ElementId elementId in elementIdList)
{
if (elementId == element.Id)
{
e = elementId;
}
else
{
HideShowEleIds.Add(elementId);
}
}
view.HideElements(HideShowEleIds);
string name = element.Id.ToString();
tr.Commit();
ExportToIfc(uidoc, tr, element.Id.ToString(), Path);
tran.Start();
view.UnhideElements(HideShowEleIds);
tran.Commit();
}
#endregion
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
整个代码为:
using System;
using System.Windows;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading.Tasks;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Architecture;
using Autodesk.Revit.DB.Electrical;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.DB.Plumbing;
using Autodesk.Revit.UI;
using Autodesk.Revit.DB.IFC;
using Autodesk.Revit.DB.ExternalService;
using Autodesk.Revit.DB.Events;
namespace IFCExport
{
[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
public class Class1 : IExternalCommand
{
#region
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
UIApplication uiapp = commandData.Application;
UIDocument uidoc = uiapp.ActiveUIDocument;
Document doc = uidoc.Document;
ElementId viewId = uidoc.ActiveView.Id;
List<Element> elementList = new List<Element>();
List<ElementId> elementIdList = new List<ElementId>();
string Path = "";
System.Windows.Forms.FolderBrowserDialog dialog = new System.Windows.Forms.FolderBrowserDialog();
dialog.Description = "请选择ifc的目标文件夹";
if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
if (string.IsNullOrEmpty(dialog.SelectedPath))
{
TaskDialog.Show("提示", "路径不能为空!");
}
else
{
Path = dialog.SelectedPath;
}
}
#region 过滤构件
//获取墙
FilteredElementCollector CollectionWall = new FilteredElementCollector(doc, viewId);
// ElementFilter filterWall = new ElementCategoryFilter(BuiltInCategory.OST_Walls);
ICollection<Element> eleWalls = CollectionWall.OfClass(typeof(Wall)).ToElements().ToList();
elementList.AddRange(eleWalls);
//风管
FilteredElementCollector CollectionDuct = new FilteredElementCollector(doc, viewId);
ICollection<Element> eleDucts = CollectionDuct.OfClass(typeof(Duct)).ToElements();
elementList.AddRange(eleDucts);
//获取自建族实例
FilteredElementCollector CollectionFamIns = new FilteredElementCollector(doc, viewId);
ICollection<Element> eleFamIns = CollectionFamIns.OfClass(typeof(FamilyInstance)).ToElements();
elementList.AddRange(eleFamIns);
//屋顶 roof
FilteredElementCollector CollectionRoof = new FilteredElementCollector(doc, viewId);
ICollection<Element> eleRoof = CollectionRoof.OfClass(typeof(RoofType)).ToElements();
elementList.AddRange(eleRoof);
//天花板 ceiling
FilteredElementCollector CollectionCeil = new FilteredElementCollector(doc, viewId);
ICollection<Element> eleCeil = CollectionCeil.OfClass(typeof(Ceiling)).ToElements();
elementList.AddRange(eleCeil);
//楼板 Floor
FilteredElementCollector CollectionFloor = new FilteredElementCollector(doc, viewId);
ICollection<Element> eleFloor = CollectionFloor.OfClass(typeof(Floor)).ToElements();
elementList.AddRange(eleFloor);
//管道 pipe
FilteredElementCollector CollectionPipe = new FilteredElementCollector(doc, viewId);
ICollection<Element> elePipe = CollectionPipe.OfClass(typeof(Pipe)).ToElements();
elementList.AddRange(elePipe);
//电缆桥架 Cabletray
FilteredElementCollector CollectionCabletray = new FilteredElementCollector(doc, viewId);
List<Element> eleCable = CollectionCabletray.OfClass(typeof(CableTray)).ToElements().ToList();
elementList.AddRange(eleCable);
//线管 conduit
FilteredElementCollector CollectionCond = new FilteredElementCollector(doc, viewId);
ICollection<Element> eleCond = CollectionCond.OfClass(typeof(Conduit)).ToElements();
elementList.AddRange(eleCond);
//楼梯 stair
//FilteredElementCollector CollectionStair = new FilteredElementCollector(doc, viewId);
//ICollection<Element> eleStair = CollectionStair.OfClass(typeof(Stairs)).ToElements();
//elementList.AddRange(eleStair);
//扶手 railing
FilteredElementCollector CollectionRail = new FilteredElementCollector(doc, viewId);
ICollection<Element> eleRail = CollectionRail.OfClass(typeof(Railing)).ToElements();
elementList.AddRange(eleRail);
//扶手顶端 toprailing
FilteredElementCollector CollectionTopRail = new FilteredElementCollector(doc, viewId);
ICollection<Element> eleTopRail = CollectionTopRail.OfClass(typeof(TopRail)).ToElements();
elementList.AddRange(eleTopRail);
//梁(beam和FamilySymbol类型)
FilteredElementCollector CollectionBeam = new FilteredElementCollector(doc, viewId);
ICollection<Element> eleBeam = CollectionBeam.OfClass(typeof(BeamSystem)).ToElements();
elementList.AddRange(eleBeam);
//幕墙竖梃
FilteredElementCollector CollectionMullion = new FilteredElementCollector(doc, viewId);
ICollection<Element> eleMullion = CollectionMullion.OfClass(typeof(MullionType)).ToElements();
elementList.AddRange(eleMullion);
//// 其他 family类型
FilteredElementCollector CollectionFamily = new FilteredElementCollector(doc);
ICollection<Element> eleFamily = CollectionRail.OfClass(typeof(Family)).ToElements();
elementList.AddRange(eleFamily);
////// 其他 Element
//FilteredElementCollector CollectionElement = new FilteredElementCollector(doc);
//ICollection<Element> eleElement = CollectionElement.OfClass(typeof(ElementType)).ToElements();
//elementList.AddRange(eleElement);
//TaskDialog.Show("提示", "Element:" + eleElement.Count);
#region Category针对性分类
//其他读不到的针对性分类element类型(楼梯)
FilteredElementCollector CollectionStairs = new FilteredElementCollector(doc, viewId);
ElementFilter StairsFilter = new ElementCategoryFilter(BuiltInCategory.OST_Stairs);
ICollection<Element> eleStairs = CollectionStairs.WherePasses(StairsFilter).ToElements();
elementList.AddRange(eleStairs);
//坡道
FilteredElementCollector CollectionRamps = new FilteredElementCollector(doc, viewId);
ElementFilter RampsFilter = new ElementCategoryFilter(BuiltInCategory.OST_Ramps);
ICollection<Element> eleRamps = CollectionRamps.WherePasses(RampsFilter).ToElements();
elementList.AddRange(eleRamps);
//洞口截面
FilteredElementCollector CollectionCeilingOpening = new FilteredElementCollector(doc, viewId);
ElementFilter CeilingOpeningFilter = new ElementCategoryFilter(BuiltInCategory.OST_CeilingOpening);
ICollection<Element> eleCeilingOpening = CollectionCeilingOpening.WherePasses(CeilingOpeningFilter).ToElements();
elementList.AddRange(eleCeilingOpening);
//Group类型
FilteredElementCollector CollectionGroup = new FilteredElementCollector(doc, viewId);
ElementFilter GroupFilter = new ElementCategoryFilter(BuiltInCategory.OST_IOSModelGroups);
ICollection<Element> eleGroup = CollectionGroup.WherePasses(GroupFilter).ToElements();
elementList.AddRange(eleGroup);
////Panel类型
//FilteredElementCollector CollectionPanel = new FilteredElementCollector(doc, viewId);
//ElementFilter PanelFilter = new ElementCategoryFilter(BuiltInCategory.OST_CurtainWallPanels);
//ICollection<Element> elePanel = CollectionPanel.WherePasses(PanelFilter).ToElements();
//elementList.AddRange(elePanel);
//TaskDialog.Show("提示", "panel:" + elePanel.Count);
#endregion
#endregion
//遍历element
foreach (var ele in elementList)
{
// FamilyInstance fam = ele as FamilyInstance;
if (ele != null && !(ele is Panel))
{
elementIdList.Add(ele.Id);
}
}
#region 隐藏构件导出模型
Transaction tr = new Transaction(doc, "tr");
Transaction tran = new Transaction(doc, "tran");
Transaction transaction = new Transaction(doc,"delect");
View view = uidoc.ActiveView;
foreach (Element element in elementList)
{
List<ElementId> HideShowEleIds = new List<ElementId>();
tr.Start();
ElementId e = viewId;
foreach (ElementId elementId in elementIdList)
{
if (elementId == element.Id)
{
e = elementId;
}
else
{
HideShowEleIds.Add(elementId);
}
}
view.HideElements(HideShowEleIds);
string name = element.Id.ToString();
tr.Commit();
ExportToIfc(uidoc, tr, element.Id.ToString(), Path);
tran.Start();
view.UnhideElements(HideShowEleIds);
tran.Commit();
transaction.Start();
elementIdList.Remove(element.Id);
doc.Delete(element.Id);
transaction.Commit();
}
#endregion
return Result.Succeeded;
}
#endregion
#region exportToifc
public static Result ExportToIfc(UIDocument uidoc, Transaction transaction, string name, string Path)
{
transaction.Start();
Result r = Result.Failed;
IFCExportOptions opt = new IFCExportOptions();
opt.ExportBaseQuantities = true;
// ExporterIFC exporter;
opt.FilterViewId = uidoc.ActiveView.Id;
// exporter.GetOptions();
opt.FileVersion = IFCVersion.IFC2x3;
opt.AddOption("ExportIFCCommonPropertySets", true.ToString());
opt.AddOption("UseActiveViewGeometry",true.ToString());
// opt.AddOption("ExportVisibleElementsInView",true.ToString());
// opt.AddOption("Export only elements visible in view", true.ToString());
opt.AddOption("TessellationLevelOfDetail", "0");
opt.AddOption("ActiveViewId", uidoc.ActiveView.Id.ToString());
uidoc.Document.Export(Path, name, opt);
transaction.Commit();
r = Result.Succeeded;
return r;
}
#endregion
}
}
————————————————
版权声明:本文为CSDN博主「李小喵Y」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_36266612/article/details/85047457
来源:CSDN
作者:星野之鹰001
链接:https://blog.csdn.net/gloryeagle/article/details/104790565