mirror of
https://github.com/IfcOpenShell/IfcOpenShell.git
synced 2026-08-13 10:57:49 +00:00
Generate functions for all API usecases for better static code features. See #2693.
This commit is contained in:
@@ -15,3 +15,8 @@
|
||||
#
|
||||
# You should have received a copy of the GNU Lesser General Public License
|
||||
# along with IfcOpenShell. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
from .assign_object import assign_object
|
||||
from .change_nest import change_nest
|
||||
from .reorder_nesting import reorder_nesting
|
||||
from .unassign_object import unassign_object
|
||||
|
||||
@@ -22,156 +22,152 @@ import ifcopenshell.util.element
|
||||
from typing import Union
|
||||
|
||||
|
||||
class Usecase:
|
||||
def __init__(
|
||||
self,
|
||||
file: ifcopenshell.file,
|
||||
related_objects: list[ifcopenshell.entity_instance],
|
||||
relating_object: ifcopenshell.entity_instance,
|
||||
):
|
||||
"""Assigns objects as nested children to a parent host
|
||||
def assign_object(
|
||||
file: ifcopenshell.file,
|
||||
related_objects: list[ifcopenshell.entity_instance],
|
||||
relating_object: ifcopenshell.entity_instance,
|
||||
) -> Union[ifcopenshell.entity_instance, None]:
|
||||
"""Assigns objects as nested children to a parent host
|
||||
|
||||
All physical IFC model elements must be part of a hierarchical tree
|
||||
called the "spatial decomposition", where large things are made up of
|
||||
smaller things. This tree always begins at an "IfcProject" and is then
|
||||
broken down using "decomposition" relationships, of which aggregation is
|
||||
the first relationship you will use.
|
||||
All physical IFC model elements must be part of a hierarchical tree
|
||||
called the "spatial decomposition", where large things are made up of
|
||||
smaller things. This tree always begins at an "IfcProject" and is then
|
||||
broken down using "decomposition" relationships, of which aggregation is
|
||||
the first relationship you will use.
|
||||
|
||||
Another type of "decomposition" relationship is known as "nesting".
|
||||
Nesting is used when an child object is physically attached to a parent
|
||||
host object, through a physical predetermined connection point. The
|
||||
child object must be specifically designed to attach to a other objects
|
||||
at specific positions with a particular form factor. Examples include
|
||||
faucets which must always be attached through a predrilled hole in a
|
||||
basin. Alternatively, it could be a modular attachment with a
|
||||
correlating male and female joint that must join at a particular point.
|
||||
Because there is a strict connection point, when the parent moves, all
|
||||
nested children must move with the parent. Another example might be a
|
||||
predrilled hole in a door panel where hardware must fit through.
|
||||
Another type of "decomposition" relationship is known as "nesting".
|
||||
Nesting is used when an child object is physically attached to a parent
|
||||
host object, through a physical predetermined connection point. The
|
||||
child object must be specifically designed to attach to a other objects
|
||||
at specific positions with a particular form factor. Examples include
|
||||
faucets which must always be attached through a predrilled hole in a
|
||||
basin. Alternatively, it could be a modular attachment with a
|
||||
correlating male and female joint that must join at a particular point.
|
||||
Because there is a strict connection point, when the parent moves, all
|
||||
nested children must move with the parent. Another example might be a
|
||||
predrilled hole in a door panel where hardware must fit through.
|
||||
|
||||
Nesting relationships are not very commonly used in most design and
|
||||
construction models. Its main usecase is in modular construction, kit of
|
||||
parts, or fabrication models.
|
||||
Nesting relationships are not very commonly used in most design and
|
||||
construction models. Its main usecase is in modular construction, kit of
|
||||
parts, or fabrication models.
|
||||
|
||||
As a product may only have a single location in the "spatial
|
||||
decomposition" tree, assigning an nesting relationship will remove any
|
||||
previous aggregation, containment, or nesting relationships it may have.
|
||||
As a product may only have a single location in the "spatial
|
||||
decomposition" tree, assigning an nesting relationship will remove any
|
||||
previous aggregation, containment, or nesting relationships it may have.
|
||||
|
||||
IFC placements follow a convention where the placement is relative to
|
||||
its parent in the spatial hierarchy. If your product has a placement,
|
||||
its placement will be recalculated to follow this convention.
|
||||
IFC placements follow a convention where the placement is relative to
|
||||
its parent in the spatial hierarchy. If your product has a placement,
|
||||
its placement will be recalculated to follow this convention.
|
||||
|
||||
For physical connections which are part of a distribution system, such
|
||||
as a plug connecting into a GPO, or a duct connecting to an AHU, or two
|
||||
pipe segments connecting with a bend, tee, or wye fitting, you should
|
||||
not nest the two objects directly. Instead, you should nest a connection
|
||||
port, which determines the type of compatible distribution flow that can
|
||||
be connected to it. To do this, do not use this function, but instead
|
||||
use the more specific functions in the ifcopenshell.api.system module.
|
||||
For physical connections which are part of a distribution system, such
|
||||
as a plug connecting into a GPO, or a duct connecting to an AHU, or two
|
||||
pipe segments connecting with a bend, tee, or wye fitting, you should
|
||||
not nest the two objects directly. Instead, you should nest a connection
|
||||
port, which determines the type of compatible distribution flow that can
|
||||
be connected to it. To do this, do not use this function, but instead
|
||||
use the more specific functions in the ifcopenshell.api.system module.
|
||||
|
||||
Note that nesting relationships may also be used by non-physical
|
||||
elements, such as cost items or tasks. In this context, nesting means
|
||||
that there is an implied order to the child cost items or tasks (i.e.
|
||||
task 1 should be shown before task 2). It is not necessary to use this
|
||||
function for nesting non-physical elements. Instead, it is recommended
|
||||
to instead just use the relevant API functions, like
|
||||
ifcopenshell.api.cost.add_cost_item or
|
||||
ifcopenshell.api.sequence.add_task.
|
||||
Note that nesting relationships may also be used by non-physical
|
||||
elements, such as cost items or tasks. In this context, nesting means
|
||||
that there is an implied order to the child cost items or tasks (i.e.
|
||||
task 1 should be shown before task 2). It is not necessary to use this
|
||||
function for nesting non-physical elements. Instead, it is recommended
|
||||
to instead just use the relevant API functions, like
|
||||
ifcopenshell.api.cost.add_cost_item or
|
||||
ifcopenshell.api.sequence.add_task.
|
||||
|
||||
:param related_objects: The list of children of the nesting relationship,
|
||||
typically IfcElements.
|
||||
:type related_objects: list[ifcopenshell.entity_instance]
|
||||
:param relating_object: The host parent of the nesting relationship,
|
||||
typically an IfcElement.
|
||||
:type relating_object: ifcopenshell.entity_instance
|
||||
:return: The IfcRelNests relationship instance
|
||||
or `None` if `related_objects` was empty list.
|
||||
:rtype: Union[ifcopenshell.entity_instance, None]
|
||||
:param related_objects: The list of children of the nesting relationship,
|
||||
typically IfcElements.
|
||||
:type related_objects: list[ifcopenshell.entity_instance]
|
||||
:param relating_object: The host parent of the nesting relationship,
|
||||
typically an IfcElement.
|
||||
:type relating_object: ifcopenshell.entity_instance
|
||||
:return: The IfcRelNests relationship instance
|
||||
or `None` if `related_objects` was empty list.
|
||||
:rtype: Union[ifcopenshell.entity_instance, None]
|
||||
|
||||
Example:
|
||||
Example:
|
||||
|
||||
.. code:: python
|
||||
.. code:: python
|
||||
|
||||
# Faucets are designed to attach onto a sink through a predrilled hole.
|
||||
sink = ifcopenshell.api.run("root.create_entity", model,
|
||||
ifc_class="IfcSanitaryTerminal", predefined_type="SINK")
|
||||
faucet = ifcopenshell.api.run("root.create_entity", model,
|
||||
ifc_class="IfcValve", predefined_type="FAUCET")
|
||||
ifcopenshell.api.run("nest.assign_object", model, related_objects=[faucet], relating_object=sink)
|
||||
"""
|
||||
self.file = file
|
||||
self.settings = {"related_objects": related_objects, "relating_object": relating_object}
|
||||
# Faucets are designed to attach onto a sink through a predrilled hole.
|
||||
sink = ifcopenshell.api.run("root.create_entity", model,
|
||||
ifc_class="IfcSanitaryTerminal", predefined_type="SINK")
|
||||
faucet = ifcopenshell.api.run("root.create_entity", model,
|
||||
ifc_class="IfcValve", predefined_type="FAUCET")
|
||||
ifcopenshell.api.run("nest.assign_object", model, related_objects=[faucet], relating_object=sink)
|
||||
"""
|
||||
settings = {"related_objects": related_objects, "relating_object": relating_object}
|
||||
|
||||
def execute(self) -> Union[ifcopenshell.entity_instance, None]:
|
||||
if not self.settings["related_objects"]:
|
||||
return
|
||||
if not settings["related_objects"]:
|
||||
return
|
||||
|
||||
ifc2x3 = self.file.schema == "IFC2X3"
|
||||
ifc2x3 = file.schema == "IFC2X3"
|
||||
|
||||
related_objects = set(self.settings["related_objects"])
|
||||
relating_object = self.settings["relating_object"]
|
||||
related_objects = set(settings["related_objects"])
|
||||
relating_object = settings["relating_object"]
|
||||
if ifc2x3:
|
||||
is_nested_by = next((i for i in relating_object.IsDecomposedBy if i.is_a("IfcRelNests")), None)
|
||||
else:
|
||||
is_nested_by = next((i for i in relating_object.IsNestedBy), None)
|
||||
|
||||
previous_nests_rels: set[ifcopenshell.entity_instance] = set()
|
||||
objects_without_nests: list[ifcopenshell.entity_instance] = []
|
||||
objects_with_nests: list[ifcopenshell.entity_instance] = []
|
||||
|
||||
# check if there is anything to change
|
||||
for object in related_objects:
|
||||
if ifc2x3:
|
||||
is_nested_by = next((i for i in relating_object.IsDecomposedBy if i.is_a("IfcRelNests")), None)
|
||||
object_rel = next((i for i in object.Decomposes if i.is_a("IfcRelNests")), None)
|
||||
else:
|
||||
is_nested_by = next((i for i in relating_object.IsNestedBy), None)
|
||||
object_rel = next(iter(object.Nests), None)
|
||||
|
||||
previous_nests_rels: set[ifcopenshell.entity_instance] = set()
|
||||
objects_without_nests: list[ifcopenshell.entity_instance] = []
|
||||
objects_with_nests: list[ifcopenshell.entity_instance] = []
|
||||
if object_rel is None:
|
||||
objects_without_nests.append(object)
|
||||
continue
|
||||
|
||||
# check if there is anything to change
|
||||
for object in related_objects:
|
||||
if ifc2x3:
|
||||
object_rel = next((i for i in object.Decomposes if i.is_a("IfcRelNests")), None)
|
||||
else:
|
||||
object_rel = next(iter(object.Nests), None)
|
||||
# either is_nested_by is None or product is part of different rel
|
||||
if object_rel != is_nested_by:
|
||||
previous_nests_rels.add(object_rel)
|
||||
objects_with_nests.append(object)
|
||||
|
||||
if object_rel is None:
|
||||
objects_without_nests.append(object)
|
||||
continue
|
||||
|
||||
# either is_nested_by is None or product is part of different rel
|
||||
if object_rel != is_nested_by:
|
||||
previous_nests_rels.add(object_rel)
|
||||
objects_with_nests.append(object)
|
||||
|
||||
# products with already assigned nestings will be skipped
|
||||
|
||||
objects_to_change = objects_without_nests + objects_with_nests
|
||||
# nothing to change
|
||||
if not objects_to_change:
|
||||
return is_nested_by
|
||||
|
||||
# NOTE: An object can both be nested and assigned to a container or an aggregate.
|
||||
|
||||
# unassign elements from previous nests
|
||||
for nests in previous_nests_rels:
|
||||
cur_related_objects = set(nests.RelatedObjects) - related_objects
|
||||
if cur_related_objects:
|
||||
nests.RelatedObjects = list(cur_related_objects)
|
||||
ifcopenshell.api.run("owner.update_owner_history", self.file, **{"element": nests})
|
||||
else:
|
||||
history = nests.OwnerHistory
|
||||
self.file.remove(nests)
|
||||
if history:
|
||||
ifcopenshell.util.element.remove_deep2(self.file, history)
|
||||
|
||||
# assign elements to a new nesting
|
||||
if is_nested_by:
|
||||
is_nested_by.RelatedObjects = list(set(is_nested_by.RelatedObjects) | related_objects)
|
||||
ifcopenshell.api.run("owner.update_owner_history", self.file, **{"element": is_nested_by})
|
||||
else:
|
||||
is_nested_by = self.file.create_entity(
|
||||
"IfcRelNests",
|
||||
**{
|
||||
"GlobalId": ifcopenshell.guid.new(),
|
||||
"OwnerHistory": ifcopenshell.api.run("owner.create_owner_history", self.file),
|
||||
"RelatedObjects": list(related_objects),
|
||||
"RelatingObject": relating_object,
|
||||
}
|
||||
)
|
||||
|
||||
# NOTE: Creating a nesting relationship doesn't localize the object's placement,
|
||||
# unlike assigning it to an aggregate or a container.
|
||||
# products with already assigned nestings will be skipped
|
||||
|
||||
objects_to_change = objects_without_nests + objects_with_nests
|
||||
# nothing to change
|
||||
if not objects_to_change:
|
||||
return is_nested_by
|
||||
|
||||
# NOTE: An object can both be nested and assigned to a container or an aggregate.
|
||||
|
||||
# unassign elements from previous nests
|
||||
for nests in previous_nests_rels:
|
||||
cur_related_objects = set(nests.RelatedObjects) - related_objects
|
||||
if cur_related_objects:
|
||||
nests.RelatedObjects = list(cur_related_objects)
|
||||
ifcopenshell.api.run("owner.update_owner_history", file, **{"element": nests})
|
||||
else:
|
||||
history = nests.OwnerHistory
|
||||
file.remove(nests)
|
||||
if history:
|
||||
ifcopenshell.util.element.remove_deep2(file, history)
|
||||
|
||||
# assign elements to a new nesting
|
||||
if is_nested_by:
|
||||
is_nested_by.RelatedObjects = list(set(is_nested_by.RelatedObjects) | related_objects)
|
||||
ifcopenshell.api.run("owner.update_owner_history", file, **{"element": is_nested_by})
|
||||
else:
|
||||
is_nested_by = file.create_entity(
|
||||
"IfcRelNests",
|
||||
**{
|
||||
"GlobalId": ifcopenshell.guid.new(),
|
||||
"OwnerHistory": ifcopenshell.api.run("owner.create_owner_history", file),
|
||||
"RelatedObjects": list(related_objects),
|
||||
"RelatingObject": relating_object,
|
||||
}
|
||||
)
|
||||
|
||||
# NOTE: Creating a nesting relationship doesn't localize the object's placement,
|
||||
# unlike assigning it to an aggregate or a container.
|
||||
|
||||
return is_nested_by
|
||||
|
||||
@@ -21,29 +21,26 @@ import ifcopenshell.api
|
||||
import ifcopenshell.util.element
|
||||
|
||||
|
||||
class Usecase:
|
||||
def __init__(self, file, item=None, new_parent=None):
|
||||
"""Assigns a cost item to a new parent cost item"""
|
||||
self.file = file
|
||||
self.settings = {"item": item, "new_parent": new_parent}
|
||||
def change_nest(file, item=None, new_parent=None) -> None:
|
||||
"""Assigns a cost item to a new parent cost item"""
|
||||
settings = {"item": item, "new_parent": new_parent}
|
||||
|
||||
def execute(self):
|
||||
if not self.settings["item"].Nests:
|
||||
return
|
||||
nests = self.settings["item"].Nests[0]
|
||||
related_objects = list(nests.RelatedObjects)
|
||||
related_objects.remove(self.settings["item"])
|
||||
if related_objects:
|
||||
nests.RelatedObjects = related_objects
|
||||
ifcopenshell.api.run("owner.update_owner_history", self.file, **{"element": nests})
|
||||
else:
|
||||
history = nests.OwnerHistory
|
||||
self.file.remove(nests)
|
||||
if history:
|
||||
ifcopenshell.util.element.remove_deep2(self.file, history)
|
||||
ifcopenshell.api.run(
|
||||
"nest.assign_object",
|
||||
self.file,
|
||||
related_objects=[self.settings["item"]],
|
||||
relating_object=self.settings["new_parent"],
|
||||
)
|
||||
if not settings["item"].Nests:
|
||||
return
|
||||
nests = settings["item"].Nests[0]
|
||||
related_objects = list(nests.RelatedObjects)
|
||||
related_objects.remove(settings["item"])
|
||||
if related_objects:
|
||||
nests.RelatedObjects = related_objects
|
||||
ifcopenshell.api.run("owner.update_owner_history", file, **{"element": nests})
|
||||
else:
|
||||
history = nests.OwnerHistory
|
||||
file.remove(nests)
|
||||
if history:
|
||||
ifcopenshell.util.element.remove_deep2(file, history)
|
||||
ifcopenshell.api.run(
|
||||
"nest.assign_object",
|
||||
file,
|
||||
related_objects=[settings["item"]],
|
||||
relating_object=settings["new_parent"],
|
||||
)
|
||||
|
||||
@@ -17,20 +17,17 @@
|
||||
# along with IfcOpenShell. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
|
||||
class Usecase:
|
||||
def __init__(self, file, item=None, old_index=0, new_index=0):
|
||||
"""Reorders an item in a nesting set"""
|
||||
self.file = file
|
||||
self.settings = {"item": item, "old_index":old_index, "new_index": new_index}
|
||||
def reorder_nesting(file, item=None, old_index=0, new_index=0) -> None:
|
||||
"""Reorders an item in a nesting set"""
|
||||
settings = {"item": item, "old_index": old_index, "new_index": new_index}
|
||||
|
||||
def execute(self):
|
||||
if not self.settings["item"].Nests:
|
||||
return
|
||||
nesting_set = self.settings["item"].Nests[0]
|
||||
if not self.settings["old_index"]:
|
||||
old_index = nesting_set.RelatedObjects.index(self.settings["item"])
|
||||
else:
|
||||
old_index = self.settings["old_index"]
|
||||
items = list(getattr(nesting_set, "RelatedObjects") or [])
|
||||
items.insert(self.settings["new_index"], items.pop(old_index))
|
||||
setattr(nesting_set, "RelatedObjects", items)
|
||||
if not settings["item"].Nests:
|
||||
return
|
||||
nesting_set = settings["item"].Nests[0]
|
||||
if not settings["old_index"]:
|
||||
old_index = nesting_set.RelatedObjects.index(settings["item"])
|
||||
else:
|
||||
old_index = settings["old_index"]
|
||||
items = list(getattr(nesting_set, "RelatedObjects") or [])
|
||||
items.insert(settings["new_index"], items.pop(old_index))
|
||||
setattr(nesting_set, "RelatedObjects", items)
|
||||
|
||||
@@ -21,57 +21,54 @@ import ifcopenshell.api
|
||||
import ifcopenshell.util.element
|
||||
|
||||
|
||||
class Usecase:
|
||||
def __init__(self, file: ifcopenshell.file, related_objects: list[ifcopenshell.entity_instance]):
|
||||
"""Unassigns related_objects from their nests.
|
||||
def unassign_object(file: ifcopenshell.file, related_objects: list[ifcopenshell.entity_instance]) -> None:
|
||||
"""Unassigns related_objects from their nests.
|
||||
|
||||
An object (the whole within a decomposition) is Nested by zero or one more smaller objects.
|
||||
This function will remove this nesting relationship.
|
||||
An object (the whole within a decomposition) is Nested by zero or one more smaller objects.
|
||||
This function will remove this nesting relationship.
|
||||
|
||||
If the object is not part of a nesting relationship, nothing will happen.
|
||||
If the object is not part of a nesting relationship, nothing will happen.
|
||||
|
||||
:param related_objects: The list of children of the nesting relationship,
|
||||
typically IfcElements.
|
||||
:type related_objects: list[ifcopenshell.entity_instance]
|
||||
:return: None
|
||||
:rtype: None
|
||||
:param related_objects: The list of children of the nesting relationship,
|
||||
typically IfcElements.
|
||||
:type related_objects: list[ifcopenshell.entity_instance]
|
||||
:return: None
|
||||
:rtype: None
|
||||
|
||||
Example:
|
||||
Example:
|
||||
|
||||
.. code:: python
|
||||
.. code:: python
|
||||
|
||||
task = ifcopenshell.api.run("root.create_entity", model, ifc_class="IfcTasks")
|
||||
subtask1 = ifcopenshell.api.run("root.create_entity", model, ifc_class="IfcTask")
|
||||
subtask2 = ifcopenshell.api.run("root.create_entity", model, ifc_class="IfcTask")
|
||||
ifcopenshell.api.run("nest.assign_object", model, related_objects=[subtask1], relating_object=task)
|
||||
ifcopenshell.api.run("nest.assign_object", model, related_objects=[subtask2], relating_object=task)
|
||||
# nothing is returned
|
||||
rel = ifcopenshell.api.run("nest.unassign_object", model, related_objects=[subtask1])
|
||||
# nothing is returned, relationship is removed
|
||||
ifcopenshell.api.run("nest.unassign_object", model, related_objects=[subtask2])
|
||||
"""
|
||||
self.file = file
|
||||
self.settings = {"related_objects": related_objects}
|
||||
task = ifcopenshell.api.run("root.create_entity", model, ifc_class="IfcTasks")
|
||||
subtask1 = ifcopenshell.api.run("root.create_entity", model, ifc_class="IfcTask")
|
||||
subtask2 = ifcopenshell.api.run("root.create_entity", model, ifc_class="IfcTask")
|
||||
ifcopenshell.api.run("nest.assign_object", model, related_objects=[subtask1], relating_object=task)
|
||||
ifcopenshell.api.run("nest.assign_object", model, related_objects=[subtask2], relating_object=task)
|
||||
# nothing is returned
|
||||
rel = ifcopenshell.api.run("nest.unassign_object", model, related_objects=[subtask1])
|
||||
# nothing is returned, relationship is removed
|
||||
ifcopenshell.api.run("nest.unassign_object", model, related_objects=[subtask2])
|
||||
"""
|
||||
settings = {"related_objects": related_objects}
|
||||
|
||||
def execute(self) -> None:
|
||||
related_objects = set(self.settings["related_objects"])
|
||||
ifc2x3 = self.file.schema == "IFC2X3"
|
||||
if ifc2x3:
|
||||
rels = set(
|
||||
rel
|
||||
for object in related_objects
|
||||
if (rel := next((rel for rel in object.Decomposes if rel.is_a("IfcRelNests")), None))
|
||||
)
|
||||
related_objects = set(settings["related_objects"])
|
||||
ifc2x3 = file.schema == "IFC2X3"
|
||||
if ifc2x3:
|
||||
rels = set(
|
||||
rel
|
||||
for object in related_objects
|
||||
if (rel := next((rel for rel in object.Decomposes if rel.is_a("IfcRelNests")), None))
|
||||
)
|
||||
else:
|
||||
rels = set(rel for object in related_objects if (rel := next((rel for rel in object.Nests), None)))
|
||||
|
||||
for rel in rels:
|
||||
related_objects = set(rel.RelatedObjects) - related_objects
|
||||
if related_objects:
|
||||
rel.RelatedObjects = list(related_objects)
|
||||
ifcopenshell.api.run("owner.update_owner_history", file, **{"element": rel})
|
||||
else:
|
||||
rels = set(rel for object in related_objects if (rel := next((rel for rel in object.Nests), None)))
|
||||
|
||||
for rel in rels:
|
||||
related_objects = set(rel.RelatedObjects) - related_objects
|
||||
if related_objects:
|
||||
rel.RelatedObjects = list(related_objects)
|
||||
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)
|
||||
history = rel.OwnerHistory
|
||||
file.remove(rel)
|
||||
if history:
|
||||
ifcopenshell.util.element.remove_deep2(file, history)
|
||||
|
||||
Reference in New Issue
Block a user