This commit is contained in:
Andrej730
2025-03-07 12:17:04 +05:00
parent 269e137ea2
commit 590288496e
14 changed files with 147 additions and 106 deletions
@@ -262,7 +262,8 @@ class AssignMaterial(bpy.types.Operator, tool.Ifc.Operator):
if not (material_type := properties.material_type): if not (material_type := properties.material_type):
if not (obj := context.active_object): if not (obj := context.active_object):
return "" return ""
material_type = obj.BIMObjectMaterialProperties.material_type omprops = tool.Material.get_object_material_props(obj)
material_type = omprops.material_type
description = "Assign current IfcMaterial to the selected objects" description = "Assign current IfcMaterial to the selected objects"
if material_type != "IfcMaterial": if material_type != "IfcMaterial":
@@ -294,14 +295,12 @@ class AddConstituent(bpy.types.Operator, tool.Ifc.Operator):
def _execute(self, context): def _execute(self, context):
obj = bpy.data.objects.get(self.obj) if self.obj else context.active_object obj = bpy.data.objects.get(self.obj) if self.obj else context.active_object
omprops = tool.Material.get_object_material_props(obj)
self.file = tool.Ifc.get() self.file = tool.Ifc.get()
ifcopenshell.api.run( ifcopenshell.api.material.add_constituent(
"material.add_constituent",
self.file, self.file,
**{ constituent_set=self.file.by_id(self.constituent_set),
"constituent_set": self.file.by_id(self.constituent_set), material=self.file.by_id(int(omprops.material)),
"material": self.file.by_id(int(obj.BIMObjectMaterialProperties.material)),
},
) )
@@ -330,13 +329,14 @@ class AddProfile(bpy.types.Operator, tool.Ifc.Operator):
def _execute(self, context): def _execute(self, context):
obj = bpy.data.objects.get(self.obj) if self.obj else context.active_object obj = bpy.data.objects.get(self.obj) if self.obj else context.active_object
assert obj
self.file = tool.Ifc.get() self.file = tool.Ifc.get()
props = tool.Material.get_material_props() props = tool.Material.get_material_props()
ifcopenshell.api.run( omprops = tool.Material.get_object_material_props(obj)
"material.add_profile", ifcopenshell.api.material.add_profile(
self.file, self.file,
profile_set=self.file.by_id(self.profile_set), profile_set=self.file.by_id(self.profile_set),
material=self.file.by_id(int(obj.BIMObjectMaterialProperties.material)), material=self.file.by_id(int(omprops.material)),
profile=self.file.by_id(int(props.profiles)), profile=self.file.by_id(int(props.profiles)),
) )
@@ -367,11 +367,13 @@ class AddLayer(bpy.types.Operator, tool.Ifc.Operator):
def _execute(self, context): def _execute(self, context):
obj = bpy.data.objects.get(self.obj) if self.obj else context.active_object obj = bpy.data.objects.get(self.obj) if self.obj else context.active_object
assert obj
omprops = tool.Material.get_object_material_props(obj)
layer_set = tool.Ifc.get().by_id(self.layer_set) layer_set = tool.Ifc.get().by_id(self.layer_set)
ifcopenshell.api.material.add_layer( ifcopenshell.api.material.add_layer(
tool.Ifc.get(), tool.Ifc.get(),
layer_set=layer_set, layer_set=layer_set,
material=tool.Ifc.get().by_id(int(obj.BIMObjectMaterialProperties.material)), material=tool.Ifc.get().by_id(int(omprops.material)),
) )
slab.DumbSlabPlaner().regenerate_from_layer_set(layer_set) slab.DumbSlabPlaner().regenerate_from_layer_set(layer_set)
wall.DumbWallPlaner().regenerate_from_layer_set(layer_set) wall.DumbWallPlaner().regenerate_from_layer_set(layer_set)
@@ -445,12 +447,13 @@ class AddListItem(bpy.types.Operator, tool.Ifc.Operator):
def _execute(self, context): def _execute(self, context):
obj = bpy.data.objects.get(self.obj) if self.obj else context.active_object obj = bpy.data.objects.get(self.obj) if self.obj else context.active_object
assert obj
omprops = tool.Material.get_object_material_props(obj)
self.file = tool.Ifc.get() self.file = tool.Ifc.get()
ifcopenshell.api.run( ifcopenshell.api.material.add_list_item(
"material.add_list_item",
self.file, self.file,
material_list=self.file.by_id(self.list_item_set), material_list=self.file.by_id(self.list_item_set),
material=self.file.by_id(int(obj.BIMObjectMaterialProperties.material)), material=self.file.by_id(int(omprops.material)),
) )
@@ -484,10 +487,12 @@ class EnableEditingAssignedMaterial(bpy.types.Operator):
def execute(self, context): def execute(self, context):
obj = bpy.data.objects.get(self.obj) if self.obj else context.active_object obj = bpy.data.objects.get(self.obj) if self.obj else context.active_object
props = obj.BIMObjectMaterialProperties assert obj
props = tool.Material.get_object_material_props(obj)
props.is_editing = True props.is_editing = True
element = tool.Ifc.get_entity(obj) element = tool.Ifc.get_entity(obj)
material = ifcopenshell.util.element.get_material(element) material = ifcopenshell.util.element.get_material(element)
assert material
if material.is_a("IfcMaterial"): if material.is_a("IfcMaterial"):
props.material = str(material.id()) props.material = str(material.id())
@@ -545,7 +550,8 @@ class DisableEditingAssignedMaterial(bpy.types.Operator):
def execute(self, context): def execute(self, context):
bpy.ops.bim.disable_editing_material_set_item() bpy.ops.bim.disable_editing_material_set_item()
obj = bpy.data.objects.get(self.obj) if self.obj else context.active_object obj = bpy.data.objects.get(self.obj) if self.obj else context.active_object
props = obj.BIMObjectMaterialProperties assert obj
props = tool.Material.get_object_material_props(obj)
props.is_editing = False props.is_editing = False
return {"FINISHED"} return {"FINISHED"}
@@ -562,7 +568,7 @@ class EditAssignedMaterial(bpy.types.Operator, tool.Ifc.Operator):
self.file = tool.Ifc.get() self.file = tool.Ifc.get()
active_obj = bpy.data.objects.get(self.obj) if self.obj else context.active_object active_obj = bpy.data.objects.get(self.obj) if self.obj else context.active_object
assert active_obj assert active_obj
props = active_obj.BIMObjectMaterialProperties props = tool.Material.get_object_material_props(active_obj)
element = tool.Ifc.get_entity(active_obj) element = tool.Ifc.get_entity(active_obj)
assert element assert element
material = ifcopenshell.util.element.get_material(element) material = ifcopenshell.util.element.get_material(element)
@@ -628,7 +634,8 @@ class EnableEditingMaterialSetItemProfile(bpy.types.Operator):
def execute(self, context): def execute(self, context):
obj = bpy.data.objects.get(self.obj) if self.obj else context.active_object obj = bpy.data.objects.get(self.obj) if self.obj else context.active_object
self.props = obj.BIMObjectMaterialProperties assert obj
self.props = tool.Material.get_object_material_props(obj)
self.props.active_material_set_item_id = self.material_set_item self.props.active_material_set_item_id = self.material_set_item
self.props.material_set_item_profile_attributes.clear() self.props.material_set_item_profile_attributes.clear()
profile = tool.Ifc.get().by_id(self.material_set_item).Profile profile = tool.Ifc.get().by_id(self.material_set_item).Profile
@@ -644,7 +651,8 @@ class DisableEditingMaterialSetItemProfile(bpy.types.Operator):
def execute(self, context): def execute(self, context):
obj = bpy.data.objects.get(self.obj) if self.obj else context.active_object obj = bpy.data.objects.get(self.obj) if self.obj else context.active_object
self.props = obj.BIMObjectMaterialProperties assert obj
self.props = tool.Material.get_object_material_props(obj)
self.props.active_material_set_item_id = 0 self.props.active_material_set_item_id = 0
self.props.material_set_item_profile_attributes.clear() self.props.material_set_item_profile_attributes.clear()
return {"FINISHED"} return {"FINISHED"}
@@ -659,7 +667,8 @@ class EditMaterialSetItemProfile(bpy.types.Operator, tool.Ifc.Operator):
def _execute(self, context): def _execute(self, context):
obj = bpy.data.objects.get(self.obj) if self.obj else context.active_object obj = bpy.data.objects.get(self.obj) if self.obj else context.active_object
self.props = obj.BIMObjectMaterialProperties assert obj
self.props = tool.Material.get_object_material_props(obj)
attributes = bonsai.bim.helper.export_attributes(self.props.material_set_item_profile_attributes) attributes = bonsai.bim.helper.export_attributes(self.props.material_set_item_profile_attributes)
profile = tool.Ifc.get().by_id(self.material_set_item).Profile profile = tool.Ifc.get().by_id(self.material_set_item).Profile
ifcopenshell.api.run("profile.edit_profile", tool.Ifc.get(), profile=profile, attributes=attributes) ifcopenshell.api.run("profile.edit_profile", tool.Ifc.get(), profile=profile, attributes=attributes)
@@ -678,11 +687,13 @@ class EnableEditingMaterialSetItem(bpy.types.Operator):
def execute(self, context): def execute(self, context):
self.file = tool.Ifc.get() self.file = tool.Ifc.get()
obj = bpy.data.objects.get(self.obj) if self.obj else context.active_object obj = bpy.data.objects.get(self.obj) if self.obj else context.active_object
assert obj
self.mprops = tool.Material.get_material_props() self.mprops = tool.Material.get_material_props()
self.props = obj.BIMObjectMaterialProperties self.props = tool.Material.get_object_material_props(obj)
self.props.active_material_set_item_id = self.material_set_item self.props.active_material_set_item_id = self.material_set_item
element = tool.Ifc.get_entity(obj) element = tool.Ifc.get_entity(obj)
assert element
material = ifcopenshell.util.element.get_material(element, should_skip_usage=True) material = ifcopenshell.util.element.get_material(element, should_skip_usage=True)
material_set_item = self.file.by_id(self.material_set_item) material_set_item = self.file.by_id(self.material_set_item)
@@ -706,7 +717,8 @@ class DisableEditingMaterialSetItem(bpy.types.Operator):
def execute(self, context): def execute(self, context):
obj = bpy.data.objects.get(self.obj) if self.obj else context.active_object obj = bpy.data.objects.get(self.obj) if self.obj else context.active_object
props = obj.BIMObjectMaterialProperties assert obj
props = tool.Material.get_object_material_props(obj)
props.active_material_set_item_id = 0 props.active_material_set_item_id = 0
return {"FINISHED"} return {"FINISHED"}
@@ -721,10 +733,13 @@ class EditMaterialSetItem(bpy.types.Operator, tool.Ifc.Operator):
def _execute(self, context): def _execute(self, context):
obj = bpy.data.objects.get(self.obj) if self.obj else context.active_object obj = bpy.data.objects.get(self.obj) if self.obj else context.active_object
self.file = tool.Ifc.get() self.file = tool.Ifc.get()
props = obj.BIMObjectMaterialProperties assert obj
props = tool.Material.get_object_material_props(obj)
mprops = tool.Material.get_material_props() mprops = tool.Material.get_material_props()
element = tool.Ifc.get_entity(obj) element = tool.Ifc.get_entity(obj)
assert element
material = ifcopenshell.util.element.get_material(element, should_skip_usage=True) material = ifcopenshell.util.element.get_material(element, should_skip_usage=True)
assert material
attributes = bonsai.bim.helper.export_attributes(props.material_set_item_attributes) attributes = bonsai.bim.helper.export_attributes(props.material_set_item_attributes)
@@ -733,7 +748,7 @@ class EditMaterialSetItem(bpy.types.Operator, tool.Ifc.Operator):
self.file, self.file,
constituent=self.file.by_id(self.material_set_item), constituent=self.file.by_id(self.material_set_item),
attributes=attributes, attributes=attributes,
material=self.file.by_id(int(obj.BIMObjectMaterialProperties.material_set_item_material)), material=self.file.by_id(int(props.material_set_item_material)),
) )
elif material.is_a("IfcMaterialLayerSet"): elif material.is_a("IfcMaterialLayerSet"):
layer = self.file.by_id(self.material_set_item) layer = self.file.by_id(self.material_set_item)
@@ -741,7 +756,7 @@ class EditMaterialSetItem(bpy.types.Operator, tool.Ifc.Operator):
self.file, self.file,
layer=layer, layer=layer,
attributes=attributes, attributes=attributes,
material=self.file.by_id(int(obj.BIMObjectMaterialProperties.material_set_item_material)), material=self.file.by_id(int(props.material_set_item_material)),
) )
slab.DumbSlabPlaner().regenerate_from_layer(layer) slab.DumbSlabPlaner().regenerate_from_layer(layer)
wall.DumbWallPlaner().regenerate_from_layer(layer) wall.DumbWallPlaner().regenerate_from_layer(layer)
@@ -755,7 +770,7 @@ class EditMaterialSetItem(bpy.types.Operator, tool.Ifc.Operator):
profile=self.file.by_id(self.material_set_item), profile=self.file.by_id(self.material_set_item),
attributes=attributes, attributes=attributes,
profile_def=profile_def, profile_def=profile_def,
material=self.file.by_id(int(obj.BIMObjectMaterialProperties.material_set_item_material)), material=self.file.by_id(int(props.material_set_item_material)),
) )
else: else:
pass pass
+1 -1
View File
@@ -156,7 +156,7 @@ class BIM_PT_object_material(Panel):
assert obj assert obj
self.file = tool.Ifc.get() self.file = tool.Ifc.get()
self.oprops = tool.Blender.get_object_bim_props(obj) self.oprops = tool.Blender.get_object_bim_props(obj)
self.props = obj.BIMObjectMaterialProperties self.props = tool.Material.get_object_material_props(obj)
self.mprops = tool.Material.get_material_props() self.mprops = tool.Material.get_material_props()
if not ObjectMaterialData.data["materials"]: if not ObjectMaterialData.data["materials"]:
+5 -6
View File
@@ -21,6 +21,7 @@ import json
import bmesh import bmesh
import ifcopenshell import ifcopenshell
import ifcopenshell.api import ifcopenshell.api
import ifcopenshell.api.geometry
import ifcopenshell.util.element import ifcopenshell.util.element
import ifcopenshell.util.placement import ifcopenshell.util.placement
import ifcopenshell.util.representation import ifcopenshell.util.representation
@@ -343,8 +344,7 @@ class DumbSlabPlaner:
else: else:
props = tool.Model.get_model_props() props = tool.Model.get_model_props()
x_angle = 0 if tool.Cad.is_x(props.x_angle, 0, tolerance=0.001) else props.x_angle x_angle = 0 if tool.Cad.is_x(props.x_angle, 0, tolerance=0.001) else props.x_angle
new_rep = ifcopenshell.api.run( new_rep = ifcopenshell.api.geometry.add_slab_representation(
"geometry.add_slab_representation",
tool.Ifc.get(), tool.Ifc.get(),
context=body_context, context=body_context,
depth=thickness * self.unit_scale, depth=thickness * self.unit_scale,
@@ -368,15 +368,14 @@ class DumbSlabPlaner:
else: else:
props = tool.Model.get_model_props() props = tool.Model.get_model_props()
x_angle = 0 if tool.Cad.is_x(props.x_angle, 0, tolerance=0.001) else props.x_angle x_angle = 0 if tool.Cad.is_x(props.x_angle, 0, tolerance=0.001) else props.x_angle
representation = ifcopenshell.api.run( representation = ifcopenshell.api.geometry.add_slab_representation(
"geometry.add_slab_representation",
tool.Ifc.get(), tool.Ifc.get(),
context=body_context, context=body_context,
depth=thickness * self.unit_scale, depth=thickness * self.unit_scale,
x_angle=x_angle, x_angle=x_angle,
) )
ifcopenshell.api.run( ifcopenshell.api.geometry.assign_representation(
"geometry.assign_representation", tool.Ifc.get(), product=element, representation=representation tool.Ifc.get(), product=element, representation=representation
) )
bonsai.core.geometry.switch_representation( bonsai.core.geometry.switch_representation(
+2 -2
View File
@@ -37,7 +37,7 @@ from bpy.props import (
from typing import TYPE_CHECKING, Union from typing import TYPE_CHECKING, Union
def get_profile_classes(self, context): def get_profile_classes(self: "BIMProfileProperties", context: bpy.types.Context) -> list[tuple[str, str, str]]:
if not ProfileData.is_loaded: if not ProfileData.is_loaded:
ProfileData.load() ProfileData.load()
return ProfileData.data["profile_classes"] return ProfileData.data["profile_classes"]
@@ -62,7 +62,7 @@ class Profile(PropertyGroup):
ifc_definition_id: int ifc_definition_id: int
def update_active_profile_index(self, context): def update_active_profile_index(self: "BIMProfileProperties", context: bpy.types.Context) -> None:
ProfileData.data["active_profile_users"] = ProfileData.active_profile_users() ProfileData.data["active_profile_users"] = ProfileData.active_profile_users()
+3 -1
View File
@@ -212,7 +212,9 @@ class MaterialSetItemPsetsData(Data):
@classmethod @classmethod
def load(cls): def load(cls):
psets = {} psets = {}
ifc_definition_id = bpy.context.active_object.BIMObjectMaterialProperties.active_material_set_item_id obj = bpy.context.active_object
assert obj
ifc_definition_id = tool.Material.get_object_material_props(obj).active_material_set_item_id
if ifc_definition_id: if ifc_definition_id:
psets = cls.psetqtos(tool.Ifc.get().by_id(ifc_definition_id)) psets = cls.psetqtos(tool.Ifc.get().by_id(ifc_definition_id))
cls.data = {"psets": psets} cls.data = {"psets": psets}
+5 -3
View File
@@ -109,10 +109,12 @@ def get_material_set_pset_names(self, context):
return psetnames[ifc_class] return psetnames[ifc_class]
def get_material_set_item_pset_names(self, context): def get_material_set_item_pset_names(self, context) -> list[tuple[str, str, str]]:
global psetnames global psetnames
ifc_definition_id = context.active_object.BIMObjectMaterialProperties.active_material_set_item_id obj = context.active_object
if not ifc_definition_id: assert obj
omprops = tool.Material.get_object_material_props(obj)
if not omprops.active_material_set_item_id:
return [] return []
ifc_class = tool.Ifc.get().by_id(ifc_definition_id).is_a() ifc_class = tool.Ifc.get().by_id(ifc_definition_id).is_a()
if ifc_class not in psetnames: if ifc_class not in psetnames:
+2 -1
View File
@@ -459,7 +459,8 @@ class BIM_PT_material_set_item_psets(Panel):
obj = context.active_object obj = context.active_object
assert obj assert obj
if not obj.BIMObjectMaterialProperties.active_material_set_item_id: omprops = tool.Material.get_object_material_props(obj)
if not omprops.active_material_set_item_id:
self.layout.label(text="No Material Set Item Edited.") self.layout.label(text="No Material Set Item Edited.")
return return
+14 -3
View File
@@ -44,6 +44,7 @@ from typing_extensions import assert_never
if TYPE_CHECKING: if TYPE_CHECKING:
from bonsai.bim.prop import BIMProperties, BIMObjectProperties from bonsai.bim.prop import BIMProperties, BIMObjectProperties
T = TypeVar("T")
VIEWPORT_ATTRIBUTES = [ VIEWPORT_ATTRIBUTES = [
"view_matrix", "view_matrix",
@@ -199,7 +200,9 @@ class Blender(bonsai.core.tool.Blender):
props = tool.Material.get_material_props() props = tool.Material.get_material_props()
return props.materials[props.active_material_index].ifc_definition_id return props.materials[props.active_material_index].ifc_definition_id
elif obj_type == "MaterialSetItem": elif obj_type == "MaterialSetItem":
return bpy.data.objects.get(obj).BIMObjectMaterialProperties.active_material_set_item_id obj_ = bpy.data.objects[obj]
omprops = tool.Material.get_object_material_props(obj_)
return omprops.active_material_set_item_id
elif obj_type == "Task": elif obj_type == "Task":
tprops = tool.Sequence.get_task_tree_props() tprops = tool.Sequence.get_task_tree_props()
return tprops.tasks[context.scene.BIMWorkScheduleProperties.active_task_index].ifc_definition_id return tprops.tasks[context.scene.BIMWorkScheduleProperties.active_task_index].ifc_definition_id
@@ -1466,9 +1469,9 @@ class Blender(bonsai.core.tool.Blender):
def set_prop_from_path(cls, bpy_object: bpy.types.bpy_struct, prop_path: str, value: Any) -> None: def set_prop_from_path(cls, bpy_object: bpy.types.bpy_struct, prop_path: str, value: Any) -> None:
"""Set `data_block` property value using path from `path_from_id`.""" """Set `data_block` property value using path from `path_from_id`."""
T = TypeVar("T", bound=bpy.types.bpy_struct) T_ = TypeVar("T_", bound=bpy.types.bpy_struct)
def path_resolve(obj: T, prop_path: str) -> tuple[T, str]: def path_resolve(obj: T_, prop_path: str) -> tuple[T_, str]:
if "." in prop_path: if "." in prop_path:
extra_path, prop_path = prop_path.rsplit(".", 1) extra_path, prop_path = prop_path.rsplit(".", 1)
obj = obj.path_resolve(extra_path) obj = obj.path_resolve(extra_path)
@@ -1605,3 +1608,11 @@ class Blender(bonsai.core.tool.Blender):
if isinstance(obj, bpy.types.Object): if isinstance(obj, bpy.types.Object):
return tool.Blender.get_object_bim_props(obj).ifc_definition_id return tool.Blender.get_object_bim_props(obj).ifc_definition_id
return tool.Style.get_material_style_props(obj).ifc_definition_id return tool.Style.get_material_style_props(obj).ifc_definition_id
@classmethod
def get_active_uilist_element(
cls, collection: bpy.types.bpy_prop_collection_idprop[T], index: int
) -> Union[T, None]:
if 0 <= index < len(collection):
return collection[index]
return None
+10 -3
View File
@@ -33,7 +33,7 @@ from typing_extensions import assert_never
if TYPE_CHECKING: if TYPE_CHECKING:
# Avoid circular imports. # Avoid circular imports.
from bonsai.bim.module.material.prop import Material as MaterialItem from bonsai.bim.module.material.prop import Material as MaterialItem
from bonsai.bim.module.material.prop import BIMMaterialProperties from bonsai.bim.module.material.prop import BIMMaterialProperties, BIMObjectMaterialProperties
class Material(bonsai.core.tool.Material): class Material(bonsai.core.tool.Material):
@@ -41,6 +41,10 @@ class Material(bonsai.core.tool.Material):
def get_material_props(cls) -> BIMMaterialProperties: def get_material_props(cls) -> BIMMaterialProperties:
return bpy.context.scene.BIMMaterialProperties return bpy.context.scene.BIMMaterialProperties
@classmethod
def get_object_material_props(cls, obj: bpy.types.Object) -> BIMObjectMaterialProperties:
return obj.BIMObjectMaterialProperties
@classmethod @classmethod
def disable_editing_materials(cls) -> None: def disable_editing_materials(cls) -> None:
props = tool.Material.get_material_props() props = tool.Material.get_material_props()
@@ -203,11 +207,14 @@ class Material(bonsai.core.tool.Material):
@classmethod @classmethod
def get_object_ui_material_type(cls) -> str: def get_object_ui_material_type(cls) -> str:
active_obj = bpy.context.active_object active_obj = bpy.context.active_object
return active_obj.BIMObjectMaterialProperties.material_type assert active_obj
return tool.Material.get_object_material_props(active_obj).material_type
@classmethod @classmethod
def get_object_ui_active_material(cls) -> ifcopenshell.entity_instance: def get_object_ui_active_material(cls) -> ifcopenshell.entity_instance:
return tool.Ifc.get().by_id(int(bpy.context.active_object.BIMObjectMaterialProperties.material)) obj = bpy.context.active_object
assert obj
return tool.Ifc.get().by_id(int(tool.Material.get_object_material_props(obj).material))
@classmethod @classmethod
def get_material( def get_material(
+1 -4
View File
@@ -123,16 +123,13 @@ class Profile(bonsai.core.tool.Profile):
@classmethod @classmethod
def get_active_profile_ui(cls) -> Union[bonsai.bim.module.profile.prop.Profile, None]: def get_active_profile_ui(cls) -> Union[bonsai.bim.module.profile.prop.Profile, None]:
props = cls.get_profile_props() props = cls.get_profile_props()
index = props.active_profile_index return tool.Blender.get_active_uilist_element(props.profiles, props.active_profile_index)
if len(props.profiles) > index >= 0:
return props.profiles[index]
# Lengths are in meters. # Lengths are in meters.
DEFAULT_PROFILE_ATTRS = { DEFAULT_PROFILE_ATTRS = {
"IfcCircleProfileDef": { "IfcCircleProfileDef": {
"Radius": 0.05, "Radius": 0.05,
}, },
# TODO: test after debug
"IfcAsymmetricIShapeProfileDef": { "IfcAsymmetricIShapeProfileDef": {
"BottomFlangeWidth": 0.1, "BottomFlangeWidth": 0.1,
"BottomFlangeThickness": 0.01, "BottomFlangeThickness": 0.01,
@@ -27,10 +27,11 @@ def add_slab_representation(
file: ifcopenshell.file, file: ifcopenshell.file,
context: ifcopenshell.entity_instance, context: ifcopenshell.entity_instance,
depth: float = 0.2, depth: float = 0.2,
# TODO: document remaining args.
direction_sense: str = "POSITIVE", direction_sense: str = "POSITIVE",
offset: float = 0.0, offset: float = 0.0,
x_angle: float = 0.0, x_angle: float = 0.0,
clippings: Optional[list[Union[Clipping, dict[str, Any]]]] = None, clippings: Optional[list[Union[Clipping, ifcopenshell.entity_instance]]] = None,
polyline: Optional[list[tuple[float, float]]] = None, polyline: Optional[list[tuple[float, float]]] = None,
) -> ifcopenshell.entity_instance: ) -> ifcopenshell.entity_instance:
""" """
@@ -55,60 +56,74 @@ def add_slab_representation(
""" """
usecase = Usecase() usecase = Usecase()
usecase.file = file usecase.file = file
usecase.settings = { return usecase.execute(
"context": context, context,
"depth": depth, depth,
"direction_sense": direction_sense, direction_sense,
"offset": offset, offset,
"x_angle": x_angle, x_angle,
"clippings": clippings if clippings is not None else [], clippings if clippings is not None else [],
"polyline": polyline, polyline,
} )
return usecase.execute()
class Usecase: class Usecase:
file: ifcopenshell.file file: ifcopenshell.file
settings: dict[str, Any]
def execute(self): def execute(
self.settings["unit_scale"] = ifcopenshell.util.unit.calculate_unit_scale(self.file) self,
return self.file.createIfcShapeRepresentation( context: ifcopenshell.entity_instance,
self.settings["context"], depth: float,
self.settings["context"].ContextIdentifier, direction_sense: str,
"Clipping" if self.settings["clippings"] else "SweptSolid", offset: float,
x_angle: float,
clippings: list[Union[Clipping, ifcopenshell.entity_instance]],
polyline: Optional[list[tuple[float, float]]],
) -> ifcopenshell.entity_instance:
self.unit_scale = ifcopenshell.util.unit.calculate_unit_scale(self.file)
self.clippings = clippings
self.depth = depth
self.direction_sense = direction_sense
self.offset = offset
self.x_angle = x_angle
self.polyline = polyline
return self.file.create_entity(
"IfcShapeRepresentation",
context,
context.ContextIdentifier,
"Clipping" if self.clippings else "SweptSolid",
[self.create_item()], [self.create_item()],
) )
def create_item(self): def create_item(self) -> ifcopenshell.entity_instance:
size = self.convert_si_to_unit(1) size = self.convert_si_to_unit(1)
points = ((0.0, 0.0), (size, 0.0), (size, size), (0.0, size), (0.0, 0.0)) points = ((0.0, 0.0), (size, 0.0), (size, size), (0.0, size), (0.0, 0.0))
if self.settings["polyline"]: if self.polyline:
points = [ points = [
(self.convert_si_to_unit(p[0]), self.convert_si_to_unit(p[1] * abs(1 / cos(self.settings["x_angle"])))) (self.convert_si_to_unit(p[0]), self.convert_si_to_unit(p[1] * abs(1 / cos(self.x_angle))))
for p in self.settings["polyline"] for p in self.polyline
] ]
if self.file.schema == "IFC2X3": if self.file.schema == "IFC2X3":
curve = self.file.createIfcPolyline([self.file.createIfcCartesianPoint(p) for p in points]) curve = self.file.createIfcPolyline([self.file.createIfcCartesianPoint(p) for p in points])
else: else:
curve = self.file.createIfcIndexedPolyCurve(self.file.createIfcCartesianPointList2D(points)) curve = self.file.createIfcIndexedPolyCurve(self.file.createIfcCartesianPointList2D(points))
if self.settings["x_angle"]: if self.x_angle:
direction_ratios = (0.0, sin(self.settings["x_angle"]), cos(self.settings["x_angle"])) direction_ratios = (0.0, sin(self.x_angle), cos(self.x_angle))
else: else:
direction_ratios = (0.0, 0.0, 1.0) direction_ratios = (0.0, 0.0, 1.0)
offset_direction = direction_ratios # offset direction doesn't change if direction_sense is negative offset_direction = direction_ratios # offset direction doesn't change if direction_sense is negative
extrusion_direction = self.file.createIfcDirection(direction_ratios) extrusion_direction = self.file.createIfcDirection(direction_ratios)
if self.settings["direction_sense"] == "NEGATIVE": if self.direction_sense == "NEGATIVE":
direction_ratios = tuple((-n for n in direction_ratios)) direction_ratios = tuple((-n for n in direction_ratios))
extrusion_direction = self.file.createIfcDirection(direction_ratios) extrusion_direction = self.file.createIfcDirection(direction_ratios)
perpendicular_offset = self.convert_si_to_unit(self.settings["offset"]) * abs(1 / cos(self.settings["x_angle"])) perpendicular_offset = self.convert_si_to_unit(self.offset) * abs(1 / cos(self.x_angle))
perpendicular_depth = self.convert_si_to_unit(self.settings["depth"]) * abs(1 / cos(self.settings["x_angle"])) perpendicular_depth = self.convert_si_to_unit(self.depth) * abs(1 / cos(self.x_angle))
position = None position = None
# default position for IFC2X3 where .Position is not optional # default position for IFC2X3 where .Position is not optional
if self.file.schema == "IFC2X3" or self.settings["offset"] != 0: if self.file.schema == "IFC2X3" or self.offset != 0:
position_vector = ( position_vector = (
offset_direction[0] * perpendicular_offset, offset_direction[0] * perpendicular_offset,
offset_direction[1] * perpendicular_offset, offset_direction[1] * perpendicular_offset,
@@ -120,26 +135,27 @@ class Usecase:
self.file.createIfcDirection((1.0, 0.0, 0.0)), self.file.createIfcDirection((1.0, 0.0, 0.0)),
) )
extrusion = self.file.createIfcExtrudedAreaSolid( extrusion = self.file.create_entity(
"IfcExtrudedAreaSolid",
self.file.createIfcArbitraryClosedProfileDef("AREA", None, curve), self.file.createIfcArbitraryClosedProfileDef("AREA", None, curve),
position, position,
extrusion_direction, extrusion_direction,
perpendicular_depth, perpendicular_depth,
) )
if self.settings["clippings"]: if self.clippings:
return self.apply_clippings(extrusion) return self.apply_clippings(extrusion)
return extrusion return extrusion
def apply_clippings(self, first_operand): def apply_clippings(self, first_operand: ifcopenshell.entity_instance) -> ifcopenshell.entity_instance:
while self.settings["clippings"]: while self.clippings:
clipping = self.settings["clippings"].pop() clipping = self.clippings.pop()
if isinstance(clipping, ifcopenshell.entity_instance): if isinstance(clipping, ifcopenshell.entity_instance):
new = ifcopenshell.util.element.copy(self.file, clipping) new = ifcopenshell.util.element.copy(self.file, clipping)
new.FirstOperand = first_operand new.FirstOperand = first_operand
first_operand = new first_operand = new
else: # Clipping else: # Clipping
first_operand = clipping.apply(self.file, first_operand, self.settings["unit_scale"]) first_operand = clipping.apply(self.file, first_operand, self.unit_scale)
return first_operand return first_operand
def convert_si_to_unit(self, co): def convert_si_to_unit(self, co: float) -> float:
return co / self.settings["unit_scale"] return co / self.unit_scale
@@ -31,12 +31,9 @@ def assign_group(
twice. twice.
:param products: A list of IfcProduct elements to assign to the group :param products: A list of IfcProduct elements to assign to the group
:type products: list[ifcopenshell.entity_instance]
:param group: The IfcGroup to assign the products to :param group: The IfcGroup to assign the products to
:type group: ifcopenshell.entity_instance
:return: The IfcRelAssignsToGroup relationship :return: The IfcRelAssignsToGroup relationship
or `None` if `products` was empty list. or `None` if `products` was empty list.
:rtype: Union[ifcopenshell.entity_instance, None]
Example: Example:
@@ -19,24 +19,22 @@
import ifcopenshell import ifcopenshell
import ifcopenshell.api.group import ifcopenshell.api.group
import ifcopenshell.util.system import ifcopenshell.util.system
from typing import Union
def assign_system( def assign_system(
file: ifcopenshell.file, file: ifcopenshell.file,
products: list[ifcopenshell.entity_instance], products: list[ifcopenshell.entity_instance],
system: ifcopenshell.entity_instance, system: ifcopenshell.entity_instance,
) -> None: ) -> Union[ifcopenshell.entity_instance, None]:
"""Assigns distribution elements to a system """Assigns distribution elements to a system
Note that it is not necessary to assign distribution ports to a system. Note that it is not necessary to assign distribution ports to a system.
:param products: The list of IfcDistributionElements to assign to the system. :param products: The list of IfcDistributionElements to assign to the system.
:type products: list[ifcopenshell.entity_instance]
:param system: The IfcSystem you want to assign the element to. :param system: The IfcSystem you want to assign the element to.
:type system: ifcopenshell.entity_instance
:return: The IfcRelAssignsToGroup relationship :return: The IfcRelAssignsToGroup relationship
or `None` if `products` was empty list. or `None` if `products` was empty list.
:rtype: [ifcopenshell.entity_instance, None]
Example: Example:
@@ -52,14 +50,6 @@ def assign_system(
# This duct is part of the system # This duct is part of the system
ifcopenshell.api.system.assign_system(model, products=[duct], system=system) ifcopenshell.api.system.assign_system(model, products=[duct], system=system)
""" """
settings = {
"products": products,
"system": system,
}
system = settings["system"]
products = settings["products"]
if not all(ifcopenshell.util.system.is_assignable(failed_product := product, system) for product in products): if not all(ifcopenshell.util.system.is_assignable(failed_product := product, system) for product in products):
raise TypeError(f"You cannot assign an {failed_product.is_a()} to an {system.is_a()}") raise TypeError(f"You cannot assign an {failed_product.is_a()} to an {system.is_a()}")
@@ -16,9 +16,12 @@
# You should have received a copy of the GNU Lesser General Public License # You should have received a copy of the GNU Lesser General Public License
# along with IfcPatch. If not, see <http://www.gnu.org/licenses/>. # along with IfcPatch. If not, see <http://www.gnu.org/licenses/>.
import ifcopenshell
import logging
class Patcher: class Patcher:
def __init__(self, file, logger, ifc_class="IfcSite"): def __init__(self, file: ifcopenshell.file, logger: logging.Logger, ifc_class: str = "IfcSite"):
"""Resets the location of a spatial element to 0,0,0 """Resets the location of a spatial element to 0,0,0
Another more specialised patch to fix incorrect coordinate usage is to Another more specialised patch to fix incorrect coordinate usage is to
@@ -26,7 +29,6 @@ class Patcher:
to 0,0,0. to 0,0,0.
:param ifc_class: The class of spatial element to reset coordinates for. :param ifc_class: The class of spatial element to reset coordinates for.
:type ifc_class: str
Example: Example:
@@ -39,13 +41,15 @@ class Patcher:
self.logger = logger self.logger = logger
self.ifc_class = ifc_class self.ifc_class = ifc_class
def patch(self): def patch(self) -> None:
project = self.file.by_type("IfcProject")[0] project = self.file.by_type("IfcProject")[0]
spatial_elements = self.find_decomposed_ifc_class(project, self.ifc_class) spatial_elements = self.find_decomposed_ifc_class(project, self.ifc_class)
for spatial_element in spatial_elements: for spatial_element in spatial_elements:
self.patch_placement_to_origin(spatial_element) self.patch_placement_to_origin(spatial_element)
def find_decomposed_ifc_class(self, element, ifc_class): def find_decomposed_ifc_class(
self, element: ifcopenshell.entity_instance, ifc_class: str
) -> list[ifcopenshell.entity_instance]:
results = [] results = []
rel_aggregates = element.IsDecomposedBy rel_aggregates = element.IsDecomposedBy
if not rel_aggregates: if not rel_aggregates:
@@ -57,7 +61,7 @@ class Patcher:
results.extend(self.find_decomposed_ifc_class(part, ifc_class)) results.extend(self.find_decomposed_ifc_class(part, ifc_class))
return results return results
def patch_placement_to_origin(self, element): def patch_placement_to_origin(self, element: ifcopenshell.entity_instance) -> None:
element.ObjectPlacement.RelativePlacement.Location.Coordinates = (0.0, 0.0, 0.0) element.ObjectPlacement.RelativePlacement.Location.Coordinates = (0.0, 0.0, 0.0)
if element.ObjectPlacement.RelativePlacement.Axis: if element.ObjectPlacement.RelativePlacement.Axis:
element.ObjectPlacement.RelativePlacement.Axis.DirectionRatios = (0.0, 0.0, 1.0) element.ObjectPlacement.RelativePlacement.Axis.DirectionRatios = (0.0, 0.0, 1.0)