mirror of
https://github.com/IfcOpenShell/IfcOpenShell.git
synced 2026-08-10 01:41:57 +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_container import assign_container
|
||||
from .dereference_structure import dereference_structure
|
||||
from .reference_structure import reference_structure
|
||||
from .unassign_container import unassign_container
|
||||
|
||||
@@ -23,163 +23,159 @@ import ifcopenshell.util.placement
|
||||
from typing import Union
|
||||
|
||||
|
||||
class Usecase:
|
||||
def __init__(
|
||||
self,
|
||||
file: ifcopenshell.file,
|
||||
products: list[ifcopenshell.entity_instance],
|
||||
relating_structure: ifcopenshell.entity_instance,
|
||||
):
|
||||
"""Assigns products to be contained hierarchically in a space
|
||||
def assign_container(
|
||||
file: ifcopenshell.file,
|
||||
products: list[ifcopenshell.entity_instance],
|
||||
relating_structure: ifcopenshell.entity_instance,
|
||||
) -> Union[ifcopenshell.entity_instance, None]:
|
||||
"""Assigns products to be contained hierarchically in a space
|
||||
|
||||
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. See
|
||||
ifcopenshell.api.aggregate.assign_object for more details about
|
||||
aggregation.
|
||||
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. See
|
||||
ifcopenshell.api.aggregate.assign_object for more details about
|
||||
aggregation.
|
||||
|
||||
The IfcProject will be "decomposed" into spatial structure elements.
|
||||
These are virtual spaces like stes, buildings, storeys, and spaces (i.e.
|
||||
rooms). You can't physically touch these spaces, but you can touch the
|
||||
products contained within these spaces.
|
||||
The IfcProject will be "decomposed" into spatial structure elements.
|
||||
These are virtual spaces like stes, buildings, storeys, and spaces (i.e.
|
||||
rooms). You can't physically touch these spaces, but you can touch the
|
||||
products contained within these spaces.
|
||||
|
||||
To state that a product is contained in a space, you will use a
|
||||
"containment" relationship. Containment is a very common relationship
|
||||
used to create the hierarchical spatial decomposition tree. For example,
|
||||
you might say that "This wall is on the third building storey", or "this
|
||||
table is in the living room space".
|
||||
To state that a product is contained in a space, you will use a
|
||||
"containment" relationship. Containment is a very common relationship
|
||||
used to create the hierarchical spatial decomposition tree. For example,
|
||||
you might say that "This wall is on the third building storey", or "this
|
||||
table is in the living room space".
|
||||
|
||||
The distinguishing factor between aggregation and containment is that
|
||||
aggregation occurs between objects of the same type (e.g. a large space
|
||||
is made up of smaller spaces), whereas containment is between two
|
||||
different types: explicitly saying that a physical product exists within
|
||||
a virtual space.
|
||||
The distinguishing factor between aggregation and containment is that
|
||||
aggregation occurs between objects of the same type (e.g. a large space
|
||||
is made up of smaller spaces), whereas containment is between two
|
||||
different types: explicitly saying that a physical product exists within
|
||||
a virtual space.
|
||||
|
||||
Containment is critical in construction management, to know which
|
||||
objects are in which spaces, as often you would divide your construction
|
||||
schedule into storey by storey, or zone by zone. Containment is also
|
||||
critical in facility management, as it indicates through which space
|
||||
equipment may be accessed for maintenance purposes.
|
||||
Containment is critical in construction management, to know which
|
||||
objects are in which spaces, as often you would divide your construction
|
||||
schedule into storey by storey, or zone by zone. Containment is also
|
||||
critical in facility management, as it indicates through which space
|
||||
equipment may be accessed for maintenance purposes.
|
||||
|
||||
As a product may only have a single location in the "spatial
|
||||
decomposition" tree, assigning an aggregate 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 aggregate relationship will remove any
|
||||
previous aggregation, containment, or nesting relationships it may have.
|
||||
|
||||
:param products: A list of physical IfcElements existing in the space.
|
||||
:type products: list[ifcopenshell.entity_instance]
|
||||
:param relating_structure: The IfcSpatialStructureElement element, such
|
||||
as IfcBuilding, IfcBuildingStorey, or IfcSpace that the element
|
||||
exists in.
|
||||
:return: The IfcRelContainedInSpatialStructure relationship instance
|
||||
or `None` if `products` was empty list.
|
||||
:rtype: Union[ifcopenshell.entity_instance, None]
|
||||
:param products: A list of physical IfcElements existing in the space.
|
||||
:type products: list[ifcopenshell.entity_instance]
|
||||
:param relating_structure: The IfcSpatialStructureElement element, such
|
||||
as IfcBuilding, IfcBuildingStorey, or IfcSpace that the element
|
||||
exists in.
|
||||
:return: The IfcRelContainedInSpatialStructure relationship instance
|
||||
or `None` if `products` was empty list.
|
||||
:rtype: Union[ifcopenshell.entity_instance, None]
|
||||
|
||||
Example:
|
||||
Example:
|
||||
|
||||
.. code:: python
|
||||
.. code:: python
|
||||
|
||||
project = ifcopenshell.api.run("root.create_entity", model, ifc_class="IfcProject")
|
||||
site = ifcopenshell.api.run("root.create_entity", model, ifc_class="IfcSite")
|
||||
building = ifcopenshell.api.run("root.create_entity", model, ifc_class="IfcBuilding")
|
||||
storey = ifcopenshell.api.run("root.create_entity", model, ifc_class="IfcBuildingStorey")
|
||||
space = ifcopenshell.api.run("root.create_entity", model, ifc_class="IfcSpace")
|
||||
project = ifcopenshell.api.run("root.create_entity", model, ifc_class="IfcProject")
|
||||
site = ifcopenshell.api.run("root.create_entity", model, ifc_class="IfcSite")
|
||||
building = ifcopenshell.api.run("root.create_entity", model, ifc_class="IfcBuilding")
|
||||
storey = ifcopenshell.api.run("root.create_entity", model, ifc_class="IfcBuildingStorey")
|
||||
space = ifcopenshell.api.run("root.create_entity", model, ifc_class="IfcSpace")
|
||||
|
||||
# The project contains a site (note that project aggregation is a special case in IFC)
|
||||
ifcopenshell.api.run("aggregate.assign_object", model, products=[site], relating_object=project)
|
||||
# The project contains a site (note that project aggregation is a special case in IFC)
|
||||
ifcopenshell.api.run("aggregate.assign_object", model, products=[site], relating_object=project)
|
||||
|
||||
# The site has a building, the building has a storey, and the storey has a space
|
||||
ifcopenshell.api.run("aggregate.assign_object", model, products=[building], relating_object=site)
|
||||
ifcopenshell.api.run("aggregate.assign_object", model, products=[storey], relating_object=building)
|
||||
ifcopenshell.api.run("aggregate.assign_object", model, products=[space], relating_object=storey)
|
||||
# The site has a building, the building has a storey, and the storey has a space
|
||||
ifcopenshell.api.run("aggregate.assign_object", model, products=[building], relating_object=site)
|
||||
ifcopenshell.api.run("aggregate.assign_object", model, products=[storey], relating_object=building)
|
||||
ifcopenshell.api.run("aggregate.assign_object", model, products=[space], relating_object=storey)
|
||||
|
||||
# Create a wall and furniture
|
||||
wall = ifcopenshell.api.run("root.create_entity", model, ifc_class="IfcWall")
|
||||
furniture = ifcopenshell.api.run("root.create_entity", model, ifc_class="IfcFurniture")
|
||||
# Create a wall and furniture
|
||||
wall = ifcopenshell.api.run("root.create_entity", model, ifc_class="IfcWall")
|
||||
furniture = ifcopenshell.api.run("root.create_entity", model, ifc_class="IfcFurniture")
|
||||
|
||||
# The wall is in the storey, and the furniture is in the space
|
||||
ifcopenshell.api.run("spatial.assign_container", model, products=[wall], relating_structure=storey)
|
||||
ifcopenshell.api.run("spatial.assign_container", model, products=[furniture], relating_structure=space)
|
||||
"""
|
||||
self.file = file
|
||||
self.settings = {
|
||||
"products": products,
|
||||
"relating_structure": relating_structure,
|
||||
}
|
||||
# The wall is in the storey, and the furniture is in the space
|
||||
ifcopenshell.api.run("spatial.assign_container", model, products=[wall], relating_structure=storey)
|
||||
ifcopenshell.api.run("spatial.assign_container", model, products=[furniture], relating_structure=space)
|
||||
"""
|
||||
settings = {
|
||||
"products": products,
|
||||
"relating_structure": relating_structure,
|
||||
}
|
||||
|
||||
def execute(self) -> Union[ifcopenshell.entity_instance, None]:
|
||||
if not self.settings["products"]:
|
||||
return
|
||||
if not settings["products"]:
|
||||
return
|
||||
|
||||
products = set(self.settings["products"])
|
||||
relating_structure = self.settings["relating_structure"]
|
||||
structure_rel = next(iter(relating_structure.ContainsElements), None)
|
||||
products = set(settings["products"])
|
||||
relating_structure = settings["relating_structure"]
|
||||
structure_rel = next(iter(relating_structure.ContainsElements), None)
|
||||
|
||||
previous_containers_rels: set[ifcopenshell.entity_instance] = set()
|
||||
products_without_containers: list[ifcopenshell.entity_instance] = []
|
||||
products_with_containers: list[ifcopenshell.entity_instance] = []
|
||||
previous_containers_rels: set[ifcopenshell.entity_instance] = set()
|
||||
products_without_containers: list[ifcopenshell.entity_instance] = []
|
||||
products_with_containers: list[ifcopenshell.entity_instance] = []
|
||||
|
||||
# check if there is anything to change
|
||||
for product in products:
|
||||
product_rel = next(iter(product.ContainedInStructure), None)
|
||||
# check if there is anything to change
|
||||
for product in products:
|
||||
product_rel = next(iter(product.ContainedInStructure), None)
|
||||
|
||||
if product_rel is None:
|
||||
products_without_containers.append(product)
|
||||
continue
|
||||
if product_rel is None:
|
||||
products_without_containers.append(product)
|
||||
continue
|
||||
|
||||
# either structure_rel is None or product is part of different rel
|
||||
if product_rel != structure_rel:
|
||||
previous_containers_rels.add(product_rel)
|
||||
products_with_containers.append(product)
|
||||
# either structure_rel is None or product is part of different rel
|
||||
if product_rel != structure_rel:
|
||||
previous_containers_rels.add(product_rel)
|
||||
products_with_containers.append(product)
|
||||
|
||||
# products with already assigned containers will be skipped
|
||||
# products with already assigned containers will be skipped
|
||||
|
||||
products_to_change = products_without_containers + products_with_containers
|
||||
# nothing to change
|
||||
if not products_to_change:
|
||||
return structure_rel
|
||||
products_to_change = products_without_containers + products_with_containers
|
||||
# nothing to change
|
||||
if not products_to_change:
|
||||
return structure_rel
|
||||
|
||||
# can be either only aggregated or only contained at the same time
|
||||
ifcopenshell.api.run("aggregate.unassign_object", self.file, products=products_without_containers)
|
||||
# can be either only aggregated or only contained at the same time
|
||||
ifcopenshell.api.run("aggregate.unassign_object", file, products=products_without_containers)
|
||||
|
||||
# unassign elements from previous containers
|
||||
for rel in previous_containers_rels:
|
||||
related_elements = set(rel.RelatedElements) - products
|
||||
if related_elements:
|
||||
rel.RelatedElements = list(related_elements)
|
||||
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)
|
||||
|
||||
# assign elements to a new container
|
||||
if structure_rel:
|
||||
structure_rel.RelatedElements = list(set(structure_rel.RelatedElements) | products)
|
||||
ifcopenshell.api.run("owner.update_owner_history", self.file, **{"element": structure_rel})
|
||||
# unassign elements from previous containers
|
||||
for rel in previous_containers_rels:
|
||||
related_elements = set(rel.RelatedElements) - products
|
||||
if related_elements:
|
||||
rel.RelatedElements = list(related_elements)
|
||||
ifcopenshell.api.run("owner.update_owner_history", file, **{"element": rel})
|
||||
else:
|
||||
structure_rel = self.file.create_entity(
|
||||
"IfcRelContainedInSpatialStructure",
|
||||
**{
|
||||
"GlobalId": ifcopenshell.guid.new(),
|
||||
"OwnerHistory": ifcopenshell.api.run("owner.create_owner_history", self.file),
|
||||
"RelatedElements": list(products),
|
||||
"RelatingStructure": self.settings["relating_structure"],
|
||||
}
|
||||
history = rel.OwnerHistory
|
||||
file.remove(rel)
|
||||
if history:
|
||||
ifcopenshell.util.element.remove_deep2(file, history)
|
||||
|
||||
# assign elements to a new container
|
||||
if structure_rel:
|
||||
structure_rel.RelatedElements = list(set(structure_rel.RelatedElements) | products)
|
||||
ifcopenshell.api.run("owner.update_owner_history", file, **{"element": structure_rel})
|
||||
else:
|
||||
structure_rel = file.create_entity(
|
||||
"IfcRelContainedInSpatialStructure",
|
||||
**{
|
||||
"GlobalId": ifcopenshell.guid.new(),
|
||||
"OwnerHistory": ifcopenshell.api.run("owner.create_owner_history", file),
|
||||
"RelatedElements": list(products),
|
||||
"RelatingStructure": settings["relating_structure"],
|
||||
}
|
||||
)
|
||||
|
||||
# localize placement relative to a new container for affected products
|
||||
for product in products_to_change:
|
||||
placement = getattr(product, "ObjectPlacement", None)
|
||||
if placement and placement.is_a("IfcLocalPlacement"):
|
||||
ifcopenshell.api.run(
|
||||
"geometry.edit_object_placement",
|
||||
file,
|
||||
product=product,
|
||||
matrix=ifcopenshell.util.placement.get_local_placement(product.ObjectPlacement),
|
||||
is_si=False,
|
||||
)
|
||||
|
||||
# localize placement relative to a new container for affected products
|
||||
for product in products_to_change:
|
||||
placement = getattr(product, "ObjectPlacement", None)
|
||||
if placement and placement.is_a("IfcLocalPlacement"):
|
||||
ifcopenshell.api.run(
|
||||
"geometry.edit_object_placement",
|
||||
self.file,
|
||||
product=product,
|
||||
matrix=ifcopenshell.util.placement.get_local_placement(product.ObjectPlacement),
|
||||
is_si=False,
|
||||
)
|
||||
|
||||
return structure_rel
|
||||
return structure_rel
|
||||
|
||||
@@ -21,70 +21,66 @@ import ifcopenshell.api
|
||||
import ifcopenshell.util.element
|
||||
|
||||
|
||||
class Usecase:
|
||||
def __init__(
|
||||
self,
|
||||
file: ifcopenshell.file,
|
||||
products: list[ifcopenshell.entity_instance],
|
||||
relating_structure: ifcopenshell.entity_instance,
|
||||
):
|
||||
"""Dereferences a list of products and space
|
||||
def dereference_structure(
|
||||
file: ifcopenshell.file,
|
||||
products: list[ifcopenshell.entity_instance],
|
||||
relating_structure: ifcopenshell.entity_instance,
|
||||
) -> None:
|
||||
"""Dereferences a list of products and space
|
||||
|
||||
:param products: The list of physical IfcElements that exists in the space.
|
||||
:type products: list[ifcopenshell.entity_instance]
|
||||
:param relating_structure: The IfcSpatialStructureElement element, such
|
||||
as IfcBuilding, IfcBuildingStorey, or IfcSpace that the element
|
||||
exists in.
|
||||
:return: None
|
||||
:rtype: None
|
||||
:param products: The list of physical IfcElements that exists in the space.
|
||||
:type products: list[ifcopenshell.entity_instance]
|
||||
:param relating_structure: The IfcSpatialStructureElement element, such
|
||||
as IfcBuilding, IfcBuildingStorey, or IfcSpace that the element
|
||||
exists in.
|
||||
:return: None
|
||||
:rtype: None
|
||||
|
||||
Example:
|
||||
Example:
|
||||
|
||||
.. code:: python
|
||||
.. code:: python
|
||||
|
||||
project = ifcopenshell.api.run("root.create_entity", model, ifc_class="IfcProject")
|
||||
site = ifcopenshell.api.run("root.create_entity", model, ifc_class="IfcSite")
|
||||
building = ifcopenshell.api.run("root.create_entity", model, ifc_class="IfcBuilding")
|
||||
storey1 = ifcopenshell.api.run("root.create_entity", model, ifc_class="IfcBuildingStorey")
|
||||
storey2 = ifcopenshell.api.run("root.create_entity", model, ifc_class="IfcBuildingStorey")
|
||||
storey3 = ifcopenshell.api.run("root.create_entity", model, ifc_class="IfcBuildingStorey")
|
||||
project = ifcopenshell.api.run("root.create_entity", model, ifc_class="IfcProject")
|
||||
site = ifcopenshell.api.run("root.create_entity", model, ifc_class="IfcSite")
|
||||
building = ifcopenshell.api.run("root.create_entity", model, ifc_class="IfcBuilding")
|
||||
storey1 = ifcopenshell.api.run("root.create_entity", model, ifc_class="IfcBuildingStorey")
|
||||
storey2 = ifcopenshell.api.run("root.create_entity", model, ifc_class="IfcBuildingStorey")
|
||||
storey3 = ifcopenshell.api.run("root.create_entity", model, ifc_class="IfcBuildingStorey")
|
||||
|
||||
# The project contains a site (note that project aggregation is a special case in IFC)
|
||||
ifcopenshell.api.run("aggregate.assign_object", model, products=[site], relating_object=project)
|
||||
# The project contains a site (note that project aggregation is a special case in IFC)
|
||||
ifcopenshell.api.run("aggregate.assign_object", model, products=[site], relating_object=project)
|
||||
|
||||
# The site has a building, the building has a storey, and the storey has a space
|
||||
ifcopenshell.api.run("aggregate.assign_object", model, products=[building], relating_object=site)
|
||||
ifcopenshell.api.run("aggregate.assign_object", model, products=[storey], relating_object=building)
|
||||
ifcopenshell.api.run("aggregate.assign_object", model, products=[space], relating_object=storey)
|
||||
# The site has a building, the building has a storey, and the storey has a space
|
||||
ifcopenshell.api.run("aggregate.assign_object", model, products=[building], relating_object=site)
|
||||
ifcopenshell.api.run("aggregate.assign_object", model, products=[storey], relating_object=building)
|
||||
ifcopenshell.api.run("aggregate.assign_object", model, products=[space], relating_object=storey)
|
||||
|
||||
# Create a column, this column spans 3 storeys
|
||||
column = ifcopenshell.api.run("root.create_entity", model, ifc_class="IfcWall")
|
||||
# Create a column, this column spans 3 storeys
|
||||
column = ifcopenshell.api.run("root.create_entity", model, ifc_class="IfcWall")
|
||||
|
||||
# The column is contained in the lowermost storey
|
||||
ifcopenshell.api.run("spatial.assign_container", model, products=[column], relating_structure=storey1)
|
||||
# The column is contained in the lowermost storey
|
||||
ifcopenshell.api.run("spatial.assign_container", model, products=[column], relating_structure=storey1)
|
||||
|
||||
# And referenced in the others
|
||||
ifcopenshell.api.run("spatial.reference_structure", model, products=[column], relating_structure=storey2)
|
||||
ifcopenshell.api.run("spatial.reference_structure", model, products=[column], relating_structure=storey3)
|
||||
# And referenced in the others
|
||||
ifcopenshell.api.run("spatial.reference_structure", model, products=[column], relating_structure=storey2)
|
||||
ifcopenshell.api.run("spatial.reference_structure", model, products=[column], relating_structure=storey3)
|
||||
|
||||
# Actually, it only goes up to storey 2.
|
||||
ifcopenshell.api.run("spatial.dereference_structure", model, products=[column], relating_structure=storey3)
|
||||
"""
|
||||
self.file = file
|
||||
self.settings = {"products": products, "relating_structure": relating_structure}
|
||||
# Actually, it only goes up to storey 2.
|
||||
ifcopenshell.api.run("spatial.dereference_structure", model, products=[column], relating_structure=storey3)
|
||||
"""
|
||||
settings = {"products": products, "relating_structure": relating_structure}
|
||||
|
||||
def execute(self) -> None:
|
||||
products = set(self.settings["products"])
|
||||
for rel in self.settings["relating_structure"].ReferencesElements:
|
||||
related_elements = set(rel.RelatedElements)
|
||||
if not related_elements.intersection(products):
|
||||
continue
|
||||
related_elements = related_elements - products
|
||||
if related_elements:
|
||||
rel.RelatedElements = list(related_elements)
|
||||
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)
|
||||
products = set(settings["products"])
|
||||
for rel in settings["relating_structure"].ReferencesElements:
|
||||
related_elements = set(rel.RelatedElements)
|
||||
if not related_elements.intersection(products):
|
||||
continue
|
||||
related_elements = related_elements - products
|
||||
if related_elements:
|
||||
rel.RelatedElements = list(related_elements)
|
||||
ifcopenshell.api.run("owner.update_owner_history", file, **{"element": rel})
|
||||
else:
|
||||
history = rel.OwnerHistory
|
||||
file.remove(rel)
|
||||
if history:
|
||||
ifcopenshell.util.element.remove_deep2(file, history)
|
||||
|
||||
@@ -22,103 +22,99 @@ import ifcopenshell.util.element
|
||||
from typing import Union
|
||||
|
||||
|
||||
class Usecase:
|
||||
def __init__(
|
||||
self,
|
||||
file: ifcopenshell.file,
|
||||
products: list[ifcopenshell.entity_instance],
|
||||
relating_structure: ifcopenshell.entity_instance,
|
||||
):
|
||||
"""Denote that a list products is related to a list of spatial structures
|
||||
def reference_structure(
|
||||
file: ifcopenshell.file,
|
||||
products: list[ifcopenshell.entity_instance],
|
||||
relating_structure: ifcopenshell.entity_instance,
|
||||
) -> Union[ifcopenshell.entity_instance, None]:
|
||||
"""Denote that a list products is related to a list of spatial structures
|
||||
|
||||
This is similar to ifcopenshell.api.spatial.assign_container, except
|
||||
that containment can only occur between a product and a single spatial
|
||||
structure element. This is fine if a wall is on level 1, but not
|
||||
appropriate if you have a multistorey column on multiple levels, or a
|
||||
door with a to and from space, or a stair going from one floor to
|
||||
another floor. This is where spatial referencing is used.
|
||||
This is similar to ifcopenshell.api.spatial.assign_container, except
|
||||
that containment can only occur between a product and a single spatial
|
||||
structure element. This is fine if a wall is on level 1, but not
|
||||
appropriate if you have a multistorey column on multiple levels, or a
|
||||
door with a to and from space, or a stair going from one floor to
|
||||
another floor. This is where spatial referencing is used.
|
||||
|
||||
Typically, the product will be contained in the lowermost, constructed
|
||||
first, or primarily accessible space. For a multistorey column or stair,
|
||||
the column or stair will therefore be contained in the lowermost storey.
|
||||
Then, any other storeys will be referenced.
|
||||
Typically, the product will be contained in the lowermost, constructed
|
||||
first, or primarily accessible space. For a multistorey column or stair,
|
||||
the column or stair will therefore be contained in the lowermost storey.
|
||||
Then, any other storeys will be referenced.
|
||||
|
||||
Referencing is non-hierarchical, so a door may be referenced in multiple
|
||||
spaces simultaneously.
|
||||
Referencing is non-hierarchical, so a door may be referenced in multiple
|
||||
spaces simultaneously.
|
||||
|
||||
:param products: The list of physical IfcElements that exists in the space.
|
||||
:type products: list[ifcopenshell.entity_instance]
|
||||
:param relating_structure: The IfcSpatialStructureElement element, such
|
||||
as IfcBuilding, IfcBuildingStorey, or IfcSpace that the element
|
||||
exists in.
|
||||
:type relating_structure: ifcopenshell.entity_instance
|
||||
:return: The IfcRelReferencedInSpatialStructure relationship instance
|
||||
or `None` if `products` was an empty list.
|
||||
:rtype: Union[ifcopenshell.entity_instance, None]
|
||||
:param products: The list of physical IfcElements that exists in the space.
|
||||
:type products: list[ifcopenshell.entity_instance]
|
||||
:param relating_structure: The IfcSpatialStructureElement element, such
|
||||
as IfcBuilding, IfcBuildingStorey, or IfcSpace that the element
|
||||
exists in.
|
||||
:type relating_structure: ifcopenshell.entity_instance
|
||||
:return: The IfcRelReferencedInSpatialStructure relationship instance
|
||||
or `None` if `products` was an empty list.
|
||||
:rtype: Union[ifcopenshell.entity_instance, None]
|
||||
|
||||
Example:
|
||||
Example:
|
||||
|
||||
.. code:: python
|
||||
.. code:: python
|
||||
|
||||
project = ifcopenshell.api.run("root.create_entity", model, ifc_class="IfcProject")
|
||||
site = ifcopenshell.api.run("root.create_entity", model, ifc_class="IfcSite")
|
||||
building = ifcopenshell.api.run("root.create_entity", model, ifc_class="IfcBuilding")
|
||||
storey1 = ifcopenshell.api.run("root.create_entity", model, ifc_class="IfcBuildingStorey")
|
||||
storey2 = ifcopenshell.api.run("root.create_entity", model, ifc_class="IfcBuildingStorey")
|
||||
storey3 = ifcopenshell.api.run("root.create_entity", model, ifc_class="IfcBuildingStorey")
|
||||
project = ifcopenshell.api.run("root.create_entity", model, ifc_class="IfcProject")
|
||||
site = ifcopenshell.api.run("root.create_entity", model, ifc_class="IfcSite")
|
||||
building = ifcopenshell.api.run("root.create_entity", model, ifc_class="IfcBuilding")
|
||||
storey1 = ifcopenshell.api.run("root.create_entity", model, ifc_class="IfcBuildingStorey")
|
||||
storey2 = ifcopenshell.api.run("root.create_entity", model, ifc_class="IfcBuildingStorey")
|
||||
storey3 = ifcopenshell.api.run("root.create_entity", model, ifc_class="IfcBuildingStorey")
|
||||
|
||||
# The project contains a site (note that project aggregation is a special case in IFC)
|
||||
ifcopenshell.api.run("aggregate.assign_object", model, products=[site], relating_object=project)
|
||||
# The project contains a site (note that project aggregation is a special case in IFC)
|
||||
ifcopenshell.api.run("aggregate.assign_object", model, products=[site], relating_object=project)
|
||||
|
||||
# The site has a building, the building has a storey, and the storey has a space
|
||||
ifcopenshell.api.run("aggregate.assign_object", model, products=[building], relating_object=site)
|
||||
ifcopenshell.api.run("aggregate.assign_object", model, products=[storey], relating_object=building)
|
||||
ifcopenshell.api.run("aggregate.assign_object", model, products=[space], relating_object=storey)
|
||||
# The site has a building, the building has a storey, and the storey has a space
|
||||
ifcopenshell.api.run("aggregate.assign_object", model, products=[building], relating_object=site)
|
||||
ifcopenshell.api.run("aggregate.assign_object", model, products=[storey], relating_object=building)
|
||||
ifcopenshell.api.run("aggregate.assign_object", model, products=[space], relating_object=storey)
|
||||
|
||||
# Create a column, this column spans 3 storeys
|
||||
column = ifcopenshell.api.run("root.create_entity", model, ifc_class="IfcWall")
|
||||
# Create a column, this column spans 3 storeys
|
||||
column = ifcopenshell.api.run("root.create_entity", model, ifc_class="IfcWall")
|
||||
|
||||
# The column is contained in the lowermost storey
|
||||
ifcopenshell.api.run("spatial.assign_container", model, products=[column], relating_structure=storey1)
|
||||
# The column is contained in the lowermost storey
|
||||
ifcopenshell.api.run("spatial.assign_container", model, products=[column], relating_structure=storey1)
|
||||
|
||||
# And referenced in the others
|
||||
ifcopenshell.api.run(
|
||||
"spatial.reference_structure", model, products=[column], relating_structure=[storey2, storey3]
|
||||
)
|
||||
"""
|
||||
self.file = file
|
||||
self.settings = {
|
||||
"products": products,
|
||||
"relating_structure": relating_structure,
|
||||
}
|
||||
# And referenced in the others
|
||||
ifcopenshell.api.run(
|
||||
"spatial.reference_structure", model, products=[column], relating_structure=[storey2, storey3]
|
||||
)
|
||||
"""
|
||||
settings = {
|
||||
"products": products,
|
||||
"relating_structure": relating_structure,
|
||||
}
|
||||
|
||||
def execute(self) -> Union[ifcopenshell.entity_instance, None]:
|
||||
structure = self.settings["relating_structure"]
|
||||
products = set(self.settings["products"])
|
||||
structure = settings["relating_structure"]
|
||||
products = set(settings["products"])
|
||||
|
||||
if not products:
|
||||
return
|
||||
if not products:
|
||||
return
|
||||
|
||||
referenced = ifcopenshell.util.element.get_structure_referenced_elements(structure)
|
||||
products_to_assign = products - referenced
|
||||
rel = next(iter(structure.ReferencesElements), None)
|
||||
|
||||
if not products_to_assign:
|
||||
return rel
|
||||
|
||||
if rel is None:
|
||||
rel = self.file.create_entity(
|
||||
"IfcRelReferencedInSpatialStructure",
|
||||
**{
|
||||
"GlobalId": ifcopenshell.guid.new(),
|
||||
"OwnerHistory": ifcopenshell.api.run("owner.create_owner_history", self.file),
|
||||
"RelatedElements": list(products_to_assign),
|
||||
"RelatingStructure": structure,
|
||||
}
|
||||
)
|
||||
else:
|
||||
related_elements = set(rel.RelatedElements) | products_to_assign
|
||||
rel.RelatedElements = list(related_elements)
|
||||
ifcopenshell.api.run("owner.update_owner_history", self.file, **{"element": rel})
|
||||
referenced = ifcopenshell.util.element.get_structure_referenced_elements(structure)
|
||||
products_to_assign = products - referenced
|
||||
rel = next(iter(structure.ReferencesElements), None)
|
||||
|
||||
if not products_to_assign:
|
||||
return rel
|
||||
|
||||
if rel is None:
|
||||
rel = file.create_entity(
|
||||
"IfcRelReferencedInSpatialStructure",
|
||||
**{
|
||||
"GlobalId": ifcopenshell.guid.new(),
|
||||
"OwnerHistory": ifcopenshell.api.run("owner.create_owner_history", file),
|
||||
"RelatedElements": list(products_to_assign),
|
||||
"RelatingStructure": structure,
|
||||
}
|
||||
)
|
||||
else:
|
||||
related_elements = set(rel.RelatedElements) | products_to_assign
|
||||
rel.RelatedElements = list(related_elements)
|
||||
ifcopenshell.api.run("owner.update_owner_history", file, **{"element": rel})
|
||||
|
||||
return rel
|
||||
|
||||
@@ -21,56 +21,53 @@ import ifcopenshell.api
|
||||
import ifcopenshell.util.element
|
||||
|
||||
|
||||
class Usecase:
|
||||
def __init__(self, file: ifcopenshell.file, products: list[ifcopenshell.entity_instance]):
|
||||
"""Unassigns a container from products.
|
||||
def unassign_container(file: ifcopenshell.file, products: list[ifcopenshell.entity_instance]) -> None:
|
||||
"""Unassigns a container from products.
|
||||
|
||||
:param product: A list of IfcProducts to remove the containment from.
|
||||
:type product: list[ifcopenshell.entity_instance]
|
||||
:return: None
|
||||
:rtype: None
|
||||
:param product: A list of IfcProducts to remove the containment from.
|
||||
:type product: list[ifcopenshell.entity_instance]
|
||||
:return: None
|
||||
:rtype: None
|
||||
|
||||
Example:
|
||||
Example:
|
||||
|
||||
.. code:: python
|
||||
.. code:: python
|
||||
|
||||
project = ifcopenshell.api.run("root.create_entity", model, ifc_class="IfcProject")
|
||||
site = ifcopenshell.api.run("root.create_entity", model, ifc_class="IfcSite")
|
||||
building = ifcopenshell.api.run("root.create_entity", model, ifc_class="IfcBuilding")
|
||||
storey = ifcopenshell.api.run("root.create_entity", model, ifc_class="IfcBuildingStorey")
|
||||
project = ifcopenshell.api.run("root.create_entity", model, ifc_class="IfcProject")
|
||||
site = ifcopenshell.api.run("root.create_entity", model, ifc_class="IfcSite")
|
||||
building = ifcopenshell.api.run("root.create_entity", model, ifc_class="IfcBuilding")
|
||||
storey = ifcopenshell.api.run("root.create_entity", model, ifc_class="IfcBuildingStorey")
|
||||
|
||||
# The project contains a site (note that project aggregation is a special case in IFC)
|
||||
ifcopenshell.api.run("aggregate.assign_object", model, products=[site], relating_object=project)
|
||||
# The project contains a site (note that project aggregation is a special case in IFC)
|
||||
ifcopenshell.api.run("aggregate.assign_object", model, products=[site], relating_object=project)
|
||||
|
||||
# The site has a building, the building has a storey, and the storey has a space
|
||||
ifcopenshell.api.run("aggregate.assign_object", model, products=[building], relating_object=site)
|
||||
ifcopenshell.api.run("aggregate.assign_object", model, products=[storey], relating_object=building)
|
||||
# The site has a building, the building has a storey, and the storey has a space
|
||||
ifcopenshell.api.run("aggregate.assign_object", model, products=[building], relating_object=site)
|
||||
ifcopenshell.api.run("aggregate.assign_object", model, products=[storey], relating_object=building)
|
||||
|
||||
# Create a wall
|
||||
wall = ifcopenshell.api.run("root.create_entity", model, ifc_class="IfcWall")
|
||||
# Create a wall
|
||||
wall = ifcopenshell.api.run("root.create_entity", model, ifc_class="IfcWall")
|
||||
|
||||
# The wall is in the storey
|
||||
ifcopenshell.api.run("spatial.assign_container", model, products=[wall], relating_structure=storey)
|
||||
# The wall is in the storey
|
||||
ifcopenshell.api.run("spatial.assign_container", model, products=[wall], relating_structure=storey)
|
||||
|
||||
# Not anymore!
|
||||
ifcopenshell.api.run("spatial.unassign_container", model, products=[wall])
|
||||
"""
|
||||
self.file = file
|
||||
self.settings = {
|
||||
"products": products,
|
||||
}
|
||||
# Not anymore!
|
||||
ifcopenshell.api.run("spatial.unassign_container", model, products=[wall])
|
||||
"""
|
||||
settings = {
|
||||
"products": products,
|
||||
}
|
||||
|
||||
def execute(self) -> None:
|
||||
products = set(self.settings["products"])
|
||||
rels = set(rel for product in products if (rel := next(iter(product.ContainedInStructure), None)))
|
||||
products = set(settings["products"])
|
||||
rels = set(rel for product in products if (rel := next(iter(product.ContainedInStructure), None)))
|
||||
|
||||
for rel in rels:
|
||||
related_elements = set(rel.RelatedElements) - products
|
||||
if related_elements:
|
||||
rel.RelatedElements = list(related_elements)
|
||||
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)
|
||||
for rel in rels:
|
||||
related_elements = set(rel.RelatedElements) - products
|
||||
if related_elements:
|
||||
rel.RelatedElements = list(related_elements)
|
||||
ifcopenshell.api.run("owner.update_owner_history", file, element=rel)
|
||||
else:
|
||||
history = rel.OwnerHistory
|
||||
file.remove(rel)
|
||||
if history:
|
||||
ifcopenshell.util.element.remove_deep2(file, history)
|
||||
|
||||
Reference in New Issue
Block a user