使用IfcOpenShell模块的Python脚本报错,寻求技术支持
IFC构件碰撞检测脚本报错求助
尝试运行基于IfcOpenShell的IFC构件碰撞检测Python脚本时持续报错,已试过更换Python3.9/3.10版本、重装升级pip、统一文件路径等方法,均未解决。
相关代码
主脚本
# 导入ifc_functions.py中的函数 import IFCOPENSHELL from ifc_fucntions import * # 定义IFC文件名 ifc_file_name = "Centrale Bibliotheek Rotterdam,IFC" # 打开IFC文件 ifc_file = open_ifc(ifc_file_name) # 定义需要检测的实体列表 entity_list = ["IfcWall", "IfcSlab"] # 创建空的碰撞列表 clash_list = [] # 遍历IFC文件中的所有IfcProduct实体 for entity in ifc_file.by_type("IfcProduct"): # 检查实体是否在目标列表中 if entity.is_a() in entity_list: # 再次遍历所有IfcProduct实体 for entity2 in ifc_file.by_type("IfcProduct"): # 检查实体2是否在目标列表中 if entity2.is_a() in entity_list: # 检查两个实体的几何是否存在并判断是否碰撞 if entity.geometry and entity2.geometry: if entity.geometry.intersects(entity2.geometry): # 将碰撞添加到列表 clash_list.append([entity, entity2]) # 打印碰撞列表 print("碰撞结果:") for clash in clash_list: print(clash[0].GlobalId, clash[1].GlobalId)
ifc_functions.py文件
import IFCOPENSHELL def open_ifc(): ifc_file = IFCOPENSHELL.open("Centrale Bibliotheek Rotterdam.open_ifc") return ifc_file
测试代码片段
from ifc_functions import open_ifc ifc_file = open_ifc("Centrale Bibliotheek Rotterdam.IFC")
报错情况
核心错误为:TypeError: open_ifc() takes 0 positional arguments but 1 was given
问题修复方案
1. 修复函数参数不匹配问题
ifc_functions.py中的open_ifc函数未定义参数,但调用时传入了文件名,这是直接报错原因。修改该文件:
import IFCOPENSHELL def open_ifc(ifc_file_path): ifc_file = IFCOPENSHELL.open(ifc_file_path) return ifc_file
2. 修正文件名拼写错误
主脚本中文件名的后缀分隔符错误,将"Centrale Bibliotheek Rotterdam,IFC"改为"Centrale Bibliotheek Rotterdam.IFC"
3. 优化碰撞检测逻辑(避免重复记录)
原脚本会重复记录A-B和B-A的碰撞,添加判断过滤重复:
# 在遍历entity2时增加判断 if entity2.is_a() in entity_list and entity.GlobalId != entity2.GlobalId: # ...碰撞检测逻辑... if geom1.intersects(geom2): if [entity2, entity] not in clash_list: clash_list.append([entity, entity2])
4. 修复几何访问方式问题
部分IFC实体无法直接通过entity.geometry获取几何,需用IfcOpenShell几何工具加载,修正碰撞检测部分代码:
import ifcopenshell.geom # 提前设置几何转换参数 settings = ifcopenshell.geom.settings() settings.set(settings.USE_WORLD_COORDS, True) for entity in ifc_file.by_type("IfcProduct"): if entity.is_a() in entity_list: try: # 加载实体几何 shape1 = ifcopenshell.geom.create_shape(settings, entity) geom1 = shape1.geometry except: continue # 跳过无法加载几何的实体 for entity2 in ifc_file.by_type("IfcProduct"): if entity2.is_a() in entity_list and entity.GlobalId != entity2.GlobalId: try: shape2 = ifcopenshell.geom.create_shape(settings, entity2) geom2 = shape2.geometry except: continue if geom1.intersects(geom2): if [entity2, entity] not in clash_list: clash_list.append([entity, entity2])
内容的提问来源于stack exchange,提问作者lebor
相关产品推荐
相关产品推荐

