mirror of
https://github.com/IfcOpenShell/IfcOpenShell.git
synced 2026-09-16 21:42:19 +00:00
project.unassign_declaration - support batching #4474
This commit is contained in:
@@ -338,7 +338,7 @@ class UnassignLibraryDeclaration(bpy.types.Operator):
|
|||||||
ifcopenshell.api.run(
|
ifcopenshell.api.run(
|
||||||
"project.unassign_declaration",
|
"project.unassign_declaration",
|
||||||
self.file,
|
self.file,
|
||||||
definition=self.file.by_id(self.definition),
|
definitions=[self.file.by_id(self.definition)],
|
||||||
relating_context=self.file.by_type("IfcProjectLibrary")[0],
|
relating_context=self.file.by_type("IfcProjectLibrary")[0],
|
||||||
)
|
)
|
||||||
element_name = self.props.active_library_element
|
element_name = self.props.active_library_element
|
||||||
|
|||||||
@@ -120,6 +120,9 @@ ARGUMENTS_DEPRECATION = {
|
|||||||
"project.assign_declaration": partial(
|
"project.assign_declaration": partial(
|
||||||
batching_argument_deprecation, prev_argument="definition", new_argument="definitions"
|
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:
|
class Usecase:
|
||||||
def __init__(self, file, definition=None, relating_context=None):
|
def __init__(
|
||||||
"""Unassigns an object to a project or project library
|
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.
|
Typically used to remove an asset from a project library.
|
||||||
|
|
||||||
:param definition: The object you want to undeclare. Typically an asset.
|
:param definitions: The list of objects you want to undeclare.
|
||||||
:type definition: ifcopenshell.entity_instance.entity_instance
|
Typically a list of assets.
|
||||||
|
:type definitions: list[ifcopenshell.entity_instance.entity_instance]
|
||||||
:param relating_context: The IfcProject, or more commonly the
|
:param relating_context: The IfcProject, or more commonly the
|
||||||
IfcProjectLibrary that you want the object to no longer be part of.
|
IfcProjectLibrary that you want the object to no longer be part of.
|
||||||
:type relating_context: ifcopenshell.entity_instance.entity_instance
|
: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)
|
ifcopenshell.api.run("project.assign_declaration", library, definitions=[context], relating_context=root)
|
||||||
|
|
||||||
# Remove the library from our project
|
# 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.file = file
|
||||||
self.settings = {
|
self.settings = {
|
||||||
"definition": definition,
|
"definitions": definitions,
|
||||||
"relating_context": relating_context,
|
"relating_context": relating_context,
|
||||||
}
|
}
|
||||||
|
|
||||||
def execute(self):
|
def execute(self):
|
||||||
if not self.settings["definition"].HasContext:
|
definitions = set(self.settings["definitions"])
|
||||||
return
|
rels = {rel for obj in definitions if (rel := next(iter(obj.HasContext), None))}
|
||||||
rel = self.settings["definition"].HasContext[0]
|
|
||||||
related_definitions = set(rel.RelatedDefinitions) or set()
|
for rel in rels:
|
||||||
related_definitions.remove(self.settings["definition"])
|
related_definitions = set(rel.RelatedDefinitions) - definitions
|
||||||
if len(related_definitions):
|
if related_definitions:
|
||||||
rel.RelatedDefinitions = list(related_definitions)
|
rel.RelatedDefinitions = list(related_definitions)
|
||||||
ifcopenshell.api.run("owner.update_owner_history", self.file, **{"element": rel})
|
ifcopenshell.api.run("owner.update_owner_history", self.file, **{"element": rel})
|
||||||
else:
|
else:
|
||||||
history = rel.OwnerHistory
|
history = rel.OwnerHistory
|
||||||
self.file.remove(rel)
|
self.file.remove(rel)
|
||||||
if history:
|
if history:
|
||||||
ifcopenshell.util.element.remove_deep2(self.file, history)
|
ifcopenshell.util.element.remove_deep2(self.file, history)
|
||||||
|
|||||||
@@ -57,7 +57,7 @@ class Usecase:
|
|||||||
ifcopenshell.api.run(
|
ifcopenshell.api.run(
|
||||||
"project.unassign_declaration",
|
"project.unassign_declaration",
|
||||||
self.file,
|
self.file,
|
||||||
definition=self.settings["work_schedule"],
|
definitions=[self.settings["work_schedule"]],
|
||||||
relating_context=self.file.by_type("IfcContext")[0],
|
relating_context=self.file.by_type("IfcContext")[0],
|
||||||
)
|
)
|
||||||
rel_aggregates = ifcopenshell.api.run(
|
rel_aggregates = ifcopenshell.api.run(
|
||||||
|
|||||||
@@ -62,7 +62,7 @@ class Usecase:
|
|||||||
ifcopenshell.api.run(
|
ifcopenshell.api.run(
|
||||||
"project.unassign_declaration",
|
"project.unassign_declaration",
|
||||||
self.file,
|
self.file,
|
||||||
definition=self.settings["task"],
|
definitions=[self.settings["task"]],
|
||||||
relating_context=self.file.by_type("IfcContext")[0],
|
relating_context=self.file.by_type("IfcContext")[0],
|
||||||
)
|
)
|
||||||
if self.settings["task"].TaskTime:
|
if self.settings["task"].TaskTime:
|
||||||
|
|||||||
@@ -50,7 +50,7 @@ class Usecase:
|
|||||||
ifcopenshell.api.run(
|
ifcopenshell.api.run(
|
||||||
"project.unassign_declaration",
|
"project.unassign_declaration",
|
||||||
self.file,
|
self.file,
|
||||||
definition=self.settings["work_calendar"],
|
definitions=[self.settings["work_calendar"]],
|
||||||
relating_context=self.file.by_type("IfcContext")[0],
|
relating_context=self.file.by_type("IfcContext")[0],
|
||||||
)
|
)
|
||||||
if self.settings["work_calendar"].Controls:
|
if self.settings["work_calendar"].Controls:
|
||||||
|
|||||||
@@ -50,7 +50,7 @@ class Usecase:
|
|||||||
ifcopenshell.api.run(
|
ifcopenshell.api.run(
|
||||||
"project.unassign_declaration",
|
"project.unassign_declaration",
|
||||||
self.file,
|
self.file,
|
||||||
definition=self.settings["work_plan"],
|
definitions=[self.settings["work_plan"]],
|
||||||
relating_context=self.file.by_type("IfcContext")[0],
|
relating_context=self.file.by_type("IfcContext")[0],
|
||||||
)
|
)
|
||||||
history = self.settings["work_plan"].OwnerHistory
|
history = self.settings["work_plan"].OwnerHistory
|
||||||
|
|||||||
@@ -54,7 +54,7 @@ class Usecase:
|
|||||||
ifcopenshell.api.run(
|
ifcopenshell.api.run(
|
||||||
"project.unassign_declaration",
|
"project.unassign_declaration",
|
||||||
self.file,
|
self.file,
|
||||||
definition=self.settings["work_schedule"],
|
definitions=[self.settings["work_schedule"]],
|
||||||
relating_context=self.file.by_type("IfcContext")[0],
|
relating_context=self.file.by_type("IfcContext")[0],
|
||||||
)
|
)
|
||||||
if self.settings["work_schedule"].Declares:
|
if self.settings["work_schedule"].Declares:
|
||||||
|
|||||||
@@ -0,0 +1,82 @@
|
|||||||
|
# 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/>.
|
||||||
|
|
||||||
|
import test.bootstrap
|
||||||
|
import ifcopenshell.api
|
||||||
|
from typing import Union
|
||||||
|
|
||||||
|
|
||||||
|
# NOTE: supported only in IFC4+
|
||||||
|
class TestUnassignDeclaration(test.bootstrap.IFC4):
|
||||||
|
def get_context(self, definition: ifcopenshell.entity_instance) -> Union[ifcopenshell.entity_instance, None]:
|
||||||
|
rel = next(iter(definition.HasContext), None)
|
||||||
|
if rel is not None:
|
||||||
|
return rel.RelatingContext
|
||||||
|
|
||||||
|
def test_unassigning_a_definition(self):
|
||||||
|
library = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcProjectLibrary")
|
||||||
|
element_type = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWallType")
|
||||||
|
element_type2 = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWallType")
|
||||||
|
ifcopenshell.api.run(
|
||||||
|
"project.assign_declaration", self.file, definitions=[element_type, element_type2], relating_context=library
|
||||||
|
)
|
||||||
|
ifcopenshell.api.run(
|
||||||
|
"project.unassign_declaration",
|
||||||
|
self.file,
|
||||||
|
definitions=[element_type, element_type2],
|
||||||
|
relating_context=library,
|
||||||
|
)
|
||||||
|
assert self.get_context(element_type) == None
|
||||||
|
assert len(self.file.by_type("IfcRelDeclares")) == 0
|
||||||
|
|
||||||
|
def test_doing_nothing_if_there_was_no_declaration(self):
|
||||||
|
library = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcProjectLibrary")
|
||||||
|
element_type = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWallType")
|
||||||
|
element_type2 = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWallType")
|
||||||
|
ifcopenshell.api.run(
|
||||||
|
"project.unassign_declaration",
|
||||||
|
self.file,
|
||||||
|
definitions=[element_type, element_type2],
|
||||||
|
relating_context=library,
|
||||||
|
)
|
||||||
|
assert self.get_context(element_type) == None
|
||||||
|
assert self.get_context(element_type2) == None
|
||||||
|
|
||||||
|
def test_updating_the_rel_when_a_reference_is_removed_with_multipled_elements(self):
|
||||||
|
library = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcProjectLibrary")
|
||||||
|
element_type1 = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWallType")
|
||||||
|
element_type2 = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWallType")
|
||||||
|
element_type3 = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWallType")
|
||||||
|
ifcopenshell.api.run(
|
||||||
|
"project.assign_declaration", self.file, definitions=[element_type1], relating_context=library
|
||||||
|
)
|
||||||
|
rel = self.file.by_type("IfcRelDeclares")[0]
|
||||||
|
|
||||||
|
ifcopenshell.api.run(
|
||||||
|
"project.assign_declaration",
|
||||||
|
self.file,
|
||||||
|
definitions=[element_type2, element_type3],
|
||||||
|
relating_context=library,
|
||||||
|
)
|
||||||
|
ifcopenshell.api.run(
|
||||||
|
"project.unassign_declaration",
|
||||||
|
self.file,
|
||||||
|
definitions=[element_type1, element_type2],
|
||||||
|
relating_context=library,
|
||||||
|
)
|
||||||
|
assert rel.RelatedDefinitions == (element_type3,)
|
||||||
@@ -23,6 +23,7 @@ import ifcopenshell.util.constraint
|
|||||||
import ifcopenshell.util.element
|
import ifcopenshell.util.element
|
||||||
import ifcopenshell.util.system
|
import ifcopenshell.util.system
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
from typing import Union
|
||||||
|
|
||||||
|
|
||||||
def deprecation_check(test):
|
def deprecation_check(test):
|
||||||
@@ -326,3 +327,24 @@ class TestTemporarySupportForDeprecatedAPIArguments(test.bootstrap.IFC4):
|
|||||||
)
|
)
|
||||||
assert get_declared_definitions(library) == {element_type}
|
assert get_declared_definitions(library) == {element_type}
|
||||||
assert len(self.file.by_type("IfcRelDeclares")) == 1
|
assert len(self.file.by_type("IfcRelDeclares")) == 1
|
||||||
|
|
||||||
|
@deprecation_check
|
||||||
|
def test_unassigning_a_definition(self):
|
||||||
|
def get_context(definition: ifcopenshell.entity_instance) -> Union[ifcopenshell.entity_instance, None]:
|
||||||
|
rel = next(iter(definition.HasContext), None)
|
||||||
|
if rel is not None:
|
||||||
|
return rel.RelatingContext
|
||||||
|
|
||||||
|
library = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcProjectLibrary")
|
||||||
|
element_type = ifcopenshell.api.run("root.create_entity", self.file, ifc_class="IfcWallType")
|
||||||
|
ifcopenshell.api.run(
|
||||||
|
"project.assign_declaration", self.file, definitions=[element_type], relating_context=library
|
||||||
|
)
|
||||||
|
ifcopenshell.api.run(
|
||||||
|
"project.unassign_declaration",
|
||||||
|
self.file,
|
||||||
|
definition=element_type,
|
||||||
|
relating_context=library,
|
||||||
|
)
|
||||||
|
assert get_context(element_type) == None
|
||||||
|
assert len(self.file.by_type("IfcRelDeclares")) == 0
|
||||||
|
|||||||
Reference in New Issue
Block a user