Optimise update owner history to speed up complex operations

This commit is contained in:
Dion Moult
2021-09-20 10:17:16 +10:00
parent 2dac81ee8d
commit 90e481ce2c
7 changed files with 154 additions and 21 deletions
@@ -1,4 +1,5 @@
# Note: it is the intent for you to override these with your own functions
users = {}
def get_person(ifc):
@@ -14,3 +15,20 @@ def get_organisation(ifc):
def get_application(ifc):
applications = ifc.by_type("IfcApplication") or [None]
return applications[0]
def get_user(ifc):
person = get_person(ifc)
organisation = get_organisation(ifc)
if not person or not organisation:
return
key = f"{person.id()}-{organisation.id()}"
user = users.get(key)
if not user:
for element in ifc.by_type("IfcPersonAndOrganization"):
if element.ThePerson == person and element.TheOrganization == organisation:
users[key] = element
user = element
if not user:
return ifc.create_entity("IfcPersonAndOrganization", ThePerson=person, TheOrganization=organisation)
return user
@@ -1,6 +1,7 @@
import time
import ifcopenshell
import ifcopenshell.api
import ifcopenshell.util.element
class Usecase:
@@ -13,34 +14,20 @@ class Usecase:
def execute(self):
if not hasattr(self.settings["element"], "OwnerHistory"):
return
self.settings["person"] = ifcopenshell.api.owner.settings.get_person(self.file)
self.settings["organisation"] = ifcopenshell.api.owner.settings.get_organisation(self.file)
user = ifcopenshell.api.owner.settings.get_user(self.file)
application = ifcopenshell.api.owner.settings.get_application(self.file)
if not user or not application:
return
if not self.settings["element"].OwnerHistory:
self.settings["element"].OwnerHistory = ifcopenshell.api.run(
"owner.create_owner_history", self.file, **self.settings
)
return self.settings["element"].OwnerHistory
if len(self.file.get_inverse(self.settings["element"].OwnerHistory)) > 1:
old_history = self.settings["element"].OwnerHistory
self.settings["element"].OwnerHistory = self.file.create_entity("IfcOwnerHistory")
for i, attribute in enumerate(old_history):
self.settings["element"].OwnerHistory[i] = attribute
user = self.get_user()
application = ifcopenshell.api.owner.settings.get_application(self.file)
new = ifcopenshell.util.element.copy(self.file, self.settings["element"].OwnerHistory)
self.settings["element"].OwnerHistory = new
self.settings["element"].OwnerHistory.ChangeAction = "MODIFIED"
self.settings["element"].OwnerHistory.LastModifiedDate = int(time.time())
self.settings["element"].OwnerHistory.LastModifyingUser = user
self.settings["element"].OwnerHistory.LastModifyingApplication = application
return self.settings["element"].OwnerHistory
def get_user(self):
for element in self.file.by_type("IfcPersonAndOrganization"):
if (
element.ThePerson == self.settings["person"]
and element.TheOrganization == self.settings["organisation"]
):
return element
return self.file.create_entity(
"IfcPersonAndOrganization",
**{"ThePerson": self.settings["person"], "TheOrganization": self.settings["organisation"]},
)