Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_writer_gds(tmpdir):
lib = gdspy.GdsLibrary()
c1 = gdspy.Cell("gw_rw_gds_1", True)
c1.add(gdspy.Rectangle((0, -1), (1, 2), 2, 4))
c1.add(gdspy.Label("label", (1, -1), "w", 45, 1.5, True, 5, 6))
c2 = gdspy.Cell("gw_rw_gds_2", True)
c2.add(gdspy.Round((0, 0), 1, number_of_points=32, max_points=20))
c3 = gdspy.Cell("gw_rw_gds_3", True)
c3.add(gdspy.CellReference(c1, (0, 1), -90, 2, True))
c4 = gdspy.Cell("gw_rw_gds_4", True)
c4.add(gdspy.CellArray(c2, 2, 3, (1, 4), (-1, -2), 180, 0.5, True))
lib.add((c1, c2, c3, c4))
fname1 = str(tmpdir.join("test1.gds"))
writer1 = gdspy.GdsWriter(fname1, name="lib", unit=2e-3, precision=1e-5)
for c in lib.cell_dict.values():
writer1.write_cell(c)
writer1.close()
lib1 = gdspy.GdsLibrary(unit=1e-3)
lib1.read_gds(
fname1,
units="convert",
rename={"gw_rw_gds_1": "1"},
layers={2: 4},
datatypes={4: 2},
texttypes={6: 7},
)
def test_empty():
name = "ca_empty"
c = gdspy.Cell(name)
ref = gdspy.CellArray(name, 2, 3, (3, 2), (1, -1), 90, 2, True)
ref.translate(-1, 1)
assert ref.area() == 0
assert ref.area(True) == dict()
assert ref.get_bounding_box() is None
assert ref.get_polygons() == []
assert ref.get_polygons(True) == dict()
assert ref.origin[0] == ref.origin[1] == 0
def test_noreference():
name = "ca_noreference"
with pytest.warns(UserWarning):
ref = gdspy.CellArray(name, 2, 3, (3, 2), (1, -1), 90, 2, True)
ref.translate(-1, 1)
assert ref.ref_cell == name
assert ref.area() == 0
assert ref.area(True) == dict()
assert ref.get_bounding_box() is None
assert ref.get_polygons() == []
assert ref.get_polygons(True) == dict()
assert ref.origin[0] == ref.origin[1] == 0
def test_notempty():
name = "ca_notempty"
c = gdspy.Cell(name)
ref = gdspy.CellArray(name, 2, 3, (3, 2), (1, -1), 90, 2, True)
ref.translate(-1, 1)
c.add(gdspy.Rectangle((0, 0), (1, 2), 2, 3))
assert ref.area() == 48
assert ref.area(True) == {(2, 3): 48}
err = numpy.array(((0, 0), (8, 5))) - ref.get_bounding_box()
assert numpy.max(numpy.abs(err)) < 1e-15
assert ref.origin[0] == ref.origin[1] == 0
r = gdspy.boolean(
[gdspy.Rectangle((0, 0), (8, 2)), gdspy.Rectangle((0, 3), (8, 5))],
ref.get_polygons(),
"xor",
1e-6,
0,
)
assert r is None
d = ref.get_polygons(True)
def tree():
c = [gdspy.Cell("tree_" + unique()) for _ in range(8)]
lib = gdspy.GdsLibrary()
lib.add(c)
c[0].add(gdspy.CellReference(c[1]))
c[0].add(gdspy.CellReference(c[3]))
c[1].add(gdspy.CellReference(c[2]))
c[1].add(gdspy.CellArray(c[2], 2, 1, (0, 0)))
c[1].add(gdspy.CellArray(c[3], 2, 1, (0, 0)))
c[4].add(gdspy.CellReference(c[3]))
c[6].add(gdspy.CellArray(c[5], 2, 1, (0, 0)))
return lib, c
def get_gdspy_cell(self, executor=None):
import gdspy
if self.cell_gdspy is None:
self.cell_gdspy = gdspy.Cell(self.name)
for sub_cell in self.cells:
angle = np.rad2deg(sub_cell['angle']) if sub_cell['angle'] is not None else None
if sub_cell['columns'] == 1 and sub_cell['rows'] == 1 and not sub_cell['spacing']:
self.cell_gdspy.add(
gdspy.CellReference(sub_cell['cell'].get_gdspy_cell(executor), origin=sub_cell['origin'],
rotation=angle, magnification=sub_cell['magnification'],
x_reflection=sub_cell['x_reflection']))
else:
self.cell_gdspy.add(
gdspy.CellArray(sub_cell['cell'].get_gdspy_cell(executor), origin=sub_cell['origin'],
rotation=angle, magnification=sub_cell['magnification'],
x_reflection=sub_cell['x_reflection'], columns=sub_cell['columns'],
rows=sub_cell['rows'], spacing=sub_cell['spacing']))
for layer, geometries in self.layer_dict.items():
for geometry in geometries:
if executor:
executor.submit(convert_to_layout_objs, geometry, layer, library='gdspy') \
.add_done_callback(lambda future: self.cell_gdspy.add(future.result()))
else:
self.cell_gdspy.add(convert_to_layout_objs(geometry, layer, library='gdspy'))
return self.cell_gdspy
path.segment((io_gap - 2 * bend_radius, 0), relative=True)
path.turn(bend_radius, "l")
path.segment((0, 300 - bend_radius + wg_gap * i), relative=True)
c.add(path)
dx = width / 2 + gap
c.add(
gdspy.boolean(
gdspy.boolean(
ring_bus, gdspy.copy(ring_margin, dx, 300), "or", precision=1e-4
),
gdspy.copy(ring_hole, dx, 300),
"not",
precision=1e-4,
).translate(input_gap * i, 0)
)
c.add(gdspy.CellArray(taper, len(ring_gaps), 1, (input_gap, 0), (0, 0)))
c.add(
gdspy.CellArray(
grat, len(ring_gaps), 1, (input_gap, 0), (io_gap, 900 + taper_len)
)
)
# Save to a gds file and check out the output
gdspy.write_gds("photonics.gds")
gdspy.LayoutViewer()
# References
# Create a cell with a component that is used repeatedly
contact = gdspy.Cell("CONTACT")
contact.add([p1, p2, p3, p4])
# Create a cell with the complete device
device = gdspy.Cell("DEVICE")
device.add(cutout)
# Add 2 references to the component changing size and orientation
ref1 = gdspy.CellReference(contact, (3.5, 1), magnification=0.25)
ref2 = gdspy.CellReference(contact, (1, 3.5), magnification=0.25, rotation=90)
device.add([ref1, ref2])
# The final layout has several repetitions of the complete device
main = gdspy.Cell("MAIN")
main.add(gdspy.CellArray(device, 3, 2, (6, 7)))
draw(main, "references")
# Polygonal-Only Paths
# Start a path at (0, 0) with width 1
path1 = gdspy.Path(1, (0, 0))
# Add a segment to the path goin in the '+y' direction
path1.segment(4, "+y")
# Further segments or turns will folow the current path direction
# to ensure continuity
path1.turn(2, "r")
path1.segment(1)
path1.turn(3, "rr")
draw(gdspy.Cell("polygonal-only_paths").add(path1))