NSDT工具推荐Three.js AI纹理开发包 - YOLO合成数据生成器 - GLTF/GLB在线编辑 - 3D模型格式在线转换 - 可编程3D场景编辑器 - REVIT导出3D模型插件 - 3D模型语义搜索引擎 - Three.js虚拟轴心开发包 - 3D模型在线减面 - STL模型在线切割

IFC 是用于存储 BIM 数据的 ISO 标准格式。IfcOpenShell 是一个包含 Python 库的项目,可用于解析 IFC 文件。首先,你需要下载并安装 IfcOpenShell python。目前没有 IfcOpenShell 的 API 文档,但考虑到它主要源自 IFC 规范,因此不难理解。

在解析 IFC 文件时,如果你想使用 Linux、Windows 和 Mac 上可用的开源查看器查看 IFC 文件,我建议你阅读如何在 Linux 上查看 BIM IFC 文件。这些查看器是开源的,并且相对严格,与 Revit 等商业查看器相比,它们在正确解析 IFC 方面做得很差,或者像 Solibri 这样的查看器在解析方面非常宽松,并推广结构不良的 IFC。

下面的示例使用 2X3,因为目前假设大多数 IFC 文件都是 2X3,但这些原则仍然适用于 IFC4。截至撰写本文时,IfcOpenShell 根据编译时标志确定 IFC2X3 或 IFC4 解析,默认情况下设置为 IFC2X3。如果你希望使用 IFC4,则需要使用  USE_IFC4=Y 编译 IfcOpenShell。

让我们从加载 IFC 文件开始:

import ifcopenshell
ifc_file = ifcopenshell.open('/path/to/your/file.ifc')

获取 IFC 类的所有类型,并列出它们:

products = ifc_file.by_type('IfcProduct')
for product in products:
    print(product.is_a())

仅打印关键字本身就会吐出定义它的 IFC 字符串:

print(product) # Prints #38=IfcWall('3OFfnkBQ0HwPPAt4e_Z09T',#5,'Wall','',$,#35,#37,$)
The is_a() function also works as a boolean.

ifc_file.by_type('IfcWall')[0].is_a('IfcWall') # True
ifc_file.by_type('IfcWall')[0].is_a('IfcSlab') # False

这种 Python 式小写下划线命名惯例以 by_type()is_a() 结尾。其余所有参数均由使用 CapsCase 的 IFC 模式生成。其余命名也不符合常见的编程惯例(例如,谓词前缀如 is应仅返回布尔值)。你通常会发现命名令人困惑的参数,它们暗示布尔值或其他值,但可能会返回集合或其他值。

通过简单的点符号获取任何 IFC 数据的属性。参数名称与 IFC 模式中显示的内容完全匹配,包括 CapsCase 惯例。

wall = ifc_file.by_type('IfcWall')[0]
print(wall.GlobalId)
print(wall.Name)

该参数还返回集合和关系,例如在以下示例中,我们想要列出与墙关联的属性集。根据 IfcWall 规范:

与 IfcWall 相关的属性集由 IfcPropertySet 定义,并由 IfcRelDefinesByProperties 关系附加。它可以通过逆 IsDefinedBy 关系访问。

因此,我们可以使用 IsDefinedBy 关系,它返回一组 IfcRelDefines FOR RelatedObjects;IfcRelDefines 是一个抽象超类型,因此在这种特殊情况下(属性集关系),我们期望 IfcRelDefinesByProperties 具有 RelatingPropertyDefinition 来存储属性集本身。此属性集是 IfcPropertySetDefinition。它再次是一个抽象超类型,但至少它在此级别指定了一个 Name 参数。这是属性集的名称。

wall = ifc_file.by_type('IfcWall')[0]
for definition in wall.IsDefinedBy:
    # To support IFC2X3, we need to filter our results.
    if definition.is_a('IfcRelDefinesByProperties'):
        property_set = definition.RelatingPropertyDefinition
        print(property_set.Name) # Might return Pset_WallCommon

在这种情况下,抽象超类型 IfcPropertySetDefinition 仅由 IfcPropertySet 子类型化,它有一个容易混淆的 HasProperties 参数,该参数包含一组 IfcProperty。`IfcProperty` 的类型各不相同,我们可以使用 is_a() 检查。

for property in property_set.HasProperties:if property.is_a('IfcPropertySingleValue'):print(property.Name)print(property.NominalValue.wrappedValue)

其他数据(例如数量使用)也使用 isDefinedBy 关系,但我们可以使用 is_a() 进行区分。

wall = ifc_file.by_type('IfcWall')[0]
for definition in wall.IsDefinedBy:
    related_data = definition.RelatingPropertyDefinition
    if related_data.is_a('IfcPropertySet'):
        pass
    elif related_data.is_a('IfcElementQuantity'):
        print_element_quantities(related_data)

IFC 数量有很多种,因此我们必须小心处理。但此示例仅处理 IfcQuantityLength

def print_element_quantities(element_quantity):
    for quantity in element_quantity.Quantities:
        print(quantity.Name)
        if quantity.is_a('IfcQuantityLength'):
            print(quantity.lengthValue)

你还可以从任何 IFC 元素的放置开始获取几何数据。IFC 对象放置很复杂,因此你应该注意查看坐标如何从 IFC 中的各种空间容器中派生出来。

if wall.ObjectPlacement.PlacementRelTo:
    # Inherit the coordinates of its parents
    pass
local_coordinates = wall.ObjectPlacement.RelativePlacement.Location[0]

最后,你还可以获得 IFC 元素的实际几何表示。与放置一样,IFC 表示也很复杂。类型很多,并且 IFC 元素可能在不同的上下文中有多种表示。这超出了快速演示的范围。在这个例子中,我们不会详细介绍,而只是处理从轴挤出的典型对象,这是一种非常常见的对象类型,如下所示。

我们还将看到如何访问挤压中使用的基础轮廓,如下所示:

此代码将访问相关的几何数据:

geometry = wall.Representation.Representations[0].Items[0] # An IfcExtrudedAreaSolid in this example
print(geometry.Position.Location[0]) # The centroid of the wall, so if the wall axis goes from (0, 0, 0) to (4, 0, 0) it will be (2, 0, 0)
print(geometry.ExtrudeDirection) # A vector pointing up (0, 0, 1)
print(geometry.Depth) # The height of the wall, say 3000
print(geometry.SweptArea) # A closed and filled area curve that can be extruded into a manifold, solid object
print(geometry.SweptArea.OuterCurve.Points) # the list of points that are in the polyline

这些点(即 geometry.SweptArea.OuterCurve.Points)给出相对于 geometry.Position.Location[0] 的坐标。

几何图形非常微妙,除非你想自找麻烦,否则我不建议你大胆地手动编辑它。有很多工具可以帮助你做到这一点,或者将几何图形可视化。

有关更多示例,可以在此处查看使用 IfcOpenShell 构建简单查看器的示例。


原文链接:Using IfcOpenShell to parse IFC files with Python

BimAnt翻译整理,转载请标明出处