首页 > 其他 > 详细

xBIM之二:构建墙和门窗

时间:2019-06-05 11:27:58      阅读:104      评论:0      收藏:0      [点我收藏+]

研究了两天,终于实现了利用xBIM自动输出墙和门窗

技术分享图片

 比较粗糙的源码如下:

技术分享图片
  1  private void Form1_Load(object sender, EventArgs e)
  2         {
  3             //first create and initialise a model called Hello Wall
  4             Console.WriteLine("Initialising the IFC Project....");
  5             using (var model = CreateandInitModel("HelloWall"))
  6             {
  7                 if (model != null)
  8                 {
  9                     IfcBuilding building = CreateBuilding(model, "Default Building");
 10                     IfcWallStandardCase wall = CreateWall(model, 4000, 300, 2400);
 11                     IfcDoor door = CreateDoor(model, wall, 1000, 300, 2200);
 12                     IfcWindow widnow = CreateWindow(model, wall, 1000, 300, 1000);
 13                     if (wall != null) AddPropertiesToWall(model, wall);
 14                     using (var txn = model.BeginTransaction("Add Wall"))
 15                     {
 16                         building.AddElement(wall);
 17                         building.AddElement(door);
 18                         building.AddElement(widnow);
 19                         txn.Commit();
 20                     }
 21 
 22                     if (wall != null)
 23                     {
 24                         try
 25                         {
 26                             Console.WriteLine("Standard Wall successfully created....");
 27                             //保存到文件
 28                             model.SaveAs("HelloWallIfc4.ifc", IfcStorageType.Ifc);
 29                             Console.WriteLine("HelloWallIfc4.ifc has been successfully written");
 30                         }
 31                         catch (Exception ex)
 32                         {
 33                             Console.WriteLine("Failed to save HelloWall.ifc");
 34                             Console.WriteLine(ex.Message);
 35                         }
 36                     }
 37                 }
 38                 else
 39                 {
 40                     Console.WriteLine("Failed to initialise the model");
 41                 }
 42             }
 43             Console.WriteLine("Press any key to exit to view the IFC file....");
 44             Console.ReadKey();
 45            // LaunchNotepad("HelloWallIfc4.ifc");
 46         }
 47         // private static void LaunchNotepad(string fileName)
 48         //{
 49         //    Process p;
 50         //    try
 51         //    {
 52 
 53         //        p = new Process {StartInfo = {FileName = fileName, CreateNoWindow = false}};
 54         //        p.Start();
 55         //    }
 56         //    catch (Exception ex)
 57         //    {
 58         //        Console.WriteLine("Exception Occurred :{0},{1}",
 59         //                  ex.Message, ex.StackTrace);
 60         //    }
 61         //}
 62 
 63         private static IfcBuilding CreateBuilding(IfcStore model, string name)
 64         {
 65             //启动事务
 66             using (var txn = model.BeginTransaction("Create Building"))
 67             {
 68                 var building = model.Instances.New<IfcBuilding>();
 69                 building.Name = name;
 70 
 71                 building.CompositionType = IfcElementCompositionEnum.ELEMENT;
 72                 var localPlacement = model.Instances.New<IfcLocalPlacement>();                   
 73                 building.ObjectPlacement = localPlacement;
 74                 var placement = model.Instances.New<IfcAxis2Placement3D>();
 75                 localPlacement.RelativePlacement = placement;
 76                 placement.Location = model.Instances.New<IfcCartesianPoint>(p=>p.SetXYZ(0,0,0));
 77                 //获取项目
 78                 var project = model.Instances.OfType<IfcProject>().FirstOrDefault();
 79                 project.AddBuilding(building);
 80                 txn.Commit();
 81                 return building;                               
 82             }          
 83         }
 84 
 85 
 86 
 87         /// <summary>
 88         /// 设置模型的基本参数、单元、所有权等
 89         /// </summary>
 90         /// <param name="projectName">项目名称</param>
 91         /// <returns></returns>
 92         private static IfcStore CreateandInitModel(string projectName)
 93         {
 94             //首先我们需要为模型创建凭证、这个在之前的文章都有解释
 95             var credentials = new XbimEditorCredentials
 96             {
 97                 ApplicationDevelopersName = "xBimTeam",
 98                 ApplicationFullName = "Hello Wall Application",
 99                 ApplicationIdentifier = "HelloWall.exe",
100                 ApplicationVersion = "1.0",
101                 EditorsFamilyName = "Team",
102                 EditorsGivenName = "xBIM",
103                 EditorsOrganisationName = "xBimTeam"
104             };
105             //那么先创建 IfcStore,IfcStore 是IFC4 格式存放在内存中而不是数据库
106             //如果模型大于50MB的Ifc或者需要强大的事务处理,数据库在性能方面通常会更好
107  
108             var model = IfcStore.Create(credentials, IfcSchemaVersion.Ifc4,XbimStoreType.InMemoryModel);
109             // 启动事务、将所有的模型更改为 ACID
110             using (var txn = model.BeginTransaction("Initialise Model"))
111             {
112 
113                 //常见项目信息
114                 var project = model.Instances.New<IfcProject>();
115                 //设置单位   这里是英制 
116                 project.Initialize(ProjectUnits.SIUnitsUK);
117                 project.Name = projectName;
118                 
119                 //提交修改
120                 txn.Commit();
121             }
122             return model; 
123 
124         }
125 
126         /// <summary>
127         /// 创建墙
128         /// </summary>
129         /// <param name="model"></param>
130         /// <param name="length">矩形的长度 </param>
131         /// <param name="width">矩形占地面积的宽度(墙的宽度)</param>
132         /// <param name="height">墙高度</param>
133         /// <returns></returns>
134         static private IfcWallStandardCase CreateWall(IfcStore model, double length, double width, double height)
135         {
136             
137             //启动事务
138             using (var txn = model.BeginTransaction("Create Wall"))
139             {
140                 var wall = model.Instances.New<IfcWallStandardCase>();
141                 wall.Name = "A Standard rectangular wall";
142 
143                
144                
145                 // 墙的矩形剖面
146                 //var rectProf = model.Instances.New<IfcRectangleProfileDef>();
147                 //rectProf.ProfileType = IfcProfileTypeEnum.AREA;
148                 //rectProf.XDim = width;
149                 //rectProf.YDim = length;
150 
151                 //var insertPoint = model.Instances.New<IfcCartesianPoint>();
152                 //insertPoint.SetXY(0, 400); //在任意位置插入
153                 //rectProf.Position = model.Instances.New<IfcAxis2Placement2D>();
154                 //rectProf.Position.Location = insertPoint;
155 
156                 //var curve=model.Instances.New<IfcCurve>();
157                 IfcPolyline curve = model.Instances.New<IfcPolyline>();
158                 var pt0 = model.Instances.New<IfcCartesianPoint>();
159                 pt0.SetXY(0, 0); //在任意位置插入
160                 var pt1 = model.Instances.New<IfcCartesianPoint>();
161                 pt1.SetXY(0, length / 2); //在任意位置插入
162                 var pt2 = model.Instances.New<IfcCartesianPoint>();
163                 pt2.SetXY(0, length); //在任意位置插入
164                 curve.Points.Add(pt0);
165                 curve.Points.Add(pt1);
166                 curve.Points.Add(pt2);
167 
168                 var rectProf = model.Instances.New<IfcCenterLineProfileDef>();
169                 rectProf.ProfileType = IfcProfileTypeEnum.AREA;
170                 rectProf.Thickness = width;
171                 rectProf.Curve = curve;
172 
173                 //模型区域实心
174                 var body = model.Instances.New<IfcExtrudedAreaSolid>();
175                 body.Depth = height;
176                 body.SweptArea = rectProf;
177                 body.ExtrudedDirection = model.Instances.New<IfcDirection>();
178                 body.ExtrudedDirection.SetXYZ(0, 0, 1);
179 
180                 //在模型中插入几何参数
181                 var origin = model.Instances.New<IfcCartesianPoint>();
182                 origin.SetXYZ(0, 0, 0);
183                 body.Position = model.Instances.New<IfcAxis2Placement3D>();
184                 body.Position.Location = origin;
185 
186                 //创建一个定义形状来保存几何
187                 var shape = model.Instances.New<IfcShapeRepresentation>();
188                 var modelContext = model.Instances.OfType<IfcGeometricRepresentationContext>().FirstOrDefault();
189                 shape.ContextOfItems = modelContext;
190                 shape.RepresentationType = "SweptSolid";
191                 shape.RepresentationIdentifier = "Body";
192                 shape.Items.Add(body);
193 
194                 //创建产品定义并将模型几何添加到墙上
195                 var rep = model.Instances.New<IfcProductDefinitionShape>();
196                 rep.Representations.Add(shape);        
197                 wall.Representation = rep;
198                 
199                 //把墙放到模型中
200                 var lp = model.Instances.New<IfcLocalPlacement>();
201                 var ax3D = model.Instances.New<IfcAxis2Placement3D>();
202                 ax3D.Location = origin;
203                 ax3D.RefDirection = model.Instances.New<IfcDirection>();
204                 ax3D.RefDirection.SetXYZ(0, 1, 0);
205                 ax3D.Axis = model.Instances.New<IfcDirection>();
206                 ax3D.Axis.SetXYZ(0, 0, 1);
207                 lp.RelativePlacement = ax3D;
208                 wall.ObjectPlacement = lp;
209                
210                 //Where子句:IfcWallStandard依赖于提供一个IfcMaterialLayerSetUsage
211                 var ifcMaterialLayerSetUsage = model.Instances.New<IfcMaterialLayerSetUsage>();
212                 var ifcMaterialLayerSet = model.Instances.New<IfcMaterialLayerSet>();
213                 var ifcMaterialLayer = model.Instances.New<IfcMaterialLayer>();
214                 ifcMaterialLayer.LayerThickness = 10;
215                 ifcMaterialLayerSet.MaterialLayers.Add(ifcMaterialLayer);
216                 ifcMaterialLayerSetUsage.ForLayerSet = ifcMaterialLayerSet;
217                 ifcMaterialLayerSetUsage.LayerSetDirection = IfcLayerSetDirectionEnum.AXIS2;
218                 ifcMaterialLayerSetUsage.DirectionSense = IfcDirectionSenseEnum.NEGATIVE;
219                 ifcMaterialLayerSetUsage.OffsetFromReferenceLine = 150;
220 
221                 //添加材料到墙上
222                 var material = model.Instances.New<IfcMaterial>();
223                 material.Name = "some material";
224                 var ifcRelAssociatesMaterial = model.Instances.New<IfcRelAssociatesMaterial>();
225                 ifcRelAssociatesMaterial.RelatingMaterial = material;
226                 ifcRelAssociatesMaterial.RelatedObjects.Add(wall);
227 
228                 ifcRelAssociatesMaterial.RelatingMaterial = ifcMaterialLayerSetUsage;
229 
230                 //IfcPresentationLayerAssignment对于IfcWall或IfcWallStandardCase中的CAD演示是必需的 
231                 var ifcPresentationLayerAssignment = model.Instances.New<IfcPresentationLayerAssignment>();
232                 ifcPresentationLayerAssignment.Name = "some ifcPresentationLayerAssignment";
233                 ifcPresentationLayerAssignment.AssignedItems.Add(shape);
234                 // 如果IfcPolyline具有两个点,则对于IfcWall是必需的
235                 var ifcPolyline = model.Instances.New<IfcPolyline>();
236                 var startPoint = model.Instances.New<IfcCartesianPoint>();
237                 startPoint.SetXY(0, 0);
238                 var endPoint = model.Instances.New<IfcCartesianPoint>();
239                 endPoint.SetXY(4000, 0);
240                 ifcPolyline.Points.Add(startPoint);
241                 ifcPolyline.Points.Add(endPoint);
242 
243                 var shape2D = model.Instances.New<IfcShapeRepresentation>();
244                 shape2D.ContextOfItems = modelContext;
245                 shape2D.RepresentationIdentifier = "Axis";
246                 shape2D.RepresentationType = "Curve2D";
247                 shape2D.Items.Add(ifcPolyline);
248                 rep.Representations.Add(shape2D);
249 
250 
251                 //var rectDoor = model.Instances.New<IfcRectangleProfileDef>();
252                 //rectDoor.ProfileType = IfcProfileTypeEnum.AREA;
253                 //rectDoor.XDim = width;
254                 //rectDoor.YDim = length;
255 
256                 //var insertPoint = model.Instances.New<IfcCartesianPoint>();
257                 //insertPoint.SetXY(0, 400); //在任意位置插入
258                 //rectDoor.Position = model.Instances.New<IfcAxis2Placement2D>();
259                 //rectDoor.Position.Location = insertPoint;
260                 ////模型区域实心
261                 //var body_door = model.Instances.New<IfcExtrudedAreaSolid>();
262                 //body_door.Depth = height;
263                 //body_door.SweptArea = rectDoor;
264                 //body_door.ExtrudedDirection = model.Instances.New<IfcDirection>();
265                 //body_door.ExtrudedDirection.SetXYZ(0, 0, 1);
266 
267                 ////在模型中插入几何参数
268                 //body_door.Position = model.Instances.New<IfcAxis2Placement3D>();
269                 //body_door.Position.Location = origin;
270 
271                 ////创建一个定义形状来保存几何
272                 //var shape_door = model.Instances.New<IfcShapeRepresentation>();
273                 //var modelContext_door = model.Instances.OfType<IfcGeometricRepresentationContext>().FirstOrDefault();
274                 //shape_door.ContextOfItems = modelContext_door;
275                 //shape_door.RepresentationType = "SweptSolid";
276                 //shape_door.RepresentationIdentifier = "Body";
277                 //shape_door.Items.Add(body_door);
278 
279                 ////创建产品定义并将模型几何添加到墙上
280                 //var rep_door = model.Instances.New<IfcProductDefinitionShape>();
281                 //rep_door.Representations.Add(shape_door);
282 
283                 //var door = model.Instances.New<IfcDoor>();
284                 //door.Name = "A Door";
285                 //var lp_door = model.Instances.New<IfcLocalPlacement>();
286                 //var ax3D_door = model.Instances.New<IfcAxis2Placement3D>();
287                 //ax3D_door.Location = origin;
288                 //ax3D_door.RefDirection = model.Instances.New<IfcDirection>();
289                 //ax3D_door.RefDirection.SetXYZ(0, 1, 0);
290                 //ax3D_door.Axis = model.Instances.New<IfcDirection>();
291                 //ax3D_door.Axis.SetXYZ(0, 0, 1);
292                 //lp_door.RelativePlacement = ax3D_door;
293                 //door.ObjectPlacement = lp_door;
294                 //door.Representation = rep_door;
295                 //var m_OpeningEle = model.Instances.New<IfcOpeningElement>();
296                 //m_OpeningEle.Name = "Openings";
297 
298                 //var m_RelFills = model.Instances.New<IfcRelFillsElement>();
299                 //m_RelFills.RelatingOpeningElement = m_OpeningEle;
300                 //m_RelFills.RelatedBuildingElement = door;
301                 //var voidRel = model.Instances.New<IfcRelVoidsElement>();
302                 //voidRel.RelatedOpeningElement = m_OpeningEle;
303                 //voidRel.RelatingBuildingElement = wall;
304 
305                 txn.Commit();               
306                 return wall;
307             }
308 
309         }
310         static private IfcDoor CreateDoor(IfcStore model, IfcWallStandardCase wall, double length, double width, double height)
311         {
312             //启动事务
313             using (var txn = model.BeginTransaction("Create Door"))
314             {
315                 var rectDoor = model.Instances.New<IfcRectangleProfileDef>();
316                 rectDoor.ProfileType = IfcProfileTypeEnum.AREA;
317                 rectDoor.XDim = width-100;
318                 rectDoor.YDim = length;
319 
320                 var insertPoint = model.Instances.New<IfcCartesianPoint>();
321                 insertPoint.SetXY(0, 0); //在任意位置插入
322                 rectDoor.Position = model.Instances.New<IfcAxis2Placement2D>();
323                 rectDoor.Position.Location = insertPoint;
324 
325                 //模型区域实心
326                 var body_door = model.Instances.New<IfcExtrudedAreaSolid>();
327                 body_door.Depth = height;
328                 body_door.SweptArea = rectDoor;
329                 body_door.ExtrudedDirection = model.Instances.New<IfcDirection>();
330                 body_door.ExtrudedDirection.SetXYZ(0, 0, 1);
331 
332                 var origin = model.Instances.New<IfcCartesianPoint>();
333                 origin.SetXYZ(0, 0, 0);
334                 //在模型中插入几何参数
335                 body_door.Position = model.Instances.New<IfcAxis2Placement3D>();
336                 body_door.Position.Location = origin;
337 
338                 //创建一个定义形状来保存几何
339                 var shape_door = model.Instances.New<IfcShapeRepresentation>();
340                 var modelContext_door = model.Instances.OfType<IfcGeometricRepresentationContext>().FirstOrDefault();
341                 shape_door.ContextOfItems = modelContext_door;
342                 shape_door.RepresentationType = "SweptSolid";
343                 shape_door.RepresentationIdentifier = "Body";
344                 shape_door.Items.Add(body_door);
345 
346                 //创建产品定义并将模型几何添加到墙上
347                 var rep_door = model.Instances.New<IfcProductDefinitionShape>();
348                 rep_door.Representations.Add(shape_door);
349 
350                 var door = model.Instances.New<IfcDoor>();
351                 door.Name = "A Door";
352                 door.PredefinedType = IfcDoorTypeEnum.GATE;
353                 door.OperationType = IfcDoorTypeOperationEnum.DOUBLE_SWING_LEFT;
354                 door.OverallHeight = 400;
355                 door.OverallWidth = 400;
356                
357 
358                 var lp_door = model.Instances.New<IfcLocalPlacement>();
359                 var wallplace = wall.ObjectPlacement;
360                 var ax3D_door = model.Instances.New<IfcAxis2Placement3D>();
361                 var origin2 = model.Instances.New<IfcCartesianPoint>();
362                 origin2.SetXYZ(0, 1500, 10);
363                 
364                 ax3D_door.RefDirection = model.Instances.New<IfcDirection>();
365                 ax3D_door.RefDirection.SetXYZ(1,0,  0);//x轴
366                 ax3D_door.Axis = model.Instances.New<IfcDirection>();
367                 ax3D_door.Axis.SetXYZ(0, 0, 1);//Z轴
368                 ax3D_door.Location = origin2;
369                 //lp_door.RelativePlacement = wallplace.RelativePlacement;
370                 lp_door.RelativePlacement = ax3D_door;
371                 lp_door.PlacementRelTo =wallplace;
372                 door.ObjectPlacement = lp_door;
373                 door.Representation = rep_door;
374 
375                 ////////////////////////////////////////////////////////////////////
376                 var m_OpeningEle = model.Instances.New<IfcOpeningElement>();
377                 m_OpeningEle.Name = "My Openings";
378                 m_OpeningEle.PredefinedType = IfcOpeningElementTypeEnum.OPENING;
379               
380                 var rectOpening = model.Instances.New<IfcRectangleProfileDef>();
381                 rectOpening.ProfileType = IfcProfileTypeEnum.AREA;
382                 rectOpening.XDim = width;
383                 rectOpening.YDim = length;
384                 rectOpening.Position = model.Instances.New<IfcAxis2Placement2D>();
385                 rectOpening.Position.Location = insertPoint;
386 
387                 //模型区域实心
388                 var body_Opeinging = model.Instances.New<IfcExtrudedAreaSolid>();
389                 body_Opeinging.Depth = height;
390                 body_Opeinging.SweptArea = rectOpening;
391                 body_Opeinging.ExtrudedDirection = model.Instances.New<IfcDirection>();
392                 body_Opeinging.ExtrudedDirection.SetXYZ(0, 0, 1);
393                 body_Opeinging.Position = model.Instances.New<IfcAxis2Placement3D>();
394                 body_Opeinging.Position.Location = origin;
395                 var shape__Opeinging = model.Instances.New<IfcShapeRepresentation>();
396                 var modelContext__Opeinging = model.Instances.OfType<IfcGeometricRepresentationContext>().FirstOrDefault();
397                 shape__Opeinging.ContextOfItems = modelContext__Opeinging;
398                 shape__Opeinging.RepresentationType = "SweptSolid";
399                 shape__Opeinging.RepresentationIdentifier = "Body";
400                 shape__Opeinging.Items.Add(body_Opeinging);
401 
402                 //创建产品定义并将模型几何添加到墙上
403                 var rep_Opening = model.Instances.New<IfcProductDefinitionShape>();
404                 rep_Opening.Representations.Add(shape__Opeinging);
405                 m_OpeningEle.ObjectPlacement = lp_door;
406                 m_OpeningEle.Representation = rep_Opening;
407 
408                 var m_RelFills = model.Instances.New<IfcRelFillsElement>();
409                 m_RelFills.RelatingOpeningElement = m_OpeningEle;
410                 m_RelFills.RelatedBuildingElement = door;
411                 var voidRel = model.Instances.New<IfcRelVoidsElement>();
412                 voidRel.RelatedOpeningElement = m_OpeningEle;
413                 voidRel.RelatingBuildingElement = wall;
414 
415 
416                 var ifcPropertySingleValue = model.Instances.New<IfcPropertySingleValue>(psv =>
417                 {
418                     psv.Name = "Reference";
419                     psv.Description = "Reference";
420                     psv.NominalValue = new IfcTimeMeasure(150.0);
421                     psv.Unit = model.Instances.New<IfcSIUnit>(siu =>
422                     {
423                         siu.UnitType = IfcUnitEnum.TIMEUNIT;
424                         siu.Name = IfcSIUnitName.SECOND;
425                     });
426                 });
427                 //设置模型元素数量
428                 var ifcPropertySet = model.Instances.New<IfcPropertySet>(ps =>
429                 {
430                     ps.Name = "Pset_DoorCommon";
431                     ps.Description = "Property Set";
432                     ps.HasProperties.Add(ifcPropertySingleValue);
433                    
434                 });
435                 //需建立关系
436                 model.Instances.New<IfcRelDefinesByProperties>(
437                     rdbp =>
438                 {
439                     rdbp.Name = "Property Association";
440                     rdbp.Description = "IfcPropertySet associated to wall";
441                     rdbp.RelatedObjects.Add(door);
442                     rdbp.RelatingPropertyDefinition = ifcPropertySet;
443                 });
444 
445                 txn.Commit();
446                 return door;
447             }
448 
449         }
450         static private IfcWindow CreateWindow(IfcStore model, IfcWallStandardCase wall, double length, double width, double height)
451         {
452             //启动事务
453             using (var txn = model.BeginTransaction("Create Window"))
454             {
455                 var rectDoor = model.Instances.New<IfcRectangleProfileDef>();
456                 rectDoor.ProfileType = IfcProfileTypeEnum.CURVE;
457                 rectDoor.XDim = width - 100;
458                 rectDoor.YDim = length;
459 
460                 var insertPoint = model.Instances.New<IfcCartesianPoint>();
461                 insertPoint.SetXY(0, 0); //在任意位置插入
462                 rectDoor.Position = model.Instances.New<IfcAxis2Placement2D>();
463                 rectDoor.Position.Location = insertPoint;
464 
465                 //模型区域实心
466                 var body_door = model.Instances.New<IfcExtrudedAreaSolid>();
467                 body_door.Depth = height;
468                 body_door.SweptArea = rectDoor;
469                 body_door.ExtrudedDirection = model.Instances.New<IfcDirection>();
470                 body_door.ExtrudedDirection.SetXYZ(0, 0, 1);
471 
472                 var origin = model.Instances.New<IfcCartesianPoint>();
473                 origin.SetXYZ(0, 0, 0);
474                 //在模型中插入几何参数
475                 body_door.Position = model.Instances.New<IfcAxis2Placement3D>();
476                 body_door.Position.Location = origin;
477 
478                 //创建一个定义形状来保存几何
479                 var shape_door = model.Instances.New<IfcShapeRepresentation>();
480                 var modelContext_door = model.Instances.OfType<IfcGeometricRepresentationContext>().FirstOrDefault();
481                 shape_door.ContextOfItems = modelContext_door;
482                 shape_door.RepresentationType = "SweptSolid";
483                 shape_door.RepresentationIdentifier = "Body";
484                 shape_door.Items.Add(body_door);
485 
486                 //创建产品定义并将模型几何添加到墙上
487                 var rep_door = model.Instances.New<IfcProductDefinitionShape>();
488                 rep_door.Representations.Add(shape_door);
489 
490                 // var door = model.Instances.New<IfcDoorStandardCase>();
491                 //door.Name = "A Door";
492                 //door.PredefinedType = IfcDoorTypeEnum.GATE;
493                 //door.OperationType = IfcDoorTypeOperationEnum.DOUBLE_SWING_LEFT;
494                 //door.OverallHeight = 400;
495                 //door.OverallWidth = 400;
496                 var door = model.Instances.New<IfcWindow>();
497                 door.Name = "A Door";
498                 door.PredefinedType = IfcWindowTypeEnum.WINDOW;
499                 // door.OperationType = IfcDoorTypeOperationEnum.DOUBLE_SWING_LEFT;
500                 door.OverallHeight = 400;
501                 door.OverallWidth = 400;
502                 door.PartitioningType = IfcWindowTypePartitioningEnum.SINGLE_PANEL;
503 
504                 var windowType = model.Instances.New<IfcWindowType>();
505                 windowType.Name = "Window";
506                 windowType.Description = "ddddd";
507                 windowType.PartitioningType = IfcWindowTypePartitioningEnum.SINGLE_PANEL;
508                 var windowType_Rel= model.Instances.New<IfcRelDefinesByType>();
509                 windowType_Rel.RelatedObjects.Add(door);
510                 windowType_Rel.RelatingType = windowType;
511                 var lp_door = model.Instances.New<IfcLocalPlacement>();
512                 var wallplace = wall.ObjectPlacement;
513                 var ax3D_door = model.Instances.New<IfcAxis2Placement3D>();
514                 var origin2 = model.Instances.New<IfcCartesianPoint>();
515                 origin2.SetXYZ(0, 3000, 1000);
516 
517                 ax3D_door.RefDirection = model.Instances.New<IfcDirection>();
518                 ax3D_door.RefDirection.SetXYZ(1, 0, 0);//x轴
519                 ax3D_door.Axis = model.Instances.New<IfcDirection>();
520                 ax3D_door.Axis.SetXYZ(0, 0, 1);//Z轴
521                 ax3D_door.Location = origin2;
522                 //lp_door.RelativePlacement = wallplace.RelativePlacement;
523                 lp_door.RelativePlacement = ax3D_door;
524                 lp_door.PlacementRelTo = wallplace;
525                 door.ObjectPlacement = lp_door;
526                 door.Representation = rep_door;
527                // var m_door_style = model.Instances.New<IfcSurfaceStyle>();
528                 ////////////////////////////////////////////////////////////////////
529                 var m_OpeningEle = model.Instances.New<IfcOpeningElement>();
530                 m_OpeningEle.Name = "My Openings";
531                 m_OpeningEle.PredefinedType = IfcOpeningElementTypeEnum.OPENING;
532 
533                 var rectOpening = model.Instances.New<IfcRectangleProfileDef>();
534                 rectOpening.ProfileType = IfcProfileTypeEnum.AREA;
535                 rectOpening.XDim = width;
536                 rectOpening.YDim = length;
537                 rectOpening.Position = model.Instances.New<IfcAxis2Placement2D>();
538                 rectOpening.Position.Location = insertPoint;
539 
540                 //模型区域实心
541                 var body_Opeinging = model.Instances.New<IfcExtrudedAreaSolid>();
542                 body_Opeinging.Depth = height;
543                 body_Opeinging.SweptArea = rectOpening;
544                 body_Opeinging.ExtrudedDirection = model.Instances.New<IfcDirection>();
545                 body_Opeinging.ExtrudedDirection.SetXYZ(0, 0, 1);
546                 body_Opeinging.Position = model.Instances.New<IfcAxis2Placement3D>();
547                 body_Opeinging.Position.Location = origin;
548                 var shape__Opeinging = model.Instances.New<IfcShapeRepresentation>();
549                 var modelContext__Opeinging = model.Instances.OfType<IfcGeometricRepresentationContext>().FirstOrDefault();
550                 shape__Opeinging.ContextOfItems = modelContext__Opeinging;
551                 shape__Opeinging.RepresentationType = "SweptSolid";
552                 shape__Opeinging.RepresentationIdentifier = "Body";
553                 shape__Opeinging.Items.Add(body_Opeinging);
554 
555                 //创建产品定义并将模型几何添加到墙上
556                 var rep_Opening = model.Instances.New<IfcProductDefinitionShape>();
557                 rep_Opening.Representations.Add(shape__Opeinging);
558                 m_OpeningEle.ObjectPlacement = lp_door;
559                 m_OpeningEle.Representation = rep_Opening;
560 
561                 var m_RelFills = model.Instances.New<IfcRelFillsElement>();
562                 m_RelFills.RelatingOpeningElement = m_OpeningEle;
563                 m_RelFills.RelatedBuildingElement = door;
564                 var voidRel = model.Instances.New<IfcRelVoidsElement>();
565                 voidRel.RelatedOpeningElement = m_OpeningEle;
566                 voidRel.RelatingBuildingElement = wall;
567 
568                 var material98 = model.Instances.New<IfcMaterial>();
569                 material98.Name = "Glass";
570                 var material100 = model.Instances.New<IfcMaterial>();
571                 material100.Name = "Wood";
572 
573                 var n_ifcMaterialConstituentSet = model.Instances.New<IfcMaterialConstituentSet>();
574                 var n_ifcMaterialConstituent = model.Instances.New<IfcMaterialConstituent>();
575                 n_ifcMaterialConstituent.Category = "Framing";
576                 n_ifcMaterialConstituent.Material = material98;
577                 var n_ifcMaterialConstituent100 = model.Instances.New<IfcMaterialConstituent>();
578                 n_ifcMaterialConstituent100.Category = "Framing";
579                 n_ifcMaterialConstituent100.Material = material100;
580                 //n_ifcMaterialConstituent.Model = door;
581                 n_ifcMaterialConstituentSet.MaterialConstituents.Add(n_ifcMaterialConstituent);
582                 n_ifcMaterialConstituentSet.MaterialConstituents.Add(n_ifcMaterialConstituent100);
583                 var ifcRelAssociatesMaterial = model.Instances.New<IfcRelAssociatesMaterial>();
584                 ifcRelAssociatesMaterial.RelatingMaterial = n_ifcMaterialConstituentSet;
585                 ifcRelAssociatesMaterial.RelatedObjects.Add(door);
586 
587                 var ifcPropertySingleValue = model.Instances.New<IfcPropertySingleValue>(psv =>
588                 {
589                     psv.Name = "Reference";
590                     psv.Description = "Reference";
591                     psv.NominalValue = new IfcTimeMeasure(150.0);
592                     psv.Unit = model.Instances.New<IfcSIUnit>(siu =>
593                     {
594                         siu.UnitType = IfcUnitEnum.TIMEUNIT;
595                         siu.Name = IfcSIUnitName.SECOND;
596                     });
597                 });
598                 var ifcPropertySingleValue2 = model.Instances.New<IfcPropertySingleValue>(psv =>
599                 {
600                     psv.Name = "FireRating";
601                     psv.Description = "";
602                    
603                 });
604                 var ifcPropertySingleValue3 = model.Instances.New<IfcPropertySingleValue>(psv =>
605                 {
606                     psv.Name = "AcousticRating";
607                     psv.Description = "AcousticRating";
608                     
609                 });
610                 var ifcPropertySingleValue4 = model.Instances.New<IfcPropertySingleValue>(psv =>
611                 {
612                     psv.Name = "IsExternal";
613                     psv.Description = "IsExternal";
614                     psv.NominalValue = new IfcBoolean(true);
615 
616                 });
617                 var ifcPropertySingleValue5 = model.Instances.New<IfcPropertySingleValue>(psv =>
618                 {
619                     psv.Name = "Infiltration";
620                     psv.Description = "Infiltration";
621                     psv.NominalValue = new IfcReal(0.3);
622 
623                 }); 
624                 var ifcPropertySingleValue6 = model.Instances.New<IfcPropertySingleValue>(psv =>
625                  {
626                      psv.Name = "ThermalTransmittance";
627                      psv.Description = "ThermalTransmittance";
628                      psv.NominalValue = new IfcReal(0.24);
629 
630                  });
631                 //设置模型元素数量
632                 var ifcPropertySet = model.Instances.New<IfcPropertySet>(ps =>
633                 {
634                     ps.Name = "Pset_WindowCommon";
635                     ps.Description = "Property Set";
636                     ps.HasProperties.Add(ifcPropertySingleValue);
637                     ps.HasProperties.Add(ifcPropertySingleValue2);
638                     ps.HasProperties.Add(ifcPropertySingleValue3);
639                     ps.HasProperties.Add(ifcPropertySingleValue4);
640                     ps.HasProperties.Add(ifcPropertySingleValue5);
641                     ps.HasProperties.Add(ifcPropertySingleValue6);
642                 });
643                 //需建立关系
644                 model.Instances.New<IfcRelDefinesByProperties>(
645                     rdbp =>
646                     {
647                         rdbp.Name = "Property Association";
648                         rdbp.Description = "IfcPropertySet associated to wall";
649                         rdbp.RelatedObjects.Add(door);
650                         rdbp.RelatingPropertyDefinition = ifcPropertySet;
651                     });
652 
653                 txn.Commit();
654                 return door;
655             }
656 
657         }
658         /// <summary>
659         /// 给墙添加属性
660         /// </summary>
661         /// <param name="model">XbimModel</param>
662         /// <param name="wall"></param>
663         static private void AddPropertiesToWall(IfcStore model, IfcWallStandardCase wall)
664         {
665             using (var txn = model.BeginTransaction("Create Wall"))
666             {
667                 CreateElementQuantity(model, wall);
668                 CreateSimpleProperty(model, wall); 
669                 txn.Commit(); 
670             }
671         }
672 
673         private static void CreateSimpleProperty(IfcStore model, IfcWallStandardCase wall)
674         {
675             var ifcPropertySingleValue = model.Instances.New<IfcPropertySingleValue>(psv =>
676             {
677                 psv.Name = "IfcPropertySingleValue:Time";
678                 psv.Description = "";
679                 psv.NominalValue = new IfcTimeMeasure(150.0);
680                 psv.Unit = model.Instances.New<IfcSIUnit>(siu =>
681                 {
682                     siu.UnitType = IfcUnitEnum.TIMEUNIT;
683                     siu.Name = IfcSIUnitName.SECOND;
684                 });
685             });
686             var ifcPropertyEnumeratedValue = model.Instances.New<IfcPropertyEnumeratedValue>(pev =>
687             {
688                 pev.Name = "IfcPropertyEnumeratedValue:Music";
689                 pev.EnumerationReference = model.Instances.New<IfcPropertyEnumeration>(pe =>
690                     {
691                         pe.Name = "Notes";
692                         pe.EnumerationValues.Add(new IfcLabel("Do"));
693                         pe.EnumerationValues.Add(new IfcLabel("Re"));
694                         pe.EnumerationValues.Add(new IfcLabel("Mi"));
695                         pe.EnumerationValues.Add(new IfcLabel("Fa"));
696                         pe.EnumerationValues.Add(new IfcLabel("So"));
697                         pe.EnumerationValues.Add(new IfcLabel("La"));
698                         pe.EnumerationValues.Add(new IfcLabel("Ti"));
699                     });
700                 pev.EnumerationValues.Add(new IfcLabel("Do"));
701                 pev.EnumerationValues.Add(new IfcLabel("Re"));
702                 pev.EnumerationValues.Add(new IfcLabel("Mi"));
703 
704             });
705             var ifcPropertyBoundedValue = model.Instances.New<IfcPropertyBoundedValue>(pbv => 
706             {
707                 pbv.Name = "IfcPropertyBoundedValue:Mass";
708                 pbv.Description = "";
709                 pbv.UpperBoundValue = new IfcMassMeasure(5000.0);
710                 pbv.LowerBoundValue = new IfcMassMeasure(1000.0);
711                 pbv.Unit = model.Instances.New<IfcSIUnit>(siu =>
712                 {
713                     siu.UnitType = IfcUnitEnum.MASSUNIT;
714                     siu.Name = IfcSIUnitName.GRAM;
715                     siu.Prefix = IfcSIPrefix.KILO;                  
716                 });
717             });
718 
719             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), };
720             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), };
721             var ifcPropertyTableValue = model.Instances.New<IfcPropertyTableValue>(ptv =>
722             {
723                 ptv.Name = "IfcPropertyTableValue:Sound";
724                 foreach (var item in definingValues)
725                 {
726                     ptv.DefiningValues.Add(item);
727                 }
728                 foreach (var item in definedValues)
729                 {
730                     ptv.DefinedValues.Add(item);
731                 }
732                 ptv.DefinedUnit = model.Instances.New<IfcContextDependentUnit>(cd =>
733                 {
734                     cd.Dimensions = model.Instances.New<IfcDimensionalExponents>(de =>
735                     {
736                         de.LengthExponent = 0;
737                         de.MassExponent = 0;
738                         de.TimeExponent = 0;
739                         de.ElectricCurrentExponent = 0;
740                         de.ThermodynamicTemperatureExponent = 0;
741                         de.AmountOfSubstanceExponent = 0;
742                         de.LuminousIntensityExponent = 0;
743                     });
744                     cd.UnitType = IfcUnitEnum.FREQUENCYUNIT;
745                     cd.Name = "dB";
746                 });
747 
748 
749             });
750 
751             var listValues = new List<IfcLabel> { new IfcLabel("Red"), new IfcLabel("Green"), new IfcLabel("Blue"), new IfcLabel("Pink"), new IfcLabel("White"), new IfcLabel("Black"), };
752             var ifcPropertyListValue = model.Instances.New<IfcPropertyListValue>(plv =>
753             {
754                 plv.Name = "IfcPropertyListValue:Colours";
755                 foreach (var item in listValues)
756                 {
757                     plv.ListValues.Add(item);
758                 }
759             });
760 
761             var ifcMaterial = model.Instances.New<IfcMaterial>(m =>
762             {
763                 m.Name = "Brick";
764             });
765             var ifcPrValueMaterial = model.Instances.New<IfcPropertyReferenceValue>(prv =>
766             {
767                 prv.Name = "IfcPropertyReferenceValue:Material";
768                 prv.PropertyReference = ifcMaterial;
769             });
770 
771 
772             var ifcMaterialList = model.Instances.New<IfcMaterialList>(ml =>
773                 {
774                     ml.Materials.Add(ifcMaterial);
775                     ml.Materials.Add(model.Instances.New<IfcMaterial>(m =>{m.Name = "Cavity";}));
776                     ml.Materials.Add(model.Instances.New<IfcMaterial>(m => { m.Name = "Block"; }));
777                 });
778 
779 
780             var ifcMaterialLayer = model.Instances.New<IfcMaterialLayer>(ml =>
781             {
782                 ml.Material = ifcMaterial;
783                 ml.LayerThickness = 100.0;
784             });
785             var ifcPrValueMatLayer = model.Instances.New<IfcPropertyReferenceValue>(prv =>
786             {
787                 prv.Name = "IfcPropertyReferenceValue:MaterialLayer";
788                 prv.PropertyReference = ifcMaterialLayer;
789             });
790 
791             var ifcDocumentReference = model.Instances.New<IfcDocumentReference>(dr =>
792             {
793                 dr.Name = "Document";
794                 dr.Location = "c://Documents//TheDoc.Txt";
795             });
796             var ifcPrValueRef = model.Instances.New<IfcPropertyReferenceValue>(prv =>
797             {
798                 prv.Name = "IfcPropertyReferenceValue:Document";
799                 prv.PropertyReference = ifcDocumentReference;
800             });
801 
802             var ifcTimeSeries = model.Instances.New<IfcRegularTimeSeries>(ts =>
803             {
804                 ts.Name = "Regular Time Series";
805                 ts.Description = "Time series of events";
806                 ts.StartTime = new IfcDateTime("2015-02-14T12:01:01");
807                 ts.EndTime = new IfcDateTime("2015-05-15T12:01:01");
808                 ts.TimeSeriesDataType = IfcTimeSeriesDataTypeEnum.CONTINUOUS;
809                 ts.DataOrigin = IfcDataOriginEnum.MEASURED;
810                 ts.TimeStep = 604800; //7 days in secs
811             });
812 
813             var ifcPrValueTimeSeries = model.Instances.New<IfcPropertyReferenceValue>(prv =>
814             {
815                 prv.Name = "IfcPropertyReferenceValue:TimeSeries";
816                 prv.PropertyReference = ifcTimeSeries;
817             });
818 
819             var ifcAddress = model.Instances.New<IfcPostalAddress>(a =>
820             {
821                 a.InternalLocation = "Room 101";
822                 a.AddressLines.AddRange(new[] { new IfcLabel("12 New road"), new IfcLabel("DoxField" ) });
823                 a.Town = "Sunderland";
824                 a.PostalCode = "DL01 6SX";
825             });
826             var ifcPrValueAddress = model.Instances.New<IfcPropertyReferenceValue>(prv =>
827             {
828                 prv.Name = "IfcPropertyReferenceValue:Address";
829                 prv.PropertyReference = ifcAddress;
830             });
831             var ifcTelecomAddress = model.Instances.New<IfcTelecomAddress>(a =>
832             {
833                 a.TelephoneNumbers.Add(new IfcLabel("01325 6589965"));
834                 a.ElectronicMailAddresses.Add(new IfcLabel("bob@bobsworks.com"));
835             });
836             var ifcPrValueTelecom = model.Instances.New<IfcPropertyReferenceValue>(prv =>
837             {
838                 prv.Name = "IfcPropertyReferenceValue:Telecom";
839                 prv.PropertyReference = ifcTelecomAddress;
840             });
841 
842 
843 
844             //设置模型元素数量
845             var ifcPropertySet = model.Instances.New<IfcPropertySet>(ps =>
846             {              
847                 ps.Name = "Test:IfcPropertySet";
848                 ps.Description = "Property Set";
849                 ps.HasProperties.Add(ifcPropertySingleValue);
850                 ps.HasProperties.Add(ifcPropertyEnumeratedValue);
851                 ps.HasProperties.Add(ifcPropertyBoundedValue);
852                 ps.HasProperties.Add(ifcPropertyTableValue);
853                 ps.HasProperties.Add(ifcPropertyListValue);
854                 ps.HasProperties.Add(ifcPrValueMaterial);
855                 ps.HasProperties.Add(ifcPrValueMatLayer);
856                 ps.HasProperties.Add(ifcPrValueRef);
857                 ps.HasProperties.Add(ifcPrValueTimeSeries);
858                 ps.HasProperties.Add(ifcPrValueAddress);
859                 ps.HasProperties.Add(ifcPrValueTelecom);             
860             });
861 
862             //需建立关系
863             model.Instances.New<IfcRelDefinesByProperties>(rdbp =>
864             {                
865                 rdbp.Name = "Property Association";
866                 rdbp.Description = "IfcPropertySet associated to wall";
867                 rdbp.RelatedObjects.Add(wall);
868                 rdbp.RelatingPropertyDefinition = ifcPropertySet;
869             });
870         }
871 
872         private static void CreateElementQuantity(IfcStore model, IfcWallStandardCase wall)
873         {
874             //创建模型元素数量
875             //首先我们需模型简单物理量,首先将使用模型量长度
876             var ifcQuantityArea = model.Instances.New<IfcQuantityLength>(qa =>
877             {
878                 qa.Name = "IfcQuantityArea:Area";
879                 qa.Description = "";
880                 qa.Unit = model.Instances.New<IfcSIUnit>(siu =>
881                 {
882                     siu.UnitType = IfcUnitEnum.LENGTHUNIT;
883                     siu.Prefix = IfcSIPrefix.MILLI;
884                     siu.Name = IfcSIUnitName.METRE;
885                 });
886                 qa.LengthValue = 100.0;
887 
888             });
889             //上下文相关单元的数量计数
890             var ifcContextDependentUnit = model.Instances.New<IfcContextDependentUnit>(cd =>
891                 {
892                     cd.Dimensions = model.Instances.New<IfcDimensionalExponents>(de =>
893                         {
894                             de.LengthExponent = 1;
895                             de.MassExponent = 0;
896                             de.TimeExponent = 0;
897                             de.ElectricCurrentExponent = 0;
898                             de.ThermodynamicTemperatureExponent = 0;
899                             de.AmountOfSubstanceExponent = 0;
900                             de.LuminousIntensityExponent = 0;
901                         });
902                     cd.UnitType = IfcUnitEnum.LENGTHUNIT;
903                     cd.Name = "Elephants";
904                 });
905                 var ifcQuantityCount = model.Instances.New<IfcQuantityCount>(qc =>
906                 {
907                     qc.Name = "IfcQuantityCount:Elephant";
908                     qc.CountValue = 12;
909                     qc.Unit = ifcContextDependentUnit;
910                 });
911 
912 
913              //使用转换单位
914             var ifcConversionBasedUnit = model.Instances.New<IfcConversionBasedUnit>(cbu =>
915             {
916                 cbu.ConversionFactor = model.Instances.New<IfcMeasureWithUnit>(mu =>
917                 {
918                     mu.ValueComponent = new IfcRatioMeasure(25.4);
919                     mu.UnitComponent = model.Instances.New<IfcSIUnit>(siu =>
920                     {
921                         siu.UnitType = IfcUnitEnum.LENGTHUNIT;
922                         siu.Prefix = IfcSIPrefix.MILLI;
923                         siu.Name = IfcSIUnitName.METRE;
924                     });
925 
926                 });
927                 cbu.Dimensions = model.Instances.New<IfcDimensionalExponents>(de =>
928                 {
929                     de.LengthExponent = 1;
930                     de.MassExponent = 0;
931                     de.TimeExponent = 0;
932                     de.ElectricCurrentExponent = 0;
933                     de.ThermodynamicTemperatureExponent = 0;
934                     de.AmountOfSubstanceExponent = 0;
935                     de.LuminousIntensityExponent = 0;
936                 });
937                 cbu.UnitType = IfcUnitEnum.LENGTHUNIT;
938                 cbu.Name = "Inch";
939             });
940             var ifcQuantityLength = model.Instances.New<IfcQuantityLength>(qa =>
941             {
942                 qa.Name = "IfcQuantityLength:Length";
943                 qa.Description = "";
944                 qa.Unit = ifcConversionBasedUnit;
945                 qa.LengthValue = 24.0;
946             });
947 
948             //lets create the IfcElementQuantity
949             var ifcElementQuantity = model.Instances.New<IfcElementQuantity>(eq =>
950             {               
951                 eq.Name = "Test:IfcElementQuantity";
952                 eq.Description = "Measurement quantity";
953                 eq.Quantities.Add(ifcQuantityArea);
954                 eq.Quantities.Add(ifcQuantityCount);
955                 eq.Quantities.Add(ifcQuantityLength);
956             });
957 
958             //下步 建议关系
959             model.Instances.New<IfcRelDefinesByProperties>(rdbp =>
960             {              
961                 rdbp.Name = "Area Association";
962                 rdbp.Description = "IfcElementQuantity associated to wall";
963                 rdbp.RelatedObjects.Add(wall);
964                 rdbp.RelatingPropertyDefinition = ifcElementQuantity;
965             });
966         }
967     
968     }
Code

 参考资料:

IFC4国内说明文档:http://www.vfkjsd.cn/ifc/ifc4/index.htm

IFC4官方说明文档:http://www.buildingsmart-tech.org/ifc/IFC4/final/html/link/ifcarbitraryclosedprofiledef.htm  

xBIM 应用与学习 https://www.cnblogs.com/w2011/p/8407286.html

xBIM之二:构建墙和门窗

原文:https://www.cnblogs.com/yhlx125/p/10978404.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!