This commit is contained in:
Andrej730
2025-03-11 13:07:54 +05:00
parent c1bc36ab86
commit ed0b1c5009
11 changed files with 253 additions and 173 deletions
@@ -27,7 +27,7 @@ import ifcopenshell.util.representation
import ifcopenshell.util.type
import ifcopenshell.util.schema
import ifcopenshell.util.element
from typing import Optional, Union, Literal, Any
from typing import Optional, Union, Literal
def reassign_class(
@@ -56,7 +56,6 @@ def reassign_class(
Reassigning type class to occurrence (and vice versa) is supported.
:param product: The IfcProduct that you want to change the class of.
:type product: ifcopenshell.entity_instance
: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
too. User defined types are also allowed, just type what you want.
@@ -77,25 +76,18 @@ def reassign_class(
"""
usecase = Usecase()
usecase.file = file
usecase.settings = {
"product": product,
"ifc_class": ifc_class,
"predefined_type": predefined_type,
}
return usecase.execute()
return usecase.execute(product, ifc_class, predefined_type)
class Usecase:
file: ifcopenshell.file
settings: dict[str, Any]
def execute(self):
ifc_class: str = self.settings["ifc_class"]
product: ifcopenshell.entity_instance = self.settings["product"]
predefined_type: Union[str, None] = self.settings["predefined_type"]
def execute(
self, product: ifcopenshell.entity_instance, ifc_class: str, predefined_type: Union[str, None]
) -> ifcopenshell.entity_instance:
was_type_product_before = product.is_a("IfcTypeProduct")
schema = ifcopenshell.schema_by_name(self.file.schema)
is_type_product_after: bool
is_type_product_after = schema.declaration_by_name(ifc_class)._is("IfcTypeProduct")
if was_type_product_before == is_type_product_after:
@@ -28,9 +28,7 @@ def unassign_type(file: ifcopenshell.file, related_objects: list[ifcopenshell.en
and material usages associated with the previously assigned type.
:param related_objects: List of IfcElement occurrences.
:type related_objects: list[ifcopenshell.entity_instance]
:return: None
:rtype: None
Example:
@@ -50,23 +48,21 @@ def unassign_type(file: ifcopenshell.file, related_objects: list[ifcopenshell.en
# Change our mind. Maybe it's a different type?
ifcopenshell.api.type.unassign_type(model, related_objects=[furniture])
"""
settings = {"related_objects": related_objects}
related_objects = set(settings["related_objects"])
related_objects_set = set(related_objects)
if file.schema == "IFC2X3":
rels = set(
rel
for object in related_objects
for object in related_objects_set
if (rel := next((rel for rel in object.IsDefinedBy if rel.is_a("IfcRelDefinesByType")), None))
)
else:
rels = set(rel for object in related_objects if (rel := next((rel for rel in object.IsTypedBy), None)))
rels = set(rel for object in related_objects_set if (rel := next((rel for rel in object.IsTypedBy), None)))
for rel in rels:
related_objects = set(rel.RelatedObjects) - related_objects
if related_objects:
rel.RelatedObjects = list(related_objects)
related_objects_set = set(rel.RelatedObjects) - related_objects_set
if related_objects_set:
rel.RelatedObjects = list(related_objects_set)
ifcopenshell.api.owner.update_owner_history(file, **{"element": rel})
else:
history = rel.OwnerHistory