diff --git a/src/ifcopenshell-python/ifcopenshell/api/project/append_asset.py b/src/ifcopenshell-python/ifcopenshell/api/project/append_asset.py index 5a4aa71f34..462c4a9181 100644 --- a/src/ifcopenshell-python/ifcopenshell/api/project/append_asset.py +++ b/src/ifcopenshell-python/ifcopenshell/api/project/append_asset.py @@ -84,12 +84,12 @@ class Usecase: # Now we can easily append our wall type from our libary wall_type = ifcopenshell.api.run("project.append_asset", model, library=library, element=wall_type) """ - self.file = file + self.file: ifcopenshell.file = file self.settings = {"library": library, "element": element} def execute(self): # mapping of old element ids to new elements - self.added_elements:dict[int, ifcopenshell.entity_instance] = {} + self.added_elements: dict[int, ifcopenshell.entity_instance] = {} self.whitelisted_inverse_attributes = {} if self.settings["element"].is_a("IfcTypeProduct"): self.target_class = "IfcTypeProduct" @@ -268,6 +268,7 @@ class Usecase: def reuse_existing_contexts(self): added_contexts = set([e for e in self.added_elements.values() if e.is_a("IfcGeometricRepresentationContext")]) + added_contexts -= set(self.existing_contexts) for added_context in added_contexts: equivalent_existing_context = self.get_equivalent_existing_context(added_context) if not equivalent_existing_context: diff --git a/src/ifcopenshell-python/test/api/project/test_append_asset.py b/src/ifcopenshell-python/test/api/project/test_append_asset.py index b813c93eac..020c2bcf37 100644 --- a/src/ifcopenshell-python/test/api/project/test_append_asset.py +++ b/src/ifcopenshell-python/test/api/project/test_append_asset.py @@ -44,6 +44,26 @@ class TestAppendAsset(test.bootstrap.IFC4): ifcopenshell.api.run("project.append_asset", self.file, library=library, element=element) assert len(self.file.by_type("IfcWallType")) == 1 + def test_reuse_an_existing_context_if_it_was_added_from_library_previously(self): + library = ifcopenshell.api.run("project.create_file") + project = ifcopenshell.api.run("root.create_entity", library, ifc_class="IfcProject") + lib_context = ifcopenshell.api.run("context.add_context", library, context_type="Model") + + self.file.add(project) # will add project and it's contexts + + material = ifcopenshell.api.run("material.add_material", library, name="Material") + style = ifcopenshell.api.run("style.add_style", library) + ifcopenshell.api.run( + "style.assign_material_style", library, material=material, style=style, context=lib_context + ) + + ifcopenshell.api.run("project.append_asset", self.file, library=library, element=material) + assert len(self.file.by_type("IfcMaterial")) == 1 + assert len(self.file.by_type("IfcGeometricRepresentationContext")) == 1 + context = self.file.by_type("IfcMaterial")[0].HasRepresentation[0].Representations[0].ContextOfItems + # make sure it's still valid + assert context.WorldCoordinateSystem + def test_append_a_single_type_product_even_though_an_inverse_material_relationship_is_shared(self): library = ifcopenshell.api.run("project.create_file") element = ifcopenshell.api.run("root.create_entity", library, ifc_class="IfcWallType") @@ -202,24 +222,40 @@ class TestAppendAsset(test.bootstrap.IFC4): style = ifcopenshell.api.run("style.add_style", library) context = ifcopenshell.api.run("context.add_context", library, context_type="Model") ifcopenshell.api.run("style.assign_material_style", library, material=material, style=style, context=context) + ifcopenshell.api.run("project.append_asset", self.file, library=library, element=material) assert len(self.file.by_type("IfcMaterial")) == 1 assert len(self.file.by_type("IfcGeometricRepresentationContext")) == 1 context = self.file.by_type("IfcMaterial")[0].HasRepresentation[0].Representations[0].ContextOfItems assert context == file_context + # make sure it's still valid assert context.WorldCoordinateSystem def test_append_a_material_with_a_representation_and_reuse_an_existing_subcontext(self): ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcProject") file_context = ifcopenshell.api.run("context.add_context", self.file, context_type="Model") - file_subcontext = ifcopenshell.api.run("context.add_context", self.file, parent=file_context, context_type="Model", context_identifier="Body", target_view="MODEL_VIEW") + file_subcontext = ifcopenshell.api.run( + "context.add_context", + self.file, + parent=file_context, + context_type="Model", + context_identifier="Body", + target_view="MODEL_VIEW", + ) library = ifcopenshell.api.run("project.create_file") ifcopenshell.api.run("root.create_entity", library, ifc_class="IfcProject") material = ifcopenshell.api.run("material.add_material", library, name="Material") style = ifcopenshell.api.run("style.add_style", library) context = ifcopenshell.api.run("context.add_context", library, context_type="Model") - subcontext = ifcopenshell.api.run("context.add_context", library, parent=context, context_type="Model", context_identifier="Body", target_view="MODEL_VIEW") + subcontext = ifcopenshell.api.run( + "context.add_context", + library, + parent=context, + context_type="Model", + context_identifier="Body", + target_view="MODEL_VIEW", + ) ifcopenshell.api.run("style.assign_material_style", library, material=material, style=style, context=subcontext) ifcopenshell.api.run("project.append_asset", self.file, library=library, element=material) assert len(self.file.by_type("IfcMaterial")) == 1 @@ -227,6 +263,7 @@ class TestAppendAsset(test.bootstrap.IFC4): assert len(self.file.by_type("IfcGeometricRepresentationSubContext", include_subtypes=False)) == 1 subcontext = self.file.by_type("IfcMaterial")[0].HasRepresentation[0].Representations[0].ContextOfItems assert subcontext == file_subcontext + # make sure it's still valid assert subcontext.ParentContext.WorldCoordinateSystem def test_append_a_material_with_a_representation_and_reuse_an_existing_context_by_a_new_subcontext(self): @@ -238,8 +275,14 @@ class TestAppendAsset(test.bootstrap.IFC4): material = ifcopenshell.api.run("material.add_material", library, name="Material") style = ifcopenshell.api.run("style.add_style", library) context = ifcopenshell.api.run("context.add_context", library, context_type="Model") - subcontext = ifcopenshell.api.run("context.add_context", library, context_type="Model", - context_identifier="Body", target_view="MODEL_VIEW", parent=context) + subcontext = ifcopenshell.api.run( + "context.add_context", + library, + context_type="Model", + context_identifier="Body", + target_view="MODEL_VIEW", + parent=context, + ) ifcopenshell.api.run("style.assign_material_style", library, material=material, style=style, context=subcontext) ifcopenshell.api.run("project.append_asset", self.file, library=library, element=material) assert len(self.file.by_type("IfcMaterial")) == 1