mirror of
https://github.com/IfcOpenShell/IfcOpenShell.git
synced 2026-08-10 01:41:57 +00:00
constraint.assign_constraint - support batching #4474
This commit is contained in:
@@ -111,6 +111,9 @@ ARGUMENTS_DEPRECATION = {
|
||||
"spatial.dereference_structure": partial(
|
||||
batching_argument_deprecation, prev_argument="product", new_argument="products"
|
||||
),
|
||||
"constraint.assign_constraint": partial(
|
||||
batching_argument_deprecation, prev_argument="product", new_argument="products"
|
||||
),
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -18,11 +18,17 @@
|
||||
|
||||
import ifcopenshell
|
||||
import ifcopenshell.api
|
||||
from typing import Union
|
||||
|
||||
|
||||
class Usecase:
|
||||
def __init__(self, file, product=None, constraint=None):
|
||||
"""Assigns a constraint to a product
|
||||
def __init__(
|
||||
self,
|
||||
file: ifcopenshell.file,
|
||||
products: list[ifcopenshell.entity_instance],
|
||||
constraint: ifcopenshell.entity_instance,
|
||||
):
|
||||
"""Assigns a constraint to a list of products
|
||||
|
||||
This assigns a relationship between a product and a constraint, so that
|
||||
when a product's properties and quantities do not match the requirements
|
||||
@@ -32,37 +38,58 @@ class Usecase:
|
||||
constraints are inherited from the type. This way, it is not necessary
|
||||
to create lots of constraint assignments.
|
||||
|
||||
:param product: The product the constraint applies to. This is anything
|
||||
:param products: The list of products the constraint applies to. This is anything
|
||||
which can have properties or quantities.
|
||||
:type product: ifcopenshell.entity_instance.entity_instance
|
||||
:type products: list[ifcopenshell.entity_instance.entity_instance]
|
||||
:param constraint: The IfcObjective constraint
|
||||
:type constraint: ifcopenshell.entity_instance.entity_instance
|
||||
:return: The new or updated IfcRelAssociatesConstraint relationship
|
||||
or `None` if `products` was an empty list.
|
||||
:rtype: ifcopenshell.entity_instance.entity_instance
|
||||
"""
|
||||
self.file = file
|
||||
self.settings = {
|
||||
"product": product,
|
||||
"products": products,
|
||||
"constraint": constraint,
|
||||
}
|
||||
|
||||
def execute(self):
|
||||
rel = self.get_constraint_rel()
|
||||
related_objects = set(rel.RelatedObjects) if rel.RelatedObjects else set()
|
||||
related_objects.add(self.settings["product"])
|
||||
rel.RelatedObjects = list(related_objects)
|
||||
ifcopenshell.api.run("owner.update_owner_history", self.file, **{"element": rel})
|
||||
return rel
|
||||
def execute(self) -> Union[ifcopenshell.entity_instance, None]:
|
||||
products = set(self.settings["products"])
|
||||
if not products:
|
||||
return
|
||||
|
||||
self.constraint = self.settings["constraint"]
|
||||
|
||||
rels = self.get_constraint_rels()
|
||||
related_objects = set()
|
||||
for rel in rels:
|
||||
related_objects.update(rel.RelatedObjects)
|
||||
|
||||
products_to_assign = products - related_objects
|
||||
if not products_to_assign:
|
||||
return rels[0]
|
||||
|
||||
rel = next(iter(rels), None)
|
||||
|
||||
if rel:
|
||||
related_objects = set(rel.RelatedObjects) | products_to_assign
|
||||
rel.RelatedObjects = list(related_objects)
|
||||
ifcopenshell.api.run("owner.update_owner_history", self.file, **{"element": rel})
|
||||
return rel
|
||||
|
||||
def get_constraint_rel(self):
|
||||
for rel in self.file.by_type("IfcRelAssociatesConstraint"):
|
||||
if rel.RelatingConstraint == self.settings["constraint"]:
|
||||
return rel
|
||||
return self.file.create_entity(
|
||||
"IfcRelAssociatesConstraint",
|
||||
**{
|
||||
"GlobalId": ifcopenshell.guid.new(),
|
||||
"OwnerHistory": ifcopenshell.api.run("owner.create_owner_history", self.file),
|
||||
"RelatingConstraint": self.settings["constraint"],
|
||||
"RelatingConstraint": self.constraint,
|
||||
"RelatedObjects": list(products_to_assign),
|
||||
}
|
||||
)
|
||||
|
||||
def get_constraint_rels(self) -> list[ifcopenshell.entity_instance]:
|
||||
rels = []
|
||||
for rel in self.file.get_inverse(self.constraint):
|
||||
if rel.is_a("IfcRelAssociatesConstraint"):
|
||||
rels.append(rel)
|
||||
return rels
|
||||
|
||||
Reference in New Issue
Block a user