bim.reassign_class - fix issue in ifc2x3 when selected product class would be ignored

E.g. you select IfcSlab and change it to IfcRoof - since there is not IfcRoofType in IFC2X3 it would figure the matching product type is IfcBeamType (which is confusing too but that's another subject) and change this slab's type object to IfcBeamType which consequently change IfcSlab to IfcBeam instead of IfcRoof that was selected originally.

Noticed investigating #5918
This commit is contained in:
Andrej730
2025-03-11 18:35:34 +05:00
parent 5b95bb37e6
commit 977d814d39
3 changed files with 43 additions and 5 deletions
@@ -157,6 +157,9 @@ class ReassignClass(bpy.types.Operator, tool.Ifc.Operator):
product=element, product=element,
ifc_class=ifc_class_, ifc_class=ifc_class_,
predefined_type=predefined_type, predefined_type=predefined_type,
# Provide occurrence class in all cases as it won't really matter
# for non-IfcTypeProducts.
occurrence_class=ifc_class,
) )
reassigned_elements.add(element) reassigned_elements.add(element)
@@ -35,6 +35,7 @@ def reassign_class(
product: ifcopenshell.entity_instance, product: ifcopenshell.entity_instance,
ifc_class: str = "IfcBuildingElementProxy", ifc_class: str = "IfcBuildingElementProxy",
predefined_type: Optional[str] = None, predefined_type: Optional[str] = None,
occurrence_class: Optional[str] = None,
) -> ifcopenshell.entity_instance: ) -> ifcopenshell.entity_instance:
"""Changes the class of a product """Changes the class of a product
@@ -59,6 +60,11 @@ def reassign_class(
:param ifc_class: The new IFC class you want to change it to. :param ifc_class: The new IFC class you want to change it to.
:param predefined_type: In case you want to change the predefined type :param predefined_type: In case you want to change the predefined type
too. User defined types are also allowed, just type what you want. too. User defined types are also allowed, just type what you want.
:param occurrence_class: IFC class to assign to occurrences in case
if provided ``ifc_class`` is IfcTypeProduct.
If omitted, class will be deduced automatically from the type.
Only really needed in IFC2X3, since in IFC4+ there is no ambiguity on
what class to assign to occurrences.
:return: The newly modified product. :return: The newly modified product.
Example: Example:
@@ -76,15 +82,20 @@ def reassign_class(
""" """
usecase = Usecase() usecase = Usecase()
usecase.file = file usecase.file = file
return usecase.execute(product, ifc_class, predefined_type) return usecase.execute(product, ifc_class, predefined_type, occurrence_class)
class Usecase: class Usecase:
file: ifcopenshell.file file: ifcopenshell.file
def execute( def execute(
self, product: ifcopenshell.entity_instance, ifc_class: str, predefined_type: Union[str, None] self,
product: ifcopenshell.entity_instance,
ifc_class: str,
predefined_type: Union[str, None],
occurrence_class: Union[str, None],
) -> ifcopenshell.entity_instance: ) -> ifcopenshell.entity_instance:
self.occurrence_class = occurrence_class
was_type_product_before = product.is_a("IfcTypeProduct") was_type_product_before = product.is_a("IfcTypeProduct")
schema = ifcopenshell.schema_by_name(self.file.schema) schema = ifcopenshell.schema_by_name(self.file.schema)
is_type_product_after: bool is_type_product_after: bool
@@ -154,7 +165,7 @@ class Usecase:
for rep in representations: for rep in representations:
ifcopenshell.api.geometry.assign_representation(self.file, product=element, representation=rep) ifcopenshell.api.geometry.assign_representation(self.file, product=element, representation=rep)
# Keep IFC valid. # Keep IFC valid (PlacementForShapeRepresentation).
if switch_type == "type_to_occurrence" and representations: if switch_type == "type_to_occurrence" and representations:
ifcopenshell.api.geometry.edit_object_placement(self.file, product=element) ifcopenshell.api.geometry.edit_object_placement(self.file, product=element)
@@ -169,7 +180,10 @@ class Usecase:
element = self.reassign_class(element, ifc_class, predefined_type) element = self.reassign_class(element, ifc_class, predefined_type)
if element.is_a("IfcTypeProduct"): if element.is_a("IfcTypeProduct"):
for occurrence in ifcopenshell.util.element.get_types(element): for occurrence in ifcopenshell.util.element.get_types(element):
ifc_class_ = ifcopenshell.util.type.get_applicable_entities(ifc_class, self.file.schema)[0] if self.occurrence_class:
ifc_class_ = self.occurrence_class
else:
ifc_class_ = next(iter(ifcopenshell.util.type.get_applicable_entities(ifc_class, self.file.schema)))
self.reassign_class(occurrence, ifc_class_, predefined_type) self.reassign_class(occurrence, ifc_class_, predefined_type)
else: else:
element_type = ifcopenshell.util.element.get_type(element) element_type = ifcopenshell.util.element.get_type(element)
@@ -198,4 +198,25 @@ class TestReassignClass(test.bootstrap.IFC4):
class TestReassignClassIFC2X3(test.bootstrap.IFC2X3, TestReassignClass): class TestReassignClassIFC2X3(test.bootstrap.IFC2X3, TestReassignClass):
pass def test_providing_occurrence_class(self):
element_type = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcWallType")
element1 = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcWall")
ifcopenshell.api.type.assign_type(self.file, related_objects=[element1], relating_type=element_type)
element2 = ifcopenshell.api.root.create_entity(self.file, ifc_class="IfcWall")
ifcopenshell.api.type.assign_type(self.file, related_objects=[element2], relating_type=element_type)
new_element_type = ifcopenshell.api.root.reassign_class(
self.file,
product=element_type,
ifc_class="IfcBuildingElementProxyType",
occurrence_class="IfcRoof",
)
assert new_element_type.is_a("IfcBuildingElementProxyType")
occurrences = ifcopenshell.util.element.get_types(new_element_type)
assert len(occurrences) == 2
# Assign IfcRoof instead of IfcBuildingElementProxy.
assert all(o.is_a("IfcRoof") for o in occurrences)
# original clases are gone
assert len(self.file.by_type("IfcWall")) == 0
assert len(self.file.by_type("IfcWallType")) == 0