diff --git a/src/blenderbim/blenderbim/core/geometry.py b/src/blenderbim/blenderbim/core/geometry.py index 539e47204c..63ba4e6961 100644 --- a/src/blenderbim/blenderbim/core/geometry.py +++ b/src/blenderbim/blenderbim/core/geometry.py @@ -183,18 +183,18 @@ def remove_representation( for element in geometry.get_elements_of_type(element_type): obj = ifc.get_object(element) if obj: - obj = geometry.replace_object_with_empty(obj) + geometry.switch_from_representation(obj, representation) obj = ifc.get_object(element_type) if obj: - obj = geometry.replace_object_with_empty(obj) + geometry.switch_from_representation(obj, representation) ifc.run("geometry.unassign_representation", product=element_type, representation=representation) - ifc.run("geometry.remove_representation", representation=representation) else: data = geometry.get_representation_data(representation) if data and geometry.has_data_users(data): - geometry.replace_object_with_empty(obj) + geometry.switch_from_representation(obj, representation) ifc.run("geometry.unassign_representation", product=element, representation=representation) - ifc.run("geometry.remove_representation", representation=representation) + + ifc.run("geometry.remove_representation", representation=representation) if data: geometry.delete_data(data) diff --git a/src/blenderbim/blenderbim/tool/geometry.py b/src/blenderbim/blenderbim/tool/geometry.py index cc9e6cdc52..bf39e32075 100644 --- a/src/blenderbim/blenderbim/tool/geometry.py +++ b/src/blenderbim/blenderbim/tool/geometry.py @@ -862,6 +862,42 @@ class Geometry(blenderbim.core.tool.Geometry): apply_openings=True, ) + @classmethod + def switch_from_representation(cls, obj: bpy.types.Object, representation: ifcopenshell.entity_instance) -> None: + """Switch object representation to any other besides `representation`. + + If no other representation present, will replace object with an empty. + Method assumes that `obj` does have a current representation (it could be not `representation`). + """ + element = tool.Ifc.get_entity(obj) + assert element + + active_representation = tool.Geometry.get_active_representation(obj) + active_representation = tool.Geometry.resolve_mapped_representation(active_representation) + if active_representation != representation: + return + + new_representation = None + for r in cls.get_representations_iter(element): + if r != representation: + new_representation = r + break + + # `representation` is the only representation for object. + if new_representation is None: + cls.replace_object_with_empty(obj) + return + + blenderbim.core.geometry.switch_representation( + tool.Ifc, + tool.Geometry, + obj=obj, + representation=new_representation, + should_reload=False, + should_sync_changes_first=False, + is_global=True, + ) + @classmethod def remove_representation_item(cls, representation_item: ifcopenshell.entity_instance) -> None: # NOTE: we assume it's not the last representation item