From 56f0c19a85f626dc0f1d07183788ce815fb7b37a Mon Sep 17 00:00:00 2001 From: Andrej730 Date: Thu, 30 Jan 2025 18:44:41 +0500 Subject: [PATCH] append_asset to support IfcPresentationLayerAssignment #6023 --- .../ifcopenshell/api/project/append_asset.py | 27 +++++++++++--- .../test/api/project/test_append_asset.py | 35 +++++++++++++++++++ 2 files changed, 58 insertions(+), 4 deletions(-) diff --git a/src/ifcopenshell-python/ifcopenshell/api/project/append_asset.py b/src/ifcopenshell-python/ifcopenshell/api/project/append_asset.py index c5b4808f98..f99d5238de 100644 --- a/src/ifcopenshell-python/ifcopenshell/api/project/append_asset.py +++ b/src/ifcopenshell-python/ifcopenshell/api/project/append_asset.py @@ -256,7 +256,8 @@ class Usecase: self.whitelisted_inverse_attributes = { "IfcObjectDefinition": ["HasAssociations"], self.base_material_class: ["HasExternalReferences", "HasProperties", "HasRepresentation"], - "IfcRepresentationItem": ["StyledByItem"], + "IfcRepresentationItem": ["StyledByItem", "LayerAssignment"], + "IfcRepresentation": ["LayerAssignments"], } self.existing_contexts = self.file.by_type("IfcGeometricRepresentationContext") element = self.add_element(self.settings["element"]) @@ -269,7 +270,11 @@ class Usecase: "IfcObject": ["IsDefinedBy.IfcRelDefinesByProperties"], "IfcElement": ["HasOpenings"], self.base_material_class: ["HasExternalReferences", "HasProperties", "HasRepresentation"], - "IfcRepresentationItem": ["StyledByItem"], + "IfcRepresentationItem": [ + "StyledByItem", + "LayerAssignments" if self.file.schema == "IFC2X3" else "LayerAssignment", + ], + "IfcRepresentation": ["LayerAssignments"], } self.existing_contexts = self.file.by_type("IfcGeometricRepresentationContext") element = self.add_element(self.settings["element"]) @@ -362,6 +367,13 @@ class Usecase: # relationships that can reference many other assets that we are not # interested in. + # For layer assignment we don't want to add it's items + # to avoid adding representations / items that are not related to current append_asset. + skip_not_reused_entities_attr_i = None + if element.is_a("IfcPresentationLayerAssignment"): + # 3 IfcPresentationLayerAssignment.AssignedItems + skip_not_reused_entities_attr_i = 2 + element_identity = element.wrapped_data.identity() # Check if inverse element was created before. @@ -386,8 +398,15 @@ class Usecase: elif isinstance(attribute, tuple) and attribute and isinstance(attribute[0], ifcopenshell.entity_instance): new_attribute = [] for item in attribute: - if not self.is_another_asset(item): - new_attribute.append(self.add_element(item)) + if self.is_another_asset(item): + continue + if skip_not_reused_entities_attr_i is not None and i == skip_not_reused_entities_attr_i: + identity = item.wrapped_data.identity() + if (item := self.reuse_identities.get(identity)) is None: + continue + else: + item = self.add_element(item) + new_attribute.append(item) # If rel exists we need to make sure previously assigned elements are untouched # e.g. not to assign a material or a pset from element. if existing_rel: 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 cee115df4c..c2718f5612 100644 --- a/src/ifcopenshell-python/test/api/project/test_append_asset.py +++ b/src/ifcopenshell-python/test/api/project/test_append_asset.py @@ -17,6 +17,7 @@ # along with IfcOpenShell. If not, see . import test.bootstrap +import ifcopenshell.api.layer import ifcopenshell.api.geometry import ifcopenshell.api.georeference import ifcopenshell.api.pset @@ -527,6 +528,40 @@ class TestAppendAssetIFC2X3(test.bootstrap.IFC2X3): == 0.01 ) + def test_append_presentation_layer_for_representation_item(self): + library = ifcopenshell.api.project.create_file(version=self.file.schema) + ifcopenshell.api.root.create_entity(library, ifc_class="IfcProject") + element = ifcopenshell.api.root.create_entity(library, ifc_class="IfcWall") + builder = ShapeBuilder(library) + model = ifcopenshell.api.context.add_context(library, context_type="Model") + item = builder.extrude(builder.profile(builder.rectangle((1, 1)))) + representation = builder.get_representation(model, item) + ifcopenshell.api.geometry.assign_representation(library, element, representation) + layer = ifcopenshell.api.layer.add_layer(library, name="TestLayer") + ifcopenshell.api.layer.assign_layer(library, items=[item], layer=layer) + + ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcProject") + ifcopenshell.api.project.append_asset(self.file, library=library, element=element) + extrusions = tuple(self.file.by_type("IfcExtrudedAreaSolid")) + assert self.file.by_type("IfcPresentationLayerAssignment")[0].AssignedItems == extrusions + + def test_append_presentation_layer_for_representation(self): + library = ifcopenshell.api.project.create_file(version=self.file.schema) + ifcopenshell.api.root.create_entity(library, ifc_class="IfcProject") + element = ifcopenshell.api.root.create_entity(library, ifc_class="IfcWall") + builder = ShapeBuilder(library) + model = ifcopenshell.api.context.add_context(library, context_type="Model") + item = builder.extrude(builder.profile(builder.rectangle((1, 1)))) + representation = builder.get_representation(model, item) + ifcopenshell.api.geometry.assign_representation(library, element, representation) + layer = ifcopenshell.api.layer.add_layer(library, name="TestLayer") + ifcopenshell.api.layer.assign_layer(library, items=[representation], layer=layer) + + ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcProject") + ifcopenshell.api.project.append_asset(self.file, library=library, element=element) + representations = tuple(self.file.by_type("IfcRepresentation")) + assert self.file.by_type("IfcPresentationLayerAssignment")[0].AssignedItems == representations + class TestAppendAssetIFC4(test.bootstrap.IFC4, TestAppendAssetIFC2X3): # NOTE: breaks in IFC2X3 since IfcProfileDef doesn't have "HasProperties" inverse in ifc2x3