Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def assertsame(c1, c2, tolerance=1e-6):
d1 = c1.get_polygons(by_spec=True)
d2 = c2.get_polygons(by_spec=True)
for key in d1:
assert key in d2
result = gdspy.boolean(
d1[key], d2[key], "xor", precision=1e-7, layer=key[0], datatype=100
)
if result is not None:
r1 = gdspy.boolean(
d1[key],
gdspy.offset(d2[key], tolerance, precision=1e-7),
"not",
precision=1e-7,
layer=key[0],
datatype=99,
)
r2 = gdspy.boolean(
d2[key],
gdspy.offset(d1[key], tolerance, precision=1e-7),
"not",
precision=1e-7,
layer=key[0],
datatype=99,
)
# if not (r1 is None and r2 is None):
# c1.add(result)
# c2.add(result)
def polyop_manh(polygon_gdspy, # type: Union[gdspy.Polygon, gdspy.PolygonSet]
manh_grid_size, # type: float
do_manh, # type: bool
):
if do_manh:
manh_type = 'inc'
else:
manh_type = 'non'
if isinstance(polygon_gdspy, gdspy.Polygon):
coord_list = manh_skill(polygon_gdspy.points, manh_grid_size, manh_type)
polygon_out = gdspy.offset(gdspy.Polygon(coord_list),
0, tolerance=10, max_points=MAX_POINTS, join_first=True)
elif isinstance(polygon_gdspy, gdspy.PolygonSet):
coord_list = manh_skill(polygon_gdspy.polygons[0], manh_grid_size, manh_type)
polygon_out = gdspy.offset(gdspy.Polygon(coord_list),
0, tolerance=10, max_points=MAX_POINTS, join_first=True)
for poly in polygon_gdspy.polygons:
coord_list = manh_skill(poly, manh_grid_size, manh_type)
polygon_append = gdspy.offset(gdspy.Polygon(coord_list),
0, tolerance=10, max_points=MAX_POINTS, join_first=True)
polygon_out = gdspy.offset(gdspy.fast_boolean(polygon_out, polygon_append, 'or'),
0, tolerance=10, max_points=MAX_POINTS, join_first=True)
else:
raise ValueError('polygon_gdspy should be either a Polygon or PolygonSet')
return polygon_out
# 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, "slice_operation")
# Offset Operation
rect1 = gdspy.Rectangle((-4, -4), (1, 1))
rect2 = gdspy.Rectangle((-1, -1), (4, 4))
# Offset both polygons
# Because we join them first, a single polygon is created.
outer = gdspy.offset([rect1, rect2], 0.5, join_first=True, layer=1)
draw(gdspy.Cell("offset_operation").add([outer, rect1, rect2]))
# Fillet Operation
multi_path = gdspy.Path(2, (-3, -2))
multi_path.segment(4, "+x")
multi_path.turn(2, "l").turn(2, "r")
multi_path.segment(4)
# Create a copy with joined polygons and no fracturing
joined = gdspy.boolean(multi_path, None, "or", max_points=0)
joined.translate(0, -5)
# Fillet applied to each polygon in the path
multi_path.fillet(0.5)
# Fillet applied to the joined copy
# Here we subtract the previously created spiral from a rectangle with
# the 'not' operation.
oper_cell.add(gdspy.boolean(gdspy.Rectangle((10, -4), (17, 4)), path3, 'not', layer=1))
# Polygon offset (inset and outset) can be used, for instance, to
# define safety margins around shapes.
spec = {'layer': 7}
path4 = gdspy.Path(0.5, (21, -5)).segment(3, '+x', **spec).turn(4, 'r', **spec).turn(4, 'rr', **spec).segment(3, **spec)
oper_cell.add(path4)
# Merge all parts into a single polygon.
merged = gdspy.boolean(path4, None, 'or', max_points=0)
# Offset the path shape by 0.5 and add it to the cell.
oper_cell.add(gdspy.offset(merged, 1, layer=8))
# ------------------------------------------------------------------ #
# SLICING POLYGONS
# ------------------------------------------------------------------ #
# If there is the need to cut a polygon or set of polygons, it's better
# to use the slice function than set up a boolean operation, since it
# runs much faster. Slices are multiple cuts perpendicular to an axis.
slice_cell = gdspy.Cell('SLICE')
original = gdspy.Round((0, 0), 10, inner_radius=5)
# Slice the original ring along x = -7 and x = 7.
result = gdspy.slice(original, [-7, 7], 0, layer=1)
# The result is a tuple of polygon sets, one for each slice. To keep
# add the region betwen our 2 cuts, we chose result[1].
The amount to shrink the polygon
do_cleanup : bool
Optional parameter to force whether point cleanup should occur.
Returns
-------
polygon_undersized : Union[gdspy.Polygon, gdspy.PolygonSet, None]
The undersized polygon
"""
if polygon is None:
return None
else:
if offset < 0:
print('Warning: offset = %f < 0 indicates you are doing oversize')
polygon_undersized = gdspy.offset(polygon, -offset, max_points=MAX_SIZE, join_first=True,
join='miter',
tolerance=self.offset_tolerance,
precision=self.global_operation_precision)
polygon_undersized = self.dataprep_cleanup_gdspy(
polygon_undersized,
do_cleanup=do_cleanup if do_cleanup is not None else self.do_cleanup
)
return polygon_undersized
----------
polygon : Union[gdspy.Polygon, gdspy.PolygonSet]
The polygon to clean
do_cleanup : bool
True to perform the cleanup. False will return input polygon unchanged
Returns
-------
clean_polygon : Union[gdspy.Polygon, gdspy.PolygonSet]
The cleaned up polygon
"""
if do_cleanup:
if polygon is None:
clean_polygon = None
elif isinstance(polygon, (gdspy.Polygon, gdspy.PolygonSet)):
clean_polygon = gdspy.offset(
polygons=polygon,
distance=0,
tolerance=self.offset_tolerance,
max_points=MAX_SIZE,
join_first=True,
precision=self.global_clean_up_grid_size,
)
clean_coords = []
if isinstance(clean_polygon, gdspy.Polygon):
clean_coords = self.global_grid_size * np.round(clean_polygon.points / self.global_grid_size, 0)
clean_polygon = gdspy.Polygon(points=clean_coords)
elif isinstance(clean_polygon, gdspy.PolygonSet):
for poly in clean_polygon.polygons:
clean_coords.append(self.global_grid_size * np.round(poly / self.global_grid_size, 0))
clean_polygon = gdspy.PolygonSet(polygons=clean_coords)
else:
manh_type = 'non'
if isinstance(polygon_gdspy, gdspy.Polygon):
coord_list = manh_skill(polygon_gdspy.points, manh_grid_size, manh_type)
polygon_out = gdspy.offset(gdspy.Polygon(coord_list),
0, tolerance=10, max_points=MAX_POINTS, join_first=True)
elif isinstance(polygon_gdspy, gdspy.PolygonSet):
coord_list = manh_skill(polygon_gdspy.polygons[0], manh_grid_size, manh_type)
polygon_out = gdspy.offset(gdspy.Polygon(coord_list),
0, tolerance=10, max_points=MAX_POINTS, join_first=True)
for poly in polygon_gdspy.polygons:
coord_list = manh_skill(poly, manh_grid_size, manh_type)
polygon_append = gdspy.offset(gdspy.Polygon(coord_list),
0, tolerance=10, max_points=MAX_POINTS, join_first=True)
polygon_out = gdspy.offset(gdspy.fast_boolean(polygon_out, polygon_append, 'or'),
0, tolerance=10, max_points=MAX_POINTS, join_first=True)
else:
raise ValueError('polygon_gdspy should be either a Polygon or PolygonSet')
return polygon_out
offset : float
The amount to grow the polygon
do_cleanup : bool
Optional parameter to force whether point cleanup should occur.
Returns
-------
polygon_oversized : Union[gdspy.Polygon, gdspy.PolygonSet, None]
The oversized polygon
"""
if polygon is None:
return None
else:
if offset < 0:
print('Warning: offset = %f < 0 indicates you are doing undersize')
polygon_oversized = gdspy.offset(polygon, offset, max_points=MAX_SIZE, join_first=True,
join='miter',
tolerance=self.offset_tolerance,
precision=self.global_operation_precision)
polygon_oversized = self.dataprep_cleanup_gdspy(
polygon_oversized,
do_cleanup=do_cleanup if do_cleanup is not None else self.do_cleanup
)
return polygon_oversized