159 lines
4.2 KiB
Python
159 lines
4.2 KiB
Python
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
|
|
|
|
class Progressor:
|
|
def __init__(self, wm, precision = 0.001):
|
|
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()
|
|
|
|
self.wm.progress_begin(0, t)
|
|
self.report = t
|
|
|
|
@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
|
|
self.wm.progress_end()
|
|
|
|
def __call__(self, update):
|
|
self.total = update.total
|
|
self.current = update.current
|
|
|
|
def build_object(context, layergroups, lib, struct):
|
|
name = struct.name
|
|
|
|
verts = {}
|
|
faces = {}
|
|
|
|
print("structure name: {}".format(name))
|
|
|
|
root = bpy.data.objects.new(name, None)
|
|
context.scene.objects.link(root)
|
|
|
|
for element in struct.elements:
|
|
# handle all elements
|
|
if isinstance(element, gds.Boundary):
|
|
if element.layer not in faces:
|
|
verts[element.layer] = []
|
|
faces[element.layer] = []
|
|
|
|
indices = []
|
|
for point in element.points[:-1]:
|
|
pos = lib.normalize_coord(point)
|
|
verts[element.layer].append((pos[0], pos[1], 0))
|
|
|
|
indices.append(len(verts[element.layer])-1)
|
|
|
|
faces[element.layer].append(tuple(indices))
|
|
|
|
# build structure object
|
|
for layer in verts.keys():
|
|
if layer not in layergroups:
|
|
layergroups[layer] = bpy.data.groups.new("layer{}".format(layer))
|
|
|
|
meshname = "{}[{}]".format(name, layer)
|
|
|
|
mesh = bpy.data.meshes.new(meshname)
|
|
mesh.from_pydata(verts[layer], [], faces[layer])
|
|
mesh.update()
|
|
|
|
obj = bpy.data.objects.new(meshname, mesh)
|
|
|
|
context.scene.objects.link(obj)
|
|
layergroups[layer].objects.link(obj)
|
|
obj.parent = root
|
|
|
|
return root
|
|
|
|
def load(context,
|
|
filepath,
|
|
*,
|
|
global_clamp_size=0.0,
|
|
use_smooth_groups=True,
|
|
use_edges=True,
|
|
use_split_objects=True,
|
|
use_split_groups=True,
|
|
use_image_search=True,
|
|
use_groups_as_vgroups=False,
|
|
use_cycles=True,
|
|
relpath=None,
|
|
global_matrix=None
|
|
):
|
|
|
|
path = pathlib.Path(filepath)
|
|
lib = None
|
|
print(lib)
|
|
|
|
with ProgressReport(context.window_manager) as progress:
|
|
progress.enter_substeps(2, "Importing GDS %r..." % filepath)
|
|
prog = Progressor(context.window_manager)
|
|
with open(filepath, "rb") as f:
|
|
lib = gds.parse_file(f, progress_func=prog)
|
|
|
|
progress.step("Done, linking references")
|
|
lib.link_refs(prog)
|
|
progress.step("Done, building meshes")
|
|
prog.end()
|
|
|
|
layergroups = {}
|
|
|
|
for name, value in lib.structures.items():
|
|
build_object(context, layergroups, lib, value)
|
|
|
|
# process all references
|
|
for name, value in lib.structures.items():
|
|
root = bpy.data.objects[name]
|
|
|
|
for reference in value.references:
|
|
sname = reference.structure.name
|
|
|
|
if not sname in bpy.data.objects:
|
|
print("object not build for referenced structure \"{}\"".format(sname))
|
|
obj = build_object(context, layergroups, lib, reference.structure)
|
|
else:
|
|
obj = bpy.data.objects[sname]
|
|
|
|
#create copy of object
|
|
obj = obj.copy()
|
|
|
|
# reparent new instance
|
|
obj.parent = root
|
|
pos = lib.normalize_coord(reference.position)
|
|
obj.position = (pos[0], pos[1], 0)
|
|
|
|
progress.leave_substeps("Done")
|
|
|
|
return {"FINISHED"}
|
|
|