From 23522c623135bbe805e38fbab4a73ce5675717d1 Mon Sep 17 00:00:00 2001 From: Julian Daube Date: Thu, 4 Jul 2019 21:22:20 +0200 Subject: [PATCH] blender: relocate importing code into operator --- __init__.py | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/__init__.py b/__init__.py index b40dedf..6854c55 100644 --- a/__init__.py +++ b/__init__.py @@ -12,7 +12,9 @@ if "bpy" in locals(): import importlib if "import_gds" in locals(): importlib.reload(import_gds) - + if "gds" in locals(): + importlib.reload(gds) + import bpy from bpy.props import ( @@ -21,6 +23,7 @@ from bpy.props import ( StringProperty, EnumProperty, ) + from bpy_extras.io_utils import ( ImportHelper, ExportHelper, @@ -102,6 +105,7 @@ class ImportGDS(bpy.types.Operator, ImportHelper, IOOBJOrientationHelper): def execute(self, context): # print("Selected: " + context.active_object.name) from . import import_gds + from . import gds # ignore axis helper arguments keywords = self.as_keywords(ignore=("axis_forward", @@ -113,20 +117,32 @@ class ImportGDS(bpy.types.Operator, ImportHelper, IOOBJOrientationHelper): if bpy.data.is_saved and context.user_preferences.filepaths.use_relative_paths: import os keywords["relpath"] = os.path.dirname(bpy.data.filepath) + + # do the file import + progressCounter = import_gds.Progressor(context.window_manager) - result= import_gds.load(context, **keywords) + try: + with open(keywords["filepath"], "rb") as file: + lib = gds.parse_file(file, progressCounter) + except gds.ParserError as e: + self.report({"ERROR_INVALID_INPUT"}, "cannot parse given gds file: {}".format(e)) + + progressCounter.end() + sceneInserter = import_gds.SceneInserter(context, lib, top_cell=keywords["top_cell_name"]) - # properly report errors to user in ui - if len(result) != 0 and "CANCELLED" in result: + if not sceneInserter(progressCounter): self.report({"ERROR_INVALID_INPUT"}, "Could not find structure with name {}".format(keywords["top_cell_name"])) + return {"CANCELLED"} + + return {"FINISHED"} - return result def draw(self, context): layout = self.layout row = layout.row() row.prop(self, "top_cell_name") + # row = layout.row(align=True) # row.prop(self, "use_smooth_groups") # row.prop(self, "use_edges")