group.unassign_group - support batching #4474

This commit is contained in:
Andrej730
2024-04-05 16:57:55 +05:00
parent df9a306730
commit eebcc48238
8 changed files with 108 additions and 18 deletions
@@ -63,8 +63,9 @@ class Usecase:
)
rel = self.settings["group"].IsGroupedBy[0]
related_objects = set(rel.RelatedObjects) or set()
for obj in self.settings["products"]:
related_objects.add(obj)
rel.RelatedObjects = list(related_objects)
products = set(self.settings["products"])
if products.issubset(related_objects):
return rel
rel.RelatedObjects = list(related_objects | products)
ifcopenshell.api.run("owner.update_owner_history", self.file, **{"element": rel})
return rel
@@ -22,13 +22,13 @@ import ifcopenshell.util.element
class Usecase:
def __init__(self, file, product=None, group=None):
"""Unassigns a product from a group
def __init__(self, file: ifcopenshell.file, products: list[ifcopenshell.entity_instance], group: ifcopenshell.entity_instance):
"""Unassigns products from a group
If the product isn't assigned to the group, nothing will happen.
:param product: A IfcProduct element to unassign from the group
:type product: ifcopenshell.entity_instance.entity_instance
:param products: A list of IfcProduct elements to unassign from the group
:type products: list[ifcopenshell.entity_instance.entity_instance]
:param group: The IfcGroup to unassign from
:type group: ifcopenshell.entity_instance.entity_instance
:return: None
@@ -43,21 +43,22 @@ class Usecase:
ifcopenshell.api.run("group.assign_group", model, products=furniture, group=group)
bad_furniture = furniture[0]
ifcopenshell.api.run("group.unassign_group", model, product=bad_furniture, group=group)
ifcopenshell.api.run("group.unassign_group", model, products=[bad_furniture], group=group)
"""
self.file = file
self.settings = {
"product": product,
"products": products,
"group": group,
}
def execute(self):
def execute(self) -> None:
if not self.settings["group"].IsGroupedBy:
return
rel = self.settings["group"].IsGroupedBy[0]
related_objects = set(rel.RelatedObjects) or set()
related_objects.remove(self.settings["product"])
if len(related_objects):
products = set(self.settings["products"])
related_objects -= products
if related_objects:
rel.RelatedObjects = list(related_objects)
ifcopenshell.api.run("owner.update_owner_history", self.file, **{"element": rel})
else:
@@ -0,0 +1,45 @@
# IfcOpenShell - IFC toolkit and geometry engine
# Copyright (C) 2022 Dion Moult <dion@thinkmoult.com>
#
# This file is part of IfcOpenShell.
#
# IfcOpenShell is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# IfcOpenShell is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with IfcOpenShell. If not, see <http://www.gnu.org/licenses/>.
import test.bootstrap
import ifcopenshell.api
class TestAssignGroup(test.bootstrap.IFC4):
def test_assign_group_for_multiple_elements(self):
element = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcPump")
element2 = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcPump")
group = ifcopenshell.api.run("group.add_group", self.file)
# assign group for multiple elements
ifcopenshell.api.run("group.assign_group", self.file, products=[element, element2], group=group)
assert len(rels := self.file.by_type("IfcRelAssignsToGroup")) == 1
rel = rels[0]
assert rel.RelatingGroup == group
assert rel.RelatedObjects == (element, element2)
def test_reuse_existing_relationship(self):
self.test_assign_group_for_multiple_elements()
rel = self.file.by_type("IfcRelAssignsToGroup")[0]
element = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcPump")
group = self.file.by_type("IfcGroup")[0]
ifcopenshell.api.run("group.assign_group", self.file, products=[element], group=group)
assert len(self.file.by_type("IfcRelAssignsToGroup")) == 1
assert rel.RelatingGroup == group
assert set(rel.RelatedObjects) == set(self.file.by_type("IfcPump"))
@@ -0,0 +1,43 @@
# IfcOpenShell - IFC toolkit and geometry engine
# Copyright (C) 2022 Dion Moult <dion@thinkmoult.com>
#
# This file is part of IfcOpenShell.
#
# IfcOpenShell is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# IfcOpenShell is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with IfcOpenShell. If not, see <http://www.gnu.org/licenses/>.
import test.bootstrap
import ifcopenshell.api
class TestAssignGroup(test.bootstrap.IFC4):
def test_unassignment(self):
element = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcPump")
element2 = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcPump")
element3 = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcPump")
group = ifcopenshell.api.run("group.add_group", self.file)
ifcopenshell.api.run("group.assign_group", self.file, products=[element, element2, element3], group=group)
ifcopenshell.api.run("group.unassign_group", self.file, products=[element2, element3], group=group)
assert len(rels := self.file.by_type("IfcRelAssignsToGroup")) == 1
rel = rels[0]
assert rel.RelatingGroup == group
assert rel.RelatedObjects == (element,)
def test_remove_relationship_unassigning_last_element(self):
element = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcPump")
element2 = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcPump")
group = ifcopenshell.api.run("group.add_group", self.file)
ifcopenshell.api.run("group.assign_group", self.file, products=[element, element2], group=group)
ifcopenshell.api.run("group.unassign_group", self.file, products=[element, element2], group=group)
assert len(self.file.by_type("IfcRelAssignsToGroup")) == 0