Fix #2785. Fix bug where invalid models from Revit that reused material usages could create invalid models.

This commit is contained in:
Dion Moult
2023-02-21 13:48:24 +11:00
parent 03a66d2806
commit 2429f507ad
2 changed files with 43 additions and 5 deletions
@@ -58,6 +58,7 @@ class Usecase:
if self.settings["product"].is_a("IfcTypeObject"):
material = ifcopenshell.util.element.get_material(self.settings["product"])
if material.is_a() in ["IfcMaterialLayerSet", "IfcMaterialProfileSet"]:
# Remove set usages
for inverse in self.file.get_inverse(material):
if self.file.schema == "IFC2X3":
if not inverse.is_a("IfcMaterialLayerSetUsage"):
@@ -76,7 +77,8 @@ class Usecase:
if rel.is_a("IfcRelAssociatesMaterial"):
if rel.RelatingMaterial.is_a() in ["IfcMaterialLayerSetUsage", "IfcMaterialProfileSetUsage"]:
# Warning: this may leave the model in a non-compliant state.
self.file.remove(rel.RelatingMaterial)
if self.file.get_total_inverses(rel.RelatingMaterial) == 1 and len(rel.RelatedObjects) == 1:
self.file.remove(rel.RelatingMaterial)
if len(rel.RelatedObjects) == 1:
self.file.remove(rel)
continue
@@ -25,7 +25,9 @@ class TestUnassignMaterial(test.bootstrap.IFC4):
def test_unassign_single_material(self):
element = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall")
material = ifcopenshell.api.run("material.add_material", self.file, name="CON01")
ifcopenshell.api.run("material.assign_material", self.file, product=element, type="IfcMaterial", material=material)
ifcopenshell.api.run(
"material.assign_material", self.file, product=element, type="IfcMaterial", material=material
)
ifcopenshell.api.run("material.unassign_material", self.file, product=element)
assert len(self.file.by_type("IfcRelAssociatesMaterial")) == 0
assert len(self.file.by_type("IfcWall")) == 1
@@ -35,8 +37,12 @@ class TestUnassignMaterial(test.bootstrap.IFC4):
element1 = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall")
element2 = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall")
material = ifcopenshell.api.run("material.add_material", self.file, name="CON01")
ifcopenshell.api.run("material.assign_material", self.file, product=element1, type="IfcMaterial", material=material)
ifcopenshell.api.run("material.assign_material", self.file, product=element2, type="IfcMaterial", material=material)
ifcopenshell.api.run(
"material.assign_material", self.file, product=element1, type="IfcMaterial", material=material
)
ifcopenshell.api.run(
"material.assign_material", self.file, product=element2, type="IfcMaterial", material=material
)
ifcopenshell.api.run("material.unassign_material", self.file, product=element2)
assert element1.HasAssociations
assert not element2.HasAssociations
@@ -68,6 +74,34 @@ class TestUnassignMaterial(test.bootstrap.IFC4):
assert len(self.file.by_type("IfcMaterialLayerSet")) == 1
assert len(self.file.by_type("IfcMaterialLayerSetUsage")) == 0
def test_unassign_material_layer_set_usage_from_element_with_multiple_invalid_usages(self):
element_type = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWallType")
element = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall")
element2 = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall")
ifcopenshell.api.run("type.assign_type", self.file, related_object=element, relating_type=element_type)
ifcopenshell.api.run("type.assign_type", self.file, related_object=element2, relating_type=element_type)
material = ifcopenshell.api.run("material.add_material", self.file, name="CON01")
ifcopenshell.api.run("material.assign_material", self.file, product=element_type, type="IfcMaterialLayerSet")
rel = ifcopenshell.api.run(
"material.assign_material", self.file, product=element, type="IfcMaterialLayerSetUsage"
)
# In some invalid IFCs from Revit, they reuse usages. Let's recreate this invalid scenario
rel.RelatedObjects = list(rel.RelatedObjects) + [element2]
ifcopenshell.api.run("material.unassign_material", self.file, product=element)
assert len(self.file.by_type("IfcRelAssociatesMaterial")) == 2
for rel in self.file.by_type("IfcRelAssociatesMaterial"):
assert rel.RelatingMaterial
assert element_type.HasAssociations
assert len(self.file.by_type("IfcWallType")) == 1
assert len(self.file.by_type("IfcWall")) == 2
assert len(self.file.by_type("IfcMaterialLayerSet")) == 1
assert len(self.file.by_type("IfcMaterialLayerSetUsage")) == 1
assert ifcopenshell.util.element.get_material(element, should_inherit=False) is None
assert ifcopenshell.util.element.get_material(element2, should_inherit=False).is_a("IfcMaterialLayerSetUsage")
def test_unassign_material_profile_set_from_type(self):
element = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWallType")
material = ifcopenshell.api.run("material.add_material", self.file, name="CON01")
@@ -104,7 +138,9 @@ class TestUnassignMaterial(test.bootstrap.IFC4):
def test_unassign_element_material_list(self):
element = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall")
material = ifcopenshell.api.run("material.add_material", self.file, name="CON01")
ifcopenshell.api.run("material.assign_material", self.file, product=element, type="IfcMaterialList", material=material)
ifcopenshell.api.run(
"material.assign_material", self.file, product=element, type="IfcMaterialList", material=material
)
ifcopenshell.api.run("material.unassign_material", self.file, product=element)
assert len(self.file.by_type("IfcRelAssociatesMaterial")) == 0
assert len(self.file.by_type("IfcMaterialList")) == 1