1+ import bpy
2+ import ifcopenshell
3+ import blenderbim
4+ from blenderbim .bim .ifc import IfcStore
5+ import blenderbim .tool as tool
6+ from blenderbim .bim .module .pset .data import Data as PsetData
7+ import bmesh
8+
9+ def calculate_volume (obj ):
10+ bm = bmesh .new ()
11+ bm .from_mesh (obj .data )
12+ volume = bm .calc_volume (signed = True )
13+ bm .free ()
14+ return volume
15+
16+ def calculate_height (obj ):
17+ # Example calculation - adjust as needed
18+ z_coords = [v .co .z for v in obj .data .vertices ]
19+ height = max (z_coords ) - min (z_coords )
20+ return height
21+
22+ def set_ifc_class_for_bulk (self , context , material ):
23+ # Get the active IFC file
24+ ifc_file = IfcStore .get_file ()
25+ if not ifc_file :
26+ print ("No IFC file found. Ensure you're working in a BlenderBIM project." )
27+ return
28+
29+ # Create a new Pset template file and the Pset template for Bulk
30+ pset_template = ifcopenshell .api .run ("pset_template.add_pset_template" , ifc_file , name = "dProB_Bulk" )
31+ ifcopenshell .api .run ("pset_template.add_prop_template" , ifc_file , pset_template = pset_template , name = "BulkMaterial" , description = "Limited to what dProB is able to interpret!" )
32+ ifcopenshell .api .run ("pset_template.add_prop_template" , ifc_file , pset_template = pset_template , name = "BulkVolume" , primary_measure_type = "IfcVolumeMeasure" )
33+ ifcopenshell .api .run ("pset_template.add_prop_template" , ifc_file , pset_template = pset_template , name = "BulkHeight" , primary_measure_type = "IfcLengthMeasure" )
34+ blenderbim .bim .handler .refresh_ui_data ()
35+ blenderbim .bim .schema .reload (tool .Ifc .get ().schema )
36+
37+ # Loop through all selected objects
38+ for obj in bpy .context .selected_objects :
39+ if obj .type == 'MESH' :
40+
41+ # Set the active object
42+ bpy .context .view_layer .objects .active = obj
43+ # Assign the IFC class
44+ bpy .ops .bim .assign_class (ifc_class = "IfcBuilding" )
45+ # get the ifc object
46+ ifc_obj = IfcStore .get_file ().by_id (obj .BIMObjectProperties .ifc_definition_id )
47+ # check if the object is valid
48+ if not ifc_obj :
49+ print (f"Object { obj .name } has no IFC object." )
50+ continue
51+ pset = ifcopenshell .api .run ("pset.add_pset" , ifc_file , product = ifc_obj , name = "dProB_Bulk" )
52+
53+ volume = calculate_volume (obj )
54+ height = calculate_height (obj )
55+
56+ # Set the properties of the Pset
57+ new_values = {
58+ "BulkMaterial" : material ,
59+ "BulkVolume" : volume ,
60+ "BulkHeight" : height
61+ }
62+ ifcopenshell .api .run ("pset.edit_pset" , ifc_file , pset = pset , properties = new_values )
63+
64+ print (f"Assigned IfcBuilding and custom Pset to { obj .name } ." )
65+
66+
67+ class SetIfcClassForBulkOperator (bpy .types .Operator ):
68+ """Set IFC Class of selected Objects for custom dProB-Bulk"""
69+ bl_idname = "object.set_ifc_class_for_bulk_operator"
70+ bl_label = "Set IFC Class for Bulk"
71+
72+ def execute (self , context ):
73+ material = context .scene .bulk_material
74+ set_ifc_class_for_bulk (self , context , material )
75+ return {'FINISHED' }
76+
77+
78+ def register ():
79+ bpy .utils .register_class (SetIfcClassForBulkOperator )
80+
81+ def unregister ():
82+ bpy .utils .unregister_class (SetIfcClassForBulkOperator )
0 commit comments