First attempt at ripping out agnostic code into ifcopenshell.api namespace. See #1399.

This commit is contained in:
Dion Moult
2021-03-25 10:48:31 +11:00
parent 911d703f57
commit d77bc6bdaa
190 changed files with 388 additions and 437 deletions
@@ -0,0 +1,11 @@
class Usecase:
def __init__(self, file, settings=None):
self.file = file
self.settings = {"assigned_object": None, "ifc_class": "IfcPostalAddress"}
for key, value in settings.items():
self.settings[key] = value
def execute(self):
addresses = list(self.settings["assigned_object"].Addresses) if self.settings["assigned_object"].Addresses else []
addresses.append(self.file.create_entity(self.settings["ifc_class"], "OFFICE"))
self.settings["assigned_object"].Addresses = addresses
@@ -0,0 +1,6 @@
class Usecase:
def __init__(self, file):
self.file = file
def execute(self):
self.file.createIfcOrganization("APTR", "Aperture Science")
@@ -0,0 +1,6 @@
class Usecase:
def __init__(self, file):
self.file = file
def execute(self):
self.file.createIfcPerson("HSeldon", "Seldon", "Hari")
@@ -0,0 +1,11 @@
class Usecase:
def __init__(self, file, settings=None):
self.file = file
self.settings = {"assigned_object": None}
for key, value in settings.items():
self.settings[key] = value
def execute(self):
roles = list(self.settings["assigned_object"].Roles) if self.settings["assigned_object"].Roles else []
roles.append(self.file.createIfcActorRole("ARCHITECT"))
self.settings["assigned_object"].Roles = roles
@@ -0,0 +1,62 @@
import bpy
import time
import addon_utils
import ifcopenshell.api.owner.create_owner_history as create_owner_history_usecase
import ifcopenshell.api.owner.update_owner_history as update_owner_history_usecase
from blenderbim.bim.ifc import IfcStore
def update_owner_history(element, change_action=None):
if not element.OwnerHistory:
element.OwnerHistory = create_owner_history()
return
file = IfcStore.get_file()
return update_owner_history_usecase.Usecase(
file,
{
"element": element,
"person": file.by_id(int(bpy.context.scene.BIMOwnerProperties.user_person)),
"organisation": file.by_id(int(bpy.context.scene.BIMOwnerProperties.user_organisation)),
"ApplicationIdentifier": "BlenderBIM",
"ApplicationFullName": "BlenderBIM Add-on",
"Version": get_application_version(),
"ChangeAction": change_action or "MODIFIED",
},
).execute()
def create_owner_history(change_action=None):
file = IfcStore.get_file()
if (
not bpy.context.scene.BIMOwnerProperties.user_person
or not bpy.context.scene.BIMOwnerProperties.user_organisation
):
if file.schema == "IFC2X3":
assert False, "A person and organisation is required in IFC2X3."
return None
return create_owner_history_usecase.Usecase(
file,
{
"person": file.by_id(int(bpy.context.scene.BIMOwnerProperties.user_person)),
"organisation": file.by_id(int(bpy.context.scene.BIMOwnerProperties.user_organisation)),
"ApplicationIdentifier": "BlenderBIM",
"ApplicationFullName": "BlenderBIM Add-on",
"Version": get_application_version(),
"ChangeAction": change_action or "ADDED",
},
).execute()
def get_application_version():
return ".".join(
[
str(x)
for x in [
addon.bl_info.get("version", (-1, -1, -1))
for addon in addon_utils.modules()
if addon.bl_info["name"] == "BlenderBIM"
][0]
]
)
@@ -0,0 +1,101 @@
import time
import ifcopenshell
class Usecase:
def __init__(self, file, settings=None):
self.file = file
self.settings = {
"person": None,
"organisation": None,
"ApplicationIdentifier": "",
"ApplicationFullName": "",
"Version": "",
"ChangeAction": "NOTDEFINED",
}
for key, value in settings.items():
self.settings[key] = value
def execute(self):
user = self.get_user()
application = self.get_application()
return self.file.create_entity(
"IfcOwnerHistory",
**{
"OwningUser": user,
"OwningApplication": application,
"State": "READWRITE",
"ChangeAction": self.settings["ChangeAction"],
"LastModifiedDate": int(time.time()),
"LastModifyingUser": user,
"LastModifyingApplication": application,
"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"]},
)
def get_application(self):
for element in self.file.by_type("IfcApplication"):
if element.ApplicationIdentifier == self.settings["ApplicationIdentifier"]:
return element
return self.file.create_entity(
"IfcApplication",
**{
"ApplicationDeveloper": self.get_application_organisation(),
"Version": self.settings["Version"],
"ApplicationFullName": self.settings["ApplicationFullName"],
"ApplicationIdentifier": self.settings["ApplicationIdentifier"],
},
)
def get_application_organisation(self):
return self.file.create_entity(
"IfcOrganization",
**{
"Name": "IfcOpenShell",
"Description": "IfcOpenShell is an open source (LGPL) software library that helps users and software developers to work with the IFC file format.",
"Roles": [
self.file.create_entity("IfcActorRole", **{"Role": "USERDEFINED", "UserDefinedRole": "CONTRIBUTOR"})
],
"Addresses": [
self.file.create_entity(
"IfcTelecomAddress",
**{
"Purpose": "USERDEFINED",
"UserDefinedPurpose": "WEBPAGE",
"Description": "The main webpage of the software collection.",
"WWWHomePageURL": "https://ifcopenshell.org",
},
),
self.file.create_entity(
"IfcTelecomAddress",
**{
"Purpose": "USERDEFINED",
"UserDefinedPurpose": "WEBPAGE",
"Description": "The BlenderBIM Add-on webpage of the software collection.",
"WWWHomePageURL": "https://blenderbim.org",
},
),
self.file.create_entity(
"IfcTelecomAddress",
**{
"Purpose": "USERDEFINED",
"UserDefinedPurpose": "REPOSITORY",
"Description": "The source code repository of the software collection.",
"WWWHomePageURL": "https://github.com/IfcOpenShell/IfcOpenShell.git",
},
),
],
},
)
@@ -0,0 +1,65 @@
class Data:
is_loaded = False
people = {}
organisations = {}
addresses = {}
roles = {}
@classmethod
def purge(cls):
cls.is_loaded = False
cls.people = {}
cls.organisations = {}
cls.addresses = {}
cls.roles = {}
@classmethod
def load(cls, file):
if not file:
return
cls.people = {}
cls.organisations = {}
cls.addresses = {}
cls.roles = {}
for person in file.by_type("IfcPerson"):
data = person.get_info()
data["is_engaged"] = bool(person.EngagedIn)
cls.people[person.id()] = data
roles = []
if data["Roles"]:
for role in data["Roles"]:
roles.append(role.id())
data["Roles"] = roles
addresses = []
if data["Addresses"]:
for address in data["Addresses"]:
addresses.append(address.id())
data["Addresses"] = addresses
for organisation in file.by_type("IfcOrganization"):
data = organisation.get_info()
data["is_engaged"] = (
bool(organisation.IsRelatedBy) or bool(organisation.Relates) or bool(organisation.Engages)
)
cls.organisations[organisation.id()] = data
roles = []
if data["Roles"]:
for role in data["Roles"]:
roles.append(role.id())
data["Roles"] = roles
addresses = []
if data["Addresses"]:
for address in data["Addresses"]:
addresses.append(address.id())
data["Addresses"] = addresses
for address in file.by_type("IfcAddress"):
cls.addresses[address.id()] = address.get_info()
for role in file.by_type("IfcActorRole"):
cls.roles[role.id()] = role.get_info()
cls.is_loaded = True
@@ -0,0 +1,13 @@
class Usecase:
def __init__(self, file, settings=None):
self.file = file
self.settings = {
"address": None,
"attributes": {}
}
for key, value in settings.items():
self.settings[key] = value
def execute(self):
for name, value in self.settings["attributes"].items():
setattr(self.settings["address"], name, value)
@@ -0,0 +1,13 @@
class Usecase:
def __init__(self, file, settings=None):
self.file = file
self.settings = {
"organisation": None,
"attributes": {}
}
for key, value in settings.items():
self.settings[key] = value
def execute(self):
for name, value in self.settings["attributes"].items():
setattr(self.settings["organisation"], name, value)
@@ -0,0 +1,13 @@
class Usecase:
def __init__(self, file, settings=None):
self.file = file
self.settings = {
"person": None,
"attributes": {}
}
for key, value in settings.items():
self.settings[key] = value
def execute(self):
for name, value in self.settings["attributes"].items():
setattr(self.settings["person"], name, value)
@@ -0,0 +1,13 @@
class Usecase:
def __init__(self, file, settings=None):
self.file = file
self.settings = {
"role": None,
"attributes": {}
}
for key, value in settings.items():
self.settings[key] = value
def execute(self):
for name, value in self.settings["attributes"].items():
setattr(self.settings["role"], name, value)
@@ -0,0 +1,9 @@
class Usecase:
def __init__(self, file, settings=None):
self.file = file
self.settings = {"address": None}
for key, value in settings.items():
self.settings[key] = value
def execute(self):
self.file.remove(self.settings["address"])
@@ -0,0 +1,9 @@
class Usecase:
def __init__(self, file, settings=None):
self.file = file
self.settings = {"organisation": None}
for key, value in settings.items():
self.settings[key] = value
def execute(self):
self.file.remove(self.settings["organisation"])
@@ -0,0 +1,9 @@
class Usecase:
def __init__(self, file, settings=None):
self.file = file
self.settings = {"person": None}
for key, value in settings.items():
self.settings[key] = value
def execute(self):
self.file.remove(self.settings["person"])
@@ -0,0 +1,9 @@
class Usecase:
def __init__(self, file, settings=None):
self.file = file
self.settings = {"role": None}
for key, value in settings.items():
self.settings[key] = value
def execute(self):
self.file.remove(self.settings["role"])
@@ -0,0 +1,99 @@
import time
import ifcopenshell
class Usecase:
def __init__(self, file, settings=None):
self.file = file
self.settings = {
"OwnerHistory": None,
"person": None,
"organisation": None,
"ApplicationIdentifier": "",
"ApplicationFullName": "",
"Version": "",
"ChangeAction": "NOTDEFINED",
}
for key, value in settings.items():
self.settings[key] = value
def execute(self):
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 = self.get_application()
self.settings["element"].OwnerHistory.ChangeAction = self.settings["ChangeAction"]
self.settings["element"].OwnerHistory.LastModifiedDate = int(time.time())
self.settings["element"].OwnerHistory.LastModifyingUser = user
self.settings["element"].OwnerHistory.LastModifyingApplication = application
return self.settings["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"]},
)
def get_application(self):
for element in self.file.by_type("IfcApplication"):
if element.ApplicationIdentifier == self.settings["ApplicationIdentifier"]:
return element
return self.file.create_entity(
"IfcApplication",
**{
"ApplicationDeveloper": self.get_application_organisation(),
"Version": self.settings["Version"],
"ApplicationFullName": self.settings["ApplicationFullName"],
"ApplicationIdentifier": self.settings["ApplicationIdentifier"],
},
)
def get_application_organisation(self):
return self.file.create_entity(
"IfcOrganization",
**{
"Name": "IfcOpenShell",
"Description": "IfcOpenShell is an open source (LGPL) software library that helps users and software developers to work with the IFC file format.",
"Roles": [
self.file.create_entity("IfcActorRole", **{"Role": "USERDEFINED", "UserDefinedRole": "CONTRIBUTOR"})
],
"Addresses": [
self.file.create_entity(
"IfcTelecomAddress",
**{
"Purpose": "USERDEFINED",
"UserDefinedPurpose": "WEBPAGE",
"Description": "The main webpage of the software collection.",
"WWWHomePageURL": "https://ifcopenshell.org",
},
),
self.file.create_entity(
"IfcTelecomAddress",
**{
"Purpose": "USERDEFINED",
"UserDefinedPurpose": "WEBPAGE",
"Description": "The BlenderBIM Add-on webpage of the software collection.",
"WWWHomePageURL": "https://blenderbim.org",
},
),
self.file.create_entity(
"IfcTelecomAddress",
**{
"Purpose": "USERDEFINED",
"UserDefinedPurpose": "REPOSITORY",
"Description": "The source code repository of the software collection.",
"WWWHomePageURL": "https://github.com/IfcOpenShell/IfcOpenShell.git",
},
),
],
},
)