Refactor profile module to use data class

This commit is contained in:
Dion Moult
2022-10-29 23:45:04 +11:00
parent 9a25db6584
commit bf57c8b1bf
3 changed files with 56 additions and 35 deletions
@@ -0,0 +1,38 @@
# BlenderBIM Add-on - OpenBIM Blender Add-on
# Copyright (C) 2022 Dion Moult <dion@thinkmoult.com>
#
# This file is part of BlenderBIM Add-on.
#
# BlenderBIM Add-on is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# BlenderBIM Add-on is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with BlenderBIM Add-on. If not, see <http://www.gnu.org/licenses/>.
import bpy
import blenderbim.tool as tool
def refresh():
ProfileData.is_loaded = False
class ProfileData:
data = {}
is_loaded = False
@classmethod
def load(cls):
cls.data = {"total_profiles": cls.total_profiles()}
cls.is_loaded = True
@classmethod
def total_profiles(cls):
return len(tool.Ifc.get().by_type("IfcProfileDef"))
@@ -1,5 +1,5 @@
# BlenderBIM Add-on - OpenBIM Blender Add-on
# Copyright (C) 2020, 2021 Dion Moult <dion@thinkmoult.com>
# Copyright (C) 2020, 2021, 2022 Dion Moult <dion@thinkmoult.com>
#
# This file is part of BlenderBIM Add-on.
#
@@ -19,8 +19,7 @@
import bpy
import ifcopenshell.api
import blenderbim.bim.helper
from blenderbim.bim.ifc import IfcStore
from ifcopenshell.api.profile.data import Data
import blenderbim.tool as tool
class LoadProfiles(bpy.types.Operator):
@@ -32,10 +31,10 @@ class LoadProfiles(bpy.types.Operator):
props = context.scene.BIMProfileProperties
props.profiles.clear()
for ifc_definition_id, profile in Data.profiles.items():
for profile in tool.Ifc.get().by_type("IfcProfileDef"):
new = props.profiles.add()
new.ifc_definition_id = ifc_definition_id
new.name = profile.get("ProfileName", "") or "Unnamed"
new.ifc_definition_id = profile.id()
new.name = profile.ProfileName or "Unnamed"
props.is_editing = True
bpy.ops.bim.disable_editing_profile()
@@ -52,20 +51,14 @@ class DisableProfileEditingUI(bpy.types.Operator):
return {"FINISHED"}
class RemoveProfileDef(bpy.types.Operator):
class RemoveProfileDef(bpy.types.Operator, tool.Ifc.Operator):
bl_idname = "bim.remove_profile_def"
bl_label = "Remove Profile Definition"
bl_options = {"REGISTER", "UNDO"}
profile: bpy.props.IntProperty()
def execute(self, context):
return IfcStore.execute_ifc_operator(self, context)
def _execute(self, context):
props = context.scene.BIMProfileProperties
self.file = IfcStore.get_file()
ifcopenshell.api.run("profile.remove_profile", self.file, **{"profile": self.file.by_id(self.profile)})
Data.load(self.file)
ifcopenshell.api.run("profile.remove_profile", tool.Ifc.get(), profile=tool.Ifc.get().by_id(self.profile))
bpy.ops.bim.load_profiles()
return {"FINISHED"}
@@ -79,10 +72,7 @@ class EnableEditingProfile(bpy.types.Operator):
def execute(self, context):
props = context.scene.BIMProfileProperties
props.profile_attributes.clear()
data = Data.profiles[self.profile]
blenderbim.bim.helper.import_attributes(data["type"], props.profile_attributes, data)
blenderbim.bim.helper.import_attributes2(tool.Ifc.get().by_id(self.profile), props.profile_attributes)
props.active_profile_id = self.profile
return {"FINISHED"}
@@ -97,20 +87,15 @@ class DisableEditingProfile(bpy.types.Operator):
return {"FINISHED"}
class EditProfile(bpy.types.Operator):
class EditProfile(bpy.types.Operator, tool.Ifc.Operator):
bl_idname = "bim.edit_profile"
bl_label = "Edit Profile"
bl_options = {"REGISTER", "UNDO"}
def execute(self, context):
return IfcStore.execute_ifc_operator(self, context)
def _execute(self, context):
props = context.scene.BIMProfileProperties
attributes = blenderbim.bim.helper.export_attributes(props.profile_attributes)
self.file = IfcStore.get_file()
profile = self.file.by_id(props.active_profile_id)
ifcopenshell.api.run("profile.edit_profile", self.file, **{"profile": profile, "attributes": attributes})
Data.load(IfcStore.get_file())
profile = tool.Ifc.get().by_id(props.active_profile_id)
ifcopenshell.api.run("profile.edit_profile", tool.Ifc.get(), profile=profile, attributes=attributes)
bpy.ops.bim.load_profiles()
return {"FINISHED"}
@@ -1,5 +1,5 @@
# BlenderBIM Add-on - OpenBIM Blender Add-on
# Copyright (C) 2020, 2021 Dion Moult <dion@thinkmoult.com>
# Copyright (C) 2020, 2021, 2022 Dion Moult <dion@thinkmoult.com>
#
# This file is part of BlenderBIM Add-on.
#
@@ -17,9 +17,9 @@
# along with BlenderBIM Add-on. If not, see <http://www.gnu.org/licenses/>.
import blenderbim.bim.helper
import blenderbim.tool as tool
from bpy.types import Panel, UIList
from blenderbim.bim.ifc import IfcStore
from ifcopenshell.api.profile.data import Data
from blenderbim.bim.module.profile.data import ProfileData
class BIM_PT_profiles(Panel):
@@ -33,17 +33,15 @@ class BIM_PT_profiles(Panel):
@classmethod
def poll(cls, context):
file = IfcStore.get_file()
return file
return tool.Ifc.get()
def draw(self, context):
self.file = IfcStore.get_file()
if not Data.is_loaded:
Data.load(self.file)
if not ProfileData.is_loaded:
ProfileData.load()
self.props = context.scene.BIMProfileProperties
row = self.layout.row(align=True)
row.label(text="{} Profiles Found".format(len(Data.profiles)), icon="SNAP_GRID")
row.label(text="{} Profiles Found".format(ProfileData.data["total_profiles"]), icon="SNAP_GRID")
if self.props.is_editing:
row.operator("bim.disable_profile_editing_ui", text="", icon="CANCEL")
else: