From 416f5aef6976163f0e4bcc404717857eb1f2f421 Mon Sep 17 00:00:00 2001 From: Julian Daube Date: Wed, 3 Jul 2019 14:14:30 +0200 Subject: [PATCH] blender: make progress report work consistent --- gds/library.py | 14 +++++++++++++- import_gds.py | 47 ++++++++++++++++++++++++++++++++--------------- 2 files changed, 45 insertions(+), 16 deletions(-) diff --git a/gds/library.py b/gds/library.py index 0fa5000..4c11c9c 100644 --- a/gds/library.py +++ b/gds/library.py @@ -31,7 +31,19 @@ class Library(object): return (coord[0] * fact, coord[1] * fact) - def link_refs(self, progress_callback=None): + def link_refs(self, root, progress_callback=None): + for ref in root.references: + if isinstance(ref.structure, str): + try: + element.structure = self.structures[ref.structure] + except KeyError: + err = LinkError("dangeling sref (structure {} is not defined in library)".format(ref.structure)) + err.element = root + raise err + + return root + + def link_all_refs(self, progress_callback=None): class Progress(ProgressGetter): total = 0 current = 0 diff --git a/import_gds.py b/import_gds.py index a541c35..35c22f3 100644 --- a/import_gds.py +++ b/import_gds.py @@ -98,11 +98,14 @@ class SceneInserter(object): def link(self, obj): self.context.scene.objects.link(obj) - def build_object(self, structure): + def build_object(self, structure, progress=None): # is the object already existent => return new instance if structure.name in bpy.data.objects: return self.new_instance(bpy.data.objects[structure.name]) + if progress: + progress.total += 1 + len(structure.references) + verts = {} faces = {} @@ -149,10 +152,21 @@ class SceneInserter(object): self.layers[layer].link(obj) obj.parent = root + if progress: + progress.inc() + # handle all references # build tree depth first for reference in structure.references: - obj = self.build_object(reference.structure) + if isinstance(reference.structure, str): + if reference.structure not in self.lib.structures: + progress.inc() + continue + + # resolve structure + reference.structure = self.lib.structures[reference.structure] + + obj = self.build_object(reference.structure, progress=progress) if hasattr(reference, "size"): def add(a,b): @@ -209,6 +223,9 @@ class SceneInserter(object): obj.rotation_euler.z = math.radians(trans.rotation) obj.scale = (trans.zoom, trans.zoom, 1) + if progress: + progress.inc() + return root def new_instance(self, obj): @@ -230,7 +247,7 @@ class SceneInserter(object): def __call__(self, progress=None): class ProgressTracker: - def __init__(self, total, callback): + def __init__(self, callback, total=0): self.total = total self.current = 0 self.callback = callback @@ -245,12 +262,18 @@ class SceneInserter(object): if self.top_cell: if self.top_cell not in self.lib.structures: return None - - self.build_object(self.lib.structures[self.top_cell]) + s = self.lib.structures[self.top_cell] + + progressTracker = ProgressTracker(progress, 1+len(s.references)) + self.build_object(s,progress=progressTracker) + return True - # build all structures in object - progressTracker = ProgressTracker(2*(len(self.lib.structures)-len(self.ignore_structures)), progress) + progressTracker = ProgressTracker(progress) + progressTracker.total = len(self.lib.structures) + + # self.link_all_refs(progressTracker) + # build all structures first # since references could reference unknown objects otherwise for name, value in self.lib.structures.items(): @@ -258,9 +281,8 @@ class SceneInserter(object): if name in self.ignore_structures: continue - self.build_object(value) - progressTracker.total += len(value.references) - + self.build_object(value, progress=progressTracker) + return True @@ -285,11 +307,6 @@ def load(context, progress.step("linking references") prog.end() - lib.link_refs(prog) - - progress.step("inserting structures into scene") - prog.end() - if not SceneInserter(context=context, lib=lib, top_cell=top_cell_name)(prog): return {"CANCELLED"}