Add transaction support for adding elements. See #1709.

This commit is contained in:
Dion Moult
2021-08-31 10:25:13 +10:00
parent 0ed38a4128
commit 4accf30626
+10 -3
View File
@@ -126,7 +126,7 @@ class Transaction:
for operation in self.operations[::-1]: for operation in self.operations[::-1]:
if operation["action"] == "create": if operation["action"] == "create":
element = self.file.by_id(operation["value"]["id"]) element = self.file.by_id(operation["value"]["id"])
if hasattr(element, "GlobalId"): if hasattr(element, "GlobalId") and element.GlobalId is None:
# hack, otherwise ifcopenshell gets upset # hack, otherwise ifcopenshell gets upset
element.GlobalId = "x" element.GlobalId = "x"
self.file.remove(element) self.file.remove(element)
@@ -340,8 +340,15 @@ class file(object):
"""Adds an entity including any dependent entities to an IFC file. """Adds an entity including any dependent entities to an IFC file.
If the entity already exists, it is not re-added.""" If the entity already exists, it is not re-added."""
if self.transaction:
# TODO confirm this method of tracking added elements and use MaxId directly instead of FreshId
max_id = self.wrapped_data.FreshId()
inst.wrapped_data.this.disown() inst.wrapped_data.this.disown()
return entity_instance(self.wrapped_data.add(inst.wrapped_data, -1 if _id is None else _id), self) result = entity_instance(self.wrapped_data.add(inst.wrapped_data, -1 if _id is None else _id), self)
if self.transaction:
added_elements = [e for e in self.traverse(result) if e.id() > max_id]
[self.transaction.store_create(e) for e in reversed(added_elements)]
return result
def by_type(self, type, include_subtypes=True): def by_type(self, type, include_subtypes=True):
"""Return IFC objects filtered by IFC Type and wrapped with the entity_instance class. """Return IFC objects filtered by IFC Type and wrapped with the entity_instance class.
@@ -373,7 +380,7 @@ class file(object):
""" """
if max_levels is None: if max_levels is None:
max_levels = -1 max_levels = -1
if breadth_first: if breadth_first:
fn = self.wrapped_data.traverse_breadth_first fn = self.wrapped_data.traverse_breadth_first
else: else: