2019-07-02 14:44:42 +02:00
|
|
|
from . import gds
|
|
|
|
|
|
|
|
import importlib
|
|
|
|
if "gds" in locals():
|
|
|
|
gds = importlib.reload(gds)
|
|
|
|
|
|
|
|
import pathlib
|
|
|
|
import bpy
|
|
|
|
from bpy_extras.wm_utils.progress_report import ProgressReport
|
|
|
|
|
2019-07-02 19:39:34 +02:00
|
|
|
class Progressor:
|
2019-07-03 13:53:25 +02:00
|
|
|
def __init__(self, wm, precision = 0.0001):
|
2019-07-02 19:39:34 +02:00
|
|
|
self.wm = wm
|
|
|
|
self.report = 0
|
|
|
|
self.precision = precision
|
|
|
|
self.next_update = 0
|
|
|
|
|
|
|
|
@property
|
|
|
|
def total(self):
|
|
|
|
return self.report
|
|
|
|
|
|
|
|
@total.setter
|
|
|
|
def total(self, t):
|
|
|
|
if self.report >= t:
|
|
|
|
return
|
|
|
|
|
|
|
|
if self.report:
|
|
|
|
self.wm.progress_end()
|
2019-07-03 13:53:25 +02:00
|
|
|
self.next_update = 0
|
2019-07-02 19:39:34 +02:00
|
|
|
|
|
|
|
self.wm.progress_begin(0, t)
|
|
|
|
self.report = t
|
2019-07-03 13:53:25 +02:00
|
|
|
|
|
|
|
print ("begin new report , total {}".format(t))
|
|
|
|
|
2019-07-02 19:39:34 +02:00
|
|
|
@property
|
|
|
|
def current(self):
|
|
|
|
return 0
|
|
|
|
|
|
|
|
@current.setter
|
|
|
|
def current(self, c):
|
|
|
|
prog = float(c)/self.total
|
|
|
|
if prog >= self.next_update:
|
|
|
|
self.next_update = prog + self.precision
|
|
|
|
self.wm.progress_update(c)
|
|
|
|
|
|
|
|
def end(self):
|
|
|
|
if self.report:
|
|
|
|
self.report = 0
|
2019-07-03 13:53:25 +02:00
|
|
|
self.next_update = 0
|
|
|
|
|
2019-07-02 19:39:34 +02:00
|
|
|
self.wm.progress_end()
|
|
|
|
|
|
|
|
def __call__(self, update):
|
|
|
|
self.total = update.total
|
|
|
|
self.current = update.current
|
2019-07-02 20:05:01 +02:00
|
|
|
|
2019-07-03 13:53:25 +02:00
|
|
|
class SceneInserter(object):
|
|
|
|
class Layer(object):
|
|
|
|
def __init__(self, context, number):
|
|
|
|
self.name = "layer{}".format(number)
|
|
|
|
|
|
|
|
if self.name not in bpy.data.groups:
|
|
|
|
# create new group
|
|
|
|
self.group = bpy.data.groups.new(self.name)
|
|
|
|
self.name = self.group.name
|
|
|
|
else:
|
|
|
|
self.group = bpy.data.groups[self.name]
|
|
|
|
|
|
|
|
def link(self, obj):
|
|
|
|
self.group.objects.link(obj)
|
|
|
|
|
|
|
|
def __init__(self, context, lib, ignore_structures={}, top_cell=None):
|
|
|
|
self.lib = lib
|
|
|
|
self.context = context
|
|
|
|
|
|
|
|
self.layers = {}
|
|
|
|
self.ignore_structures = {}
|
|
|
|
|
|
|
|
self.top_cell = top_cell
|
|
|
|
if top_cell == "":
|
|
|
|
self.top_cell = None
|
|
|
|
|
|
|
|
if self.top_cell:
|
|
|
|
self.top_cell = self.top_cell.strip()
|
|
|
|
return
|
2019-07-02 20:05:01 +02:00
|
|
|
|
2019-07-03 13:53:25 +02:00
|
|
|
for sname in ignore_structures:
|
|
|
|
if sname in lib.structures:
|
|
|
|
self.ignore_structures[sname] = True
|
2019-07-02 20:05:01 +02:00
|
|
|
|
2019-07-03 13:53:25 +02:00
|
|
|
|
|
|
|
def new_empty(self, name):
|
|
|
|
obj = bpy.data.objects.new(name, None)
|
|
|
|
self.link(obj)
|
|
|
|
return obj
|
2019-07-02 20:05:01 +02:00
|
|
|
|
2019-07-03 13:53:25 +02:00
|
|
|
def link(self, obj):
|
|
|
|
self.context.scene.objects.link(obj)
|
2019-07-02 20:05:01 +02:00
|
|
|
|
2019-07-03 14:14:30 +02:00
|
|
|
def build_object(self, structure, progress=None):
|
2019-07-03 13:53:25 +02:00
|
|
|
# is the object already existent => return new instance
|
|
|
|
if structure.name in bpy.data.objects:
|
|
|
|
return self.new_instance(bpy.data.objects[structure.name])
|
2019-07-02 20:05:01 +02:00
|
|
|
|
2019-07-03 14:14:30 +02:00
|
|
|
if progress:
|
|
|
|
progress.total += 1 + len(structure.references)
|
|
|
|
|
2019-07-03 13:53:25 +02:00
|
|
|
verts = {}
|
|
|
|
faces = {}
|
2019-07-02 20:05:01 +02:00
|
|
|
|
2019-07-03 13:53:25 +02:00
|
|
|
# create control empty
|
|
|
|
root = self.new_empty(structure.name)
|
2019-07-02 20:05:01 +02:00
|
|
|
|
2019-07-03 13:53:25 +02:00
|
|
|
# reset name in case blender
|
|
|
|
# changes the object name
|
|
|
|
# (e.g. due to duplicates)
|
|
|
|
structure.name = root.name
|
|
|
|
|
|
|
|
#print("structure name: {}".format(structure.name))
|
|
|
|
|
|
|
|
# add all geometry
|
|
|
|
for element in structure.elements:
|
|
|
|
if isinstance(element, gds.Boundary):
|
|
|
|
if element.layer not in faces:
|
|
|
|
verts[element.layer] = []
|
|
|
|
faces[element.layer] = []
|
2019-07-02 20:05:01 +02:00
|
|
|
|
2019-07-03 13:53:25 +02:00
|
|
|
indices = []
|
|
|
|
for point in element.points[:-1]:
|
|
|
|
pos = self.lib.normalize_coord(point)
|
|
|
|
verts[element.layer].append((pos[0], pos[1], 0))
|
2019-07-02 20:05:01 +02:00
|
|
|
|
2019-07-03 13:53:25 +02:00
|
|
|
indices.append(len(verts[element.layer])-1)
|
2019-07-02 20:05:01 +02:00
|
|
|
|
2019-07-03 13:53:25 +02:00
|
|
|
faces[element.layer].append(tuple(indices))
|
2019-07-02 20:05:01 +02:00
|
|
|
|
2019-07-03 13:53:25 +02:00
|
|
|
# build structure mesh objects
|
|
|
|
# create mesh object for each layer
|
|
|
|
for layer in verts.keys():
|
|
|
|
if layer not in self.layers:
|
|
|
|
self.layers[layer] = SceneInserter.Layer(self.context, layer)
|
|
|
|
|
|
|
|
meshname = "{}[{}]".format(structure.name, layer)
|
|
|
|
|
|
|
|
mesh = bpy.data.meshes.new(meshname)
|
|
|
|
mesh.from_pydata(verts[layer], [], faces[layer])
|
|
|
|
mesh.update()
|
|
|
|
|
|
|
|
obj = bpy.data.objects.new(meshname, mesh)
|
|
|
|
self.link(obj)
|
|
|
|
self.layers[layer].link(obj)
|
|
|
|
obj.parent = root
|
|
|
|
|
2019-07-03 14:14:30 +02:00
|
|
|
if progress:
|
|
|
|
progress.inc()
|
|
|
|
|
2019-07-03 13:53:25 +02:00
|
|
|
# handle all references
|
|
|
|
# build tree depth first
|
|
|
|
for reference in structure.references:
|
2019-07-03 14:14:30 +02:00
|
|
|
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)
|
2019-07-03 13:53:25 +02:00
|
|
|
|
|
|
|
if hasattr(reference, "size"):
|
|
|
|
def add(a,b):
|
|
|
|
return (a[0] + b[0], a[1] + b[1])
|
|
|
|
def inv(a):
|
|
|
|
return (-a[0], -a[1])
|
|
|
|
|
|
|
|
def sub(a, b):
|
|
|
|
return add(inv(a), b)
|
|
|
|
def scale(a, f):
|
|
|
|
return (a[0] * f, a[1] * f)
|
|
|
|
|
|
|
|
vx = sub(reference.bounds[0], reference.position)
|
|
|
|
vy = sub(reference.bounds[1], reference.position)
|
|
|
|
|
|
|
|
xspace = 1.0/reference.size[0]
|
|
|
|
yspace = 1.0/reference.size[1]
|
|
|
|
|
|
|
|
master = obj
|
|
|
|
obj = self.new_empty(reference.structure.name)
|
|
|
|
master.parent = obj
|
|
|
|
master.location = (0,0,0)
|
|
|
|
|
|
|
|
# this is an aref, create object array
|
|
|
|
for i in range(0, reference.size[0]):
|
|
|
|
for j in range(0, reference.size[1]):
|
|
|
|
if i == 0 and j == 0:
|
|
|
|
continue
|
|
|
|
|
|
|
|
# calc new pos
|
|
|
|
iobj = self.new_instance(master)
|
|
|
|
|
|
|
|
pos = add(scale(vy, j*yspace), scale(vx, i*xspace))
|
|
|
|
loc = self.lib.normalize_coord(pos)
|
|
|
|
|
|
|
|
iobj.location = (loc[0], loc[1], 0)
|
|
|
|
iobj.hide = True
|
|
|
|
else:
|
|
|
|
# this is a single reference
|
|
|
|
# just copy the object
|
|
|
|
pass
|
|
|
|
|
|
|
|
# use single arrow for references
|
|
|
|
obj.empty_draw_type = "SINGLE_ARROW"
|
|
|
|
|
|
|
|
# reparent new instance
|
|
|
|
obj.parent = root
|
|
|
|
|
|
|
|
pos = self.lib.normalize_coord(reference.position)
|
|
|
|
obj.location = (pos[0], pos[1], 0)
|
|
|
|
|
|
|
|
# apply transformations
|
|
|
|
trans = reference.transformation
|
|
|
|
obj.rotation_euler.z = math.radians(trans.rotation)
|
|
|
|
obj.scale = (trans.zoom, trans.zoom, 1)
|
|
|
|
|
2019-07-03 14:14:30 +02:00
|
|
|
if progress:
|
|
|
|
progress.inc()
|
|
|
|
|
2019-07-03 13:53:25 +02:00
|
|
|
return root
|
|
|
|
|
|
|
|
def new_instance(self, obj):
|
|
|
|
root = obj.copy()
|
|
|
|
self.link(root)
|
|
|
|
|
|
|
|
for child in obj.children:
|
|
|
|
childObj = child.copy()
|
|
|
|
self.link(childObj)
|
|
|
|
|
|
|
|
childObj.parent = root
|
|
|
|
|
|
|
|
# relink copy to groups
|
|
|
|
for group in child.users_group:
|
|
|
|
group.objects.link(childObj)
|
|
|
|
|
|
|
|
return root
|
2019-07-02 20:05:01 +02:00
|
|
|
|
2019-07-03 13:53:25 +02:00
|
|
|
|
|
|
|
def __call__(self, progress=None):
|
|
|
|
class ProgressTracker:
|
2019-07-03 14:14:30 +02:00
|
|
|
def __init__(self, callback, total=0):
|
2019-07-03 13:53:25 +02:00
|
|
|
self.total = total
|
|
|
|
self.current = 0
|
|
|
|
self.callback = callback
|
|
|
|
|
|
|
|
def inc(self):
|
|
|
|
if self.callback:
|
|
|
|
self.current += 1
|
|
|
|
self.callback(self)
|
|
|
|
|
|
|
|
|
|
|
|
# build one cell only
|
|
|
|
if self.top_cell:
|
|
|
|
if self.top_cell not in self.lib.structures:
|
|
|
|
return None
|
2019-07-03 14:14:30 +02:00
|
|
|
s = self.lib.structures[self.top_cell]
|
|
|
|
|
|
|
|
progressTracker = ProgressTracker(progress, 1+len(s.references))
|
|
|
|
self.build_object(s,progress=progressTracker)
|
|
|
|
|
2019-07-03 13:53:25 +02:00
|
|
|
return True
|
|
|
|
|
2019-07-03 14:14:30 +02:00
|
|
|
progressTracker = ProgressTracker(progress)
|
|
|
|
progressTracker.total = len(self.lib.structures)
|
|
|
|
|
|
|
|
# self.link_all_refs(progressTracker)
|
|
|
|
|
2019-07-03 13:53:25 +02:00
|
|
|
# build all structures first
|
|
|
|
# since references could reference unknown objects otherwise
|
|
|
|
for name, value in self.lib.structures.items():
|
|
|
|
progressTracker.inc()
|
|
|
|
if name in self.ignore_structures:
|
|
|
|
continue
|
|
|
|
|
2019-07-03 14:14:30 +02:00
|
|
|
self.build_object(value, progress=progressTracker)
|
|
|
|
|
2019-07-03 13:53:25 +02:00
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
import math
|
2019-07-02 19:39:34 +02:00
|
|
|
|
2019-07-02 14:44:42 +02:00
|
|
|
def load(context,
|
|
|
|
filepath,
|
|
|
|
*,
|
2019-07-03 13:53:25 +02:00
|
|
|
top_cell_name=""
|
2019-07-02 14:44:42 +02:00
|
|
|
):
|
|
|
|
|
|
|
|
path = pathlib.Path(filepath)
|
2019-07-02 19:39:34 +02:00
|
|
|
lib = None
|
|
|
|
print(lib)
|
2019-07-02 14:44:42 +02:00
|
|
|
|
|
|
|
with ProgressReport(context.window_manager) as progress:
|
2019-07-02 19:39:34 +02:00
|
|
|
progress.enter_substeps(2, "Importing GDS %r..." % filepath)
|
|
|
|
prog = Progressor(context.window_manager)
|
2019-07-02 14:44:42 +02:00
|
|
|
with open(filepath, "rb") as f:
|
2019-07-02 19:39:34 +02:00
|
|
|
lib = gds.parse_file(f, progress_func=prog)
|
2019-07-02 14:44:42 +02:00
|
|
|
|
2019-07-03 13:53:25 +02:00
|
|
|
progress.step("linking references")
|
2019-07-02 19:39:34 +02:00
|
|
|
prog.end()
|
2019-07-02 14:44:42 +02:00
|
|
|
|
2019-07-03 13:53:25 +02:00
|
|
|
if not SceneInserter(context=context, lib=lib, top_cell=top_cell_name)(prog):
|
|
|
|
return {"CANCELLED"}
|
2019-07-02 14:44:42 +02:00
|
|
|
|
2019-07-03 13:53:25 +02:00
|
|
|
progress.leave_substeps("Done")
|
|
|
|
prog.end()
|
2019-07-02 20:05:01 +02:00
|
|
|
|
2019-07-03 13:53:25 +02:00
|
|
|
context.scene.update()
|
2019-07-02 19:39:34 +02:00
|
|
|
|
2019-07-02 14:44:42 +02:00
|
|
|
|
|
|
|
return {"FINISHED"}
|
|
|
|
|