blender-gdsimporter/import_gds.py

86 lines
2.5 KiB
Python
Raw Normal View History

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
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)
with ProgressReport(context.window_manager) as progress:
progress.enter_substeps(1, "Importing GDS %r..." % filepath)
with open(filepath, "rb") as f:
lib = gds.parse_file(f)
progress.step("Done, linking references")
lib.link_refs()
progress.step("Done, building meshes")
layergroups = {}
for name, value in lib.structures.items():
if not len(value.elements):
continue
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))
for layer in verts.keys():
if layer not in layergroups:
layergroups[layer] = bpy.data.groups.new("layer{}".format(layer))
meshname = "{}[{}]".format(name.replace("\x00", ""), layer)
print(layer, meshname)
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"}