From 44df1f676d29394a2315fae742b31711febd5f5b Mon Sep 17 00:00:00 2001 From: Dion Moult Date: Mon, 29 Mar 2021 16:53:11 +1100 Subject: [PATCH] Finish decoupling ownership history code from Blender --- src/blenderbim/blenderbim/bim/handler.py | 86 ++++++++++++++++--- .../api/owner/create_owner_history.py | 63 +------------- .../ifcopenshell/api/owner/settings.py | 11 +-- .../api/owner/update_owner_history.py | 63 +------------- 4 files changed, 84 insertions(+), 139 deletions(-) diff --git a/src/blenderbim/blenderbim/bim/handler.py b/src/blenderbim/blenderbim/bim/handler.py index 1937c37e31..5f462f8a5e 100644 --- a/src/blenderbim/blenderbim/bim/handler.py +++ b/src/blenderbim/blenderbim/bim/handler.py @@ -100,21 +100,24 @@ def storeIdMap(scene): bpy.context.scene.BIMProperties.guid_map = json.dumps({k: v.name for k, v in IfcStore.guid_map.items()}) -@persistent -def setDefaultProperties(scene): - ifcopenshell.api.owner.settings.get_person = ( - lambda: IfcStore.get_file().by_id(int(bpy.context.scene.BIMOwnerProperties.user_person)) - if bpy.context.scene.BIMOwnerProperties.user_person - else None +def get_application(ifc): + version = get_application_version() + for element in ifc.by_type("IfcApplication"): + if element.ApplicationIdentifier == "BlenderBIM" and element.Version == version: + return element + return ifc.create_entity( + "IfcApplication", + **{ + "ApplicationDeveloper": create_application_organisation(ifc), + "Version": get_application_version(), + "ApplicationFullName": "BlenderBIM Add-on", + "ApplicationIdentifier": "BlenderBIM", + }, ) - ifcopenshell.api.owner.settings.get_organisation = ( - lambda: IfcStore.get_file().by_id(int(bpy.context.scene.BIMOwnerProperties.user_organisation)) - if bpy.context.scene.BIMOwnerProperties.user_organisation - else None - ) - ifcopenshell.api.owner.settings.settings["ApplicationIdentifier"] = "BlenderBIM" - ifcopenshell.api.owner.settings.settings["ApplicationFullName"] = "BlenderBIM Add-on" - ifcopenshell.api.owner.settings.settings["Version"] = ".".join( + + +def get_application_version(): + return ".".join( [ str(x) for x in [ @@ -124,6 +127,61 @@ def setDefaultProperties(scene): ][0] ] ) + + +def create_application_organisation(ifc): + return ifc.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": [ifc.create_entity("IfcActorRole", **{"Role": "USERDEFINED", "UserDefinedRole": "CONTRIBUTOR"})], + "Addresses": [ + ifc.create_entity( + "IfcTelecomAddress", + **{ + "Purpose": "USERDEFINED", + "UserDefinedPurpose": "WEBPAGE", + "Description": "The main webpage of the software collection.", + "WWWHomePageURL": "https://ifcopenshell.org", + }, + ), + ifc.create_entity( + "IfcTelecomAddress", + **{ + "Purpose": "USERDEFINED", + "UserDefinedPurpose": "WEBPAGE", + "Description": "The BlenderBIM Add-on webpage of the software collection.", + "WWWHomePageURL": "https://blenderbim.org", + }, + ), + ifc.create_entity( + "IfcTelecomAddress", + **{ + "Purpose": "USERDEFINED", + "UserDefinedPurpose": "REPOSITORY", + "Description": "The source code repository of the software collection.", + "WWWHomePageURL": "https://github.com/IfcOpenShell/IfcOpenShell.git", + }, + ), + ], + }, + ) + + +@persistent +def setDefaultProperties(scene): + ifcopenshell.api.owner.settings.get_person = ( + lambda ifc : ifc.by_id(int(bpy.context.scene.BIMOwnerProperties.user_person)) + if bpy.context.scene.BIMOwnerProperties.user_person + else None + ) + ifcopenshell.api.owner.settings.get_organisation = ( + lambda ifc : ifc.by_id(int(bpy.context.scene.BIMOwnerProperties.user_organisation)) + if bpy.context.scene.BIMOwnerProperties.user_organisation + else None + ) + ifcopenshell.api.owner.settings.get_application = get_application if len(bpy.context.scene.DocProperties.drawing_styles) == 0: drawing_style = bpy.context.scene.DocProperties.drawing_styles.add() drawing_style.name = "Technical" diff --git a/src/ifcopenshell-python/ifcopenshell/api/owner/create_owner_history.py b/src/ifcopenshell-python/ifcopenshell/api/owner/create_owner_history.py index 059fadf22e..fb5561390a 100644 --- a/src/ifcopenshell-python/ifcopenshell/api/owner/create_owner_history.py +++ b/src/ifcopenshell-python/ifcopenshell/api/owner/create_owner_history.py @@ -6,18 +6,18 @@ import ifcopenshell.api.owner.settings class Usecase: def __init__(self, file, **settings): self.file = file - self.settings = ifcopenshell.api.owner.settings.settings + self.settings = {} for key, value in settings.items(): self.settings[key] = value def execute(self): - self.settings["person"] = ifcopenshell.api.owner.settings.get_person() - self.settings["organisation"] = ifcopenshell.api.owner.settings.get_organisation() + self.settings["person"] = ifcopenshell.api.owner.settings.get_person(self.file) + self.settings["organisation"] = ifcopenshell.api.owner.settings.get_organisation(self.file) if self.file.schema != "IFC2X3": if not self.settings["person"] or not self.settings["organisation"]: return user = self.get_user() - application = self.get_application() + application = ifcopenshell.api.owner.settings.get_application(self.file) return self.file.create_entity( "IfcOwnerHistory", **{ @@ -43,58 +43,3 @@ class Usecase: "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", - }, - ), - ], - }, - ) diff --git a/src/ifcopenshell-python/ifcopenshell/api/owner/settings.py b/src/ifcopenshell-python/ifcopenshell/api/owner/settings.py index d83e9ee372..f87f34a3aa 100644 --- a/src/ifcopenshell-python/ifcopenshell/api/owner/settings.py +++ b/src/ifcopenshell-python/ifcopenshell/api/owner/settings.py @@ -1,13 +1,10 @@ -def get_person(): +def get_person(ifc): pass -def get_organisation(): +def get_organisation(ifc): pass -settings = { - "ApplicationIdentifier": "", - "ApplicationFullName": "", - "Version": "", -} +def get_application(ifc): + pass diff --git a/src/ifcopenshell-python/ifcopenshell/api/owner/update_owner_history.py b/src/ifcopenshell-python/ifcopenshell/api/owner/update_owner_history.py index e44e8f7ac8..79e9283947 100644 --- a/src/ifcopenshell-python/ifcopenshell/api/owner/update_owner_history.py +++ b/src/ifcopenshell-python/ifcopenshell/api/owner/update_owner_history.py @@ -6,13 +6,13 @@ import ifcopenshell.api class Usecase: def __init__(self, file, **settings): self.file = file - self.settings = ifcopenshell.api.owner.settings.settings + self.settings = {} for key, value in settings.items(): self.settings[key] = value def execute(self): - self.settings["person"] = ifcopenshell.api.owner.settings.get_person() - self.settings["organisation"] = ifcopenshell.api.owner.settings.get_organisation() + self.settings["person"] = ifcopenshell.api.owner.settings.get_person(self.file) + self.settings["organisation"] = ifcopenshell.api.owner.settings.get_organisation(self.file) if not self.settings["element"].OwnerHistory: self.settings["element"].OwnerHistory = ifcopenshell.api.run( "owner.create_owner_history", self.file, **self.settings @@ -24,7 +24,7 @@ class Usecase: for i, attribute in enumerate(old_history): self.settings["element"].OwnerHistory[i] = attribute user = self.get_user() - application = self.get_application() + application = ifcopenshell.api.owner.settings.get_application(self.file) self.settings["element"].OwnerHistory.ChangeAction = "MODIFIED" self.settings["element"].OwnerHistory.LastModifiedDate = int(time.time()) self.settings["element"].OwnerHistory.LastModifyingUser = user @@ -42,58 +42,3 @@ class Usecase: "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", - }, - ), - ], - }, - )