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
首先利用過濾器過濾項目中的全部構件(類別可能還是不全,根據需求調整):
#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
導出時用隱藏模型,僅導出視圖可見模型:
#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
整個代碼爲:
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
}
}