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: class Usecase:
def __init__(self, file, product=None): def __init__(self, file: ifcopenshell.file, products: list[ifcopenshell.entity_instance]):
"""Unassigns a container from a product. """Unassigns a container from products.
:param product: The IfcProduct to remove the containment from. :param product: A list of IfcProducts to remove the containment from.
:type product: ifcopenshell.entity_instance.entity_instance :type product: list[ifcopenshell.entity_instance.entity_instance]
:return: None :return: None
:rtype: None :rtype: None
@@ -40,32 +40,34 @@ class Usecase:
storey = ifcopenshell.api.run("root.create_entity", model, ifc_class="IfcBuildingStorey") 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) # 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 # 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, products=[building], relating_object=site)
ifcopenshell.api.run("aggregate.assign_object", model, product=storey, relating_object=building) ifcopenshell.api.run("aggregate.assign_object", model, products=[storey], relating_object=building)
# Create a wall # Create a wall
wall = ifcopenshell.api.run("root.create_entity", model, ifc_class="IfcWall") wall = ifcopenshell.api.run("root.create_entity", model, ifc_class="IfcWall")
# The wall is in the storey # 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! # Not anymore!
ifcopenshell.api.run("spatial.unassign_container", model, product=wall) ifcopenshell.api.run("spatial.unassign_container", model, products=[wall])
""" """
self.file = file self.file = file
self.settings = { self.settings = {
"product": product, "products": products,
} }
def execute(self): def execute(self) -> None:
for rel in self.settings["product"].ContainedInStructure or []: products = set(self.settings["products"])
related_elements = list(rel.RelatedElements) rels = set(rel for product in products if (rel := next(iter(product.ContainedInStructure), None)))
related_elements.remove(self.settings["product"])
for rel in rels:
related_elements = set(rel.RelatedElements) - products
if related_elements: if related_elements:
rel.RelatedElements = related_elements rel.RelatedElements = list(related_elements)
ifcopenshell.api.run("owner.update_owner_history", self.file, element=rel) ifcopenshell.api.run("owner.update_owner_history", self.file, element=rel)
else: else:
history = rel.OwnerHistory history = rel.OwnerHistory
@@ -28,8 +28,9 @@ class TestAssignContainer(test.bootstrap.IFC4):
def test_unassigning_a_container(self): def test_unassigning_a_container(self):
element = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcBuilding") element = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcBuilding")
subelement = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall") 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) subelement2 = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall")
ifcopenshell.api.run("spatial.unassign_container", self.file, product=subelement) 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") assert not self.file.by_type("IfcRelContainedInSpatialStructure")
def test_unassigning_a_container_with_other_elements(self): 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") 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=[subelement], relating_structure=element)
ifcopenshell.api.run("spatial.assign_container", self.file, products=[subelement2], 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] rel = self.file.by_type("IfcRelContainedInSpatialStructure")[0]
assert list(rel.RelatedElements) == [subelement2] assert list(rel.RelatedElements) == [subelement2]
@@ -180,7 +180,7 @@ class SvIfcAddSpatialElement(bpy.types.Node, SverchCustomTreeNode, ifcsverchok.h
ifcopenshell.api.run( ifcopenshell.api.run(
"spatial.unassign_container", "spatial.unassign_container",
self.file, self.file,
product=removed_element, products=[removed_element],
relating_object=result, relating_object=result,
) )
for added_element in element_set - subelements: for added_element in element_set - subelements: