def assert_same_shape(sh1, sh2): precision = 3e-3 d12 = gdstk.boolean(sh1, gdstk.offset(sh2, precision, use_union=True), "not", precision, 255, 102) d21 = gdstk.boolean(sh2, gdstk.offset(sh1, precision, use_union=True), "not", precision, 255, 201) if len(d12) > 0 or len(d21) > 0: lib = gdstk.Library("Debug") cell = lib.new_cell("Debug") if hasattr(sh1, "__iter__"): cell.add(*sh1) else: cell.add(sh1) if hasattr(sh2, "__iter__"): cell.add(*sh2) else: cell.add(sh2) if len(d12) > 0: cell.add(*d12) if len(d21) > 0: cell.add(*d21) outfile = pathlib.Path(tempfile.gettempdir()) / "debug.gds" lib.write_gds(outfile) raise AssertionError("Shapes don't match. Debug library saved as %s" % outfile)
def offset_image(): text = gdstk.text("#A", 10, (0, 0), datatype=1) circle = gdstk.ellipse( (5, 11), 5, initial_angle=0, final_angle=numpy.pi, datatype=1 ) path = gdstk.FlexPath([(0, -1), (5, -10), (10, -1)], 1, datatype=1) dilated = gdstk.offset(text + [circle, path], 0.4) eroded = gdstk.offset(text + [circle, path], -0.4, use_union=True, layer=1) return gdstk.Cell("offset").add(*text, circle, path, *dilated, *eroded)
def bench_gdstk(output=None): poly = gdstk.regular_polygon((0, 0), 1.5, 6, layer=1) orig = gdstk.Cell("OFF") orig.add(poly) ref = gdstk.Reference(orig, (-3, 5), columns=4, spacing=(2, 0)) off = gdstk.offset([poly, ref], 0.2, "bevel", layer=0) boo = gdstk.boolean(off, [poly, ref], "not", layer=2) if output: cell = gdstk.Cell("MAIN") cell.add(ref, poly, *off, *boo) cell.write_svg(output, 50)
# and x=3, and the third ring across x=3 slices1 = gdstk.slice(ring1, -3, "x") slices2 = gdstk.slice(ring2, [-3, 3], "x") slices3 = gdstk.slice(ring3, 3, "x") slices = gdstk.Cell("SLICES") # Keep only the left side of slices1, the center part of slices2 # and the right side of slices3 slices.add(*slices1[0]) slices.add(*slices2[1]) slices.add(*slices3[1]) # draw slices.name = "slice_operation" draw(slices, path) slices.name = "SLICES" # Offset Operation rect1 = gdstk.rectangle((-4, -4), (1, 1)) rect2 = gdstk.rectangle((-1, -1), (4, 4)) # Erosion: because we set `use_union=True`, the inner boundaries have no effect outer = gdstk.offset([rect1, rect2], -0.5, use_union=True, layer=1) draw(gdstk.Cell("offset_operation").add(rect1, rect2, *outer), path) # Fillet Operation flexpath = gdstk.FlexPath([(-8, -4), (0, -4), (0, 4), (8, 4)], 4) filleted_path = flexpath.to_polygons()[0] filleted_path.fillet(1.5) draw(gdstk.Cell("fillet_operation").add(filleted_path), path)