2022-01-19 12:18:33 +11:00
|
|
|
# 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/>.
|
|
|
|
|
|
2021-03-27 22:52:52 +11:00
|
|
|
import ifcopenshell.api
|
2021-03-25 10:48:31 +11:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class Usecase:
|
2022-12-16 15:29:50 +11:00
|
|
|
def __init__(self, file, product=None):
|
|
|
|
|
"""Removes a product
|
|
|
|
|
|
|
|
|
|
This is effectively a smart delete function that not only removes a
|
|
|
|
|
product, but also all of its relationships. It is always recommended to
|
|
|
|
|
use this function to prevent orphaned data in your IFC model.
|
|
|
|
|
|
2023-02-21 10:59:47 +11:00
|
|
|
This is intended to be used for removing:
|
|
|
|
|
|
|
|
|
|
- IfcAnnotation
|
|
|
|
|
- IfcElement
|
|
|
|
|
- IfcElementType
|
|
|
|
|
- IfcSpatialElement
|
|
|
|
|
- IfcSpatialElementType
|
|
|
|
|
|
2022-12-16 15:29:50 +11:00
|
|
|
For example, geometric representations are removed. Placement
|
|
|
|
|
coordinates are also removed. Properties are removed. Material, type,
|
|
|
|
|
containment, aggregation, and nesting relationships are removed (but
|
|
|
|
|
naturally, the materials, types, containers, etc themselves remain).
|
|
|
|
|
|
2023-02-21 10:59:47 +11:00
|
|
|
:param product: The element to remove.
|
2022-12-16 15:29:50 +11:00
|
|
|
:type product: ifcopenshell.entity_instance.entity_instance
|
|
|
|
|
:return: None
|
|
|
|
|
:rtype: None
|
|
|
|
|
|
2023-01-10 10:16:28 +11:00
|
|
|
Example:
|
|
|
|
|
|
|
|
|
|
.. code:: python
|
2022-12-16 15:29:50 +11:00
|
|
|
|
|
|
|
|
# We have a wall.
|
|
|
|
|
wall = ifcopenshell.api.run("root.create_entity", model, ifc_class="IfcWall")
|
|
|
|
|
|
|
|
|
|
# No we don't.
|
|
|
|
|
ifcopenshell.api.run("root.remove_product", model, product=wall)
|
|
|
|
|
"""
|
2021-03-25 10:48:31 +11:00
|
|
|
self.file = file
|
2022-12-16 15:29:50 +11:00
|
|
|
self.settings = {"product": product}
|
2021-03-25 10:48:31 +11:00
|
|
|
|
|
|
|
|
def execute(self):
|
|
|
|
|
representations = []
|
|
|
|
|
if self.settings["product"].is_a("IfcProduct"):
|
|
|
|
|
if self.settings["product"].Representation:
|
|
|
|
|
representations = self.settings["product"].Representation.Representations or []
|
|
|
|
|
else:
|
|
|
|
|
representations = []
|
2023-08-03 14:46:59 +05:00
|
|
|
|
|
|
|
|
object_placement = self.settings["product"].ObjectPlacement
|
|
|
|
|
if object_placement and self.file.get_total_inverses(object_placement) == 1:
|
|
|
|
|
self.settings["product"].ObjectPlacement = None # remove the inverse for remove_deep2 to work
|
|
|
|
|
ifcopenshell.util.element.remove_deep2(self.file, object_placement)
|
2021-03-25 10:48:31 +11:00
|
|
|
elif self.settings["product"].is_a("IfcTypeProduct"):
|
|
|
|
|
representations = [rm.MappedRepresentation for rm in self.settings["product"].RepresentationMaps or []]
|
|
|
|
|
for representation in representations:
|
2021-05-14 20:19:30 +10:00
|
|
|
ifcopenshell.api.run(
|
|
|
|
|
"geometry.unassign_representation",
|
|
|
|
|
self.file,
|
|
|
|
|
**{"product": self.settings["product"], "representation": representation}
|
2021-03-27 22:52:52 +11:00
|
|
|
)
|
2021-04-05 21:18:49 +10:00
|
|
|
ifcopenshell.api.run("geometry.remove_representation", self.file, **{"representation": representation})
|
2021-05-14 20:19:30 +10:00
|
|
|
for opening in getattr(self.settings["product"], "HasOpenings", []) or []:
|
2021-05-12 22:20:58 +10:00
|
|
|
ifcopenshell.api.run("void.remove_opening", self.file, opening=opening.RelatedOpeningElement)
|
2021-05-14 20:19:30 +10:00
|
|
|
|
|
|
|
|
if self.settings["product"].is_a("IfcGrid"):
|
|
|
|
|
for axis in (
|
|
|
|
|
self.settings["product"].UAxes + self.settings["product"].VAxes + (self.settings["product"].WAxes or ())
|
|
|
|
|
):
|
|
|
|
|
ifcopenshell.api.run("grid.remove_grid_axis", self.file, axis=axis)
|
|
|
|
|
|
2021-03-25 10:48:31 +11:00
|
|
|
# TODO: remove object placement and other relationships
|
2023-01-27 12:09:17 +11:00
|
|
|
for inverse_id in [i.id() for i in self.file.get_inverse(self.settings["product"])]:
|
|
|
|
|
try:
|
|
|
|
|
inverse = self.file.by_id(inverse_id)
|
|
|
|
|
except:
|
|
|
|
|
continue
|
2022-10-08 21:12:11 +11:00
|
|
|
if inverse.is_a("IfcRelDefinesByProperties"):
|
|
|
|
|
ifcopenshell.api.run(
|
|
|
|
|
"pset.remove_pset",
|
|
|
|
|
self.file,
|
|
|
|
|
product=self.settings["product"],
|
|
|
|
|
pset=inverse.RelatingPropertyDefinition,
|
|
|
|
|
)
|
|
|
|
|
elif inverse.is_a("IfcRelAssociatesMaterial"):
|
|
|
|
|
ifcopenshell.api.run("material.unassign_material", self.file, product=self.settings["product"])
|
|
|
|
|
elif inverse.is_a("IfcRelDefinesByType"):
|
|
|
|
|
if inverse.RelatingType == self.settings["product"]:
|
|
|
|
|
for related_object in inverse.RelatedObjects:
|
|
|
|
|
ifcopenshell.api.run("type.unassign_type", self.file, related_object=related_object)
|
|
|
|
|
else:
|
|
|
|
|
ifcopenshell.api.run("type.unassign_type", self.file, related_object=self.settings["product"])
|
|
|
|
|
elif inverse.is_a("IfcRelSpaceBoundary"):
|
2023-05-02 22:14:38 +10:00
|
|
|
ifcopenshell.api.run("boundary.remove_boundary", self.file, boundary=inverse)
|
2022-10-08 21:12:11 +11:00
|
|
|
elif inverse.is_a("IfcRelFillsElement"):
|
2021-08-22 22:02:22 +10:00
|
|
|
self.file.remove(inverse)
|
|
|
|
|
elif inverse.is_a("IfcRelVoidsElement"):
|
|
|
|
|
self.file.remove(inverse)
|
2023-01-25 17:42:27 +11:00
|
|
|
elif inverse.is_a("IfcRelServicesBuildings"):
|
|
|
|
|
self.file.remove(inverse)
|
2022-05-06 10:38:38 +10:00
|
|
|
elif inverse.is_a("IfcRelNests"):
|
|
|
|
|
if inverse.RelatingObject == self.settings["product"]:
|
|
|
|
|
for subelement in inverse.RelatedObjects:
|
|
|
|
|
if subelement.is_a("IfcDistributionPort"):
|
|
|
|
|
ifcopenshell.api.run("root.remove_product", self.file, product=subelement)
|
|
|
|
|
if not inverse.RelatedObjects:
|
|
|
|
|
self.file.remove(inverse)
|
2022-09-23 23:00:07 +10:00
|
|
|
elif inverse.is_a("IfcRelAggregates"):
|
2022-09-23 23:26:29 +10:00
|
|
|
if inverse.RelatingObject == self.settings["product"] or len(inverse.RelatedObjects) == 1:
|
2022-09-23 23:00:07 +10:00
|
|
|
self.file.remove(inverse)
|
2022-09-23 23:26:29 +10:00
|
|
|
elif inverse.is_a("IfcRelContainedInSpatialStructure"):
|
|
|
|
|
if inverse.RelatingStructure == self.settings["product"] or len(inverse.RelatedElements) == 1:
|
2022-09-23 23:00:07 +10:00
|
|
|
self.file.remove(inverse)
|
2023-07-14 18:19:14 +05:00
|
|
|
elif inverse.is_a("IfcRelConnectsElements"):
|
2023-07-14 10:20:30 +05:00
|
|
|
if inverse.is_a("IfcRelConnectsWithRealizingElements"):
|
2023-07-14 18:19:14 +05:00
|
|
|
if self.settings["product"] not in (inverse.RelatingElement, inverse.RelatedElement) and any(
|
|
|
|
|
el for el in inverse.RealizingElements if el != self.settings["product"]
|
|
|
|
|
):
|
2023-07-14 10:20:30 +05:00
|
|
|
continue
|
2022-09-24 14:12:45 +10:00
|
|
|
self.file.remove(inverse)
|
2023-08-01 17:14:53 +05:00
|
|
|
elif inverse.is_a("IfcRelConnectsPorts"):
|
|
|
|
|
if self.settings["product"] not in (inverse.RelatingPort, inverse.RelatedPort):
|
|
|
|
|
# if it's not RelatingPort/RelatedPort then it's optional RealizingElement
|
|
|
|
|
# so we keep the relationship
|
|
|
|
|
continue
|
|
|
|
|
self.file.remove(inverse)
|
2023-02-21 10:59:47 +11:00
|
|
|
elif inverse.is_a("IfcRelAssignsToGroup"):
|
|
|
|
|
if len(inverse.RelatedObjects) == 1:
|
|
|
|
|
self.file.remove(inverse)
|
2023-03-19 15:17:26 +11:00
|
|
|
elif inverse.is_a("IfcRelAssignsToProduct"):
|
|
|
|
|
if inverse.RelatingProduct == self.settings["product"]:
|
|
|
|
|
self.file.remove(inverse)
|
2023-03-21 09:11:14 +11:00
|
|
|
elif len(inverse.RelatedObjects) == 1:
|
2023-03-19 15:17:26 +11:00
|
|
|
self.file.remove(inverse)
|
2021-03-25 10:48:31 +11:00
|
|
|
self.file.remove(self.settings["product"])
|