Fix #7213. IfcTester now supports checking for USERDEFINED predefined types

Can I just take this moment to complain about the overengineered
complexity of how predefined types work in IFC.
This commit is contained in:
Dion Moult
2025-10-11 21:09:41 +11:00
parent b68e8dedd2
commit cb5c488cfd
4 changed files with 102 additions and 4 deletions
@@ -565,6 +565,40 @@ def get_predefined_type(element: ifcopenshell.entity_instance) -> Union[str, Non
return predefined_type
def is_userdefined_type(element: ifcopenshell.entity_instance) -> bool:
"""Checks if the predefined type is userdefined
:param element: The IFC Element entity
:return: True if userdefined
Example:
.. code:: python
element = ifcopenshell.by_type("IfcWall")[0]
is_userdefined_type = ifcopenshell.util.element.is_userdefined_type(element)
"""
if element_type := get_type(element):
predefined_type = getattr(element_type, "PredefinedType", None)
if predefined_type == "USERDEFINED":
return True
elif not predefined_type:
predefined_type = getattr(element_type, "ElementType", ...)
if predefined_type == ...:
predefined_type = getattr(element_type, "ProcessType", None)
if predefined_type:
return True
if predefined_type and predefined_type != "NOTDEFINED":
return False
predefined_type = getattr(element, "PredefinedType", None)
if predefined_type == "USERDEFINED":
return True
elif not predefined_type:
return bool(getattr(element, "ObjectType", None))
return False
def get_type(element: ifcopenshell.entity_instance) -> Union[ifcopenshell.entity_instance, None]:
"""Retrieves the construction type element of an element occurrence.
@@ -369,6 +369,65 @@ class TestGetPredefinedTypeIFC4(test.bootstrap.IFC4):
assert subject.get_predefined_type(element_type) == "NOTDEFINED"
class TestIsUserdefinedTypeIFC4(test.bootstrap.IFC4):
def test_getting_a_predefined_element(self):
element = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcWall")
element.PredefinedType = "PARTITIONING"
assert not subject.is_userdefined_type(element)
def test_getting_an_element_userdefined_type(self):
element = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcWall")
element.PredefinedType = "USERDEFINED"
element.ObjectType = "FOOBAR"
assert subject.is_userdefined_type(element)
def test_getting_an_element_type_without_a_predefined_type_attribute(self):
element = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcAnnotation")
element.ObjectType = "FOOBAR"
assert subject.is_userdefined_type(element)
def test_getting_an_inherited_predefined_type(self):
element = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcWall")
element_type = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcWallType")
ifcopenshell.api.type.assign_type(self.file, related_objects=[element], relating_type=element_type)
element_type.PredefinedType = "PARTITIONING"
assert not subject.is_userdefined_type(element)
def test_getting_an_inherited_userdefined_type_for_an_element_type(self):
element = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcWall")
element_type = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcWallType")
ifcopenshell.api.type.assign_type(self.file, related_objects=[element], relating_type=element_type)
element_type.PredefinedType = "USERDEFINED"
element_type.ElementType = "FOOBAR"
assert subject.is_userdefined_type(element)
def test_getting_an_overriden_predefined_type(self):
element = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcWall")
element_type = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcWallType")
ifcopenshell.api.type.assign_type(self.file, related_objects=[element], relating_type=element_type)
element_type.PredefinedType = "NOTDEFINED"
element.PredefinedType = "PARTITIONING"
assert not subject.is_userdefined_type(element)
def test_getting_an_inherited_userdefined_type_for_a_process_type(self):
element = ifcopenshell.api.sequence.add_task(self.file)
element_type = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcTaskType")
ifcopenshell.api.type.assign_type(self.file, related_objects=[element], relating_type=element_type)
element_type.PredefinedType = "USERDEFINED"
element_type.ProcessType = "FOOBAR"
assert subject.is_userdefined_type(element)
def test_getting_an_element_type_predefined_type(self):
element_type = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcWallType")
element_type.PredefinedType = "PARTITIONING"
assert not subject.is_userdefined_type(element_type)
def test_getting_an_element_type_null_predefined_type(self):
element_type = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcWallType")
element_type.PredefinedType = "NOTDEFINED"
assert not subject.is_userdefined_type(element_type)
class TestGetTypeIFC4(test.bootstrap.IFC4):
def test_getting_the_type_of_a_product(self):
element = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcWall")