diff --git a/src/ifcopenshell-python/ifcopenshell/api/material/assign_material.py b/src/ifcopenshell-python/ifcopenshell/api/material/assign_material.py index 3a4f751e45..2b5a498061 100644 --- a/src/ifcopenshell-python/ifcopenshell/api/material/assign_material.py +++ b/src/ifcopenshell-python/ifcopenshell/api/material/assign_material.py @@ -1,4 +1,5 @@ import ifcopenshell +import ifcopenshell.api import ifcopenshell.util.element @@ -10,6 +11,9 @@ class Usecase: self.settings[key] = value def execute(self): + material = ifcopenshell.util.element.get_material(self.settings["product"]) + if material: + ifcopenshell.api.run("material.unassign_material", self.file, product=self.settings["product"]) if self.settings["type"] == "IfcMaterial": self.assign_ifc_material() elif self.settings["type"] == "IfcMaterialConstituentSet": diff --git a/src/ifcopenshell-python/ifcopenshell/api/material/unassign_material.py b/src/ifcopenshell-python/ifcopenshell/api/material/unassign_material.py index 8a6fad3b53..5c16e48a90 100644 --- a/src/ifcopenshell-python/ifcopenshell/api/material/unassign_material.py +++ b/src/ifcopenshell-python/ifcopenshell/api/material/unassign_material.py @@ -9,6 +9,15 @@ class Usecase: self.settings[key] = value def execute(self): - for association in self.settings["product"].HasAssociations: - if association.is_a("IfcRelAssociatesMaterial"): - self.file.remove(association) + for rel in self.settings["product"].HasAssociations: + if rel.is_a("IfcRelAssociatesMaterial"): + if rel.RelatingMaterial.is_a("IfcMaterialLayerSetUsage") or rel.RelatingMaterial.is_a( + "IfcMaterialProfileSetUsage" + ): + self.file.remove(rel.RelatingMaterial) + if len(rel.RelatedObjects) == 1: + self.file.remove(rel) + continue + related_objects = set(rel.RelatedObjects) + related_objects.remove(self.settings["product"]) + rel.RelatedObjects = list(related_object) diff --git a/src/ifcopenshell-python/ifcopenshell/api/type/assign_type.py b/src/ifcopenshell-python/ifcopenshell/api/type/assign_type.py index 38f4c84449..d11fb8f3ae 100644 --- a/src/ifcopenshell-python/ifcopenshell/api/type/assign_type.py +++ b/src/ifcopenshell-python/ifcopenshell/api/type/assign_type.py @@ -1,5 +1,6 @@ import ifcopenshell import ifcopenshell.api +import ifcopenshell.util.element class Usecase: @@ -54,6 +55,7 @@ class Usecase: ) self.map_representations() + self.map_material_usages() def map_representations(self): if not self.settings["relating_type"].RepresentationMaps: @@ -79,3 +81,20 @@ class Usecase: self.file, **{"product": self.settings["related_object"], "representation": mapped_representation} ) + + def map_material_usages(self): + type_material = ifcopenshell.util.element.get_material(self.settings["relating_type"]) + if type_material.is_a("IfcMaterialLayerSet"): + ifcopenshell.api.run( + "material.assign_material", + self.file, + product=self.settings["related_object"], + type="IfcMaterialLayerSetUsage", + ) + elif type_material.is_a("IfcMaterialProfileSet"): + ifcopenshell.api.run( + "material.assign_material", + self.file, + product=self.settings["related_object"], + type="IfcMaterialProfileSetUsage", + )