diff --git a/src/bonsai/bonsai/core/root.py b/src/bonsai/bonsai/core/root.py index 3a283a6a65..2276e0623f 100644 --- a/src/bonsai/bonsai/core/root.py +++ b/src/bonsai/bonsai/core/root.py @@ -20,8 +20,6 @@ from __future__ import annotations from typing import TYPE_CHECKING, Optional -import ifcopenshell.util.element - if TYPE_CHECKING: import bpy import ifcopenshell @@ -58,31 +56,12 @@ def copy_class( geometry.change_object_data(obj, data, is_global=True) geometry.rename_object(data, geometry.get_representation_name(ifc.get_entity(data))) # Only assign styles if element doesn't get them from material - if not _has_material_styles(ifc, new): + if not root.has_material_styles(new): root.assign_body_styles(new, obj) collector.assign(obj) return new -def _has_material_styles(ifc: type[tool.Ifc], element: ifcopenshell.entity_instance) -> bool: - """Check if element has styles defined through its material. - - Returns True if any constituent material has a style representation, - which means styles should NOT be applied directly to the geometry. - """ - materials = ifcopenshell.util.element.get_materials(element) - - if not materials: - return False - - # Check if any of the constituent materials have styles - for material in materials: - if hasattr(material, "HasRepresentation") and material.HasRepresentation: - return True - - return False - - def assign_class( ifc: type[tool.Ifc], collector: type[tool.Collector], diff --git a/src/bonsai/bonsai/core/tool.py b/src/bonsai/bonsai/core/tool.py index e6a60c9deb..342860fab2 100644 --- a/src/bonsai/bonsai/core/tool.py +++ b/src/bonsai/bonsai/core/tool.py @@ -863,6 +863,7 @@ class Root: def get_default_container(cls): pass def get_element_representation(cls, element, context): pass def get_element_type(cls, element): pass + def has_material_styles(cls, element): pass def get_object_name(cls, obj): pass def get_object_representation(cls, obj): pass def get_representation_context(cls, representation): pass diff --git a/src/bonsai/bonsai/tool/root.py b/src/bonsai/bonsai/tool/root.py index 8880a168fe..517a2f999b 100644 --- a/src/bonsai/bonsai/tool/root.py +++ b/src/bonsai/bonsai/tool/root.py @@ -54,6 +54,12 @@ class Root(bonsai.core.tool.Root): new.obj = obj new.name = opening_type + @classmethod + def has_material_styles(cls, element: ifcopenshell.entity_instance) -> bool: + """Return True if any constituent material of element has a style representation.""" + materials = ifcopenshell.util.element.get_materials(element) + return any(getattr(m, "HasRepresentation", None) for m in materials) + @classmethod def assign_body_styles(cls, element: ifcopenshell.entity_instance, obj: bpy.types.Object) -> None: # Should this even be here? Should it be in the geometry tool? diff --git a/src/bonsai/test/core/test_root.py b/src/bonsai/test/core/test_root.py index 121236803d..4fcafe6947 100644 --- a/src/bonsai/test/core/test_root.py +++ b/src/bonsai/test/core/test_root.py @@ -40,8 +40,7 @@ class TestCopyClass: collector.assign("obj").should_be_called() subject.copy_class(ifc, collector, geometry, root, obj="obj") - # def test_copy_with_new_geometry_copied_from_the_old(self, ifc, collector, geometry, root): - def test_AAAAAAAAAAAA(self, ifc, collector, geometry, root): + def test_copy_with_new_geometry_copied_from_the_old(self, ifc, collector, geometry, root): ifc.get_entity("obj").should_be_called().will_return("original_element") root.is_element_a("original_element", "IfcRelSpaceBoundary").should_be_called().will_return(False) root.get_object_representation("obj").should_be_called().will_return("representation") @@ -56,6 +55,7 @@ class TestCopyClass: ifc.get_entity("data").should_be_called().will_return("new_representation") geometry.get_representation_name("new_representation").should_be_called().will_return("name") geometry.rename_object("data", "name").should_be_called() + root.has_material_styles("element").should_be_called().will_return(False) root.assign_body_styles("element", "obj").should_be_called() collector.assign("obj").should_be_called() subject.copy_class(ifc, collector, geometry, root, obj="obj")