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

IFC 是用于存储 BIM 数据的 ISO 标准格式。 IfcOpenShell 是一个包含 Python 库的项目,可以用来解析 IFC 文件。

1、下载安装IfcOpenShell

首先,我们需要下载并安装 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。

2、加载IFC文件

让我们从加载 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,$)

is_a() 函数也可以用作布尔值:

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

这种 pythonic 小写下划线命名约定以 by_type()is_a() 结尾。 所有其余参数都是从使用 CapsCase 的 IFC 模式生成的。 其余的命名也不遵循通用的编程约定(例如,像 is 这样的谓词前缀应该只返回布尔值)。可能会发现令人困惑的命名参数,这些参数暗示布尔值或其他,但可能会返回一个集合或其他一些值。

3、读取IFC属性数据

通过简单的点符号可以获取任何 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 是一个抽象超类型,因此在这种特定情况下(属性集关系),我们期望具有 RelatingPropertyDefinition 的 IfcRelDefinesByProperties 来存储属性集本身。 此属性集是一个 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 进行子类型化,IfcPropertySet 具有一个容易混淆 HasProperties 参数,该参数包含一组 IfcPropertyIfcProperty 的类型各不相同,我们可以使用 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]

4、读取IFC几何数据

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

我们还将看到如何访问在挤出中使用的基础配置文件,如下所示:

在这种情况下,我们处理 OuterCurve,这是一条封闭的填充曲线。此代码将访问相关的几何数据:

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翻译整理,转载请标明出处