2022-01-19 12:18:33 +11:00
|
|
|
# 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-11-16 18:30:23 +11:00
|
|
|
import ifcopenshell
|
2024-04-15 16:00:40 +05:00
|
|
|
import ifcopenshell.api
|
|
|
|
|
import ifcopenshell.util.element
|
|
|
|
|
from typing import Union
|
2021-11-16 18:30:23 +11:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class Usecase:
|
2024-04-15 16:00:40 +05:00
|
|
|
def __init__(
|
|
|
|
|
self, file: ifcopenshell.file, products: ifcopenshell.entity_instance, reference: ifcopenshell.entity_instance
|
|
|
|
|
):
|
|
|
|
|
"""Associates a list products with a library reference
|
2022-12-07 12:20:29 +11:00
|
|
|
|
|
|
|
|
A product may be associated with zero, one, or many references across
|
|
|
|
|
multiple libraries. See ifcopenshell.api.library.add_reference for more
|
|
|
|
|
detail about how references work.
|
|
|
|
|
|
2024-04-15 16:00:40 +05:00
|
|
|
:param products: The list of IfcProducts you want to associate with the reference
|
|
|
|
|
:type products: list[ifcopenshell.entity_instance.entity_instance]
|
2022-12-07 12:20:29 +11:00
|
|
|
:param reference: The IfcLibraryReference you want the product to be
|
|
|
|
|
associated with.
|
|
|
|
|
:type reference: ifcopenshell.entity_instance.entity_instance
|
|
|
|
|
:return: The IfcRelAssociatesLibrary relationship entity
|
2024-04-15 16:00:40 +05:00
|
|
|
or `None` if `products` was an empty list or all products were
|
|
|
|
|
already assigned to the `reference`.
|
|
|
|
|
:rtype: Union[ifcopenshell.entity_instance.entity_instance, None]
|
2022-12-07 12:20:29 +11:00
|
|
|
|
2023-01-10 10:16:28 +11:00
|
|
|
Example:
|
|
|
|
|
|
|
|
|
|
.. code:: python
|
2022-12-07 12:20:29 +11:00
|
|
|
|
|
|
|
|
library = ifcopenshell.api.run("library.add_library", model, name="Brickschema")
|
|
|
|
|
|
|
|
|
|
# Let's create a reference to a single AHU in our Brickschema dataset
|
|
|
|
|
reference = ifcopenshell.api.run("library.add_reference", model, library=library)
|
|
|
|
|
ifcopenshell.api.run("library.edit_reference", model,
|
|
|
|
|
reference=reference, attributes={"Identification": "http://example.org/digitaltwin#AHU01"})
|
|
|
|
|
|
|
|
|
|
# Let's assume we have an AHU in our model.
|
|
|
|
|
ahu = ifcopenshell.api.run("root.create_entity", model,
|
|
|
|
|
ifc_class="IfcUnitaryEquipment", predefined_type="AIRHANDLER")
|
|
|
|
|
|
|
|
|
|
# And now assign the IFC model's AHU with its Brickschema counterpart
|
2024-04-15 16:00:40 +05:00
|
|
|
ifcopenshell.api.run("library.assign_reference", model, reference=reference, products=[ahu])
|
2022-12-07 12:20:29 +11:00
|
|
|
"""
|
2021-11-16 18:30:23 +11:00
|
|
|
self.file = file
|
|
|
|
|
self.settings = {
|
2024-04-15 16:00:40 +05:00
|
|
|
"products": products,
|
2022-12-07 12:20:29 +11:00
|
|
|
"reference": reference,
|
2021-11-16 18:30:23 +11:00
|
|
|
}
|
|
|
|
|
|
2024-04-15 16:00:40 +05:00
|
|
|
def execute(self) -> Union[ifcopenshell.entity_instance, None]:
|
|
|
|
|
# TODO: do we need to support non-ifcroot elements like we do in classification.add_reference?
|
|
|
|
|
|
|
|
|
|
referenced_elements = ifcopenshell.util.element.get_referenced_elements(self.settings["reference"])
|
|
|
|
|
products: set[ifcopenshell.entity_instance] = set(self.settings["products"])
|
|
|
|
|
products = products - referenced_elements
|
|
|
|
|
|
|
|
|
|
if not products:
|
|
|
|
|
return
|
|
|
|
|
|
2021-11-29 20:16:25 +11:00
|
|
|
if self.file.schema == "IFC2X3":
|
2024-04-15 16:00:40 +05:00
|
|
|
rel = next(
|
|
|
|
|
(
|
|
|
|
|
r
|
|
|
|
|
for r in self.file.by_type("IfcRelAssociatesLibrary")
|
|
|
|
|
if r.RelatingLibrary == self.settings["reference"]
|
|
|
|
|
),
|
|
|
|
|
None,
|
|
|
|
|
)
|
2021-11-29 20:16:25 +11:00
|
|
|
else:
|
2024-04-15 16:00:40 +05:00
|
|
|
rel = next(iter(self.settings["reference"].LibraryRefForObjects), None)
|
|
|
|
|
|
|
|
|
|
if not rel:
|
2021-11-16 18:30:23 +11:00
|
|
|
return self.file.create_entity(
|
|
|
|
|
"IfcRelAssociatesLibrary",
|
|
|
|
|
GlobalId=ifcopenshell.guid.new(),
|
|
|
|
|
OwnerHistory=ifcopenshell.api.run("owner.create_owner_history", self.file),
|
2024-04-15 16:00:40 +05:00
|
|
|
RelatedObjects=list(products),
|
2021-11-16 18:30:23 +11:00
|
|
|
RelatingLibrary=self.settings["reference"],
|
|
|
|
|
)
|
|
|
|
|
|
2024-04-15 16:00:40 +05:00
|
|
|
related_objects = set(rel.RelatedObjects) | products
|
|
|
|
|
rel.RelatedObjects = list(related_objects)
|
2021-11-16 18:30:23 +11:00
|
|
|
ifcopenshell.api.run("owner.update_owner_history", self.file, element=rel)
|
|
|
|
|
return rel
|