diff --git a/src/blenderbim/blenderbim/bim/module/lca/__init__.py b/src/blenderbim/blenderbim/bim/module/lca/__init__.py index b32db0bacd..66194db120 100644 --- a/src/blenderbim/blenderbim/bim/module/lca/__init__.py +++ b/src/blenderbim/blenderbim/bim/module/lca/__init__.py @@ -21,6 +21,7 @@ from . import ui, prop, operator classes = ( operator.CalculateLCA, + operator.ConnectOpenLCA, prop.BIMLCAProperties, ui.BIM_PT_lca, ) diff --git a/src/blenderbim/blenderbim/bim/module/lca/data.py b/src/blenderbim/blenderbim/bim/module/lca/data.py new file mode 100644 index 0000000000..da5b5450e2 --- /dev/null +++ b/src/blenderbim/blenderbim/bim/module/lca/data.py @@ -0,0 +1,49 @@ +# BlenderBIM Add-on - OpenBIM Blender Add-on +# Copyright (C) 2021 Dion Moult +# +# 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 . + +import bpy +import olca +import blenderbim.tool as tool + + +def refresh(): + LcaData.is_loaded = False + + +class LcaData: + data = {} + is_loaded = False + + @classmethod + def load(cls): + cls.data = { + "product_systems": cls.product_systems(), + } + cls.is_loaded = True + + @classmethod + def product_systems(cls): + if not bpy.context.scene.BIMLCAProperties.is_connected: + return [] + results = [] + client = olca.Client(context.preferences.addons["blenderbim"].preferences.openlca_port) + try: + results = [(ps.name, ps.name, "") for ps in client.get_all(olca.ProductSystem)] + except: + pass + return results diff --git a/src/blenderbim/blenderbim/bim/module/lca/operator.py b/src/blenderbim/blenderbim/bim/module/lca/operator.py index 06926f5009..91925d297f 100644 --- a/src/blenderbim/blenderbim/bim/module/lca/operator.py +++ b/src/blenderbim/blenderbim/bim/module/lca/operator.py @@ -43,3 +43,13 @@ class CalculateLCA(bpy.types.Operator): def invoke(self, context, event): context.window_manager.fileselect_add(self) return {"RUNNING_MODAL"} + + +class ConnectOpenLCA(bpy.types.Operator): + bl_idname = "bim.connect_openlca" + bl_label = "Connect OpenLCA" + bl_options = {"REGISTER", "UNDO"} + + def execute(self, context): + bpy.context.scene.BIMLCAProperties.is_connected = True + return {"FINISHED"} diff --git a/src/blenderbim/blenderbim/bim/module/lca/prop.py b/src/blenderbim/blenderbim/bim/module/lca/prop.py index d612b3c666..7d92c2e55b 100644 --- a/src/blenderbim/blenderbim/bim/module/lca/prop.py +++ b/src/blenderbim/blenderbim/bim/module/lca/prop.py @@ -17,7 +17,7 @@ # along with BlenderBIM Add-on. If not, see . import bpy -import olca +from blenderbim.bim.module.lca.data import LcaData from blenderbim.bim.prop import StrProperty, Attribute from bpy.types import PropertyGroup from bpy.props import ( @@ -31,25 +31,14 @@ from bpy.props import ( CollectionProperty, ) -productsystems_enum = [] - - -def purge(): - global productsystems_enum - productsystems_enum = [] - def get_product_systems(self, context): - global productsystems_enum - if not len(productsystems_enum): - client = olca.Client(context.preferences.addons["blenderbim"].preferences.openlca_port) - try: - productsystems_enum = [(ps.name, ps.name, "") for ps in client.get_all(olca.ProductSystem)] - except: - pass - return productsystems_enum + if not LcaData.is_loaded: + LcaData.load() + return LcaData.data["product_systems"] class BIMLCAProperties(PropertyGroup): + is_connected: BoolProperty(name="Is Connected") amount: FloatProperty(name="Amount") product_systems: EnumProperty(items=get_product_systems, name="Product Systems") diff --git a/src/blenderbim/blenderbim/bim/module/lca/ui.py b/src/blenderbim/blenderbim/bim/module/lca/ui.py index a48bf668b1..4a4d7d062e 100644 --- a/src/blenderbim/blenderbim/bim/module/lca/ui.py +++ b/src/blenderbim/blenderbim/bim/module/lca/ui.py @@ -30,9 +30,13 @@ class BIM_PT_lca(Panel): def draw(self, context): props = context.scene.BIMLCAProperties - row = self.layout.row() - row.prop(props, "product_systems", text="") - row = self.layout.row() - row.prop(props, "amount", text="") - row = self.layout.row() - row.operator("bim.calculate_lca") + if props.is_connected: + row = self.layout.row() + row.prop(props, "product_systems", text="") + row = self.layout.row() + row.prop(props, "amount", text="") + row = self.layout.row() + row.operator("bim.calculate_lca") + else: + row = self.layout.row() + row.operator("bim.connect_openlca")