From 93b44b43a56c67590737d760eb58d16efede95d4 Mon Sep 17 00:00:00 2001 From: Dion Moult Date: Thu, 9 Mar 2023 17:48:10 +1100 Subject: [PATCH] You can now get all materials including materials in sets via the util module. --- .../ifcopenshell/util/element.py | 33 ++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/src/ifcopenshell-python/ifcopenshell/util/element.py b/src/ifcopenshell-python/ifcopenshell/util/element.py index 9e4d7e6e7b..ca75d91aeb 100644 --- a/src/ifcopenshell-python/ifcopenshell/util/element.py +++ b/src/ifcopenshell-python/ifcopenshell/util/element.py @@ -334,7 +334,7 @@ def get_material(element, should_skip_usage=False, should_inherit=True): .. code:: python element = ifcopenshell.by_type("IfcWall")[0] - material = ifcopenshell.util.element.material(element) + material = ifcopenshell.util.element.get_material(element) """ if hasattr(element, "HasAssociations") and element.HasAssociations: for relationship in element.HasAssociations: @@ -351,6 +351,37 @@ def get_material(element, should_skip_usage=False, should_inherit=True): return get_material(relating_type, should_skip_usage) +def get_materials(element, should_inherit=True): + """Gets individual materials of an element + + If the element has a material set, the individual materials of that set are + returned as a list. + + :param should_inherit: If True, any inherited materials from associated + types will be considered. + :return: The associated materials of the element. + :rtype: list[ifcopenshell.entity_instance.entity_instance] + + Example: + + .. code:: python + + element = ifcopenshell.by_type("IfcWall")[0] + materials = ifcopenshell.util.element.get_materials(element) + """ + material = get_material(element, should_skip_usage=True, should_inherit=should_inherit) + if not material: + return [] + elif material.is_a("IfcMaterial"): + return [material] + elif material.is_a("IfcMaterialLayerSet"): + return [l.Material for l in material.MaterialLayers] + elif material.is_a("IfcMaterialProfileSet"): + return [p.Material for p in material.MaterialProfiles] + elif material.is_a("IfcMaterialConstituentSet"): + return [c.Material for c in material.MaterialConstituents] + + def get_elements_by_material(ifc_file, material): """Retrieves the elements related to a material.