See #3974. WIP review to ensure history removal in API.

This commit is contained in:
Dion Moult
2023-11-27 18:31:42 +11:00
parent f215670381
commit 14f1acadb7
22 changed files with 174 additions and 5 deletions
@@ -16,6 +16,7 @@
# 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 ifcopenshell
import ifcopenshell.util.element
@@ -48,4 +49,7 @@ class Usecase:
if geometry:
self.settings["boundary"].ConnectionGeometry = None
ifcopenshell.util.element.remove_deep2(self.file, geometry)
history = self.settings["boundary"].OwnerHistory
self.file.remove(self.settings["boundary"])
if history:
ifcopenshell.util.element.remove_deep2(self.file, history)
@@ -17,7 +17,9 @@
# along with IfcOpenShell. If not, see <http://www.gnu.org/licenses/>.
import ifcopenshell
import ifcopenshell.api
import ifcopenshell.util.element
class Usecase:
@@ -56,5 +58,8 @@ class Usecase:
self.file.remove(rel)
for rel in self.settings["information"].DocumentInfoForObjects or []:
history = rel.OwnerHistory
self.file.remove(rel)
if history:
ifcopenshell.util.element.remove_deep2(self.file, history)
self.file.remove(self.settings["information"])
@@ -18,6 +18,7 @@
import ifcopenshell
import ifcopenshell.api
import ifcopenshell.util.element
class Usecase:
@@ -61,7 +62,11 @@ class Usecase:
if not rel.is_a("IfcRelAssignsToProduct") or rel.RelatingProduct != self.settings["relating_product"]:
continue
if len(rel.RelatedObjects) == 1:
return self.file.remove(rel)
history = rel.OwnerHistory
self.file.remove(rel)
if history:
ifcopenshell.util.element.remove_deep2(self.file, history)
return
related_objects = list(rel.RelatedObjects)
related_objects.remove(self.settings["related_object"])
rel.RelatedObjects = related_objects
@@ -18,6 +18,7 @@
import ifcopenshell
import ifcopenshell.api
import ifcopenshell.util.element
class Usecase:
@@ -57,7 +58,16 @@ class Usecase:
)
elif inverse.is_a("IfcRelAssignsToGroup"):
if inverse.RelatingGroup == self.settings["group"]:
history = inverse.OwnerHistory
self.file.remove(inverse)
if history:
ifcopenshell.util.element.remove_deep2(self.file, history)
elif len(inverse.RelatedObjects) == 1:
history = inverse.OwnerHistory
self.file.remove(inverse)
if history:
ifcopenshell.util.element.remove_deep2(self.file, history)
history = self.settings["group"].OwnerHistory
self.file.remove(self.settings["group"])
if history:
ifcopenshell.util.element.remove_deep2(self.file, history)
@@ -111,6 +111,7 @@ class Usecase:
self.change_profile(element)
if old_profile and len(self.file.get_inverse(old_profile)) == 0:
# TODO: check remove deep
self.file.remove(old_profile)
def change_profile(self, element):
@@ -17,6 +17,7 @@
# along with IfcOpenShell. If not, see <http://www.gnu.org/licenses/>.
import ifcopenshell
import ifcopenshell.util.element
class Usecase:
@@ -59,7 +60,10 @@ class Usecase:
elif inverse.is_a("IfcMaterialProfile"):
self.file.remove(inverse)
elif inverse.is_a("IfcRelAssociatesMaterial"):
history = inverse.OwnerHistory
self.file.remove(inverse)
if history:
ifcopenshell.util.element.remove_deep2(self.file, history)
elif inverse.is_a("IfcMaterialProperties"):
for prop in inverse.Properties or []:
self.file.remove(prop)
@@ -17,6 +17,7 @@
# along with IfcOpenShell. If not, see <http://www.gnu.org/licenses/>.
import ifcopenshell
import ifcopenshell.util.element
class Usecase:
@@ -73,7 +74,10 @@ class Usecase:
self.file.remove(self.settings["material"])
for inverse in inverse_elements:
if inverse.is_a("IfcRelAssociatesMaterial"):
history = inverse.OwnerHistory
self.file.remove(inverse)
if history:
ifcopenshell.util.element.remove_deep2(self.file, history)
elif inverse.is_a("IfcMaterialProperties"):
for prop in inverse.Properties or []:
self.file.remove(prop)
@@ -17,6 +17,10 @@
# along with IfcOpenShell. If not, see <http://www.gnu.org/licenses/>.
import ifcopenshell
import ifcopenshell.util.element
class Usecase:
def __init__(self, file, profile=None):
"""Removes a profile item from a profile set
@@ -65,5 +69,10 @@ class Usecase:
self.settings = {"profile": profile}
def execute(self):
subelements = set()
for attribute in self.settings["profile"]:
if isinstance(attribute, ifcopenshell.entity_instance):
subelements.add(attribute)
self.file.remove(self.settings["profile"])
# TODO: deep purge
for subelement in subelements:
ifcopenshell.util.element.remove_deep2(self.file, subelement)
@@ -65,7 +65,10 @@ class Usecase:
continue
for inverse2 in self.file.get_inverse(inverse):
if inverse2.is_a("IfcRelAssociatesMaterial"):
history = inverse2.OwnerHistory
self.file.remove(inverse2)
if history:
ifcopenshell.util.element.remove_deep2(self.file, history)
else:
if not inverse.is_a("IfcMaterialUsageDefinition"):
continue
@@ -80,7 +83,10 @@ class Usecase:
if self.file.get_total_inverses(rel.RelatingMaterial) == 1 and len(rel.RelatedObjects) == 1:
self.file.remove(rel.RelatingMaterial)
if len(rel.RelatedObjects) == 1:
history = rel.OwnerHistory
self.file.remove(rel)
if history:
ifcopenshell.util.element.remove_deep2(self.file, history)
continue
related_objects = set(rel.RelatedObjects)
related_objects.remove(self.settings["product"])
@@ -16,7 +16,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/>.
import ifcopenshell.api
import ifcopenshell
import ifcopenshell.util.element
class Usecase:
@@ -48,4 +49,7 @@ class Usecase:
self.settings = {"actor": actor}
def execute(self):
history = self.settings["actor"].OwnerHistory
self.file.remove(self.settings["actor"])
if history:
ifcopenshell.util.element.remove_deep2(self.file, history)
@@ -16,6 +16,9 @@
# 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 ifcopenshell
import ifcopenshell.util.element
class Usecase:
def __init__(self, file, profile=None):
@@ -39,5 +42,10 @@ class Usecase:
self.settings = {"profile": profile}
def execute(self):
subelements = set()
for attribute in self.settings["profile"]:
if isinstance(attribute, ifcopenshell.entity_instance):
subelements.add(attribute)
self.file.remove(self.settings["profile"])
# TODO: deep purge
for subelement in subelements:
ifcopenshell.util.element.remove_deep2(self.file, subelement)
@@ -18,6 +18,7 @@
import ifcopenshell
import ifcopenshell.api
import ifcopenshell.util.element
class Usecase:
@@ -66,4 +67,7 @@ class Usecase:
rel.RelatedDefinitions = list(related_definitions)
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)
@@ -16,6 +16,9 @@
# 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 ifcopenshell
import ifcopenshell.util.element
class Usecase:
def __init__(self, file, product=None, pset=None):
@@ -72,6 +75,12 @@ class Usecase:
if enumeration and self.file.get_total_inverses(enumeration) == 1:
self.file.remove(enumeration)
self.file.remove(prop)
history = self.settings["pset"].OwnerHistory
self.file.remove(self.settings["pset"])
if history:
ifcopenshell.util.element.remove_deep2(self.file, history)
for element in to_purge:
history = getattr(element, "OwnerHistory", None)
self.file.remove(element)
if history:
ifcopenshell.util.element.remove_deep2(self.file, history)
@@ -128,48 +128,90 @@ class Usecase:
elif inverse.is_a("IfcRelSpaceBoundary"):
ifcopenshell.api.run("boundary.remove_boundary", self.file, boundary=inverse)
elif inverse.is_a("IfcRelFillsElement"):
history = inverse.OwnerHistory
self.file.remove(inverse)
if history:
ifcopenshell.util.element.remove_deep2(self.file, history)
elif inverse.is_a("IfcRelVoidsElement"):
history = inverse.OwnerHistory
self.file.remove(inverse)
if history:
ifcopenshell.util.element.remove_deep2(self.file, history)
elif inverse.is_a("IfcRelServicesBuildings"):
history = inverse.OwnerHistory
self.file.remove(inverse)
if history:
ifcopenshell.util.element.remove_deep2(self.file, history)
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:
history = inverse.OwnerHistory
self.file.remove(inverse)
if history:
ifcopenshell.util.element.remove_deep2(self.file, history)
elif inverse.is_a("IfcRelAggregates"):
if inverse.RelatingObject == self.settings["product"] or len(inverse.RelatedObjects) == 1:
history = inverse.OwnerHistory
self.file.remove(inverse)
if history:
ifcopenshell.util.element.remove_deep2(self.file, history)
elif inverse.is_a("IfcRelContainedInSpatialStructure"):
if inverse.RelatingStructure == self.settings["product"] or len(inverse.RelatedElements) == 1:
history = inverse.OwnerHistory
self.file.remove(inverse)
if history:
ifcopenshell.util.element.remove_deep2(self.file, history)
elif inverse.is_a("IfcRelConnectsElements"):
if inverse.is_a("IfcRelConnectsWithRealizingElements"):
if self.settings["product"] not in (inverse.RelatingElement, inverse.RelatedElement) and any(
el for el in inverse.RealizingElements if el != self.settings["product"]
):
continue
history = inverse.OwnerHistory
self.file.remove(inverse)
if history:
ifcopenshell.util.element.remove_deep2(self.file, history)
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
history = inverse.OwnerHistory
self.file.remove(inverse)
if history:
ifcopenshell.util.element.remove_deep2(self.file, history)
elif inverse.is_a("IfcRelAssignsToGroup"):
if len(inverse.RelatedObjects) == 1:
history = inverse.OwnerHistory
self.file.remove(inverse)
if history:
ifcopenshell.util.element.remove_deep2(self.file, history)
elif inverse.is_a("IfcRelAssignsToProduct"):
if inverse.RelatingProduct == self.settings["product"]:
history = inverse.OwnerHistory
self.file.remove(inverse)
if history:
ifcopenshell.util.element.remove_deep2(self.file, history)
elif len(inverse.RelatedObjects) == 1:
history = inverse.OwnerHistory
self.file.remove(inverse)
if history:
ifcopenshell.util.element.remove_deep2(self.file, history)
elif inverse.is_a("IfcRelFlowControlElements"):
if inverse.RelatingFlowElement == self.settings["product"]:
history = inverse.OwnerHistory
self.file.remove(inverse)
if history:
ifcopenshell.util.element.remove_deep2(self.file, history)
elif inverse.RelatedControlElements == (self.settings["product"],):
history = inverse.OwnerHistory
self.file.remove(inverse)
if history:
ifcopenshell.util.element.remove_deep2(self.file, history)
history = self.settings["product"].OwnerHistory
self.file.remove(self.settings["product"])
if history:
ifcopenshell.util.element.remove_deep2(self.file, history)
@@ -18,6 +18,7 @@
import ifcopenshell
import ifcopenshell.api
import ifcopenshell.util.element
class Usecase:
@@ -138,16 +139,28 @@ class Usecase:
def purge_existing_connections_to_other_ports(self):
for rel in self.settings["port1"].ConnectedTo or []:
if rel.RelatedPort != self.settings["port2"]:
history = rel.OwnerHistory
self.file.remove(rel)
if history:
ifcopenshell.util.element.remove_deep2(self.file, history)
for rel in self.settings["port1"].ConnectedFrom or []:
if rel.RelatingPort != self.settings["port2"]:
history = rel.OwnerHistory
self.file.remove(rel)
if history:
ifcopenshell.util.element.remove_deep2(self.file, history)
for rel in self.settings["port2"].ConnectedTo or []:
if rel.RelatedPort != self.settings["port1"]:
history = rel.OwnerHistory
self.file.remove(rel)
if history:
ifcopenshell.util.element.remove_deep2(self.file, history)
for rel in self.settings["port2"].ConnectedFrom or []:
if rel.RelatingPort != self.settings["port1"]:
history = rel.OwnerHistory
self.file.remove(rel)
if history:
ifcopenshell.util.element.remove_deep2(self.file, history)
def set_connected_to(self):
if self.settings["port1"].ConnectedTo:
@@ -175,11 +188,17 @@ class Usecase:
def purge_connected_to(self):
for rel in self.settings["port1"].ConnectedTo or []:
history = rel.OwnerHistory
self.file.remove(rel)
if history:
ifcopenshell.util.element.remove_deep2(self.file, history)
def purge_connected_from(self):
for rel in self.settings["port1"].ConnectedFrom or []:
history = rel.OwnerHistory
self.file.remove(rel)
if history:
ifcopenshell.util.element.remove_deep2(self.file, history)
def set_realising_element(self):
for rel in self.settings["port1"].ConnectedTo or []:
@@ -18,6 +18,7 @@
import ifcopenshell
import ifcopenshell.api
import ifcopenshell.util.element
class Usecase:
@@ -76,4 +77,7 @@ class Usecase:
for rel in rels:
rel.RelatingPort.FlowDirection = None
rel.RelatedPort.FlowDirection = None
history = rel.OwnerHistory
self.file.remove(rel)
if history:
ifcopenshell.util.element.remove_deep2(self.file, history)
@@ -18,6 +18,7 @@
import ifcopenshell
import ifcopenshell.api
import ifcopenshell.util.element
class Usecase:
@@ -59,7 +60,16 @@ class Usecase:
)
elif inverse.is_a("IfcRelAssignsToGroup"):
if inverse.RelatingGroup == self.settings["system"]:
history = inverse.OwnerHistory
self.file.remove(inverse)
if history:
ifcopenshell.util.element.remove_deep2(self.file, history)
elif len(inverse.RelatedObjects) == 1:
history = inverse.OwnerHistory
self.file.remove(inverse)
if history:
ifcopenshell.util.element.remove_deep2(self.file, history)
history = self.settings["system"].OwnerHistory
self.file.remove(self.settings["system"])
if history:
ifcopenshell.util.element.remove_deep2(self.file, history)
@@ -18,6 +18,7 @@
import ifcopenshell
import ifcopenshell.api
import ifcopenshell.util.element
class Usecase:
@@ -64,7 +65,11 @@ class Usecase:
if assignment.RelatingFlowElement != self.settings["relating_flow_element"]:
return
if len(assignment.RelatedControlElements) == 1:
return self.file.remove(assignment)
history = assignment.OwnerHistory
self.file.remove(assignment)
if history:
ifcopenshell.util.element.remove_deep2(self.file, history)
return
related_flow_controls = list(assignment.RelatedControlElements)
related_flow_controls.remove(self.settings["related_flow_control"])
assignment.RelatedControlElements = related_flow_controls
@@ -63,7 +63,10 @@ class Usecase:
for rel in self.settings["element"].IsNestedBy or []:
if self.settings["port"] in rel.RelatedObjects:
if len(rel.RelatedObjects) == 1:
history = rel.OwnerHistory
self.file.remove(rel)
if history:
ifcopenshell.util.element.remove_deep2(self.file, history)
return
related_objects = set(rel.RelatedObjects) or set()
related_objects.remove(self.settings["port"])
@@ -73,5 +76,8 @@ class Usecase:
def execute_ifc2x3(self):
for rel in self.settings["element"].HasPorts or []:
if rel.RelatingPort == self.settings["port"]:
history = rel.OwnerHistory
self.file.remove(rel)
if history:
ifcopenshell.util.element.remove_deep2(self.file, history)
return
@@ -18,6 +18,7 @@
import ifcopenshell
import ifcopenshell.api
import ifcopenshell.util.element
class Usecase:
@@ -64,4 +65,7 @@ class Usecase:
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)
@@ -195,7 +195,10 @@ class Usecase:
is_typed_by[0].RelatedObjects = related_objects
ifcopenshell.api.run("owner.update_owner_history", self.file, **{"element": is_typed_by[0]})
else:
history = is_typed_by[0].OwnerHistory
self.file.remove(is_typed_by[0])
if history:
ifcopenshell.util.element.remove_deep2(self.file, history)
if types:
related_objects = list(types[0].RelatedObjects)
@@ -69,4 +69,7 @@ class Usecase:
is_typed_by.RelatedObjects = related_objects
ifcopenshell.api.run("owner.update_owner_history", self.file, **{"element": is_typed_by})
else:
history = is_typed_by.OwnerHistory
self.file.remove(is_typed_by)
if history:
ifcopenshell.util.element.remove_deep2(self.file, history)