This commit is contained in:
Andrej730
2025-03-14 10:43:47 +05:00
parent 10ecac33b8
commit 0e8810c1f5
118 changed files with 869 additions and 1203 deletions
@@ -50,14 +50,10 @@ def add_reference(file: ifcopenshell.file, library: ifcopenshell.entity_instance
ifcopenshell.api.library.edit_reference(model,
reference=reference, attributes={"Identification": "http://example.org/digitaltwin#AHU01"})
"""
settings = {
"library": library,
}
if file.schema == "IFC2X3":
reference = file.createIfcLibraryReference()
references = list(settings["library"].LibraryReference or [])
references = list(library.LibraryReference or [])
references.append(reference)
settings["library"].LibraryReference = references
library.LibraryReference = references
return reference
return file.createIfcLibraryReference(ReferencedLibrary=settings["library"])
return file.createIfcLibraryReference(ReferencedLibrary=library)
@@ -33,14 +33,11 @@ def assign_reference(
detail about how references work.
:param products: The list of IfcProducts you want to associate with the reference
:type products: list[ifcopenshell.entity_instance]
:param reference: The IfcLibraryReference you want the product to be
associated with.
:type reference: ifcopenshell.entity_instance
:return: The IfcRelAssociatesLibrary relationship entity
or `None` if `products` was an empty list or all products were
already assigned to the `reference`.
:rtype: Union[ifcopenshell.entity_instance, None]
Example:
@@ -60,38 +57,33 @@ def assign_reference(
# And now assign the IFC model's AHU with its Brickschema counterpart
ifcopenshell.api.library.assign_reference(model, reference=reference, products=[ahu])
"""
settings = {
"products": products,
"reference": reference,
}
# TODO: do we need to support non-ifcroot elements like we do in classification.add_reference?
referenced_elements = ifcopenshell.util.element.get_referenced_elements(settings["reference"])
products: set[ifcopenshell.entity_instance] = set(settings["products"])
products = products - referenced_elements
referenced_elements = ifcopenshell.util.element.get_referenced_elements(reference)
products_set: set[ifcopenshell.entity_instance] = set(products)
products_set = products_set - referenced_elements
if not products:
if not products_set:
return
if file.schema == "IFC2X3":
rel = next(
(r for r in file.by_type("IfcRelAssociatesLibrary") if r.RelatingLibrary == settings["reference"]),
(r for r in file.by_type("IfcRelAssociatesLibrary") if r.RelatingLibrary == reference),
None,
)
else:
rel = next(iter(settings["reference"].LibraryRefForObjects), None)
rel = next(iter(reference.LibraryRefForObjects), None)
if not rel:
return file.create_entity(
"IfcRelAssociatesLibrary",
GlobalId=ifcopenshell.guid.new(),
OwnerHistory=ifcopenshell.api.owner.create_owner_history(file),
RelatedObjects=list(products),
RelatingLibrary=settings["reference"],
RelatedObjects=list(products_set),
RelatingLibrary=reference,
)
related_objects = set(rel.RelatedObjects) | products
related_objects = set(rel.RelatedObjects) | products_set
rel.RelatedObjects = list(related_objects)
ifcopenshell.api.owner.update_owner_history(file, element=rel)
return rel
@@ -31,11 +31,8 @@ def unassign_reference(
If the product isn't assigned to the reference, nothing will happen.
:param reference: The IfcLibraryReference to unassign from
:type reference: ifcopenshell.entity_instance
:param products: A list of IfcProduct elements to unassign from the reference
:type products: list[ifcopenshell.entity_instance]
:return: None
:rtype: None
Example:
@@ -58,24 +55,19 @@ def unassign_reference(
# Let's change our mind and unassign it.
ifcopenshell.api.library.unassign_reference(model, reference=reference, products=[ahu])
"""
settings = {"reference": reference, "products": products}
# TODO: do we need to support non-ifcroot elements like we do in classification.add_reference?
reference_rels: set[ifcopenshell.entity_instance] = set()
products = set(settings["products"])
for product in products:
products_set = set(products)
for product in products_set:
reference_rels.update(product.HasAssociations)
reference_rels = {
rel
for rel in reference_rels
if rel.is_a("IfcRelAssociatesLibrary") and rel.RelatingLibrary == settings["reference"]
rel for rel in reference_rels if rel.is_a("IfcRelAssociatesLibrary") and rel.RelatingLibrary == reference
}
for rel in reference_rels:
related_objects = set(rel.RelatedObjects) - products
related_objects = set(rel.RelatedObjects) - products_set
if related_objects:
rel.RelatedObjects = list(related_objects)
ifcopenshell.api.owner.update_owner_history(file, **{"element": rel})