From 93e7d920768ea47cd74ba94c372d6da83b476c18 Mon Sep 17 00:00:00 2001 From: Dion Moult Date: Tue, 1 Apr 2025 17:28:22 +1100 Subject: [PATCH] Fix bug where removing a context (used by representations) left invalid representations --- .../ifcopenshell/api/context/remove_context.py | 6 ++++-- .../test/api/context/test_remove_context.py | 11 ++++++++--- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/ifcopenshell-python/ifcopenshell/api/context/remove_context.py b/src/ifcopenshell-python/ifcopenshell/api/context/remove_context.py index 7fee232456..bb4dcff79a 100644 --- a/src/ifcopenshell-python/ifcopenshell/api/context/remove_context.py +++ b/src/ifcopenshell-python/ifcopenshell/api/context/remove_context.py @@ -59,5 +59,7 @@ def remove_context(file: ifcopenshell.file, context: ifcopenshell.entity_instanc else: representations_in_context = context.RepresentationsInContext file.remove(context) - for element in representations_in_context: - ifcopenshell.api.geometry.remove_representation(file, representation=element) + for rep in representations_in_context: + for element in ifcopenshell.util.element.get_elements_by_representation(file, rep): + ifcopenshell.api.geometry.unassign_representation(file, product=element, representation=rep) + ifcopenshell.api.geometry.remove_representation(file, representation=rep) diff --git a/src/ifcopenshell-python/test/api/context/test_remove_context.py b/src/ifcopenshell-python/test/api/context/test_remove_context.py index 9bc43f64d4..c941021eeb 100644 --- a/src/ifcopenshell-python/test/api/context/test_remove_context.py +++ b/src/ifcopenshell-python/test/api/context/test_remove_context.py @@ -48,11 +48,16 @@ class TestRemoveContext(test.bootstrap.IFC4): assert len(self.file.by_type("IfcMapConversion")) == 0 assert len(self.file.by_type("IfcProjectedCRS")) == 0 - def test_removing_a_context_with_references(self): + def test_removing_a_context_with_assigned_representations(self): context = self.file.createIfcGeometricRepresentationContext() - representation = self.file.createIfcRepresentation(ContextOfItems=context) + element = ifcopenshell.api.root.create_entity(self.file) + rep = self.file.createIfcRepresentation(ContextOfItems=context) + ifcopenshell.api.geometry.assign_representation(self.file, product=element, representation=rep) ifcopenshell.api.context.remove_context(self.file, context=context) - assert len([e for e in self.file]) == 0 + if self.file.schema == "IFC2X3": + assert len([e for e in self.file]) == 6 + else: + assert len([e for e in self.file]) == 1 class TestRemoveContextIFC2X3(test.bootstrap.IFC2X3, TestRemoveContext):