Optimise create owner history to not rely on a person and organisation lookup, speeding up long procedures

This commit is contained in:
Dion Moult
2021-10-01 11:05:35 +10:00
parent 8bcec29429
commit 0a27308532
11 changed files with 92 additions and 107 deletions
@@ -11,13 +11,11 @@ class Usecase:
self.settings[key] = value
def execute(self):
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 self.file.schema != "IFC2X3":
if not self.settings["person"] or not self.settings["organisation"] or not application:
if not user or not application:
return
user = self.get_user()
return self.file.create_entity(
"IfcOwnerHistory",
**{
@@ -31,15 +29,3 @@ class Usecase:
"CreationDate": int(time.time()),
},
)
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"]},
)
@@ -1,15 +1,4 @@
# Note: it is the intent for you to override these with your own functions
users = {}
def get_person(ifc):
people = ifc.by_type("IfcPerson") or [None]
return people[0]
def get_organisation(ifc):
organisations = ifc.by_type("IfcOrganization") or [None]
return organisations[0]
def get_application(ifc):
@@ -18,17 +7,5 @@ def get_application(ifc):
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
users = ifc.by_type("IfcPersonAndOrganization") or [None]
return users[0]
@@ -24,9 +24,9 @@ class Usecase:
name=self.settings["name"],
)
work_schedule.CreationDate = ifcopenshell.util.date.datetime2ifc(datetime.now(), "IfcDateTime")
person = ifcopenshell.api.owner.settings.get_person(self.file)
if person:
work_schedule.Creators = [person]
user = ifcopenshell.api.owner.settings.get_user(self.file)
if user:
work_schedule.Creators = [user.ThePerson]
work_schedule.StartTime = ifcopenshell.util.date.datetime2ifc(self.settings["start_time"], "IfcDateTime")
if self.settings["work_plan"]: