在python中使用IfcOpenShell提取大量元素

时间:2018-05-24 17:29:57

标签: python ifc

我正在尝试使用Python中的ifc来读取IfcOpenShell文件中对象的最深层次数量。到目前为止,我有:

import ifcopenshell

path = r'D:\ifcos_1\slab.ifc'
ifc_file = ifcopenshell.open(path)

geometries = ifc_file.by_type("IfcProduct")

for geometry in geometries:
    if geometry.is_a("IfcSlab"):
        print geometry
        test = geometry.IfcPhysicalQuantity()
        print test

我研究过definitions

无论我尝试为test = geometry.X()放置哪种类型的功能,我都会收到错误:

File "C:\Python27\lib\site-packages\ifcopenshell\entity_instance.py", line 48, in __getattr__
   "entity instance of type '%s' has no attribute '%s'" % (self.wrapped_data.is_a(), name))
AttributeError: entity instance of type 'IfcSlab' has no attribute 'IfcPhysicalQuantity'

不确定如何解决这个问题并希望得到帮助。

修改

获得平板和进一步参考的进一步工作:

for geometry in geometries:
    if geometry.is_a("IfcSlab"):
        print geometry
        definedBy = geometry.IsDefinedBy

        print definedBy[0]

        for each in definedBy:
            test = each.is_a()
            print test

截至目前,障碍是与IFC4的兼容性,我将尝试使用this forum post.

的说明重新编译

编辑2:

现在进一步使用IFC 2x3标准,文件包含数量信息(通过原始数据验证)。以下是相关代码:

for geometry in geometries:
    if geometry.is_a("IfcSlab"):
        definedBy = geometry.IsDefinedBy

        for line in definedBy:
        test = line.is_a()
        # print test
        if line.is_a() == 'IfcRelDefinesByProperties' or line.is_a() == 'IfcRelDefinesByType':
            step1 = line.RelatingPropertyDefinition
            step2 = step1.is_a()
            print step2

无论我在step1 = line.之后放置什么都有错误,以下任何一项都不会产生结果:

line.IfcPropertySet
line.IfcElementQuantity
line.RelatingPropertyDefiniton

此代码的输出仍为:

IfcPropertySet
IfcPropertySet
IfcPropertySet
IfcPropertySet
IfcPropertySet
IfcPropertySet
IfcPropertySet
IfcPropertySet
IfcPropertySet
IfcPropertySet
IfcElementQuantity
IfcElementQuantity

这意味着我可以访问IfcElementQuantity,但没有任何属性可以使用。我看过schema reference但找不到正确的。

2 个答案:

答案 0 :(得分:3)

实现此功能时,您应该注意导入库使用的IFC版本 - 由IfcOpenShell-Website分发的版本与IFC2X3配合使用。对于IFC4,您可能需要自己编译版本。 (您可以使用ifcopenshell.schema_identifier)检查您的IFC版本

即使我在谈论IFC2X3,我也建立了关于buildingSMART IFC 4定义的链接。在buildingSMART网页上,IFC2X3的差异以红色标记。并且IFC4定义更易于阅读(IMO)。

数量不像属性那样直接附加。它们被写为属性集,然后与元素或元素类型相关。首先,您应该确保您的IFC文件包含数量 - 否则您将找不到任何数量。通常,您从特定产品开始 - ifc_file.by_type('IfcSlab')。您可以通过反向属性访问属性集 - 这些属性通常由IFC库设置,它们不会直接作为文件中的属性出现。

我把你的示例文件缩短到大约三分之一(所以它仍然是一个有效的IFC2X3文件):

ISO-10303-21;
HEADER;FILE_DESCRIPTION(('ViewDefinition [Custom, QuantityTakeOffAddOnView, SpaceBoundary2ndLevelAddOnView]','Option [Drawing Scale: 100.000000]','Option [Global Unique Identifiers (GUID): Keep existing]','Option [Elements to export: Visible elements (on all stories)]','Option [Partial Structure Display: Entire Model]','Option [IFC Domain: All]','Option [Structural Function: All Elements]','Option [Convert Grid elements: On]','Option [Convert IFC Annotations and ARCHICAD 2D elements: Off]','Option [Convert 2D symbols of Doors and Windows: Off]','Option [Explode Composite and Complex Profile elements into parts: On]','Option [Export geometries that Participates in Collision Detection only: Off]','Option [Elements in Solid Element Operations: Extruded/revolved]','Option [Elements with junctions: Extruded/revolved without junctions]','Option [Slabs with slanted edge(s): Extruded]','Option [Use legacy geometric methods as in Coordination View 1.0: Off]','Option [IFC Site Geometry: As boundary representation (BRep)]','Option [IFC Site Location: At Project Origin]','Option [Curtain Wall export mode: Container Element]','Option [Railing export mode: Single Element]','Option [Stair export mode: Container Element]','Option [Properties To Export: All properties]','Option [Space containment: On]','Option [IFC Domain For Space Containment: All]','Option [Bounding Box: Off]','Option [Geometry to type objects: Off]','Option [Element Properties: All]','Option [Property Type Element Parameter: On]','Option [Quantity Type Element Parameter: On]','Option [IFC Base Quantities: On]','Option [Window Door Lining and Panel Parameters: On]','Option [IFC Space boundaries: On]','Option [ARCHICAD Zone Categories as IFC Space classification data: On]','Option [Element Classifications: On]'),'2;1');
FILE_NAME('D:\\Side Projects\\Paragraph3\\The database\\IFC Files\\Local tests\\ifcos_1\\slab.ifc','2018-06-13T18:28:40',('Architect'),('Building Designer Office'),'The EXPRESS Data Manager Version 5.02.0100.09 : 26 Sep 2013','IFC file generated by GRAPHISOFT ARCHICAD-64 21.0.0 INT FULL Windows version (IFC2x3 add-on version: 3005 INT FULL).','The authorising person');
FILE_SCHEMA(('IFC2X3'));
ENDSEC;
DATA;
#1= IFCPERSON($,'Undefined',$,$,$,$,$,$);
#7= IFCPERSONANDORGANIZATION(#1,#10,$);
#10= IFCORGANIZATION('GS','GRAPHISOFT','GRAPHISOFT',$,$);
#11= IFCAPPLICATION(#10,'21.0.0','ARCHICAD-64','IFC2x3 add-on version: 3005 INT FULL');
#12= IFCOWNERHISTORY(#7,#11,$,.ADDED.,$,$,$,1528907320);
#13= IFCSIUNIT(*,.LENGTHUNIT.,.MILLI.,.METRE.);
#14= IFCSIUNIT(*,.AREAUNIT.,$,.SQUARE_METRE.);
#16= IFCSIUNIT(*,.PLANEANGLEUNIT.,$,.RADIAN.);
#17= IFCMEASUREWITHUNIT(IFCPLANEANGLEMEASURE(0.0174532925199),#16);
#18= IFCDIMENSIONALEXPONENTS(0,0,0,0,0,0,0);
#19= IFCCONVERSIONBASEDUNIT(#18,.PLANEANGLEUNIT.,'DEGREE',#17);
#29= IFCUNITASSIGNMENT((#13,#14,#19));
#31= IFCDIRECTION((1.,0.,0.));
#35= IFCDIRECTION((0.,0.,1.));
#37= IFCCARTESIANPOINT((0.,0.,0.));
#39= IFCAXIS2PLACEMENT3D(#37,#35,#31);
#40= IFCDIRECTION((0.,1.));
#42= IFCGEOMETRICREPRESENTATIONCONTEXT($,'Model',3,1.00000000000E-5,#39,#40);
#45= IFCPROJECT('344O7vICcwH8qAEnwJDjSU',#12,'Project',$,$,$,$,(#42),#29);
#59= IFCLOCALPLACEMENT($,#39);
#62= IFCSITE('20FpTZCqJy2vhVJYtjuIce',#12,'Site',$,$,#59,$,$,.ELEMENT.,(47,33,34,948800),(19,3,17,204400),0.,$,$);
#68= IFCRELAGGREGATES('0Du7$nzQXCktKlPUTLFSAT',#12,$,$,#45,(#62));
#74= IFCQUANTITYLENGTH('GrossPerimeter',$,$,0.);
#76= IFCQUANTITYAREA('GrossArea',$,$,0.);
#77= IFCELEMENTQUANTITY('2GNZepdf73fvGc$0W6rozj',#12,'BaseQuantities',$,'ARCHICAD BIM Base Quantities',(#74,#76));
#82= IFCRELDEFINESBYPROPERTIES('2Hm9JvZjohDNSD2kdxZI3b',#12,$,$,(#62),#77);
#93= IFCLOCALPLACEMENT(#59,#39);
#95= IFCBUILDING('00tMo7QcxqWdIGvc4sMN2A',#12,'Building',$,$,#93,$,$,.ELEMENT.,$,$,$);
#97= IFCRELAGGREGATES('2b_h_mYcGArd6glJG2Fmbt',#12,$,$,#62,(#95));
#101= IFCQUANTITYAREA('GrossFloorArea',$,$,0.);
#102= IFCELEMENTQUANTITY('1kQMlmT0rD35a9E43iKTas',#12,'BaseQuantities',$,'ARCHICAD BIM Base Quantities',(#101));
#104= IFCRELDEFINESBYPROPERTIES('0L87OdSD3DqSTjSRlAciZL',#12,$,$,(#95),#102);
#115= IFCLOCALPLACEMENT(#93,#39);
#117= IFCBUILDINGSTOREY('1oZ0wPs_PE8ANCPg3bIs4j',#12,'Ground Floor',$,$,#115,$,$,.ELEMENT.,0.);
#119= IFCRELAGGREGATES('118jwqMnuwK1xuf97w7fU5',#12,$,$,#95,(#117));
#180= IFCSLAB('3W29Drc$H6CxK3FGIxjJNl',#12,'SLA - 001',$,$,$,$,'E0089375-9BF4-4633-B503-3D04BBB535EF',.FLOOR.);
#195= IFCRELCONTAINEDINSPATIALSTRUCTURE('04ldtj6cp2dME6CiP80Bzh',#12,$,$,(#180),#117);
#326= IFCPROPERTYSINGLEVALUE('Fragility rating',$,IFCLABEL('0'),$);
#327= IFCPROPERTYSINGLEVALUE('Tile dimensions',$,IFCLABEL('Undefined'),$);
#328= IFCPROPERTYSINGLEVALUE('Anti-static Surface',$,IFCBOOLEAN(.F.),$);
#329= IFCPROPERTYSINGLEVALUE('Non-skid Surface',$,IFCBOOLEAN(.F.),$);
#330= IFCPROPERTYSET('0LYX8AqOOS9ft8M4aJYEYa',#12,'FLOORINGS',$,(#326,#327,#328,#329));
#332= IFCRELDEFINESBYPROPERTIES('1G6WWCSQGg0PdTnW7hwMrM',#12,$,$,(#180),#330);
#335= IFCPROPERTYSINGLEVALUE('Renovation Status',$,IFCLABEL('Existing'),$);
#336= IFCPROPERTYSET('0cR6wsk2QWcLKPchA8mF3u',#12,'AC_Pset_RenovationAndPhasing',$,(#335));
#338= IFCRELDEFINESBYPROPERTIES('3nYD8KGPhoBw5okmj1JjsA',#12,$,$,(#180),#336);
#341= IFCQUANTITYLENGTH('Width',$,$,300.);
#342= IFCQUANTITYLENGTH('Perimeter',$,$,22000.);
#343= IFCQUANTITYAREA('GrossArea',$,$,28.);
#344= IFCQUANTITYAREA('NetArea',$,$,28.);
#345= IFCQUANTITYVOLUME('GrossVolume',$,$,8.4);
#346= IFCQUANTITYVOLUME('NetVolume',$,$,8.4);
#347= IFCELEMENTQUANTITY('1RfXJewSc7OCIaD$L2ZoXT',#12,'BaseQuantities',$,'ARCHICAD BIM Base Quantities',(#341,#342,#343,#344,#345,#346));
#349= IFCRELDEFINESBYPROPERTIES('085uLttAQRllG3nL_YikZ8',#12,$,$,(#180),#347);
#375= IFCQUANTITYVOLUME('Gross Volume of the Slab',$,$,8.4);
#376= IFCQUANTITYVOLUME('Gross Volume of the Slab with Holes',$,$,8.4);
#377= IFCQUANTITYLENGTH('Holes Perimeter',$,$,0.);
#378= IFCQUANTITYAREA('Holes Surface Area',$,$,0.);
#379= IFCQUANTITYLENGTH('Perimeter',$,$,22000.);
#381= IFCQUANTITYAREA('Top Surface Area',$,$,28.);
#382= IFCELEMENTQUANTITY('0DuZ12CVtssgcIQPaQ$1sp',#12,'ArchiCADQuantities',$,'ARCHICAD BIM Quantities',(#375,#376,#377,#378,#379,#381));
#384= IFCRELDEFINESBYPROPERTIES('0KgGv0Y8Fc2jg8BCPhxnM5',#12,$,$,(#180),#382);
#393= IFCSLABTYPE('0K1otpnkQcEpOBXPxnZ3dB',#12,'Timber - Floor 300',$,$,(#396),$,'14072DF3-C6E6-A63B-360B-859EF18C39CB',$,.FLOOR.);
#395= IFCRELDEFINESBYTYPE('353egCMRpZtJd$CDCoSsCb',#12,$,$,(#180),#393);
#352= IFCQUANTITYAREA('Area',$,$,28.);
#353= IFCQUANTITYLENGTH('Height',$,$,300.);
#354= IFCQUANTITYVOLUME('Net Volume',$,$,8.4);
#396= IFCELEMENTQUANTITY('1Zyxf4r7NogSp4V7ORMpET',#12,'ArchiCADQuantities',$,'ARCHICAD BIM Quantities',(#352,#353,#354));
ENDSEC;
END-ISO-10303-21;

这是一块附有面积测量的平板。它应该具有反向属性IsDefinedBy。在IFC2X3中,这指向实体IfcRelDefinesByPropertiesIfcRelDefinesByType的列表。使用IFC4,IfcRelDefinesByType将被放入逆属性IsTypedBy

每个IfcRelDefinesByProperties指向其属性RelatingPropertyDefinition中设置的属性。有各种属性集类型,您希望在搜索物理量时它是IfcElementQuantity类型。在迭代属性关系列表时,您必须在运行时检查您当前持有的类型。

数量集在属性Quantities中附加了IfcPhysicalQuantities列表。这些可以是简单数量或复数量,由多个简单量组成。对于简单数量,区域,计数或重量有特定的子类型。同样,您必须在运行时检查具体类型。

区域数量本身有一个名称和描述,以提供更多背景(不是我们的,但可能在现实世界中)。 value属性以数量类型命名,因此IfcQuantityArea具有属性AreaValue。同样感兴趣的是属性Unit,它是对值单位的引用。如果未设置(如我们的示例中所示),则需要在IfcProject实体中查找已分配的单位。

不幸的是,这可能不是全部。如果对象(这里是我们的IfcSlab)具有指定的对象类型,那么该类型也可以附加属性集(我将示例文件修改为这种情况)。你的类型实体没有逆属性,而是直接属性HasProperties。如果已设置,您可以通过它发现附加属性。

总而言之,您可能需要多个循环:

  1. 对于每个对象,获取属性集
  2. 对于每个属性集,测试它是否为数量集
  3. 对于每个数量集,请参阅数量
  4. 如果对象具有用户定义的类型,则可能重复此搜索。

    以下代码应完全执行此操作(在我的机器上使用python 3.5.4和ifcopenshell使用IFC2X3架构编写)):

    1. 从文件中获取所有平板(只有一个)
    2. 浏览逆属性IsDefinedBy中的所有实体 - 可以是IfcRelDefinesByPropertiesIFcRelDefinesByType
    3. 从实体获取属性集
    4. 检查给定的属性集是否为IfcElementQuantity,如果是,则继续打印数量。
    5. import ifcopenshell
      def print_quantities(property_definition):
        if 'IfcElementQuantity' == property_definition.is_a():
          for quantity in property_definition.Quantities:
            if 'IfcQuantityArea' == quantity.is_a():
              print('Area value: ' + str(quantity.AreaValue))
            if 'IfcQuantityVolume' == quantity.is_a():
              print('Volume value: ' + str(quantity.VolumeValue))
            if 'IfcQuantityLength' == quantity.is_a():
              print('Length value: ' + str(quantity.LengthValue))
      
      ifc_file = ifcopenshell.open('slab.ifc')
      products = ifc_file.by_type('IfcSlab')
      for product in products:
        if product.IsDefinedBy:
          definitions = product.IsDefinedBy
          for definition in definitions:
            #In IFC2X3 this could be property or type
            #in IFC4 type is in inverse attribute IsTypedBy
            if 'IfcRelDefinesByProperties' == definition.is_a():
              property_definition = definition.RelatingPropertyDefinition
              print_quantities(property_definition)
            if 'IfcRelDefinesByType' == definition.is_a():
              type = definition.RelatingType
              if type.HasPropertySets:
                for property_definition in type.HasPropertySets:
                  print_quantities(property_definition)
      

      对于该示例,结果为:

      Length value: 300.0
      Length value: 22000.0
      Area value: 28.0
      Area value: 28.0
      Volume value: 8.4
      Volume value: 8.4
      Volume value: 8.4
      Volume value: 8.4
      Length value: 0.0
      Area value: 0.0
      Length value: 22000.0
      Area value: 28.0
      Area value: 28.0
      Length value: 300.0
      Volume value: 8.4
      

答案 1 :(得分:0)

我曾经写过几个函数,以提取所有属性。

https://github.com/johannesmichael/ifc-python/blob/master/modules/ifc_pset_utils.py

可能不完整,但是可以给您一个想法。 我将所有内容提取出来以进行进一步的分析。

BaseQuantities:

def get_related_quantities(ifc_instance):
"""
Returns a list of IfcElementQuantity for given IFC ID
argument: ifc_instance
return: list of property sets
"""
quantities_list =[]
for x in ifc_instance.IsDefinedBy:
    if x.is_a("IfcRelDefinesByProperties"):
        if x.RelatingPropertyDefinition.is_a("IfcElementQuantity"):
            quantities_list.append(x.RelatingPropertyDefinition)
return quantities_list  

def get_quantity_single_value(x):
"""
Returns a dict of dicts of IfcElementQuantity single values.
Returning a dictionary of dictionaries
is used, because it is easy to transform to pandas.DataFrame 
argument: IFC Element as contained in list from get_related_property_sets()
return: dict of property single values like {"IfcName":"xx", "IfcGlobalId": "klkhlkh", ......}
"""
quantities_dicts = {}
for y in x.Quantities:
    if y.is_a('IfcQuantityArea'):
        quantities_dicts.update({y.Name:y.AreaValue})
    if y.is_a('IfcQuantityLength'):
        quantities_dicts.update({y.Name:y.LengthValue})
    if y.is_a('IfcQuantityVolume'):
        quantities_dicts.update({y.Name:y.VolumeValue})
    if y.is_a('IfcQuantityCount'):
        quantities_dicts.update({y.Name:y.CountValue})
    if y.is_a('IfcQuantityWeight'):
        quantities_dicts.update({y.Name:y.WeightValue})

return quantities_dicts 

这是针对IFC 2x3 TC1完成的 希望有帮助