import bpy import bmesh import ifcopenshell from bonsai.bim.ifc import IfcStore from ifcopenshell.util import element def get_total_edge_length(obj): if obj.type != 'MESH': return 0.0 me = obj.data bm = bmesh.new() bm.from_mesh(me) total_length = 0.0 for e in bm.edges: total_length += e.calc_length() bm.free() return total_length def add_qto(model, ifc_obj): qto = element.get_pset(ifc_obj, "Qto_CableSegmentBaseQuantities") if qto == None: qto = ifcopenshell.api.run( "pset.add_qto", model, product=ifc_obj, name="Qto_CableSegmentBaseQuantities") edit_qto(model, qto, 0.0) return qto def edit_qto(model, qto, total_length): ifcopenshell.api.run( "pset.edit_qto", model, qto=qto, properties={"Length": total_length}) model = IfcStore.get_file() cables = model.by_type("IfcCableSegment") for cable in cables: blender_object_name = f"{cable.is_a()}/{cable.Name}" obj = bpy.data.objects.get(blender_object_name) total_length = get_total_edge_length(obj) qto = add_qto(model, cable) edit_qto(model, qto, total_length) model.write(IfcStore.path)