From 7bc1992963190ebc3a619c71b8a386b4572ffa53 Mon Sep 17 00:00:00 2001 From: Dion Moult Date: Fri, 7 Feb 2025 23:52:55 +1100 Subject: [PATCH] Fix regression where adding a new occurrence should not change your active context back to body if you've switched it. --- src/bonsai/bonsai/bim/module/model/product.py | 11 ++++++++++- src/bonsai/test/bim/test_feature.py | 5 +++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/bonsai/bonsai/bim/module/model/product.py b/src/bonsai/bonsai/bim/module/model/product.py index ac55a17244..306068cf42 100644 --- a/src/bonsai/bonsai/bim/module/model/product.py +++ b/src/bonsai/bonsai/bim/module/model/product.py @@ -326,6 +326,12 @@ class AddConstrTypeInstance(bpy.types.Operator, tool.Ifc.Operator): instance_class = ifcopenshell.util.type.get_applicable_entities(ifc_class, tool.Ifc.get().schema)[0] material = ifcopenshell.util.element.get_material(relating_type) + existing_context = None + for existing_occurrence in ifcopenshell.util.element.get_types(relating_type): + if existing_obj := tool.Ifc.get_object(existing_occurrence): + existing_context = tool.Geometry.get_active_representation_context(existing_obj) + break + if material and material.is_a("IfcMaterialProfileSet"): if obj := profile.DumbProfileGenerator(relating_type).generate(): tool.Blender.select_and_activate_single_object(context, obj) @@ -415,7 +421,10 @@ class AddConstrTypeInstance(bpy.types.Operator, tool.Ifc.Operator): element = tool.Ifc.get_entity(obj) bonsai.core.type.assign_type(tool.Ifc, tool.Type, element=element, type=relating_type) - representation = ifcopenshell.util.representation.get_representation(element, "Model", "Body", "MODEL_VIEW") + if existing_context: + representation = ifcopenshell.util.representation.get_representation(element, existing_context) + else: + representation = ifcopenshell.util.representation.get_representation(element, "Model", "Body", "MODEL_VIEW") if not representation and element.Representation: representation = element.Representation.Representations[0] diff --git a/src/bonsai/test/bim/test_feature.py b/src/bonsai/test/bim/test_feature.py index 675fb3b278..edaa4f1bb8 100644 --- a/src/bonsai/test/bim/test_feature.py +++ b/src/bonsai/test/bim/test_feature.py @@ -733,8 +733,9 @@ def the_object_name_has_a_representation_type_of_context(name, type, context): element = ifc.by_id(the_object_name_exists(name).BIMObjectProperties.ifc_definition_id) context, subcontext, target_view = context.split("/") rep = ifcopenshell.util.representation.get_representation(element, context, subcontext or None, target_view or None) + rep =ifcopenshell.util.representation.resolve_representation(rep) assert rep - assert rep.RepresentationType == type + assert rep.RepresentationType == type, f"The object {name} does not have a {type} representation" @given(parsers.parse('the object "{name}" data is a "{type}" representation of "{context}"')) @@ -744,7 +745,7 @@ def the_object_name_data_is_a_type_representation_of_context(name, type, context context, subcontext, target_view = context.split("/") rep = ifc.by_id(the_object_name_exists(name).data.BIMMeshProperties.ifc_definition_id) assert rep - assert rep.RepresentationType == type + assert rep.RepresentationType == type, f"The object {name} is not a {type} representation" assert rep.ContextOfItems.ContextType == context assert rep.ContextOfItems.ContextIdentifier == subcontext assert rep.ContextOfItems.TargetView == target_view