From 78d33437ea5394c9c35fccf8b923d75e92d62f26 Mon Sep 17 00:00:00 2001 From: Dion Moult Date: Wed, 12 Jan 2022 14:11:23 +1100 Subject: [PATCH] New utility to get predefined type --- .../ifcopenshell/util/element.py | 14 ++++++++ .../test/util/test_element.py | 36 +++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/src/ifcopenshell-python/ifcopenshell/util/element.py b/src/ifcopenshell-python/ifcopenshell/util/element.py index ef0dc62c44..b59b504c2d 100644 --- a/src/ifcopenshell-python/ifcopenshell/util/element.py +++ b/src/ifcopenshell-python/ifcopenshell/util/element.py @@ -67,6 +67,20 @@ def get_properties(properties): return results +def get_predefined_type(element): + element_type = get_type(element) + if element_type: + predefined_type = getattr(element_type, "PredefinedType", None) + if predefined_type == "USERDEFINED": + predefined_type = getattr(element_type, "ElementType", None) + if predefined_type and predefined_type != "NOTDEFINED": + return predefined_type + predefined_type = getattr(element, "PredefinedType", None) + if predefined_type == "USERDEFINED": + predefined_type = getattr(element, "ObjectType", None) + return predefined_type + + def get_type(element): if element.is_a("IfcTypeObject"): return element diff --git a/src/ifcopenshell-python/test/util/test_element.py b/src/ifcopenshell-python/test/util/test_element.py index 05ff96968c..7986eec7ef 100644 --- a/src/ifcopenshell-python/test/util/test_element.py +++ b/src/ifcopenshell-python/test/util/test_element.py @@ -118,6 +118,42 @@ class TestGetPropertiesIFC4(test.bootstrap.IFC4): } +class TestGetPredefinedTypeIFC4(test.bootstrap.IFC4): + def test_getting_an_element_predefined_type(self): + element = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall") + element.PredefinedType = "PARTITIONING" + assert subject.get_predefined_type(element) == "PARTITIONING" + + def test_getting_an_element_userdefined_type(self): + element = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall") + element.PredefinedType = "USERDEFINED" + element.ObjectType = "FOOBAR" + assert subject.get_predefined_type(element) == "FOOBAR" + + def test_getting_an_inherited_predefined_type(self): + element = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall") + element_type = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWallType") + ifcopenshell.api.run("type.assign_type", self.file, related_object=element, relating_type=element_type) + element_type.PredefinedType = "PARTITIONING" + assert subject.get_predefined_type(element) == "PARTITIONING" + + def test_getting_an_inherited_userdefined_type(self): + element = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall") + element_type = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWallType") + ifcopenshell.api.run("type.assign_type", self.file, related_object=element, relating_type=element_type) + element_type.PredefinedType = "USERDEFINED" + element_type.ElementType = "FOOBAR" + assert subject.get_predefined_type(element) == "FOOBAR" + + def test_getting_an_overriden_predefined_type(self): + element = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall") + element_type = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWallType") + ifcopenshell.api.run("type.assign_type", self.file, related_object=element, relating_type=element_type) + element_type.PredefinedType = "NOTDEFINED" + element.PredefinedType = "PARTITIONING" + assert subject.get_predefined_type(element) == "PARTITIONING" + + class TestGetTypeIFC4(test.bootstrap.IFC4): def test_getting_the_type_of_a_product(self): element = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall")