mirror of
https://github.com/IfcOpenShell/IfcOpenShell.git
synced 2026-09-18 22:33:33 +00:00
library.unassign_reference - support batching #4474
This commit is contained in:
@@ -87,4 +87,4 @@ def assign_library_reference(ifc, obj=None, reference=None):
|
|||||||
|
|
||||||
|
|
||||||
def unassign_library_reference(ifc, obj=None, reference=None):
|
def unassign_library_reference(ifc, obj=None, reference=None):
|
||||||
ifc.run("library.unassign_reference", product=ifc.get_entity(obj), reference=reference)
|
ifc.run("library.unassign_reference", products=[ifc.get_entity(obj)], reference=reference)
|
||||||
|
|||||||
@@ -121,5 +121,5 @@ class TestAssignLibraryReference:
|
|||||||
class TestUnassignLibraryReference:
|
class TestUnassignLibraryReference:
|
||||||
def test_run(self, ifc):
|
def test_run(self, ifc):
|
||||||
ifc.get_entity("obj").should_be_called().will_return("product")
|
ifc.get_entity("obj").should_be_called().will_return("product")
|
||||||
ifc.run("library.unassign_reference", product="product", reference="reference").should_be_called()
|
ifc.run("library.unassign_reference", products=["product"], reference="reference").should_be_called()
|
||||||
subject.unassign_library_reference(ifc, obj="obj", reference="reference")
|
subject.unassign_library_reference(ifc, obj="obj", reference="reference")
|
||||||
|
|||||||
@@ -96,6 +96,9 @@ ARGUMENTS_DEPRECATION = {
|
|||||||
"library.assign_reference": partial(
|
"library.assign_reference": partial(
|
||||||
batching_argument_deprecation, prev_argument="product", new_argument="products"
|
batching_argument_deprecation, prev_argument="product", new_argument="products"
|
||||||
),
|
),
|
||||||
|
"library.unassign_reference": partial(
|
||||||
|
batching_argument_deprecation, prev_argument="product", new_argument="products"
|
||||||
|
),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -18,18 +18,24 @@
|
|||||||
|
|
||||||
import ifcopenshell
|
import ifcopenshell
|
||||||
import ifcopenshell.util.element
|
import ifcopenshell.util.element
|
||||||
|
import ifcopenshell.api
|
||||||
|
|
||||||
|
|
||||||
class Usecase:
|
class Usecase:
|
||||||
def __init__(self, file, reference=None, product=None):
|
def __init__(
|
||||||
"""Unassigns a product from a reference
|
self,
|
||||||
|
file: ifcopenshell.file,
|
||||||
|
reference: ifcopenshell.entity_instance,
|
||||||
|
products: list[ifcopenshell.entity_instance],
|
||||||
|
):
|
||||||
|
"""Unassigns a product of products from a reference
|
||||||
|
|
||||||
If the product isn't assigned to the reference, nothing will happen.
|
If the product isn't assigned to the reference, nothing will happen.
|
||||||
|
|
||||||
:param reference: The IfcLibraryReference to unassign from
|
:param reference: The IfcLibraryReference to unassign from
|
||||||
:type reference: ifcopenshell.entity_instance.entity_instance
|
:type reference: ifcopenshell.entity_instance.entity_instance
|
||||||
:param product: A IfcProduct element to unassign from the reference
|
:param products: A list of IfcProduct elements to unassign from the reference
|
||||||
:type product: ifcopenshell.entity_instance.entity_instance
|
:type products: list[ifcopenshell.entity_instance.entity_instance]
|
||||||
:return: None
|
:return: None
|
||||||
:rtype: None
|
:rtype: None
|
||||||
|
|
||||||
@@ -52,24 +58,33 @@ class Usecase:
|
|||||||
ifcopenshell.api.run("library.assign_reference", model, reference=reference, products=[ahu])
|
ifcopenshell.api.run("library.assign_reference", model, reference=reference, products=[ahu])
|
||||||
|
|
||||||
# Let's change our mind and unassign it.
|
# Let's change our mind and unassign it.
|
||||||
ifcopenshell.api.run("library.unassign_reference", model, reference=reference, product=ahu)
|
ifcopenshell.api.run("library.unassign_reference", model, reference=reference, products=[ahu])
|
||||||
"""
|
"""
|
||||||
|
|
||||||
self.file = file
|
self.file = file
|
||||||
self.settings = {"reference": reference, "product": product}
|
self.settings = {"reference": reference, "products": products}
|
||||||
|
|
||||||
def execute(self):
|
def execute(self):
|
||||||
rels = self.settings["reference"].LibraryRefForObjects
|
# TODO: do we need to support non-ifcroot elements like we do in classification.add_reference?
|
||||||
if not rels:
|
|
||||||
return
|
reference_rels: set[ifcopenshell.entity_instance] = set()
|
||||||
for rel in rels:
|
products = set(self.settings["products"])
|
||||||
if self.settings["product"] in rel.RelatedObjects:
|
for product in products:
|
||||||
if len(rel.RelatedObjects) == 1:
|
reference_rels.update(product.HasAssociations)
|
||||||
history = rel.OwnerHistory
|
|
||||||
self.file.remove(rel)
|
reference_rels = {
|
||||||
if history:
|
rel
|
||||||
ifcopenshell.util.element.remove_deep2(self.file, history)
|
for rel in reference_rels
|
||||||
continue
|
if rel.is_a("IfcRelAssociatesLibrary") and rel.RelatingLibrary == self.settings["reference"]
|
||||||
related_objects = list(rel.RelatedObjects)
|
}
|
||||||
related_objects.remove(self.settings["product"])
|
|
||||||
rel.RelatedObjects = related_objects
|
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,12 +18,20 @@
|
|||||||
|
|
||||||
import test.bootstrap
|
import test.bootstrap
|
||||||
import ifcopenshell.api
|
import ifcopenshell.api
|
||||||
|
import ifcopenshell.util.element
|
||||||
|
|
||||||
|
|
||||||
class TestUnassignReference(test.bootstrap.IFC4):
|
class TestUnassignReference(test.bootstrap.IFC4):
|
||||||
def test_unassigning_a_reference(self):
|
def test_unassigning_a_reference(self):
|
||||||
reference = self.file.createIfcLibraryReference()
|
reference = self.file.createIfcLibraryReference()
|
||||||
product = self.file.createIfcWall()
|
products = [self.file.createIfcWall() for i in range(3)]
|
||||||
ifcopenshell.api.run("library.assign_reference", self.file, products=[product], reference=reference)
|
ifcopenshell.api.run("library.assign_reference", self.file, products=products, reference=reference)
|
||||||
ifcopenshell.api.run("library.unassign_reference", self.file, product=product, reference=reference)
|
ifcopenshell.api.run("library.unassign_reference", self.file, products=products[:1], reference=reference)
|
||||||
|
assert ifcopenshell.util.element.get_referenced_elements(reference) == set(products[1:])
|
||||||
|
|
||||||
|
ifcopenshell.api.run("library.unassign_reference", self.file, products=products[1:], reference=reference)
|
||||||
|
assert ifcopenshell.util.element.get_referenced_elements(reference) == set()
|
||||||
assert len(self.file.by_type("IfcRelAssociatesLibrary")) == 0
|
assert len(self.file.by_type("IfcRelAssociatesLibrary")) == 0
|
||||||
|
|
||||||
|
class TestUnassignReferenceIFC2X3(test.bootstrap.IFC2X3, TestUnassignReference):
|
||||||
|
pass
|
||||||
|
|||||||
@@ -246,3 +246,11 @@ class TestTemporarySupportForDeprecatedAPIArguments(test.bootstrap.IFC4):
|
|||||||
assert reference.LibraryRefForObjects[0].RelatedObjects == (product,)
|
assert reference.LibraryRefForObjects[0].RelatedObjects == (product,)
|
||||||
ifcopenshell.api.run("library.assign_reference", self.file, product=product2, reference=reference)
|
ifcopenshell.api.run("library.assign_reference", self.file, product=product2, reference=reference)
|
||||||
assert set(reference.LibraryRefForObjects[0].RelatedObjects) == set((product, product2))
|
assert set(reference.LibraryRefForObjects[0].RelatedObjects) == set((product, product2))
|
||||||
|
|
||||||
|
@deprecation_check
|
||||||
|
def test_unassigning_a_reference(self):
|
||||||
|
reference = self.file.createIfcLibraryReference()
|
||||||
|
product = self.file.createIfcWall()
|
||||||
|
ifcopenshell.api.run("library.assign_reference", self.file, products=[product], reference=reference)
|
||||||
|
ifcopenshell.api.run("library.unassign_reference", self.file, product=product, reference=reference)
|
||||||
|
assert len(self.file.by_type("IfcRelAssociatesLibrary")) == 0
|
||||||
|
|||||||
Reference in New Issue
Block a user