Fix #1764. Bug where OpenLCA UI would freeze if it couldn't connect to OpenLCA.

This commit is contained in:
Dion Moult
2021-11-09 20:43:30 +11:00
parent fcbb6e60ac
commit 91f2f9438f
5 changed files with 75 additions and 22 deletions
@@ -21,6 +21,7 @@ from . import ui, prop, operator
classes = (
operator.CalculateLCA,
operator.ConnectOpenLCA,
prop.BIMLCAProperties,
ui.BIM_PT_lca,
)
@@ -0,0 +1,49 @@
# BlenderBIM Add-on - OpenBIM Blender Add-on
# Copyright (C) 2021 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 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
@@ -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"}
@@ -17,7 +17,7 @@
# along with BlenderBIM Add-on. If not, see <http://www.gnu.org/licenses/>.
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")
+10 -6
View File
@@ -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")