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-04-07 13:15:16 +10:00
|
|
|
import ifcopenshell
|
|
|
|
|
import ifcopenshell.api
|
2023-11-27 18:31:42 +11:00
|
|
|
import ifcopenshell.util.element
|
2021-04-07 13:15:16 +10:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class Usecase:
|
2024-05-03 14:58:32 +05:00
|
|
|
def __init__(
|
|
|
|
|
self,
|
|
|
|
|
file: ifcopenshell.file,
|
|
|
|
|
definitions: list[ifcopenshell.entity_instance],
|
|
|
|
|
relating_context: ifcopenshell.entity_instance,
|
|
|
|
|
):
|
|
|
|
|
"""Unassigns a list of objects from a project or project library
|
2022-12-14 17:28:34 +11:00
|
|
|
|
|
|
|
|
Typically used to remove an asset from a project library.
|
|
|
|
|
|
2024-05-03 14:58:32 +05:00
|
|
|
:param definitions: The list of objects you want to undeclare.
|
|
|
|
|
Typically a list of assets.
|
2024-05-06 08:39:41 +10:00
|
|
|
:type definitions: list[ifcopenshell.entity_instance]
|
2022-12-14 17:28:34 +11:00
|
|
|
:param relating_context: The IfcProject, or more commonly the
|
|
|
|
|
IfcProjectLibrary that you want the object to no longer be part of.
|
2024-05-06 08:39:41 +10:00
|
|
|
:type relating_context: ifcopenshell.entity_instance
|
2022-12-14 17:28:34 +11:00
|
|
|
:return: None
|
|
|
|
|
:rtype: None
|
|
|
|
|
|
2023-01-10 10:16:28 +11:00
|
|
|
Example:
|
|
|
|
|
|
|
|
|
|
.. code:: python
|
2022-12-14 17:28:34 +11:00
|
|
|
|
|
|
|
|
# Programmatically generate a library. You could do this visually too.
|
|
|
|
|
library = ifcopenshell.api.run("project.create_file")
|
|
|
|
|
root = ifcopenshell.api.run("root.create_entity", library, ifc_class="IfcProject", name="Demo Library")
|
|
|
|
|
context = ifcopenshell.api.run("root.create_entity", library,
|
|
|
|
|
ifc_class="IfcProjectLibrary", name="Demo Library")
|
|
|
|
|
|
|
|
|
|
# It's necessary to say our library is part of our project.
|
2024-05-03 14:37:59 +05:00
|
|
|
ifcopenshell.api.run("project.assign_declaration", library, definitions=[context], relating_context=root)
|
2022-12-14 17:28:34 +11:00
|
|
|
|
|
|
|
|
# Remove the library from our project
|
2024-05-03 14:58:32 +05:00
|
|
|
ifcopenshell.api.run("project.unassign_declaration", library, definitions=[context], relating_context=root)
|
2022-12-14 17:28:34 +11:00
|
|
|
"""
|
2021-04-07 13:15:16 +10:00
|
|
|
self.file = file
|
|
|
|
|
self.settings = {
|
2024-05-03 14:58:32 +05:00
|
|
|
"definitions": definitions,
|
2022-12-14 17:28:34 +11:00
|
|
|
"relating_context": relating_context,
|
2021-04-07 13:15:16 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def execute(self):
|
2024-05-03 14:58:32 +05:00
|
|
|
definitions = set(self.settings["definitions"])
|
|
|
|
|
rels = {rel for obj in definitions if (rel := next(iter(obj.HasContext), None))}
|
|
|
|
|
|
|
|
|
|
for rel in rels:
|
|
|
|
|
related_definitions = set(rel.RelatedDefinitions) - definitions
|
|
|
|
|
if related_definitions:
|
|
|
|
|
rel.RelatedDefinitions = list(related_definitions)
|
|
|
|
|
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)
|