From eb3e137a6e40f4454782732d61aaad2816caf2cd Mon Sep 17 00:00:00 2001 From: Dion Moult Date: Wed, 11 May 2022 09:18:11 +1000 Subject: [PATCH] #2196 Get psets now handleds inheritance by default --- .../ifcopenshell/util/element.py | 7 +++++-- src/ifcopenshell-python/test/util/test_element.py | 14 ++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/ifcopenshell-python/ifcopenshell/util/element.py b/src/ifcopenshell-python/ifcopenshell/util/element.py index 8f141cfaa3..d4ef35aee5 100644 --- a/src/ifcopenshell-python/ifcopenshell/util/element.py +++ b/src/ifcopenshell-python/ifcopenshell/util/element.py @@ -19,7 +19,7 @@ import ifcopenshell -def get_psets(element, psets_only=False, qtos_only=False): +def get_psets(element, psets_only=False, qtos_only=False, should_inherit=True): psets = {} if element.is_a("IfcTypeObject"): for definition in element.HasPropertySets or []: @@ -34,6 +34,9 @@ def get_psets(element, psets_only=False, qtos_only=False): continue psets[definition.Name] = get_property_definition(definition) elif hasattr(element, "IsDefinedBy"): + element_type = ifcopenshell.util.element.get_type(element) + if element_type and should_inherit: + psets = get_psets(element_type) for relationship in element.IsDefinedBy: if relationship.is_a("IfcRelDefinesByProperties"): definition = relationship.RelatingPropertyDefinition @@ -41,7 +44,7 @@ def get_psets(element, psets_only=False, qtos_only=False): continue if qtos_only and not definition.is_a("IfcElementQuantity"): continue - psets[definition.Name] = get_property_definition(definition) + psets.setdefault(definition.Name, {}).update(get_property_definition(definition)) return psets diff --git a/src/ifcopenshell-python/test/util/test_element.py b/src/ifcopenshell-python/test/util/test_element.py index 5ec3a49ad6..1a840fd27f 100644 --- a/src/ifcopenshell-python/test/util/test_element.py +++ b/src/ifcopenshell-python/test/util/test_element.py @@ -37,6 +37,20 @@ class TestGetPsetsIFC4(test.bootstrap.IFC4): ifcopenshell.api.run("pset.edit_pset", self.file, pset=pset, properties={"x": "y"}) assert subject.get_psets(type_element) == {"name": {"x": "y", "id": pset.id()}} + def test_getting_inherited_psets(self): + element = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall") + type_element = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWallType") + ifcopenshell.api.run("type.assign_type", self.file, related_object=element, relating_type=type_element) + pset = ifcopenshell.api.run("pset.add_pset", self.file, product=type_element, name="name") + ifcopenshell.api.run("pset.edit_pset", self.file, pset=pset, properties={"a": 1, "x": 1}) + pset = ifcopenshell.api.run("pset.add_pset", self.file, product=element, name="name") + ifcopenshell.api.run("pset.edit_pset", self.file, pset=pset, properties={"a": 2, "b": 3}) + psets = subject.get_psets(element) + assert psets["name"]["id"] == pset.id() + assert psets["name"]["a"] == 2 + assert psets["name"]["x"] == 1 + assert psets["name"]["b"] == 3 + def test_getting_the_psets_of_a_material_as_a_dictionary(self): material = self.file.createIfcMaterial() assert subject.get_psets(material) == {}