document.unassign_document - support batching #4474

This commit is contained in:
Andrej730
2024-04-15 16:54:54 +05:00
parent 097f44284f
commit 5da68721d6
8 changed files with 64 additions and 25 deletions
+1 -1
View File
@@ -113,4 +113,4 @@ def assign_document(ifc, product=None, document=None):
def unassign_document(ifc, product=None, document=None): def unassign_document(ifc, product=None, document=None):
ifc.run("document.unassign_document", product=product, document=document) ifc.run("document.unassign_document", products=[product], document=document)
+1 -1
View File
@@ -265,7 +265,7 @@ def duplicate_drawing(ifc, drawing_tool, drawing=None, should_duplicate_annotati
ifc.run("group.assign_group", group=new_group, products=[new_annotation]) ifc.run("group.assign_group", group=new_group, products=[new_annotation])
old_reference = drawing_tool.get_drawing_document(new_drawing) old_reference = drawing_tool.get_drawing_document(new_drawing)
ifc.run("document.unassign_document", product=new_drawing, document=old_reference) ifc.run("document.unassign_document", products=[new_drawing], document=old_reference)
information = ifc.run("document.add_information") information = ifc.run("document.add_information")
uri = drawing_tool.get_default_drawing_path(drawing_name) uri = drawing_tool.get_default_drawing_path(drawing_name)
+1 -1
View File
@@ -139,5 +139,5 @@ class TestAssignDocument:
class TestUnassignDocument: class TestUnassignDocument:
def test_run(self, ifc): def test_run(self, ifc):
ifc.run("document.unassign_document", product="product", document="document").should_be_called() ifc.run("document.unassign_document", products=["product"], document="document").should_be_called()
subject.unassign_document(ifc, product="product", document="document") subject.unassign_document(ifc, product="product", document="document")
+1 -1
View File
@@ -391,7 +391,7 @@ class TestDuplicateDrawing:
ifc.run("group.assign_group", group="new_group", products=["new_annotation"]).should_be_called() ifc.run("group.assign_group", group="new_group", products=["new_annotation"]).should_be_called()
drawing.get_drawing_document("new_drawing").should_be_called().will_return("old_reference") drawing.get_drawing_document("new_drawing").should_be_called().will_return("old_reference")
ifc.run("document.unassign_document", product="new_drawing", document="old_reference").should_be_called() ifc.run("document.unassign_document", products=["new_drawing"], document="old_reference").should_be_called()
ifc.run("document.add_information").should_be_called().will_return("information") ifc.run("document.add_information").should_be_called().will_return("information")
ifc.run("document.add_reference", information="information").should_be_called().will_return("reference") ifc.run("document.add_reference", information="information").should_be_called().will_return("reference")
@@ -102,6 +102,9 @@ ARGUMENTS_DEPRECATION = {
"document.assign_document": partial( "document.assign_document": partial(
batching_argument_deprecation, prev_argument="product", new_argument="products" batching_argument_deprecation, prev_argument="product", new_argument="products"
), ),
"document.unassign_document": partial(
batching_argument_deprecation, prev_argument="product", new_argument="products"
),
} }
@@ -17,16 +17,22 @@
# along with IfcOpenShell. If not, see <http://www.gnu.org/licenses/>. # along with IfcOpenShell. If not, see <http://www.gnu.org/licenses/>.
import ifcopenshell import ifcopenshell
import ifcopenshell.api
import ifcopenshell.util.element import ifcopenshell.util.element
class Usecase: class Usecase:
def __init__(self, file, product=None, document=None): def __init__(
"""Unassigns a document and a product association self,
file: ifcopenshell.file,
products: list[ifcopenshell.entity_instance],
document: ifcopenshell.entity_instance,
):
"""Unassigns a document and an association to the list of products
:param product: The object that the document reference or information is :param product: The list of objects that the document reference or information is
related to. related to.
:type product: ifcopenshell.entity_instance.entity_instance :type product: list[ifcopenshell.entity_instance.entity_instance]
:param document: The IfcDocumentReference (typically) or in rare cases :param document: The IfcDocumentReference (typically) or in rare cases
the IfcDocumentInformation that is associated with the product the IfcDocumentInformation that is associated with the product
:type document: ifcopenshell.entity_instance.entity_instance :type document: ifcopenshell.entity_instance.entity_instance
@@ -48,21 +54,36 @@ class Usecase:
ifcopenshell.api.run("document.assign_document", model, products=[storey], document=reference) ifcopenshell.api.run("document.assign_document", model, products=[storey], document=reference)
# Now let's change our mind and remove the association # Now let's change our mind and remove the association
ifcopenshell.api.run("document.unassign_document", model, product=storey, document=reference) ifcopenshell.api.run("document.unassign_document", model, products=[storey], document=reference)
""" """
self.file = file self.file = file
self.settings = { self.settings = {
"product": product, "products": products,
"document": document, "document": document,
} }
def execute(self): def execute(self):
for rel in self.settings["product"].HasAssociations: # TODO: do we need to support non-ifcroot elements like we do in classification.add_reference?
if rel.is_a("IfcRelAssociatesDocument") and rel.RelatingDocument == self.settings["document"]: # NOTE: reuses code from `library.un assign_reference`
if len(rel.RelatedObjects) == 1:
history = rel.OwnerHistory reference_rels: set[ifcopenshell.entity_instance] = set()
self.file.remove(rel) products = set(self.settings["products"])
if history: for product in products:
ifcopenshell.util.element.remove_deep2(self.file, history) reference_rels.update(product.HasAssociations)
else:
rel.RelatedObjects = [o for o in rel.RelatedObjects if o != self.settings["product"]] reference_rels = {
rel
for rel in reference_rels
if rel.is_a("IfcRelAssociatesDocument") and rel.RelatingDocument == self.settings["document"]
}
for rel in reference_rels:
related_objects = set(rel.RelatedObjects) - products
if related_objects:
rel.RelatedObjects = list(related_objects)
ifcopenshell.api.run("owner.update_owner_history", self.file, **{"element": rel})
else:
history = rel.OwnerHistory
self.file.remove(rel)
if history:
ifcopenshell.util.element.remove_deep2(self.file, history)
@@ -18,6 +18,7 @@
import test.bootstrap import test.bootstrap
import ifcopenshell.api import ifcopenshell.api
import ifcopenshell.util.element
class TestUnassignDocument(test.bootstrap.IFC4): class TestUnassignDocument(test.bootstrap.IFC4):
@@ -25,16 +26,21 @@ class TestUnassignDocument(test.bootstrap.IFC4):
element = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall") element = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall")
reference = ifcopenshell.api.run("document.add_reference", self.file, information=None) reference = ifcopenshell.api.run("document.add_reference", self.file, information=None)
ifcopenshell.api.run("document.assign_document", self.file, products=[element], document=reference) ifcopenshell.api.run("document.assign_document", self.file, products=[element], document=reference)
ifcopenshell.api.run("document.unassign_document", self.file, product=element, document=reference) ifcopenshell.api.run("document.unassign_document", self.file, products=[element], document=reference)
assert not element.HasAssociations assert not element.HasAssociations
assert not len(self.file.by_type("IfcRelAssociatesDocument")) assert not len(self.file.by_type("IfcRelAssociatesDocument"))
def test_unassigning_a_document_used_by_multiple_entities(self): def test_unassigning_a_document_used_by_multiple_entities(self):
element = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall") element = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall")
element2 = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall") element2 = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall")
element3 = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall")
reference = ifcopenshell.api.run("document.add_reference", self.file, information=None) reference = ifcopenshell.api.run("document.add_reference", self.file, information=None)
ifcopenshell.api.run("document.assign_document", self.file, products=[element], document=reference) ifcopenshell.api.run(
ifcopenshell.api.run("document.assign_document", self.file, products=[element2], document=reference) "document.assign_document", self.file, products=[element, element2, element3], document=reference
ifcopenshell.api.run("document.unassign_document", self.file, product=element, document=reference) )
assert not element.HasAssociations ifcopenshell.api.run("document.unassign_document", self.file, products=[element, element2], document=reference)
assert element2.HasAssociations[0].RelatingDocument == reference assert ifcopenshell.util.element.get_referenced_elements(reference) == {element3}
class TestUnassignDocumentIFC2X3(test.bootstrap.IFC2X3, TestUnassignDocument):
pass
@@ -261,3 +261,12 @@ class TestTemporarySupportForDeprecatedAPIArguments(test.bootstrap.IFC4):
reference = ifcopenshell.api.run("document.add_reference", self.file, information=None) reference = ifcopenshell.api.run("document.add_reference", self.file, information=None)
ifcopenshell.api.run("document.assign_document", self.file, product=element, document=reference) ifcopenshell.api.run("document.assign_document", self.file, product=element, document=reference)
assert element.HasAssociations[0].RelatingDocument == reference assert element.HasAssociations[0].RelatingDocument == reference
@deprecation_check
def test_unassigning_a_document(self):
element = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWall")
reference = ifcopenshell.api.run("document.add_reference", self.file, information=None)
ifcopenshell.api.run("document.assign_document", self.file, products=[element], document=reference)
ifcopenshell.api.run("document.unassign_document", self.file, product=element, document=reference)
assert not element.HasAssociations
assert not len(self.file.by_type("IfcRelAssociatesDocument"))