The API can now copy material sets too

This commit is contained in:
Dion Moult
2024-05-25 14:42:45 +10:00
parent 2f071bd809
commit d85f9af790
2 changed files with 125 additions and 33 deletions
@@ -21,12 +21,15 @@ import ifcopenshell.util.element
def copy_material(file: ifcopenshell.file, material: ifcopenshell.entity_instance) -> ifcopenshell.entity_instance:
"""Copies a material
"""Copies a material or material set
All material psets and styles are copied. The copied material is not
associated to any elements.
:param material: The IfcMaterial to copy
If a material set is copied, the set items are also copied. However the
underlying materials (and profiles) used within the set items are reused.
:param material: The IfcMaterialDefinition to copy
:type material: ifcopenshell.entity_instance
:return: The new copy of the material
:rtype: ifcopenshell.entity_instance
@@ -40,34 +43,54 @@ def copy_material(file: ifcopenshell.file, material: ifcopenshell.entity_instanc
# Let's duplicate the concrete material
concrete_copy = ifcopenshell.api.run("material.copy_material", model, material=concrete)
"""
settings = {"material": material}
if settings["material"].is_a("IfcMaterial"):
new = ifcopenshell.util.element.copy(file, settings["material"])
for inverse in file.get_inverse(settings["material"]):
if inverse.is_a("IfcMaterialProperties"):
# Properties must not be shared between objects for convenience of authoring
inverse = ifcopenshell.util.element.copy(file, inverse)
inverse.Material = new
props_attribute = "Properties"
if file.schema == "IFC2X3":
if not inverse.is_a("IfcExtendedMaterialProperties"):
continue
props_attribute = "ExtendedProperties"
props = getattr(inverse, props_attribute)
if not props:
continue
copied_props = []
for pset in props:
copied_props.append(ifcopenshell.util.element.copy_deep(file, pset))
setattr(inverse, props_attribute, copied_props)
elif inverse.is_a("IfcMaterialDefinitionRepresentation"):
inverse = ifcopenshell.util.element.copy_deep(
file, inverse, exclude=["IfcRepresentationContext", "IfcMaterial"]
)
inverse.RepresentedMaterial = new
if material.is_a("IfcMaterial"):
return _copy_material_with_inverses(file, material)
elif material.is_a("IfcMaterialConstituentSet"):
new = _copy_material_with_inverses(file, material)
new.MaterialConstituents = [copy_material(file, i) for i in material.MaterialConstituents]
return new
elif material.is_a("IfcMaterialConstituent"):
return _copy_material_with_inverses(file, material)
elif material.is_a("IfcMaterialLayerSet"):
new = _copy_material_with_inverses(file, material)
new.MaterialLayers = [copy_material(file, i) for i in material.MaterialLayers]
return new
elif material.is_a("IfcMaterialLayer"):
return _copy_material_with_inverses(file, material)
elif material.is_a("IfcMaterialProfileSet"):
new = _copy_material_with_inverses(file, material)
new.MaterialProfiles = [copy_material(file, i) for i in material.MaterialProfiles]
return new
elif material.is_a("IfcMaterialProfile"):
return _copy_material_with_inverses(file, material)
def _copy_material_with_inverses(file, material):
new = ifcopenshell.util.element.copy(file, material)
for inverse in file.get_inverse(material):
if inverse.is_a("IfcMaterialProperties"):
# Properties must not be shared between objects for convenience of authoring
inverse = ifcopenshell.util.element.copy(file, inverse)
inverse.Material = new
props_attribute = "Properties"
if file.schema == "IFC2X3":
if not inverse.is_a("IfcExtendedMaterialProperties"):
continue
props_attribute = "ExtendedProperties"
props = getattr(inverse, props_attribute)
if not props:
continue
copied_props = []
for pset in props:
copied_props.append(ifcopenshell.util.element.copy_deep(file, pset))
setattr(inverse, props_attribute, copied_props)
elif inverse.is_a("IfcMaterialDefinitionRepresentation"):
inverse = ifcopenshell.util.element.copy_deep(
file, inverse, exclude=["IfcRepresentationContext", "IfcMaterial"]
)
inverse.RepresentedMaterial = new
return new