spatial.unassign_container - support batching #4474

This commit is contained in:
Andrej730
2024-04-05 16:28:16 +05:00
parent 109787d869
commit cefa088b59
3 changed files with 22 additions and 19 deletions
@@ -22,11 +22,11 @@ import ifcopenshell.util.element
class Usecase:
def __init__(self, file, product=None):
"""Unassigns a container from a product.
def __init__(self, file: ifcopenshell.file, products: list[ifcopenshell.entity_instance]):
"""Unassigns a container from products.
:param product: The IfcProduct to remove the containment from.
:type product: ifcopenshell.entity_instance.entity_instance
:param product: A list of IfcProducts to remove the containment from.
:type product: list[ifcopenshell.entity_instance.entity_instance]
:return: None
:rtype: None
@@ -40,32 +40,34 @@ class Usecase:
storey = ifcopenshell.api.run("root.create_entity", model, ifc_class="IfcBuildingStorey")
# The project contains a site (note that project aggregation is a special case in IFC)
ifcopenshell.api.run("aggregate.assign_object", model, product=site, relating_object=project)
ifcopenshell.api.run("aggregate.assign_object", model, products=[site], relating_object=project)
# The site has a building, the building has a storey, and the storey has a space
ifcopenshell.api.run("aggregate.assign_object", model, product=building, relating_object=site)
ifcopenshell.api.run("aggregate.assign_object", model, product=storey, relating_object=building)
ifcopenshell.api.run("aggregate.assign_object", model, products=[building], relating_object=site)
ifcopenshell.api.run("aggregate.assign_object", model, products=[storey], relating_object=building)
# Create a wall
wall = ifcopenshell.api.run("root.create_entity", model, ifc_class="IfcWall")
# The wall is in the storey
ifcopenshell.api.run("spatial.assign_container", model, product=wall, relating_structure=storey)
ifcopenshell.api.run("spatial.assign_container", model, products=[wall], relating_structure=storey)
# Not anymore!
ifcopenshell.api.run("spatial.unassign_container", model, product=wall)
ifcopenshell.api.run("spatial.unassign_container", model, products=[wall])
"""
self.file = file
self.settings = {
"product": product,
"products": products,
}
def execute(self):
for rel in self.settings["product"].ContainedInStructure or []:
related_elements = list(rel.RelatedElements)
related_elements.remove(self.settings["product"])
def execute(self) -> None:
products = set(self.settings["products"])
rels = set(rel for product in products if (rel := next(iter(product.ContainedInStructure), None)))
for rel in rels:
related_elements = set(rel.RelatedElements) - products
if related_elements:
rel.RelatedElements = related_elements
rel.RelatedElements = list(related_elements)
ifcopenshell.api.run("owner.update_owner_history", self.file, element=rel)
else:
history = rel.OwnerHistory
@@ -28,8 +28,9 @@ class TestAssignContainer(test.bootstrap.IFC4):
def test_unassigning_a_container(self):
element = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcBuilding")
subelement = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall")
ifcopenshell.api.run("spatial.assign_container", self.file, products=[subelement], relating_structure=element)
ifcopenshell.api.run("spatial.unassign_container", self.file, product=subelement)
subelement2 = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall")
ifcopenshell.api.run("spatial.assign_container", self.file, products=[subelement, subelement2], relating_structure=element)
ifcopenshell.api.run("spatial.unassign_container", self.file, products=[subelement, subelement2])
assert not self.file.by_type("IfcRelContainedInSpatialStructure")
def test_unassigning_a_container_with_other_elements(self):
@@ -38,6 +39,6 @@ class TestAssignContainer(test.bootstrap.IFC4):
subelement2 = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall")
ifcopenshell.api.run("spatial.assign_container", self.file, products=[subelement], relating_structure=element)
ifcopenshell.api.run("spatial.assign_container", self.file, products=[subelement2], relating_structure=element)
ifcopenshell.api.run("spatial.unassign_container", self.file, product=subelement)
ifcopenshell.api.run("spatial.unassign_container", self.file, products=[subelement])
rel = self.file.by_type("IfcRelContainedInSpatialStructure")[0]
assert list(rel.RelatedElements) == [subelement2]
@@ -180,7 +180,7 @@ class SvIfcAddSpatialElement(bpy.types.Node, SverchCustomTreeNode, ifcsverchok.h
ifcopenshell.api.run(
"spatial.unassign_container",
self.file,
product=removed_element,
products=[removed_element],
relating_object=result,
)
for added_element in element_set - subelements: