Revit单构件导出IFC

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 
    }
}

### 回答1: IFCPlusPlus是一个开源的C++库,用于解析和处理IFC(Industry Foundation Classes)模型。IFC是用于构建信息模型(BIM)的开放标准,用于描述建筑、工程和设施管理领域的数据。IFCPlusPlus库通过提供一组功能强大的工具和函数,使开发人员能够轻松地解析、读取和编辑IFC模型。 IFCPlusPlus库使用了面向对象的编程技术,将IFC模型的实体、属性和关系表示为C++类和对象。开发人员可以使用这些类和对象来访问和操作模型的各个方面,包括构件构件类型、属性等。IFCPlusPlus还提供了一些方便的函数,用于查询和遍历模型的数据。 通过IFCPlusPlus库,开发人员可以轻松地解析和读取IFC文件,获取模型的各个方面的信息。开发人员可以检索构件的几何数据、属性、关系等,并根据需要对其进行编辑或分析。此外,IFCPlusPlus库还提供了功能强大的几何计算和处理工具,用于进行坐标转换、空间关系计算、碰撞检测等。 总之,IFCPlusPlus是一个强大的工具,用于解析和处理IFC模型。它提供了丰富的功能和便捷的接口,使开发人员能够高效地读取和操作IFC模型的数据,从而实现各种应用需求,如可视化、仿真、数据分析等。 ### 回答2: ifcplusplus是一个用于解析和处理IFC(Industry Foundation Classes)模型的开源库。IFC是一种用于数字化建筑信息模型(BIM)的开放国际标准,它描述了建筑和基础设施项目中的各种物理和逻辑结构。 ifcplusplus库提供了一种方便的方式来解析和访问IFC模型中的数据。它包含了一组函数和类,可以读取IFC文件,并将其转换为内部数据结构,例如对象和属性。通过使用ifcplusplus,开发人员可以轻松地从IFC模型中提取所需的信息,并在应用程序中进行处理和分析。 ifcplusplus库还支持IFC模型的更新和修改。它提供了一组函数和方法,可以直接在IFC模型中进行添加、删除和修改对象和属性。这使得开发人员能够轻松地对IFC模型进行编辑和更新,以满足应用程序的需求。 总之,ifcplusplus是一个强大的工具,用于解析和处理IFC模型。它提供了易于使用的API,使开发人员能够轻松地读取、编辑和分析IFC模型中的数据。通过使用ifcplusplus,开发人员可以更好地利用IFC标准,加快BIM应用程序的开发速度,并提供更好的用户体验。 ### 回答3: ifcplusplus是一个用于解析和处理Industry Foundation Classes (IFC)模型的开源库。IFC是一个国际标准,用于描述建筑和基础设施的信息模型。ifcplusplus提供了一套API和工具,可以在各种平台上解析和操作IFC模型。 ifcplusplus具有以下主要特点和功能: 1. 解析和加载:ifcplusplus可以读取IFC文件,并将其转换为内部数据结构,以便进行后续操作。它支持多种IFC版本和文件格式,包括IFC2x3和IFC4。 2. 数据查询:ifcplusplus提供了一套简易用的API,用于查询IFC模型中的实体和属性。用户可以根据特定的搜索条件,获取所需的信息,如构件的几何数据、属性值、关系等。 3. 几何处理:ifcplusplus支持对IFC模型中的几何数据进行处理。它可以提取构件的几何信息,如坐标、形状、尺寸等,并支持几何操作,如转换、合并、拆分等。 4. 数据修改:ifcplusplus允许用户对IFC模型进行修改和更新。用户可以添加、删除、修改实体的属性和关系,以满足特定的需求。 5. 标准兼容:ifcplusplus遵循IFC标准,并且不断更新以支持最新的IFC版本和扩展。它与其他IFC工具和库具有良好的兼容性,可以与其无缝集成。 总之,ifcplusplus是一个功能强大且易于使用的工具,可以帮助开发者解析和处理IFC模型。它为构建基于IFC的应用程序和工具提供了基础,有助于提高建筑和基础设施领域的信息交流和协作效率。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值