xBIM 基础05 3D墙案例

  • 2019 年 10 月 4 日
  • 筆記

  使用编码的形式去生成一堵墙的模型需要做很多的工作。

  1 using System;    2 using System.Collections.Generic;    3 using System.Diagnostics;    4 using System.Linq;    5 using Xbim.Common;    6 using Xbim.Common.Step21;    7 using Xbim.Ifc;    8 using Xbim.IO;    9 using Xbim.Ifc4.ActorResource;   10 using Xbim.Ifc4.DateTimeResource;   11 using Xbim.Ifc4.ExternalReferenceResource;   12 using Xbim.Ifc4.PresentationOrganizationResource;   13 using Xbim.Ifc4.GeometricConstraintResource;   14 using Xbim.Ifc4.GeometricModelResource;   15 using Xbim.Ifc4.GeometryResource;   16 using Xbim.Ifc4.Interfaces;   17 using Xbim.Ifc4.Kernel;   18 using Xbim.Ifc4.MaterialResource;   19 using Xbim.Ifc4.MeasureResource;   20 using Xbim.Ifc4.ProductExtension;   21 using Xbim.Ifc4.ProfileResource;   22 using Xbim.Ifc4.PropertyResource;   23 using Xbim.Ifc4.QuantityResource;   24 using Xbim.Ifc4.RepresentationResource;   25 using Xbim.Ifc4.SharedBldgElements;   26   27   28 namespace HelloWall   29 {   30     class Program   31     {   32         /// <summary>   33         ///  此示例演示了创建包含单个标准事例墙的符合IFC模型的最小步骤。   34         /// </summary>   35         static int Main()   36         {   37             // 首先创建并初始化一个名为hello wall的模型   38             Console.WriteLine("Initialising the IFC Project 初始化 IFc 项目....");   39             using (var model = CreateandInitModel("HelloWall"))   40             {   41                 if (model != null)   42                 {   43                     IfcBuilding building = CreateBuilding(model, "Default Building 默认建筑");   44                     IfcBuildingStorey storey = CreateStorey(building);   45                     IfcWallStandardCase wall = CreateWall(model, 4000, 300, 2400);   46   47                     if(wall != null)   48                     {   49                         AddPropertiesToWall(model, wall);   50                     }   51                     using (var txn = model.BeginTransaction("Add Wall 添加墙"))   52                     {   53                         building.AddToSpatialDecomposition(storey);   54                         storey.AddElement(wall);   55                         txn.Commit();   56                     }   57   58                     if (wall != null)   59                     {   60                         try   61                         {   62                             Console.WriteLine("Standard Wall successfully created....");   63   64                             model.SaveAs("HelloWallIfc4.ifc", StorageType.Ifc); // 保存到文件中   65   66                             Console.WriteLine("HelloWallIfc4.ifc has been successfully written");   67                         }   68                         catch (Exception e)   69                         {   70                             Console.WriteLine("Failed to save HelloWall.ifc");   71                             Console.WriteLine(e.Message);   72                         }   73                     }   74                 }   75                 else   76                 {   77                     Console.WriteLine("Failed to initialise the model");   78                 }   79             }   80             Console.WriteLine("Press any key to exit to view the IFC file....");   81             Console.ReadKey();   82             LaunchNotepad("HelloWallIfc4.ifc");   83             return 0;   84         }   85   86         private static IfcBuildingStorey CreateStorey(IfcBuilding building)   87         {   88             var model = building.Model;   89             IfcBuildingStorey storey;   90             using (var txn = model.BeginTransaction("Storey creation"))   91             {   92                 storey = model.Instances.New<IfcBuildingStorey>(s =>   93                 {   94                     s.Name = "Default storey";   95                     s.Elevation = 0.0;   96                 });   97                 txn.Commit();   98             }   99             return storey;  100         }  101  102         /// <summary>  103         ///  在记事本中打开指定的文件  104         /// </summary>  105         /// <param name="fileName"></param>  106         private static void LaunchNotepad(string fileName)  107         {  108             try  109             {  110                 var p = new Process { StartInfo = { FileName = fileName, CreateNoWindow = false } };  111                 p.Start();  112             }  113             catch (Exception ex)  114             {  115                 Console.WriteLine("Exception Occurred :{0},{1}", ex.Message, ex.StackTrace);  116             }  117         }  118  119         /// <summary>  120         ///  创建建筑对象  121         /// </summary>  122         /// <param name="model"></param>  123         /// <param name="name">建筑名称</param>  124         /// <returns></returns>  125         private static IfcBuilding CreateBuilding(IfcStore model, string name)  126         {  127             using (var txn = model.BeginTransaction("Create Building 创建建筑对象"))  128             {  129                 var building = model.Instances.New<IfcBuilding>();  130                 building.Name = name;  131                 building.CompositionType = IfcElementCompositionEnum.ELEMENT; // 组成类型  132  133                 var localPlacement = model.Instances.New<IfcLocalPlacement>();  134                 building.ObjectPlacement = localPlacement;  135  136                 var placement = model.Instances.New<IfcAxis2Placement3D>();  137                 localPlacement.RelativePlacement = placement;  138                 placement.Location = model.Instances.New<IfcCartesianPoint>(p => p.SetXYZ(0, 0, 0));  139  140                 // 获取第一个或者默认的项目  141                 var project = model.Instances.OfType<IfcProject>().FirstOrDefault();  142                 if(project != null)  143                 {  144                     project.AddBuilding(building);  145                 }  146  147                 txn.Commit();  148  149                 return building;  150             }  151         }  152  153         /// <summary>  154         ///  设置任何模型必须提供的基本参数、单位、所有权等  155         /// </summary>  156         /// <param name="projectName">项目名称</param>  157         /// <returns></returns>  158         private static IfcStore CreateandInitModel(string projectName)  159         {  160             // 首先,我们需要为新模型中的数据所有权设置一些凭证  161             var credentials = new XbimEditorCredentials  162             {  163                 ApplicationDevelopersName = "xBimTeam",  164                 ApplicationFullName = "Hello Wall Application",  165                 ApplicationIdentifier = "HelloWall.exe",  166                 ApplicationVersion = "1.0",  167                 EditorsFamilyName = "Team",  168                 EditorsGivenName = "xBIM",  169                 EditorsOrganisationName = "xBimTeam"  170             };  171  172             // 现在我们可以创建一个ifcstore,它是ifc4格式的,将保存在内存中而不是数据库中。  173             // 如果模型的容量大于50MB,或者需要健壮的事务,那么数据库在性能方面通常更好。  174  175             var model = IfcStore.Create(credentials, XbimSchemaVersion.Ifc4, XbimStoreType.InMemoryModel);  176  177             // 开始事务,因为对模型的所有更改都是ACID  178             using (var txn = model.BeginTransaction("Initialise Model 初始化模型"))  179             {  180                 var project = model.Instances.New<IfcProject>(); // 创建一个项目  181                 project.Initialize(ProjectUnits.SIUnitsUK); // 将单位设置为si(mm和米)  182                 project.Name = projectName;  183  184                 txn.Commit(); // 现在提交更改,否则它们将在using语句的范围结束时回滚。  185             }  186  187             return model;  188         }  189  190         /// <summary>  191         ///  创建一个墙和它的几何图形,许多几何图形表示是可能的,并选择挤压矩形示意图,因为这通常用于标准情况下的墙。  192         /// </summary>  193         /// <param name="model"></param>  194         /// <param name="length">矩形足迹的长度</param>  195         /// <param name="width">矩形足迹的宽度(墙的宽度)</param>  196         /// <param name="height">挤出墙的高度,挤出是垂直的</param>  197         /// <returns></returns>  198         static private IfcWallStandardCase CreateWall(IfcStore model, double length, double width, double height)  199         {  200             using (var txn = model.BeginTransaction("Create Wall 创建墙"))  201             {  202                 var wall = model.Instances.New<IfcWallStandardCase>(); //IfcWallStandardCase:IFC墙标准案例  203                 wall.Name = "A Standard rectangular wall 标准矩形墙";  204  205                 // 将墙表示为矩形轮廓,墙的矩形剖面  206                 var rectProf = model.Instances.New<IfcRectangleProfileDef>(); //IfcRectangleProfileDef:IFC矩形轮廓定义  207                 rectProf.ProfileType = IfcProfileTypeEnum.AREA;  208                 rectProf.XDim = width;  209                 rectProf.YDim = length;  210  211                 var insertPoint = model.Instances.New<IfcCartesianPoint>(); //IfcCartesianPoint:IFc 笛卡尔点  212                 insertPoint.SetXY(0, 400); //在任意位置插入  213                 rectProf.Position = model.Instances.New<IfcAxis2Placement2D>();  214                 rectProf.Position.Location = insertPoint;  215  216                 // 模型区域实心  217                 var body = model.Instances.New<IfcExtrudedAreaSolid>(); //IfcExtrudedAreaSolid:IFC拉伸区域实体  218                 body.Depth = height;  219                 body.SweptArea = rectProf;  220                 body.ExtrudedDirection = model.Instances.New<IfcDirection>();//IfcDirection:IFC方向  221                 body.ExtrudedDirection.SetXYZ(0, 0, 1);  222  223                 // 在模型中插入几何参数  224                 var origin = model.Instances.New<IfcCartesianPoint>(); //IfcCartesianPoint:IFc 笛卡尔点  225                 origin.SetXYZ(0, 0, 0);  226                 body.Position = model.Instances.New<IfcAxis2Placement3D>();  227                 body.Position.Location = origin;  228  229                 // 创建定义形状以保存几何图形  230                 var shape = model.Instances.New<IfcShapeRepresentation>();//IfcShapeRepresentation:IFC形状表示  231                 var modelContext = model.Instances.OfType<IfcGeometricRepresentationContext>().FirstOrDefault(); //IfcGeometricRepresentationContext:IFC几何表示上下文  232                 shape.ContextOfItems = modelContext;  233                 shape.RepresentationType = "SweptSolid";  234                 shape.RepresentationIdentifier = "Body";  235                 shape.Items.Add(body);  236  237                 // 创建产品定义并将模型几何图形添加到墙中  238                 var rep = model.Instances.New<IfcProductDefinitionShape>(); //IfcProductDefinitionShape:IFC产品定义形状  239                 rep.Representations.Add(shape);  240                 wall.Representation = rep;  241  242                 // 现在将墙放置到模型中  243                 var lp = model.Instances.New<IfcLocalPlacement>();  244                 var ax3D = model.Instances.New<IfcAxis2Placement3D>();  245                 ax3D.Location = origin;  246                 ax3D.RefDirection = model.Instances.New<IfcDirection>();  247                 ax3D.RefDirection.SetXYZ(0, 1, 0);  248                 ax3D.Axis = model.Instances.New<IfcDirection>();  249                 ax3D.Axis.SetXYZ(0, 0, 1);  250                 lp.RelativePlacement = ax3D;  251                 wall.ObjectPlacement = lp;  252  253                 // Where Clause: ifcwallstandardard 依赖于 ifcmmateriallayersetusage 的规定。  254                 var ifcMaterialLayerSetUsage = model.Instances.New<IfcMaterialLayerSetUsage>();  255                 var ifcMaterialLayerSet = model.Instances.New<IfcMaterialLayerSet>();  256                 var ifcMaterialLayer = model.Instances.New<IfcMaterialLayer>();  257                 ifcMaterialLayer.LayerThickness = 10;  258                 ifcMaterialLayerSet.MaterialLayers.Add(ifcMaterialLayer);  259                 ifcMaterialLayerSetUsage.ForLayerSet = ifcMaterialLayerSet;  260                 ifcMaterialLayerSetUsage.LayerSetDirection = IfcLayerSetDirectionEnum.AXIS2;  261                 ifcMaterialLayerSetUsage.DirectionSense = IfcDirectionSenseEnum.NEGATIVE;  262                 ifcMaterialLayerSetUsage.OffsetFromReferenceLine = 150;  263  264                 // 将材料添加到墙商  265                 var material = model.Instances.New<IfcMaterial>();  266                 material.Name = "some material";  267                 var ifcRelAssociatesMaterial = model.Instances.New<IfcRelAssociatesMaterial>();  268                 ifcRelAssociatesMaterial.RelatingMaterial = material;  269                 ifcRelAssociatesMaterial.RelatedObjects.Add(wall);  270  271                 ifcRelAssociatesMaterial.RelatingMaterial = ifcMaterialLayerSetUsage;  272  273                 // ifcPresentationLayerAssignment 对于 ifcwall 或 ifcwallstandardcase 中的 CAD 演示是必须的  274                 var ifcPresentationLayerAssignment = model.Instances.New<IfcPresentationLayerAssignment>();  275                 ifcPresentationLayerAssignment.Name = "some ifcPresentationLayerAssignment";  276                 ifcPresentationLayerAssignment.AssignedItems.Add(shape);  277  278  279                 // 如果 IfcPolyline 具有两个点,则对于 IfcWall 是必需的  280                 var ifcPolyline = model.Instances.New<IfcPolyline>();  281                 var startPoint = model.Instances.New<IfcCartesianPoint>();  282                 startPoint.SetXY(0, 0);  283                 var endPoint = model.Instances.New<IfcCartesianPoint>();  284                 endPoint.SetXY(4000, 0);  285                 ifcPolyline.Points.Add(startPoint);  286                 ifcPolyline.Points.Add(endPoint);  287  288                 var shape2D = model.Instances.New<IfcShapeRepresentation>();  289                 shape2D.ContextOfItems = modelContext;  290                 shape2D.RepresentationIdentifier = "Axis";  291                 shape2D.RepresentationType = "Curve2D";  292                 shape2D.Items.Add(ifcPolyline);  293                 rep.Representations.Add(shape2D);  294                 txn.Commit();  295  296                 return wall;  297             }  298         }  299  300         /// <summary>  301         ///  向墙添加一些属性,  302         /// </summary>  303         /// <param name="model">XbimModel</param>  304         /// <param name="wall"></param>  305         static private void AddPropertiesToWall(IfcStore model, IfcWallStandardCase wall)  306         {  307             using (var txn = model.BeginTransaction("Create Wall"))  308             {  309                 CreateElementQuantity(model, wall);  310                 CreateSimpleProperty(model, wall);  311                 txn.Commit();  312             }  313         }  314  315         private static void CreateSimpleProperty(IfcStore model, IfcWallStandardCase wall)  316         {  317             var ifcPropertySingleValue = model.Instances.New<IfcPropertySingleValue>(psv =>  318             {  319                 psv.Name = "IfcPropertySingleValue:Time";  320                 psv.Description = "";  321                 psv.NominalValue = new IfcTimeMeasure(150.0);  322                 psv.Unit = model.Instances.New<IfcSIUnit>(siu =>  323                 {  324                     siu.UnitType = IfcUnitEnum.TIMEUNIT;  325                     siu.Name = IfcSIUnitName.SECOND;  326                 });  327             });  328             var ifcPropertyEnumeratedValue = model.Instances.New<IfcPropertyEnumeratedValue>(pev =>  329             {  330                 pev.Name = "IfcPropertyEnumeratedValue:Music";  331                 pev.EnumerationReference = model.Instances.New<IfcPropertyEnumeration>(pe =>  332                     {  333                         pe.Name = "Notes";  334                         pe.EnumerationValues.Add(new IfcLabel("Do"));  335                         pe.EnumerationValues.Add(new IfcLabel("Re"));  336                         pe.EnumerationValues.Add(new IfcLabel("Mi"));  337                         pe.EnumerationValues.Add(new IfcLabel("Fa"));  338                         pe.EnumerationValues.Add(new IfcLabel("So"));  339                         pe.EnumerationValues.Add(new IfcLabel("La"));  340                         pe.EnumerationValues.Add(new IfcLabel("Ti"));  341                     });  342                 pev.EnumerationValues.Add(new IfcLabel("Do"));  343                 pev.EnumerationValues.Add(new IfcLabel("Re"));  344                 pev.EnumerationValues.Add(new IfcLabel("Mi"));  345  346             });  347             var ifcPropertyBoundedValue = model.Instances.New<IfcPropertyBoundedValue>(pbv =>  348             {  349                 pbv.Name = "IfcPropertyBoundedValue:Mass";  350                 pbv.Description = "";  351                 pbv.UpperBoundValue = new IfcMassMeasure(5000.0);  352                 pbv.LowerBoundValue = new IfcMassMeasure(1000.0);  353                 pbv.Unit = model.Instances.New<IfcSIUnit>(siu =>  354                 {  355                     siu.UnitType = IfcUnitEnum.MASSUNIT;  356                     siu.Name = IfcSIUnitName.GRAM;  357                     siu.Prefix = IfcSIPrefix.KILO;  358                 });  359             });  360  361             var definingValues = new List<IfcReal> { new IfcReal(100.0), new IfcReal(200.0), new IfcReal(400.0),                                                           new IfcReal(800.0), new IfcReal(1600.0), new IfcReal(3200.0), };  362             var definedValues = new List<IfcReal> { new IfcReal(20.0), new IfcReal(42.0), new IfcReal(46.0),                                                          new IfcReal(56.0), new IfcReal(60.0), new IfcReal(65.0), };  363             var ifcPropertyTableValue = model.Instances.New<IfcPropertyTableValue>(ptv =>  364             {  365                 ptv.Name = "IfcPropertyTableValue:Sound";  366                 foreach (var item in definingValues)  367                 {  368                     ptv.DefiningValues.Add(item);  369                 }  370                 foreach (var item in definedValues)  371                 {  372                     ptv.DefinedValues.Add(item);  373                 }  374                 ptv.DefinedUnit = model.Instances.New<IfcContextDependentUnit>(cd =>  375                 {  376                     cd.Dimensions = model.Instances.New<IfcDimensionalExponents>(de =>  377                     {  378                         de.LengthExponent = 0;  379                         de.MassExponent = 0;  380                         de.TimeExponent = 0;  381                         de.ElectricCurrentExponent = 0;  382                         de.ThermodynamicTemperatureExponent = 0;  383                         de.AmountOfSubstanceExponent = 0;  384                         de.LuminousIntensityExponent = 0;  385                     });  386                     cd.UnitType = IfcUnitEnum.FREQUENCYUNIT;  387                     cd.Name = "dB";  388                 });  389  390  391             });  392  393             var listValues = new List<IfcLabel> { new IfcLabel("Red"),                                                        new IfcLabel("Green"),                                                        new IfcLabel("Blue"),                                                        new IfcLabel("Pink"),                                                        new IfcLabel("White"),                                                        new IfcLabel("Black"), };  394             var ifcPropertyListValue = model.Instances.New<IfcPropertyListValue>(plv =>  395             {  396                 plv.Name = "IfcPropertyListValue:Colours";  397                 foreach (var item in listValues)  398                 {  399                     plv.ListValues.Add(item);  400                 }  401             });  402  403             var ifcMaterial = model.Instances.New<IfcMaterial>(m =>  404             {  405                 m.Name = "Brick";  406             });  407             var ifcPrValueMaterial = model.Instances.New<IfcPropertyReferenceValue>(prv =>  408             {  409                 prv.Name = "IfcPropertyReferenceValue:Material";  410                 prv.PropertyReference = ifcMaterial;  411             });  412  413  414             var ifcMaterialList = model.Instances.New<IfcMaterialList>(ml =>  415                 {  416                     ml.Materials.Add(ifcMaterial);  417                     ml.Materials.Add(model.Instances.New<IfcMaterial>(m => { m.Name = "Cavity"; }));  418                     ml.Materials.Add(model.Instances.New<IfcMaterial>(m => { m.Name = "Block"; }));  419                 });  420  421  422             var ifcMaterialLayer = model.Instances.New<IfcMaterialLayer>(ml =>  423             {  424                 ml.Material = ifcMaterial;  425                 ml.LayerThickness = 100.0;  426             });  427             var ifcPrValueMatLayer = model.Instances.New<IfcPropertyReferenceValue>(prv =>  428             {  429                 prv.Name = "IfcPropertyReferenceValue:MaterialLayer";  430                 prv.PropertyReference = ifcMaterialLayer;  431             });  432  433             var ifcDocumentReference = model.Instances.New<IfcDocumentReference>(dr =>  434             {  435                 dr.Name = "Document";  436                 dr.Location = "c://Documents//TheDoc.Txt";  437             });  438             var ifcPrValueRef = model.Instances.New<IfcPropertyReferenceValue>(prv =>  439             {  440                 prv.Name = "IfcPropertyReferenceValue:Document";  441                 prv.PropertyReference = ifcDocumentReference;  442             });  443  444             var ifcTimeSeries = model.Instances.New<IfcRegularTimeSeries>(ts =>  445             {  446                 ts.Name = "Regular Time Series";  447                 ts.Description = "Time series of events";  448                 ts.StartTime = new IfcDateTime("2015-02-14T12:01:01");  449                 ts.EndTime = new IfcDateTime("2015-05-15T12:01:01");  450                 ts.TimeSeriesDataType = IfcTimeSeriesDataTypeEnum.CONTINUOUS;  451                 ts.DataOrigin = IfcDataOriginEnum.MEASURED;  452                 ts.TimeStep = 604800; //7 days in secs  453             });  454  455             var ifcPrValueTimeSeries = model.Instances.New<IfcPropertyReferenceValue>(prv =>  456             {  457                 prv.Name = "IfcPropertyReferenceValue:TimeSeries";  458                 prv.PropertyReference = ifcTimeSeries;  459             });  460  461             var ifcAddress = model.Instances.New<IfcPostalAddress>(a =>  462             {  463                 a.InternalLocation = "Room 101";  464                 a.AddressLines.AddRange(new[] { new IfcLabel("12 New road"), new IfcLabel("DoxField") });  465                 a.Town = "Sunderland";  466                 a.PostalCode = "DL01 6SX";  467             });  468             var ifcPrValueAddress = model.Instances.New<IfcPropertyReferenceValue>(prv =>  469             {  470                 prv.Name = "IfcPropertyReferenceValue:Address";  471                 prv.PropertyReference = ifcAddress;  472             });  473             var ifcTelecomAddress = model.Instances.New<IfcTelecomAddress>(a =>  474             {  475                 a.TelephoneNumbers.Add(new IfcLabel("01325 6589965"));  476                 a.ElectronicMailAddresses.Add(new IfcLabel("[email protected]"));  477             });  478             var ifcPrValueTelecom = model.Instances.New<IfcPropertyReferenceValue>(prv =>  479             {  480                 prv.Name = "IfcPropertyReferenceValue:Telecom";  481                 prv.PropertyReference = ifcTelecomAddress;  482             });  483  484             // ifcelementQuantity 创建模型元素数量  485             var ifcPropertySet = model.Instances.New<IfcPropertySet>(ps =>  486             {  487                 ps.Name = "Test:IfcPropertySet";  488                 ps.Description = "Property Set";  489                 ps.HasProperties.Add(ifcPropertySingleValue);  490                 ps.HasProperties.Add(ifcPropertyEnumeratedValue);  491                 ps.HasProperties.Add(ifcPropertyBoundedValue);  492                 ps.HasProperties.Add(ifcPropertyTableValue);  493                 ps.HasProperties.Add(ifcPropertyListValue);  494                 ps.HasProperties.Add(ifcPrValueMaterial);  495                 ps.HasProperties.Add(ifcPrValueMatLayer);  496                 ps.HasProperties.Add(ifcPrValueRef);  497                 ps.HasProperties.Add(ifcPrValueTimeSeries);  498                 ps.HasProperties.Add(ifcPrValueAddress);  499                 ps.HasProperties.Add(ifcPrValueTelecom);  500             });  501  502             // 需要建立关系  503             model.Instances.New<IfcRelDefinesByProperties>(rdbp =>  504             {  505                 rdbp.Name = "Property Association";  506                 rdbp.Description = "IfcPropertySet associated to wall";  507                 rdbp.RelatedObjects.Add(wall);  508                 rdbp.RelatingPropertyDefinition = ifcPropertySet;  509             });  510         }  511  512         private static void CreateElementQuantity(IfcStore model, IfcWallStandardCase wall)  513         {  514             // 创建模型元素数量  515             // 首先我们需模型简单物理量,首先将使用模型量长度  516             var ifcQuantityArea = model.Instances.New<IfcQuantityLength>(qa =>  517             {  518                 qa.Name = "IfcQuantityArea:Area";  519                 qa.Description = "";  520                 qa.Unit = model.Instances.New<IfcSIUnit>(siu =>  521                 {  522                     siu.UnitType = IfcUnitEnum.LENGTHUNIT;  523                     siu.Prefix = IfcSIPrefix.MILLI;  524                     siu.Name = IfcSIUnitName.METRE;  525                 });  526                 qa.LengthValue = 100.0;  527  528             });  529  530             // 然后,上下文相关单元的数量计数  531             var ifcContextDependentUnit = model.Instances.New<IfcContextDependentUnit>(cd =>  532                 {  533                     cd.Dimensions = model.Instances.New<IfcDimensionalExponents>(de =>  534                         {  535                             de.LengthExponent = 1;  536                             de.MassExponent = 0;  537                             de.TimeExponent = 0;  538                             de.ElectricCurrentExponent = 0;  539                             de.ThermodynamicTemperatureExponent = 0;  540                             de.AmountOfSubstanceExponent = 0;  541                             de.LuminousIntensityExponent = 0;  542                         });  543                     cd.UnitType = IfcUnitEnum.LENGTHUNIT;  544                     cd.Name = "Elephants";  545                 });  546             var ifcQuantityCount = model.Instances.New<IfcQuantityCount>(qc =>  547             {  548                 qc.Name = "IfcQuantityCount:Elephant";  549                 qc.CountValue = 12;  550                 qc.Unit = ifcContextDependentUnit;  551             });  552  553  554             // 使用转换单位  555             var ifcConversionBasedUnit = model.Instances.New<IfcConversionBasedUnit>(cbu =>  556             {  557                 cbu.ConversionFactor = model.Instances.New<IfcMeasureWithUnit>(mu =>  558                 {  559                     mu.ValueComponent = new IfcRatioMeasure(25.4);  560                     mu.UnitComponent = model.Instances.New<IfcSIUnit>(siu =>  561                     {  562                         siu.UnitType = IfcUnitEnum.LENGTHUNIT;  563                         siu.Prefix = IfcSIPrefix.MILLI;  564                         siu.Name = IfcSIUnitName.METRE;  565                     });  566  567                 });  568                 cbu.Dimensions = model.Instances.New<IfcDimensionalExponents>(de =>  569                 {  570                     de.LengthExponent = 1;  571                     de.MassExponent = 0;  572                     de.TimeExponent = 0;  573                     de.ElectricCurrentExponent = 0;  574                     de.ThermodynamicTemperatureExponent = 0;  575                     de.AmountOfSubstanceExponent = 0;  576                     de.LuminousIntensityExponent = 0;  577                 });  578                 cbu.UnitType = IfcUnitEnum.LENGTHUNIT;  579                 cbu.Name = "Inch";  580             });  581             var ifcQuantityLength = model.Instances.New<IfcQuantityLength>(qa =>  582             {  583                 qa.Name = "IfcQuantityLength:Length";  584                 qa.Description = "";  585                 qa.Unit = ifcConversionBasedUnit;  586                 qa.LengthValue = 24.0;  587             });  588  589             // 创建 IfcElementQuantity  590             var ifcElementQuantity = model.Instances.New<IfcElementQuantity>(eq =>  591             {  592                 eq.Name = "Test:IfcElementQuantity";  593                 eq.Description = "Measurement quantity";  594                 eq.Quantities.Add(ifcQuantityArea);  595                 eq.Quantities.Add(ifcQuantityCount);  596                 eq.Quantities.Add(ifcQuantityLength);  597             });  598  599             // 创建关系  600             model.Instances.New<IfcRelDefinesByProperties>(rdbp =>  601             {  602                 rdbp.Name = "Area Association";  603                 rdbp.Description = "IfcElementQuantity associated to wall";  604                 rdbp.RelatedObjects.Add(wall);  605                 rdbp.RelatingPropertyDefinition = ifcElementQuantity;  606             });  607         }  608     }  609 }

生成的IFC文件可以在您选择的查看器中打开:

ISO-10303-21;  HEADER;  FILE_DESCRIPTION ((''), '2;1');  FILE_NAME ('', '2016-10-31T10:18:08', (''), (''), 'Xbim File Processor version 4.0.0.0', 'Xbim version 4.0.0.0', '');  FILE_SCHEMA (('IFC4'));  ENDSEC;  DATA;  #1=IFCPROJECT('2MSHGQD897wuyjYHKVxtyf',#2,'HelloWall',$,$,$,$,(#20,#23),#8);  #2=IFCOWNERHISTORY(#5,#6,$,.ADDED.,$,$,$,0);  #3=IFCPERSON($,'Team','xBIM',$,$,$,$,$);  #4=IFCORGANIZATION($,'xBimTeam',$,$,$);  #5=IFCPERSONANDORGANIZATION(#3,#4,$);  #7=IFCORGANIZATION($,'xBimTeam',$,$,$);  #6=IFCAPPLICATION(#7,'1.0','Hello Wall Application','HelloWall.exe');  #8=IFCUNITASSIGNMENT((#9,#10,#11,#12,#13,#14,#15,#16,#17));  #9=IFCSIUNIT(*,.LENGTHUNIT.,.MILLI.,.METRE.);  #10=IFCSIUNIT(*,.AREAUNIT.,$,.SQUARE_METRE.);  #11=IFCSIUNIT(*,.VOLUMEUNIT.,$,.CUBIC_METRE.);  #12=IFCSIUNIT(*,.SOLIDANGLEUNIT.,$,.STERADIAN.);  #13=IFCSIUNIT(*,.PLANEANGLEUNIT.,$,.RADIAN.);  #14=IFCSIUNIT(*,.MASSUNIT.,$,.GRAM.);  #15=IFCSIUNIT(*,.TIMEUNIT.,$,.SECOND.);  #16=IFCSIUNIT(*,.THERMODYNAMICTEMPERATUREUNIT.,$,.DEGREE_CELSIUS.);  #17=IFCSIUNIT(*,.LUMINOUSINTENSITYUNIT.,$,.LUMEN.);  #18=IFCCARTESIANPOINT((0.,0.,0.));  #19=IFCAXIS2PLACEMENT3D(#18,$,$);  #20=IFCGEOMETRICREPRESENTATIONCONTEXT('Building Model','Model',3,1.E-05,#19,$);  #21=IFCCARTESIANPOINT((0.,0.));  #22=IFCAXIS2PLACEMENT2D(#21,$);  #23=IFCGEOMETRICREPRESENTATIONCONTEXT('Building Plan View','Plan',2,1.E-05,#22,$);  #24=IFCBUILDING('0jQ$yvAIv6URCwLuvWnAo0',#2,'Default Building',$,$,#25,$,$,.ELEMENT.,$,$,$);  #25=IFCLOCALPLACEMENT($,#26);  #26=IFCAXIS2PLACEMENT3D(#27,$,$);  #27=IFCCARTESIANPOINT((0.,0.,0.));  #28=IFCRELAGGREGATES('3Qs6TKkPjASQ1ctGzUSQ7F',#2,$,$,#1,(#24));  #29=IFCWALLSTANDARDCASE('3NBPkknun6EuV9fpeE6rFh',#2,'A Standard rectangular wall',$,$,#39,#38,$,$);  #30=IFCRECTANGLEPROFILEDEF(.AREA.,$,#32,300.,4000.);  #31=IFCCARTESIANPOINT((0.,400.));  #32=IFCAXIS2PLACEMENT2D(#31,$);  #33=IFCEXTRUDEDAREASOLID(#30,#36,#34,2400.);  #34=IFCDIRECTION((0.,0.,1.));  #35=IFCCARTESIANPOINT((0.,0.,0.));  #36=IFCAXIS2PLACEMENT3D(#35,$,$);  #37=IFCSHAPEREPRESENTATION(#20,'Body','SweptSolid',(#33));  #38=IFCPRODUCTDEFINITIONSHAPE($,$,(#37,#52));  #39=IFCLOCALPLACEMENT($,#40);  #40=IFCAXIS2PLACEMENT3D(#35,#42,#41);  #41=IFCDIRECTION((0.,1.,0.));  #42=IFCDIRECTION((0.,0.,1.));  #43=IFCMATERIALLAYERSETUSAGE(#44,.AXIS2.,.NEGATIVE.,150.,$);  #44=IFCMATERIALLAYERSET((#45),$,$);  #45=IFCMATERIALLAYER($,10.,$,$,$,$,$);  #46=IFCMATERIAL('some material',$,$);  #47=IFCRELASSOCIATESMATERIAL('0is_vsqtn9ouErH3XVg3O8',#2,$,$,(#29),#43);  #48=IFCPRESENTATIONLAYERASSIGNMENT('some ifcPresentationLayerAssignment',$,(#37),$);  #49=IFCPOLYLINE((#50,#51));  #50=IFCCARTESIANPOINT((0.,0.));  #51=IFCCARTESIANPOINT((4000.,0.));  #52=IFCSHAPEREPRESENTATION(#20,'Axis','Curve2D',(#49));  #54=IFCSIUNIT(*,.LENGTHUNIT.,.MILLI.,.METRE.);  #53=IFCQUANTITYLENGTH('IfcQuantityArea:Area','',#54,100.,$);  #56=IFCDIMENSIONALEXPONENTS(1,0,0,0,0,0,0);  #55=IFCCONTEXTDEPENDENTUNIT(#56,.LENGTHUNIT.,'Elephants');  #57=IFCQUANTITYCOUNT('IfcQuantityCount:Elephant',$,#55,12.,$);  #60=IFCSIUNIT(*,.LENGTHUNIT.,.MILLI.,.METRE.);  #59=IFCMEASUREWITHUNIT(IFCRATIOMEASURE(25.4),#60);  #61=IFCDIMENSIONALEXPONENTS(1,0,0,0,0,0,0);  #58=IFCCONVERSIONBASEDUNIT(#61,.LENGTHUNIT.,'Inch',#59);  #62=IFCQUANTITYLENGTH('IfcQuantityLength:Length','',#58,24.,$);  #63=IFCELEMENTQUANTITY('2NzDD6BkfDFAUH5zVe3vf0',#2,'Test:IfcElementQuantity','Measurement quantity',$,(#53,#57,#62));  #64=IFCRELDEFINESBYPROPERTIES('2rxEDLnp59XvLpgGjoNnBQ',#2,'Area Association','IfcElementQuantity associated to wall',(#29),#63);  #66=IFCSIUNIT(*,.TIMEUNIT.,$,.SECOND.);  #65=IFCPROPERTYSINGLEVALUE('IfcPropertySingleValue:Time','',IFCTIMEMEASURE(150.),#66);  #68=IFCPROPERTYENUMERATION('Notes',(IFCLABEL('Do'),IFCLABEL('Re'),IFCLABEL('Mi'),IFCLABEL('Fa'),IFCLABEL('So'),IFCLABEL('La'),IFCLABEL('Ti')),$);  #67=IFCPROPERTYENUMERATEDVALUE('IfcPropertyEnumeratedValue:Music',$,(IFCLABEL('Do'),IFCLABEL('Re'),IFCLABEL('Mi')),#68);  #70=IFCSIUNIT(*,.MASSUNIT.,.KILO.,.GRAM.);  #69=IFCPROPERTYBOUNDEDVALUE('IfcPropertyBoundedValue:Mass','',IFCMASSMEASURE(5000.),IFCMASSMEASURE(1000.),#70,$);  #73=IFCDIMENSIONALEXPONENTS(0,0,0,0,0,0,0);  #72=IFCCONTEXTDEPENDENTUNIT(#73,.FREQUENCYUNIT.,'dB');  #71=IFCPROPERTYTABLEVALUE('IfcPropertyTableValue:Sound',$,(IFCREAL(100.),IFCREAL(200.),IFCREAL(400.),IFCREAL(800.),IFCREAL(1600.),IFCREAL(3200.)),(IFCREAL(20.),IFCREAL(42.),IFCREAL(46.),IFCREAL(56.),IFCREAL(60.),IFCREAL(65.)),$,$,#72,$);  #74=IFCPROPERTYLISTVALUE('IfcPropertyListValue:Colours',$,(IFCLABEL('Red'),IFCLABEL('Green'),IFCLABEL('Blue'),IFCLABEL('Pink'),IFCLABEL('White'),IFCLABEL('Black')),$);  #75=IFCMATERIAL('Brick',$,$);  #76=IFCPROPERTYREFERENCEVALUE('IfcPropertyReferenceValue:Material',$,$,#75);  #78=IFCMATERIAL('Cavity',$,$);  #79=IFCMATERIAL('Block',$,$);  #77=IFCMATERIALLIST((#75,#78,#79));  #80=IFCMATERIALLAYER(#75,100.,$,$,$,$,$);  #81=IFCPROPERTYREFERENCEVALUE('IfcPropertyReferenceValue:MaterialLayer',$,$,#80);  #82=IFCDOCUMENTREFERENCE('c://Documents//TheDoc.Txt',$,'Document',$,$);  #83=IFCPROPERTYREFERENCEVALUE('IfcPropertyReferenceValue:Document',$,$,#82);  #84=IFCREGULARTIMESERIES('Regular Time Series','Time series of events','2015-02-14T12:01:01','2015-05-15T12:01:01',.CONTINUOUS.,.MEASURED.,$,$,604800.,());  #85=IFCPROPERTYREFERENCEVALUE('IfcPropertyReferenceValue:TimeSeries',$,$,#84);  #86=IFCPOSTALADDRESS($,$,$,'Room 101',('12 New road','DoxField'),$,'Sunderland',$,'DL01 6SX',$);  #87=IFCPROPERTYREFERENCEVALUE('IfcPropertyReferenceValue:Address',$,$,#86);  #88=IFCTELECOMADDRESS($,$,$,('01325 6589965'),$,$,('[email protected]'),$,$);  #89=IFCPROPERTYREFERENCEVALUE('IfcPropertyReferenceValue:Telecom',$,$,#88);  #90=IFCPROPERTYSET('2qJSTdQSj0wQKmIdFVroG2',#2,'Test:IfcPropertySet','Property Set',(#65,#67,#69,#71,#74,#76,#81,#83,#85,#87,#89));  #91=IFCRELDEFINESBYPROPERTIES('3n83nuxoj0gA5F$UnucPP3',#2,'Property Association','IfcPropertySet associated to wall',(#29),#90);  #92=IFCRELCONTAINEDINSPATIALSTRUCTURE('1mefbELBn5JQeF1AH4vA0$',#2,$,$,(#29),#24);  ENDSEC;  END-ISO-10303-21;