Home › Forums › Nazca › Questions and Answers › Merge two structures › Reply To: Merge two structures
27 November 2021 at 13:19
#6645
Keymaster
In the case of merging polygons in a cell, the cell_iter() can help out, like described in the example below. The merge_cell_polygons function returns a new cell with the merged result, although the pyclipper may not handle all cases as desired for GDS.
import nazca as nd
from collections import defaultdict
def merge_cell_polygons(cell):
"""Flatten a cell and merge polygons per layer.
Args:
cell (Cell): cell to flatten and merge polygon of.
Returns:
Cell: flattened cell with merged polygons per layer.
"""
layerpgons = defaultdict(list)
for P in nd.cell_iter(cell, flat=True):
if P.cell_start:
for pgon, xy, bbox in P.iters['polygon']:
layerpgons[pgon.layer].append(xy)
with nd.Cell(name=f"{cell.cell_name}_merged") as C:
for layer, pgons in layerpgons.items():
merged = nd.clipper.merge_polygons(pgons)
for pgon in merged:
nd.Polygon(points=pgon, layer=layer).put(0)
return C
with nd.Cell('test') as my_cell:
# add content
merged_cell = merge_cell_polygons(my_cell)
merged_cell.put()
Ronald