project.unassign_declaration - support batching #4474

This commit is contained in:
Andrej730
2024-05-03 14:58:32 +05:00
parent de690a385c
commit ac29e152bb
10 changed files with 138 additions and 25 deletions
@@ -120,6 +120,9 @@ ARGUMENTS_DEPRECATION = {
"project.assign_declaration": partial(
batching_argument_deprecation, prev_argument="definition", new_argument="definitions"
),
"project.unassign_declaration": partial(
batching_argument_deprecation, prev_argument="definition", new_argument="definitions"
),
}
@@ -22,13 +22,19 @@ import ifcopenshell.util.element
class Usecase:
def __init__(self, file, definition=None, relating_context=None):
"""Unassigns an object to a project or project library
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
Typically used to remove an asset from a project library.
:param definition: The object you want to undeclare. Typically an asset.
:type definition: ifcopenshell.entity_instance.entity_instance
:param definitions: The list of objects you want to undeclare.
Typically a list of assets.
:type definitions: list[ifcopenshell.entity_instance.entity_instance]
:param relating_context: The IfcProject, or more commonly the
IfcProjectLibrary that you want the object to no longer be part of.
:type relating_context: ifcopenshell.entity_instance.entity_instance
@@ -49,25 +55,25 @@ class Usecase:
ifcopenshell.api.run("project.assign_declaration", library, definitions=[context], relating_context=root)
# Remove the library from our project
ifcopenshell.api.run("project.unassign_declaration", library, definition=context, relating_context=root)
ifcopenshell.api.run("project.unassign_declaration", library, definitions=[context], relating_context=root)
"""
self.file = file
self.settings = {
"definition": definition,
"definitions": definitions,
"relating_context": relating_context,
}
def execute(self):
if not self.settings["definition"].HasContext:
return
rel = self.settings["definition"].HasContext[0]
related_definitions = set(rel.RelatedDefinitions) or set()
related_definitions.remove(self.settings["definition"])
if len(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)
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)
@@ -57,7 +57,7 @@ class Usecase:
ifcopenshell.api.run(
"project.unassign_declaration",
self.file,
definition=self.settings["work_schedule"],
definitions=[self.settings["work_schedule"]],
relating_context=self.file.by_type("IfcContext")[0],
)
rel_aggregates = ifcopenshell.api.run(
@@ -62,7 +62,7 @@ class Usecase:
ifcopenshell.api.run(
"project.unassign_declaration",
self.file,
definition=self.settings["task"],
definitions=[self.settings["task"]],
relating_context=self.file.by_type("IfcContext")[0],
)
if self.settings["task"].TaskTime:
@@ -50,7 +50,7 @@ class Usecase:
ifcopenshell.api.run(
"project.unassign_declaration",
self.file,
definition=self.settings["work_calendar"],
definitions=[self.settings["work_calendar"]],
relating_context=self.file.by_type("IfcContext")[0],
)
if self.settings["work_calendar"].Controls:
@@ -50,7 +50,7 @@ class Usecase:
ifcopenshell.api.run(
"project.unassign_declaration",
self.file,
definition=self.settings["work_plan"],
definitions=[self.settings["work_plan"]],
relating_context=self.file.by_type("IfcContext")[0],
)
history = self.settings["work_plan"].OwnerHistory
@@ -54,7 +54,7 @@ class Usecase:
ifcopenshell.api.run(
"project.unassign_declaration",
self.file,
definition=self.settings["work_schedule"],
definitions=[self.settings["work_schedule"]],
relating_context=self.file.by_type("IfcContext")[0],
)
if self.settings["work_schedule"].Declares: