project.assign_declaration - support batching #4474

This commit is contained in:
Andrej730
2024-05-03 14:37:59 +05:00
parent a973b62918
commit de690a385c
19 changed files with 182 additions and 70 deletions
@@ -117,6 +117,9 @@ ARGUMENTS_DEPRECATION = {
"constraint.unassign_constraint": partial(
batching_argument_deprecation, prev_argument="product", new_argument="products"
),
"project.assign_declaration": partial(
batching_argument_deprecation, prev_argument="definition", new_argument="definitions"
),
}
@@ -61,7 +61,7 @@ class Usecase:
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")
ifcopenshell.api.run("project.assign_declaration", library, definition=context, relating_context=root)
ifcopenshell.api.run("project.assign_declaration", library, definitions=[context], relating_context=root)
# Assign units for our example library
unit = ifcopenshell.api.run("unit.add_si_unit", library,
@@ -80,7 +80,7 @@ class Usecase:
# Mark our wall type as a reusable asset in our library.
ifcopenshell.api.run("project.assign_declaration", library,
definition=wall_type, relating_context=context)
definitions=[wall_type], relating_context=context)
# Let's imagine we're starting a new project
model = ifcopenshell.api.run("project.create_file")
@@ -18,11 +18,18 @@
import ifcopenshell
import ifcopenshell.api
import ifcopenshell.util.element
from typing import Union
class Usecase:
def __init__(self, file, definition=None, relating_context=None):
"""Declares an element to the project
def __init__(
self,
file: ifcopenshell.entity_instance,
definitions: list[ifcopenshell.entity_instance],
relating_context: ifcopenshell.entity_instance,
):
"""Declares the list of elements to the project
All data in a model must be directly or indirectly related to the
project. Most data is indirectly related, existing instead within the
@@ -35,13 +42,14 @@ class Usecase:
project libraries for future use (such as an assets library). Assigning
a declaration lets you say that an object belongs to a library.
:param definition: The object you want to declare. Typically an asset.
:type definition: ifcopenshell.entity_instance.entity_instance
:param definitions: The list of objects you want to declare. 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 be part of.
:type relating_context: ifcopenshell.entity_instance.entity_instance
:return: The new IfcRelDeclares relationship
:rtype: ifcopenshell.entity_instance.entity_instance
:return: The new IfcRelDeclares relationship or None if all definitions
were already declared / do not support declaration.
:rtype: Union[ifcopenshell.entity_instance.entity_instance, None]
Example:
@@ -54,7 +62,7 @@ class Usecase:
ifc_class="IfcProjectLibrary", name="Demo Library")
# It's necessary to say our library is part of our project.
ifcopenshell.api.run("project.assign_declaration", library, definition=context, relating_context=root)
ifcopenshell.api.run("project.assign_declaration", library, definitions=[context], relating_context=root)
# Assign units for our example library
unit = ifcopenshell.api.run("unit.add_si_unit", library,
@@ -73,45 +81,61 @@ class Usecase:
# Mark our wall type as a reusable asset in our library.
ifcopenshell.api.run("project.assign_declaration", library,
definition=wall_type, relating_context=context)
definitions=[wall_type], relating_context=context)
# All done, just for fun let's save our asset library to disk for later use.
library.write("/path/to/my-library.ifc")
"""
self.file = file
self.settings = {
"definition": definition,
"definitions": definitions,
"relating_context": relating_context,
}
def execute(self):
declares = None
if self.settings["relating_context"].Declares:
declares = self.settings["relating_context"].Declares[0]
def execute(self) -> Union[ifcopenshell.entity_instance, None]:
relating_context = self.settings["relating_context"]
all_declares = relating_context.Declares
definitions = set(self.settings["definitions"])
if not hasattr(self.settings["definition"], "HasContext"):
return
previous_declares_rels: set[ifcopenshell.entity_instance] = set()
objects_without_contexts: list[ifcopenshell.entity_instance] = []
objects_with_contexts: list[ifcopenshell.entity_instance] = []
has_context = None
if self.settings["definition"].HasContext:
has_context = self.settings["definition"].HasContext[0]
# check if there is anything to change
for definition in definitions:
has_context = getattr(definition, "HasContext", None)
if has_context is None:
continue
if has_context and has_context == declares:
return
object_rel = next(iter(has_context), None)
if object_rel is None:
objects_without_contexts.append(definition)
continue
if has_context:
related_definitions = list(has_context.RelatedDefinitions)
related_definitions.remove(self.settings["definition"])
# either rel doesn't exist or product is part of different rel
if object_rel not in all_declares:
previous_declares_rels.add(object_rel)
objects_with_contexts.append(definition)
objects_to_change = objects_without_contexts + objects_with_contexts
# nothing to change
if not objects_to_change:
return None
for has_context in previous_declares_rels:
related_definitions = set(has_context.RelatedDefinitions) - objects_with_contexts
if related_definitions:
has_context.RelatedDefinitions = related_definitions
ifcopenshell.api.run("owner.update_owner_history", self.file, **{"element": has_context})
else:
history = has_context.OwnerHistory
self.file.remove(has_context)
if history:
ifcopenshell.util.element.remove_deep2(self.file, history)
declares = next(iter(all_declares), None)
if declares:
related_definitions = set(declares.RelatedDefinitions)
related_definitions.add(self.settings["definition"])
declares.RelatedDefinitions = list(related_definitions)
declares.RelatedDefinitions = list(set(declares.RelatedDefinitions) | set(objects_to_change))
ifcopenshell.api.run("owner.update_owner_history", self.file, **{"element": declares})
else:
declares = self.file.create_entity(
@@ -119,8 +143,8 @@ class Usecase:
**{
"GlobalId": ifcopenshell.guid.new(),
"OwnerHistory": ifcopenshell.api.run("owner.create_owner_history", self.file),
"RelatedDefinitions": [self.settings["definition"]],
"RelatingContext": self.settings["relating_context"],
"RelatedDefinitions": list(objects_to_change),
"RelatingContext": relating_context,
}
)
return declares
@@ -46,7 +46,7 @@ class Usecase:
ifc_class="IfcProjectLibrary", name="Demo Library")
# It's necessary to say our library is part of our project.
ifcopenshell.api.run("project.assign_declaration", library, definition=context, relating_context=root)
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)
@@ -105,7 +105,7 @@ class Usecase:
ifcopenshell.api.run(
"project.assign_declaration",
self.file,
definition=resource,
definitions=[resource],
relating_context=context,
)
return resource
@@ -92,7 +92,7 @@ class Usecase:
ifcopenshell.api.run(
"project.assign_declaration",
self.file,
definition=work_calendar,
definitions=[work_calendar],
relating_context=context,
)
return work_calendar
@@ -84,7 +84,7 @@ class Usecase:
ifcopenshell.api.run(
"project.assign_declaration",
self.file,
definition=work_plan,
definitions=[work_plan],
relating_context=context,
)
return work_plan
@@ -118,7 +118,7 @@ class Usecase:
ifcopenshell.api.run(
"project.assign_declaration",
self.file,
definition=work_schedule,
definitions=[work_schedule],
relating_context=context,
)
return work_schedule