Files
IfcOpenShell/src/ifcopenshell-python/ifcopenshell/api/constraint/assign_constraint.py
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

96 lines
3.5 KiB
Python
Raw Normal View History

# IfcOpenShell - IFC toolkit and geometry engine
# Copyright (C) 2021 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/>.
2021-01-23 16:32:52 +11:00
import ifcopenshell
import ifcopenshell.api
from typing import Union
2021-01-23 16:32:52 +11:00
class Usecase:
def __init__(
self,
file: ifcopenshell.file,
products: list[ifcopenshell.entity_instance],
constraint: ifcopenshell.entity_instance,
):
"""Assigns a constraint to a list of products
2022-11-16 08:56:55 +09:00
This assigns a relationship between a product and a constraint, so that
when a product's properties and quantities do not match the requirements
of the constraint's metrics, results can be flagged.
It is assumed (but not explicit in the IFC documentation) that
constraints are inherited from the type. This way, it is not necessary
to create lots of constraint assignments.
:param products: The list of products the constraint applies to. This is anything
2022-11-16 08:56:55 +09:00
which can have properties or quantities.
:type products: list[ifcopenshell.entity_instance]
2022-11-16 08:56:55 +09:00
:param constraint: The IfcObjective constraint
:type constraint: ifcopenshell.entity_instance
2022-11-16 08:56:55 +09:00
:return: The new or updated IfcRelAssociatesConstraint relationship
or `None` if `products` was an empty list.
:rtype: ifcopenshell.entity_instance
2022-11-16 08:56:55 +09:00
"""
2021-01-23 16:32:52 +11:00
self.file = file
self.settings = {
"products": products,
2022-11-16 08:56:55 +09:00
"constraint": constraint,
2021-01-23 16:32:52 +11:00
}
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
2021-01-23 16:32:52 +11:00
return self.file.create_entity(
"IfcRelAssociatesConstraint",
**{
"GlobalId": ifcopenshell.guid.new(),
"OwnerHistory": ifcopenshell.api.run("owner.create_owner_history", self.file),
"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