diff --git a/src/ifcopenshell-python/ifcopenshell/api/project/append_asset.py b/src/ifcopenshell-python/ifcopenshell/api/project/append_asset.py index 279bdc8fd6..ff9097c2cc 100644 --- a/src/ifcopenshell-python/ifcopenshell/api/project/append_asset.py +++ b/src/ifcopenshell-python/ifcopenshell/api/project/append_asset.py @@ -525,6 +525,7 @@ class Usecase: if added_element := reuse_identities.get(element_identity): return added_element + ifc_class = element.is_a() attributes_ = None def get_attributes() -> tuple[W.attribute, ...]: @@ -550,6 +551,13 @@ class Usecase: if existing_material is not None: reuse_identities[element_identity] = existing_material return existing_material + elif element.is_a("IfcPresentationStyle"): + style_name = element.Name + if style_name is not None: + existing_style = next((e for e in ifc_file.by_type(ifc_class) if e.Name == style_name), None) + if existing_style is not None: + reuse_identities[element_identity] = existing_style + return existing_style attrs = {} @@ -598,7 +606,7 @@ class Usecase: attrs[attr_index] = attr_value # Adding entity at the end just to keep it consistent with `file.add`. - new = ifc_file.create_entity(element.is_a()) + new = ifc_file.create_entity(ifc_class) reuse_identities[element_identity] = new for attr_index, attr_value in attrs.items(): new[attr_index] = attr_value 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 c2718f5612..9251f10592 100644 --- a/src/ifcopenshell-python/test/api/project/test_append_asset.py +++ b/src/ifcopenshell-python/test/api/project/test_append_asset.py @@ -611,18 +611,27 @@ class TestAppendAssetIFC4(test.bootstrap.IFC4, TestAppendAssetIFC2X3): assert appended_item.is_a("IfcCostItem") assert appended_item.IsNestedBy[0].RelatedObjects[0].is_a("IfcCostItem") - def test_not_duplicate_profiles_and_materials_based_on_name(self): + def test_not_duplicate_profiles_materials_styles_based_on_name(self): + # Setup library. library = ifcopenshell.api.project.create_file(version=self.file.schema) + ifcopenshell.api.root.create_entity(library, ifc_class="IfcProject") + model = ifcopenshell.api.context.add_context(library, context_type="Model") + body = ifcopenshell.api.context.add_context( + library, context_type="Model", context_identifier="Body", target_view="MODEL_VIEW", parent=model + ) column_type = ifcopenshell.api.root.create_entity(library, "IfcColumnType") library_profile = ifcopenshell.api.profile.add_parameterized_profile(library, "IfcCircleProfileDef") library_profile.ProfileName = "TestProfile" material_set = ifcopenshell.api.material.add_material_set(library, set_type="IfcMaterialProfileSet") material = ifcopenshell.api.material.add_material(library, "TestMaterial") + style = ifcopenshell.api.style.add_style(library, "TestStyle", ifc_class="IfcSurfaceStyle") + ifcopenshell.api.style.assign_material_style(library, material, style, body) ifcopenshell.api.material.add_profile(library, material_set, material, library_profile) ifcopenshell.api.material.assign_material( library, [column_type], material=material_set, type="IfcMaterialProfileSet" ) + ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcProject") # Test adding a profile with existing name. profile = ifcopenshell.api.profile.add_parameterized_profile(self.file, "IfcCircleProfileDef") profile.ProfileName = "TestProfile" @@ -636,10 +645,18 @@ class TestAppendAssetIFC4(test.bootstrap.IFC4, TestAppendAssetIFC2X3): materials = self.file.by_type("IfcMaterial") assert len(materials) == 1 and materials[0].Name == "TestMaterial" - # Test implicitly adding profile+material with existing names. + # Test adding a style with existing name. + style = ifcopenshell.api.style.add_style(self.file, "TestStyle", ifc_class="IfcSurfaceStyle") + ifcopenshell.api.project.append_asset(self.file, library, style) + styles = self.file.by_type("IfcSurfaceStyle") + assert len(styles) == 1 and styles[0].Name == "TestStyle" + + # Test implicitly adding profile+material+style with existing names. ifcopenshell.api.project.append_asset(self.file, library, column_type) assert len(self.file.by_type("IfcColumnType")) == 1 profiles = self.file.by_type("IfcProfileDef") assert len(profiles) == 1 and profiles[0].ProfileName == "TestProfile" materials = self.file.by_type("IfcMaterial") assert len(materials) == 1 and materials[0].Name == "TestMaterial" + styles = self.file.by_type("IfcSurfaceStyle") + assert len(styles) == 1 and styles[0].Name == "TestStyle"