You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Python的ifcopenshell从零创建含顶点与面的IFC三角网格?

使用ifcopenshell创建三角网格IFC模型

核心思路

IFC中没有直接的"三角曲面"实体,你需要用IfcTriangulatedFaceSet定义三角网格,再用IfcBuildingElementProxy(通用建筑元素代理)承载这个几何形状,替代墙体这类标准构件。

完整示例脚本

import ifcopenshell
import ifcopenshell.api

# 1. 创建新IFC文件(示例用IFC4版本,也可指定IFC2X3)
ifc_file = ifcopenshell.api.run("project.create_file", version="IFC4")

# 2. 构建IFC必有的项目层级结构
project = ifcopenshell.api.run("root.create_entity", ifc_file, ifc_class="IfcProject", name="TriMesh_Project")
site = ifcopenshell.api.run("root.create_entity", ifc_file, ifc_class="IfcSite", name="TriMesh_Site")
building = ifcopenshell.api.run("root.create_entity", ifc_file, ifc_class="IfcBuilding", name="TriMesh_Building")
storey = ifcopenshell.api.run("root.create_entity", ifc_file, ifc_class="IfcBuildingStorey", name="TriMesh_Storey")

# 关联层级关系
ifcopenshell.api.run("aggregate.assign_object", ifc_file, relating_object=project, product=site)
ifcopenshell.api.run("aggregate.assign_object", ifc_file, relating_object=site, product=building)
ifcopenshell.api.run("aggregate.assign_object", ifc_file, relating_object=building, product=storey)

# 3. 替换成你自己的顶点和面数据
# vertices格式:[[x1,y1,z1], [x2,y2,z2], ...],IFC默认单位是米,毫米数据需除以1000
vertices = [
    [0.0, 0.0, 0.0],
    [1.0, 0.0, 0.0],
    [1.0, 1.0, 0.0],
    [0.0, 1.0, 0.0],
    [0.5, 0.5, 1.0]
]
# faces格式:[[v1_idx, v2_idx, v3_idx], ...],需转换为IFC要求的1-based索引
faces = [
    [0, 1, 4],
    [1, 2, 4],
    [2, 3, 4],
    [3, 0, 4],
    [0, 1, 2],
    [0, 2, 3]
]

# 4. 将顶点转为IFC点列表
point_list = ifcopenshell.api.run("geometry.create_cartesian_point_list_3d", ifc_file, coordinates=vertices)

# 5. 转换面索引为1-based格式
ifc_faces = [[idx + 1 for idx in face] for face in faces]

# 6. 创建三角网格实体
triangulated_face_set = ifcopenshell.api.run(
    "geometry.create_triangulated_face_set",
    ifc_file,
    point_list=point_list,
    coord_index=ifc_faces
)

# 7. 创建几何表示上下文并关联三角网格
context = ifcopenshell.api.run("context.add_context", ifc_file, context_type="Model")
body_context = ifcopenshell.api.run("context.add_context", ifc_file, context_type="Model", context_identifier="Body", target_view="MODEL_VIEW", parent=context)
shape_representation = ifcopenshell.api.run(
    "geometry.add_representation",
    ifc_file,
    context=body_context,
    representation_type="Tessellation",
    items=[triangulated_face_set]
)

# 8. 创建代理元素承载三角网格
proxy = ifcopenshell.api.run("root.create_entity", ifc_file, ifc_class="IfcBuildingElementProxy", name="TriMesh_Proxy")
ifcopenshell.api.run("geometry.assign_representation", ifc_file, product=proxy, representation=shape_representation)

# 9. 将代理元素绑定到楼层
ifcopenshell.api.run("spatial.assign_container", ifc_file, product=proxy, relating_structure=storey)

# 10. 保存文件
ifc_file.write("tri_mesh_model.ifc")

关键注意事项

  • 索引规则:IFC顶点索引为1-based,Python数组是0-based,必须给每个面索引加1
  • 单位转换:IFC默认单位是米,若你的顶点数据是毫米,记得除以1000转换
  • 载体选择:三角网格不属于标准建筑构件,用IfcBuildingElementProxy作为通用载体可确保模型被IFC阅读器正确识别
  • 版本兼容:示例用IFC4,如需兼容旧版本可改为"IFC2X3",部分API参数需微调

内容的提问来源于stack exchange,提问作者Nils

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 03:12:18