material.unassign_material - support batching #4474

This commit is contained in:
Andrej730
2024-04-12 16:15:14 +05:00
parent cd25a50e96
commit 657ff30883
9 changed files with 181 additions and 86 deletions
@@ -21,16 +21,17 @@ import ifcopenshell.api
import ifcopenshell.util.element
class TestUnassignMaterial(test.bootstrap.IFC4):
class TestUnassignMaterialIFC2X3(test.bootstrap.IFC2X3):
def test_unassign_single_material(self):
element = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall")
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, products=[element], type="IfcMaterial", material=material
"material.assign_material", self.file, products=[element1, element2], type="IfcMaterial", material=material
)
ifcopenshell.api.run("material.unassign_material", self.file, product=element)
ifcopenshell.api.run("material.unassign_material", self.file, products=[element1, element2])
assert len(self.file.by_type("IfcRelAssociatesMaterial")) == 0
assert len(self.file.by_type("IfcWall")) == 1
assert len(self.file.by_type("IfcWall")) == 2
assert len(self.file.by_type("IfcMaterial")) == 1
def test_unassign_single_material_with_multiple_elements(self):
@@ -38,12 +39,9 @@ class TestUnassignMaterial(test.bootstrap.IFC4):
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, products=[element1], type="IfcMaterial", material=material
"material.assign_material", self.file, products=[element1, element2], type="IfcMaterial", material=material
)
ifcopenshell.api.run(
"material.assign_material", self.file, products=[element2], type="IfcMaterial", material=material
)
ifcopenshell.api.run("material.unassign_material", self.file, product=element2)
ifcopenshell.api.run("material.unassign_material", self.file, products=[element2])
assert element1.HasAssociations
assert not element2.HasAssociations
assert len(self.file.by_type("IfcRelAssociatesMaterial")) == 1
@@ -51,36 +49,63 @@ class TestUnassignMaterial(test.bootstrap.IFC4):
assert len(self.file.by_type("IfcMaterial")) == 1
def test_unassign_material_layer_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")
ifcopenshell.api.run("material.assign_material", self.file, products=[element], type="IfcMaterialLayerSet")
ifcopenshell.api.run("material.unassign_material", self.file, product=element)
element1 = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWallType")
element2 = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWallType")
ifcopenshell.api.run(
"material.assign_material", self.file, products=[element1, element2], type="IfcMaterialLayerSet"
)
ifcopenshell.api.run("material.unassign_material", self.file, products=[element1, element2])
assert len(self.file.by_type("IfcRelAssociatesMaterial")) == 0
assert len(self.file.by_type("IfcWallType")) == 1
assert len(self.file.by_type("IfcWallType")) == 2
assert len(self.file.by_type("IfcMaterialLayerSet")) == 1
def test_unassign_material_layer_set_usage_from_element(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")
ifcopenshell.api.run("type.assign_type", self.file, related_objects=[element], relating_type=element_type)
material = ifcopenshell.api.run("material.add_material", self.file, name="CON01")
element1 = 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_objects=[element1, element2], relating_type=element_type
)
ifcopenshell.api.run("material.assign_material", self.file, products=[element_type], type="IfcMaterialLayerSet")
ifcopenshell.api.run("material.assign_material", self.file, products=[element], type="IfcMaterialLayerSetUsage")
ifcopenshell.api.run("material.unassign_material", self.file, product=element)
ifcopenshell.api.run(
"material.assign_material", self.file, products=[element1, element2], type="IfcMaterialLayerSetUsage"
)
ifcopenshell.api.run("material.unassign_material", self.file, products=[element1, element2])
assert len(self.file.by_type("IfcRelAssociatesMaterial")) == 1
assert element_type.HasAssociations
assert len(self.file.by_type("IfcWallType")) == 1
assert len(self.file.by_type("IfcWall")) == 1
assert len(self.file.by_type("IfcWall")) == 2
assert len(self.file.by_type("IfcMaterialLayerSet")) == 1
assert len(self.file.by_type("IfcMaterialLayerSetUsage")) == 0
def test_unassign_material_layer_set_usage_shouldnt_remove_other_usages_of_the_type(self):
element_type = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWallType")
element1 = 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_objects=[element1, element2], relating_type=element_type
)
ifcopenshell.api.run("material.assign_material", self.file, products=[element_type], type="IfcMaterialLayerSet")
ifcopenshell.api.run(
"material.assign_material", self.file, products=[element1], type="IfcMaterialLayerSetUsage"
)
ifcopenshell.api.run(
"material.assign_material", self.file, products=[element2], type="IfcMaterialLayerSetUsage"
)
ifcopenshell.api.run("material.unassign_material", self.file, products=[element1])
assert len(self.file.by_type("IfcRelAssociatesMaterial")) == 2
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
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_objects=[element], relating_type=element_type)
ifcopenshell.api.run("type.assign_type", self.file, related_objects=[element2], relating_type=element_type)
material = ifcopenshell.api.run("material.add_material", self.file, name="CON01")
ifcopenshell.api.run("material.assign_material", self.file, products=[element_type], type="IfcMaterialLayerSet")
rel = ifcopenshell.api.run(
"material.assign_material", self.file, products=[element], type="IfcMaterialLayerSetUsage"
@@ -89,7 +114,7 @@ class TestUnassignMaterial(test.bootstrap.IFC4):
# 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)
ifcopenshell.api.run("material.unassign_material", self.file, products=[element])
assert len(self.file.by_type("IfcRelAssociatesMaterial")) == 2
for rel in self.file.by_type("IfcRelAssociatesMaterial"):
@@ -102,46 +127,72 @@ class TestUnassignMaterial(test.bootstrap.IFC4):
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")
ifcopenshell.api.run("material.assign_material", self.file, products=[element], type="IfcMaterialProfileSet")
ifcopenshell.api.run("material.unassign_material", self.file, product=element)
def test_unassign_element_material_list(self):
element1 = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall")
element2 = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall")
material1 = ifcopenshell.api.run("material.add_material", self.file, name="CON01")
ifcopenshell.api.run(
"material.assign_material",
self.file,
products=[element1, element2],
type="IfcMaterialList",
material=material1,
)
element3 = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall")
material3 = ifcopenshell.api.run("material.add_material", self.file, name="CON01")
ifcopenshell.api.run(
"material.assign_material", self.file, products=[element3], type="IfcMaterialList", material=material3
)
ifcopenshell.api.run("material.unassign_material", self.file, products=[element1, element2, element3])
assert len(self.file.by_type("IfcRelAssociatesMaterial")) == 0
assert len(self.file.by_type("IfcWallType")) == 1
assert len(self.file.by_type("IfcMaterialProfileSet")) == 1
assert len(self.file.by_type("IfcMaterialList")) == 2
assert len(self.file.by_type("IfcMaterial")) == 2
class TestUnassignMaterialIFC4(test.bootstrap.IFC4, TestUnassignMaterialIFC2X3):
def test_unassign_material_profile_set_from_type(self):
element1 = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWallType")
ifcopenshell.api.run("material.assign_material", self.file, products=[element1], type="IfcMaterialProfileSet")
element2 = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWallType")
ifcopenshell.api.run("material.assign_material", self.file, products=[element2], type="IfcMaterialProfileSet")
ifcopenshell.api.run("material.unassign_material", self.file, products=[element1, element2])
assert len(self.file.by_type("IfcRelAssociatesMaterial")) == 0
assert len(self.file.by_type("IfcWallType")) == 2
assert len(self.file.by_type("IfcMaterialProfileSet")) == 2
def test_unassign_material_profile_set_usage_from_element(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")
ifcopenshell.api.run("type.assign_type", self.file, related_objects=[element], relating_type=element_type)
material = ifcopenshell.api.run("material.add_material", self.file, name="CON01")
ifcopenshell.api.run("material.assign_material", self.file, products=[element_type], type="IfcMaterialProfileSet")
ifcopenshell.api.run("material.assign_material", self.file, products=[element], type="IfcMaterialProfileSetUsage")
ifcopenshell.api.run("material.unassign_material", self.file, product=element)
element1 = 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_objects=[element1, element2], relating_type=element_type
)
ifcopenshell.api.run(
"material.assign_material", self.file, products=[element_type], type="IfcMaterialProfileSet"
)
ifcopenshell.api.run(
"material.assign_material", self.file, products=[element1, element2], type="IfcMaterialProfileSetUsage"
)
ifcopenshell.api.run("material.unassign_material", self.file, products=[element1, element2])
assert len(self.file.by_type("IfcRelAssociatesMaterial")) == 1
assert element_type.HasAssociations
assert len(self.file.by_type("IfcWallType")) == 1
assert len(self.file.by_type("IfcWall")) == 1
assert len(self.file.by_type("IfcWall")) == 2
assert len(self.file.by_type("IfcMaterialProfileSet")) == 1
assert len(self.file.by_type("IfcMaterialProfileSetUsage")) == 0
def test_unassign_material_constituent_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")
ifcopenshell.api.run("material.assign_material", self.file, products=[element], type="IfcMaterialConstituentSet")
ifcopenshell.api.run("material.unassign_material", self.file, product=element)
assert len(self.file.by_type("IfcRelAssociatesMaterial")) == 0
assert len(self.file.by_type("IfcWallType")) == 1
assert len(self.file.by_type("IfcMaterialConstituentSet")) == 1
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")
element1 = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWallType")
element2 = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWallType")
ifcopenshell.api.run(
"material.assign_material", self.file, products=[element], type="IfcMaterialList", material=material
"material.assign_material", self.file, products=[element1, element2], type="IfcMaterialConstituentSet"
)
ifcopenshell.api.run("material.unassign_material", self.file, product=element)
element3 = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWallType")
ifcopenshell.api.run(
"material.assign_material", self.file, products=[element3], type="IfcMaterialConstituentSet"
)
ifcopenshell.api.run("material.unassign_material", self.file, products=[element1, element2, element3])
assert len(self.file.by_type("IfcRelAssociatesMaterial")) == 0
assert len(self.file.by_type("IfcMaterialList")) == 1
assert len(self.file.by_type("IfcMaterial")) == 1
assert len(self.file.by_type("IfcWallType")) == 3
assert len(self.file.by_type("IfcMaterialConstituentSet")) == 2
@@ -174,3 +174,15 @@ class TestTemporarySupportForDeprecatedAPIArguments(test.bootstrap.IFC4):
)
assert len(self.file.by_type("IfcRelAssociatesMaterial")) == 1
assert element.HasAssociations[0].RelatingMaterial == material
@deprecation_check
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, products=[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
assert len(self.file.by_type("IfcMaterial")) == 1