Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
elif operation == 'and':
# If either operand is None, output layer will have no shapes on it
if polygon1 is None or polygon2 is None:
polygon_out = None
else:
polygon_out = gdspy.fast_boolean(polygon1,
self.dataprep_oversize_gdspy(polygon2, size_amount),
'and')
polygon_out = self.dataprep_cleanup_gdspy(polygon_out, self.do_cleanup)
elif operation == 'xor':
if polygon1 is None:
polygon_out = self.dataprep_oversize_gdspy(polygon2, size_amount)
else:
polygon_out = gdspy.fast_boolean(polygon1,
self.dataprep_oversize_gdspy(polygon2, size_amount),
'xor')
polygon_out = self.dataprep_cleanup_gdspy(polygon_out, self.do_cleanup)
elif operation == 'ext':
polygon_toextend = polygon1
polygon_ref = polygon2
extended_amount = size_amount
# Round the amount to extend up based on global grid size
extended_amount = self.global_grid_size * ceil(extended_amount / self.global_grid_size)
polygon_ref_sized = self.dataprep_oversize_gdspy(polygon_ref, extended_amount)
polygon_extended = self.dataprep_oversize_gdspy(polygon_toextend, extended_amount)
polygon_extra = self.dataprep_cleanup_gdspy(gdspy.fast_boolean(polygon_extended,
polygon_ref,
self.radius - self.radius_taper
) / (self.num_taper_holes - 1)
taper_period = taper_radii * ratio
x_in = startx_in + i * taper_period
y_in = starty_in
taper_list_in.append(gdspy.Round((x_in, y_in), taper_radii))
x_out = startx_out - i * taper_period
y_out = starty_out
taper_list_out.append(gdspy.Round((x_out, y_out), taper_radii))
for hole in hole_list:
nanobeam = gdspy.fast_boolean(nanobeam, hole, "xor")
for hole_in in taper_list_in:
nanobeam = gdspy.fast_boolean(nanobeam, hole_in, "xor")
for hole_out in taper_list_out:
nanobeam = gdspy.fast_boolean(nanobeam, hole_out, "xor")
self.add(nanobeam)
self.add(nanobeam_clad)
def shapely_to_gdspy_polygon(polygon_shapely: Polygon) -> gdspy.Polygon:
if not isinstance(polygon_shapely, Polygon):
raise ValueError("input must be a Shapely Polygon")
else:
ext_coord_list = list(zip(*polygon_shapely.exterior.coords.xy))
polygon_gdspy = gdspy.Polygon(ext_coord_list)
if len(polygon_shapely.interiors):
for interior in polygon_shapely.interiors:
int_coord_list = list(zip(*interior.coords.xy))
polygon_gdspy_int = gdspy.Polygon(int_coord_list)
polygon_gdspy = dataprep_cleanup_gdspy(gdspy.fast_boolean(polygon_gdspy, polygon_gdspy_int, 'not',
max_points=MAX_POINTS,
precision=GLOBAL_OPERATION_PRECISION),
do_cleanup=GLOBAL_DO_CLEANUP)
else:
pass
return polygon_gdspy
def compute_intersection(polygon_1, polygon_2):
'''
Wrapper function around the gdspy module to take as input
two polygons and return polygon_1-polygon_2
Explicit NOT operation is only performed if the bounding boxes
of the two polygons do not intersect.
'''
if check_bounding_box(polygon_1, polygon_2):
gds_poly1 = gdspy.Polygon(polygon_1, 0)
gds_poly2 = gdspy.Polygon(polygon_2, 0)
gds_poly = gdspy.fast_boolean(
gds_poly1, gds_poly2, 'not', layer=1)
if gds_poly is None:
return []
else:
return gds_poly.polygons
else:
return [polygon_1]
elif operation == 'add':
if polygon1 is None:
polygon_out = self.dataprep_oversize_gdspy(polygon2, size_amount)
else:
polygon_out = gdspy.fast_boolean(polygon1,
self.dataprep_oversize_gdspy(polygon2, size_amount),
'or')
polygon_out = self.dataprep_cleanup_gdspy(polygon_out, do_cleanup=self.do_cleanup)
elif operation == 'sub':
if polygon1 is None:
polygon_out = None
else:
# returns polygon1 - polygon2
polygon_out = gdspy.fast_boolean(polygon1,
self.dataprep_oversize_gdspy(polygon2, size_amount),
'not')
polygon_out = self.dataprep_cleanup_gdspy(polygon_out, self.do_cleanup)
elif operation == 'and':
# If either operand is None, output layer will have no shapes on it
if polygon1 is None or polygon2 is None:
polygon_out = None
else:
polygon_out = gdspy.fast_boolean(polygon1,
self.dataprep_oversize_gdspy(polygon2, size_amount),
'and')
polygon_out = self.dataprep_cleanup_gdspy(polygon_out, self.do_cleanup)
elif operation == 'xor':
if polygon1 is None:
layer=6)
path_cell.add(l1path)
# ------------------------------------------------------------------ #
# POLYGON OPERATIONS
# ------------------------------------------------------------------ #
# Boolean operations can be executed with either gdspy polygons or
# point lists). The operations are union, intersection, subtraction,
# symmetric subtracion (respectively 'or', 'and', 'not', 'xor').
oper_cell = gdspy.Cell('OPERATIONS')
# Here we subtract the previously created spiral from a rectangle with
# the 'not' operation.
oper_cell.add(
gdspy.fast_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.fast_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))
ratio = self.period / self.radius
taper_radii = self.radius_taper + i * (
self.radius - self.radius_taper
) / (self.num_taper_holes - 1)
taper_period = taper_radii * ratio
x_in = startx_in + i * taper_period
y_in = starty_in
taper_list_in.append(gdspy.Round((x_in, y_in), taper_radii))
x_out = startx_out - i * taper_period
y_out = starty_out
taper_list_out.append(gdspy.Round((x_out, y_out), taper_radii))
for hole in hole_list:
nanobeam = gdspy.fast_boolean(nanobeam, hole, "xor")
for hole_in in taper_list_in:
nanobeam = gdspy.fast_boolean(nanobeam, hole_in, "xor")
for hole_out in taper_list_out:
nanobeam = gdspy.fast_boolean(nanobeam, hole_out, "xor")
self.add(nanobeam)
self.add(nanobeam_clad)
# Rough add the shape
# Try to find the polygon in the cache list
if polygon_key in self.polygon_cache:
polygon_rough_sized = self.polygon_cache[polygon_key]
else:
# Need to compute the roughadd shape and update the cache
polygon_rough_sized = self.dataprep_roughsize_gdspy(polygon2,
size_amount=size_amount,
do_manh=do_manh_in_rad)
self.polygon_cache[polygon_key] = polygon_rough_sized
if polygon1 is None:
polygon_out = polygon_rough_sized
else:
polygon_out = gdspy.fast_boolean(polygon1, polygon_rough_sized, 'or')
polygon_out = self.dataprep_cleanup_gdspy(polygon_out, do_cleanup=self.do_cleanup)
elif operation == 'add':
if polygon1 is None:
polygon_out = self.dataprep_oversize_gdspy(polygon2, size_amount)
else:
polygon_out = gdspy.fast_boolean(polygon1,
self.dataprep_oversize_gdspy(polygon2, size_amount),
'or')
polygon_out = self.dataprep_cleanup_gdspy(polygon_out, do_cleanup=self.do_cleanup)
elif operation == 'sub':
if polygon1 is None:
polygon_out = None
else:
# returns polygon1 - polygon2
taper_list_out = []
for i in range(int(self.num_taper_holes)):
ratio = self.period / self.radius
taper_radii = self.radius_taper + i * (
self.radius - self.radius_taper
) / (self.num_taper_holes - 1)
taper_period = taper_radii * ratio
x_in = startx_in + i * taper_period
y_in = starty_in
taper_list_in.append(gdspy.Round((x_in, y_in), taper_radii))
x_out = startx_out - i * taper_period
y_out = starty_out
taper_list_out.append(gdspy.Round((x_out, y_out), taper_radii))
for hole in hole_list:
nanobeam = gdspy.fast_boolean(nanobeam, hole, "xor")
for hole_in in taper_list_in:
nanobeam = gdspy.fast_boolean(nanobeam, hole_in, "xor")
for hole_out in taper_list_out:
nanobeam = gdspy.fast_boolean(nanobeam, hole_out, "xor")
self.add(nanobeam)
self.add(nanobeam_clad)
elif operation == 'sub':
if polygon1 is None:
polygon_out = None
else:
# returns polygon1 - polygon2
polygon_out = gdspy.fast_boolean(polygon1,
self.dataprep_oversize_gdspy(polygon2, size_amount),
'not')
polygon_out = self.dataprep_cleanup_gdspy(polygon_out, self.do_cleanup)
elif operation == 'and':
# If either operand is None, output layer will have no shapes on it
if polygon1 is None or polygon2 is None:
polygon_out = None
else:
polygon_out = gdspy.fast_boolean(polygon1,
self.dataprep_oversize_gdspy(polygon2, size_amount),
'and')
polygon_out = self.dataprep_cleanup_gdspy(polygon_out, self.do_cleanup)
elif operation == 'xor':
if polygon1 is None:
polygon_out = self.dataprep_oversize_gdspy(polygon2, size_amount)
else:
polygon_out = gdspy.fast_boolean(polygon1,
self.dataprep_oversize_gdspy(polygon2, size_amount),
'xor')
polygon_out = self.dataprep_cleanup_gdspy(polygon_out, self.do_cleanup)
elif operation == 'ext':
polygon_toextend = polygon1
polygon_ref = polygon2