blender-gdsimporter/import_gds.py

133 lines
3.6 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 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():
if not len(value.elements):
continue
print("structure name: {}".format(name))
verts = {}
faces = {}
root = bpy.data.objects.new(name, None)
context.scene.objects.link(root)
for element in value.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]:
verts[element.layer].append((point[0] * lib.units_per_dbunit, point[1]*lib.units_per_dbunit, 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
progress.leave_substeps("Done")
return {"FINISHED"}