From 286e90e299f995be8ffb2c8636285cb0ccff658a Mon Sep 17 00:00:00 2001 From: Andrej730 Date: Mon, 26 Aug 2024 15:18:20 +0500 Subject: [PATCH] Add annotation by default to the active drawing #5216 Approach in 358c834 didn't worked out because annotations are assigned to the drawing after assign_class and we can't check in assign_class whether it's going to be part of the drawing. --- src/bonsai/bonsai/core/root.py | 2 +- src/bonsai/bonsai/core/tool.py | 1 + src/bonsai/bonsai/tool/root.py | 19 ++++++++++++------- src/bonsai/test/core/test_root.py | 30 ++++++++++++++++++++++++++++++ 4 files changed, 44 insertions(+), 8 deletions(-) diff --git a/src/bonsai/bonsai/core/root.py b/src/bonsai/bonsai/core/root.py index f45d0ee142..4fc6806194 100644 --- a/src/bonsai/bonsai/core/root.py +++ b/src/bonsai/bonsai/core/root.py @@ -86,7 +86,7 @@ def assign_class( obj=obj, context=context, ifc_representation_class=ifc_representation_class, profile_set_usage=None ) - if default_container := root.get_default_container(): + if not root.is_drawing_annotation(element) and (default_container := root.get_default_container()): if root.is_spatial_element(element): ifc.run("aggregate.assign_object", products=[element], relating_object=default_container) elif root.is_containable(element): diff --git a/src/bonsai/bonsai/core/tool.py b/src/bonsai/bonsai/core/tool.py index 02e5102790..f8ddc82421 100644 --- a/src/bonsai/bonsai/core/tool.py +++ b/src/bonsai/bonsai/core/tool.py @@ -723,6 +723,7 @@ class Root: def get_object_representation(cls, obj): pass def get_representation_context(cls, representation): pass def is_containable(cls, element): pass + def is_drawing_annotation(cls, element): pass def is_element_a(cls, element, ifc_class): pass def is_spatial_element(cls, element): pass def link_object_data(cls, source_obj, destination_obj): pass diff --git a/src/bonsai/bonsai/tool/root.py b/src/bonsai/bonsai/tool/root.py index e61a0c3326..3b27b861c3 100644 --- a/src/bonsai/bonsai/tool/root.py +++ b/src/bonsai/bonsai/tool/root.py @@ -170,16 +170,21 @@ class Root(bonsai.core.tool.Root): @classmethod def is_containable(cls, element: ifcopenshell.entity_instance) -> bool: - if element.is_a("IfcElement") or element.is_a("IfcGrid"): + if element.is_a("IfcElement") or element.is_a("IfcGrid") or element.is_a("IfcAnnotation"): return True - if element.is_a("IfcAnnotation"): - if element.ObjectType == "DRAWING": - return False - drawing_group = tool.Drawing.get_drawing_group(element) - if not drawing_group: - return True return False + @classmethod + def is_drawing_annotation(cls, element: ifcopenshell.entity_instance) -> bool: + if not element.is_a("IfcAnnotation"): + return False + if element.ObjectType == "DRAWING": + return True + camera = bpy.context.scene.camera + if not camera or not tool.Ifc.get_entity(camera): + return False + return True + @classmethod def is_element_a(cls, element: ifcopenshell.entity_instance, ifc_class: str) -> bool: return element.is_a(ifc_class) diff --git a/src/bonsai/test/core/test_root.py b/src/bonsai/test/core/test_root.py index 9baff7d962..6a9f3bcad0 100644 --- a/src/bonsai/test/core/test_root.py +++ b/src/bonsai/test/core/test_root.py @@ -130,6 +130,7 @@ class TestAssignClass: obj="obj", context="context", ifc_representation_class="ifc_representation_class", profile_set_usage=None ).should_be_called() + root.is_drawing_annotation("element").should_be_called().will_return(False) root.get_default_container().should_be_called().will_return("default_container") root.is_spatial_element("element").should_be_called().will_return(True) ifc.run("aggregate.assign_object", products=["element"], relating_object="default_container").should_be_called() @@ -161,6 +162,7 @@ class TestAssignClass: obj="obj", context="context", ifc_representation_class="ifc_representation_class", profile_set_usage=None ).should_be_called() + root.is_drawing_annotation("element").should_be_called().will_return(False) root.get_default_container().should_be_called().will_return("default_container") root.is_spatial_element("element").should_be_called().will_return(False) root.is_containable("element").should_be_called().will_return(True) @@ -181,6 +183,33 @@ class TestAssignClass: ifc_representation_class="ifc_representation_class", ) + def test_assign_a_class_to_drawing_annotation_without_assigning_container(self, ifc, collector, root): + ifc.get_entity("obj").should_be_called().will_return(None) + root.get_object_name("obj").should_be_called().will_return("name") + ifc.run( + "root.create_entity", ifc_class="ifc_class", predefined_type="predefined_type", name="name" + ).should_be_called().will_return("element") + root.set_object_name("obj", "element").should_be_called() + ifc.link("element", "obj").should_be_called() + root.run_geometry_add_representation( + obj="obj", context="context", ifc_representation_class="ifc_representation_class", profile_set_usage=None + ).should_be_called() + + root.is_drawing_annotation("element").should_be_called().will_return(True) + + collector.assign("obj").should_be_called() + subject.assign_class( + ifc, + collector, + root, + obj="obj", + ifc_class="ifc_class", + predefined_type="predefined_type", + should_add_representation=True, + context="context", + ifc_representation_class="ifc_representation_class", + ) + def test_not_adding_a_representation_if_requested_no_default_container(self, ifc, collector, root): ifc.get_entity("obj").should_be_called().will_return(None) root.get_object_name("obj").should_be_called().will_return("name") @@ -189,6 +218,7 @@ class TestAssignClass: ).should_be_called().will_return("element") root.set_object_name("obj", "element").should_be_called() ifc.link("element", "obj").should_be_called() + root.is_drawing_annotation("element").should_be_called().will_return(False) root.get_default_container().should_be_called().will_return(None) collector.assign("obj").should_be_called() subject.assign_class(