IfcOpenShell 上手教程
关联项目:IfcOpenShell 前置要求:Python 3.9+(以当前版本支持矩阵为准);pip;了解 IFC 基本概念(实体类型、GlobalId、属性集);有至少一个
.ifc样例文件。 预计耗时:pip 安装约 5 分钟;跑通读取与查询约 20 分钟;完成几何提取与 glTF 转换约 1 小时。 官方仓库:github.com/IfcOpenShell/IfcOpenShell 前置要求:Python 3.9+(以当前版本支持矩阵为准);pip;了解 IFC 基本概念(实体类型、GlobalId、属性集);有至少一个.ifc样例文件。预计耗时:pip 安装约 5 分钟;跑通读取与查询约 20 分钟;完成几何提取与 glTF 转换约 1 小时。
官方仓库:github.com/IfcOpenShell/IfcOpenShell
本教程带你完成从环境准备到跑通核心工作流的完整过程。命令与示例针对主流环境给出,实际版本参数请以官方文档为准。
一、环境准备
Python 绑定推荐直接用 pip 安装预编译 wheel:
python -m pip install ifcopenshell
python -c "import ifcopenshell; print(ifcopenshell.__file__)"
若 pip 无可用 wheel,可按官方文档从源码编译 C++ 核心;IfcConvert 通常随包提供,命令行输入 IfcConvert --help 验证。
二、安装部署
验证导入与版本信息:
python -c "import ifcopenshell; print(ifcopenshell.version)"
IfcConvert --help
预期输出:打印版本号字符串;IfcConvert --help 显示可用参数说明(不编造具体 flags,实际以当前版本为准)。两条命令均无报错即安装成功。
三、快速上手:跑通第一个示例
打开一个 IFC 文件并统计墙体:
import ifcopenshell
model = ifcopenshell.open("example.ifc")
print("schema:", model.schema)
walls = model.by_type("IfcWall")
print("walls:", len(walls))
for wall in walls[:5]:
print(wall.GlobalId, wall.Name)
ifcopenshell.open() 返回 IFC 文件对象;model.schema 显示如 IFC4 的 schema 名;by_type("IfcWall") 返回该类型实体列表;GlobalId 与 Name 是 IFC 标准属性。把 example.ifc 换成你自己的模型路径即可运行。
四、核心工作流
任务:统计墙体并按类型汇总,最后导出 glTF 供 Web 查看
- 读取模型并统计构件:
import ifcopenshell
from collections import Counter
model = ifcopenshell.open("sample.ifc")
walls = model.by_type("IfcWall")
print("wall count:", len(walls))
print(Counter(w.Name for w in walls).most_common(5))
预期:输出墙总数与出现最多的名称。
- 提取第一面墙的几何数据:
import ifcopenshell.geom
settings = ifcopenshell.geom.settings()
settings.set(settings.USE_WORLD_COORDS, True)
shape = ifcopenshell.geom.create_shape(settings, walls[0])
print("vertices:", len(shape.geometry.verts), "faces:", len(shape.geometry.faces))
USE_WORLD_COORDS 让坐标落在世界坐标系;verts/faces 是扁平化的顶点与索引数组。
- 按类型筛选:
doors = model.by_type("IfcDoor")
print("doors:", len(doors))
- 整模型转 glTF:
IfcConvert sample.ifc model.glb
ls -lh model.glb
预期:生成 model.glb 文件;若报缺少依赖,检查几何库与安装包是否完整。
- 核对转换结果:把
model.glb拖入任意 glTF 查看器,确认墙、门等构件可见。
五、进阶实践
- 高层编辑 API:
ifcopenshell.api.run("root.create_entity", model, ifc_class="IfcWall")一类调用可创建实体;函数名与参数以当前版本文档为准。 - 几何设置:
settings.set(settings.USE_WORLD_COORDS, True)、settings.set(settings.WELD_VERTICES, True)等控制坐标与顶点焊接,可用项以官方文档为准。 - 常用工具函数:
ifcopenshell.util.placement.get_local_placement(element.ObjectPlacement)获取构件位置;ifcopenshell.util.unit.calculate_unit_scale(model)处理单位换算。 - 属性集读取:遍历
element.IsDefinedBy找到IfcRelDefinesByProperties,再取RelatingPropertyDefinition的属性,复杂模型建议参考官方示例。 - 批量转换脚本:用
subprocess.run(["IfcConvert", src, dst], check=True)在 Python 中编排大量文件转换。
六、常见问题
Q:ifcopenshell.open() 报文件无法识别?
A:确认文件是合法的 IFC(STEP)文本而非压缩的 IFCZIP;若是 .ifczip,先解压为 .ifc。
Q:by_type("IfcWall") 返回空?
A:不同模型和视图定义可能用 IfcWallStandardCase 或把墙放进 IfcElementAssembly;可先打印 model.by_type("IfcElement") 看实际类型。
Q:几何生成失败或提示缺少库? A:几何内核依赖系统库,优先安装官方预编译 wheel;从源码编译时按文档装好 Open CASCADE 等依赖。
Q:能直接修改并保存 IFC 吗?
A:可以,但建议用 ifcopenshell.api 而不是直接改底层行;API 签名随版本变化,请以当前文档为准,写回前保留备份。
Q:LGPL-3.0 能用于商业软件吗? A:LGPL 允许作为库用于闭源程序,但需遵守动态链接、保留声明与修改开源等条款,法务细节请以 LICENSE 文本为准。
七、学习资源
- 官方文档:https://docs.ifcopenshell.org/
- Python API 教程:https://ifcopenshell.org/python.html
- IfcConvert 文档:https://ifcopenshell.org/ifcconvert.html
- 仓库与示例:https://github.com/IfcOpenShell/IfcOpenShell