for gds_file in gds_files:
KLAYOUT.read(gds_file)
top_cell = None
max_area = 0
for cell in KLAYOUT.each_cell():
bbox = cell.bbox()
area = bbox.width() * bbox.height()
if area > max_area:
top_cell = cell
max_area = area
@rounak1068 If you have a single top cell, you can use layout.top_cell() to get the Cell object of this cell. You don't need to know the top cell name. But it will fail, if your layout contains multiple top cells.
Comments
@EugeneZelenko, @Vincent Lin kindly look into that.
@rounak1068 Here is a related post: https://www.klayout.de/forum/discussion/comment/8701#Comment_8701
The code is somewhat older. You can write it today like this:
Matthias
In above script I got error (No overload with matching arguments in Layout.clip)
@Matthias , kindly look into it. I got error while run above code.
here is my code but not working.
import pya
input_gds = "abc.gds"
KLAYOUT = pya.Layout()
KLAYOUT.read(input_gds)
top_cell = KLAYOUT.top_cell()
clip_rect = pya.DBox(50.0, 50.0, 100, 100.0)
clip_cell = KLAYOUT.create_cell("CLIP")
top_cell.clip_into(clip_cell, clip_rect)
output_gds = "outputnew.gds"
clip_cell.write(output_gds)
@rounak1068
The code from Matthias belongs to " Ruby "
if you are fan of Python, please try below
Just remind, your top cell naming should be " TOP "
Vincent
@Vincent Lin, I tried this also but I'm getting error (No overload with matching arguments in Layout.clip)
That ruby code given by @Matthias, I tried in ruby environment. Same error I'm getting.
@Matthias, now I got the point, for clipping top-Cell name is mandatory otherwise tool is got confuse which cell you are selecting.
here is my code that is working fine but kindly review once, that my approach is correct or not ?
KLAYOUT = pya.Layout()
layout = pya.CellView.active().layout()
gds_files = ["abc.gds"]
for gds_file in gds_files:
KLAYOUT.read(gds_file)
top_cell = None
max_area = 0
for cell in KLAYOUT.each_cell():
bbox = cell.bbox()
area = bbox.width() * bbox.height()
if area > max_area:
top_cell = cell
max_area = area
print(f"Found top-level cell: {top_cell.name}")
clip_rect = pya.DBox(50.0, 50.0, 110.0, 110.0)
clip_cell = layout.clip(top_cell, clip_rect)
clip_cell.write("clip.gds")
In above script first I'm finding top-cell name then I'm doing clipping operation.
@Matthias, In your Code also If I'm giving top cell name, then its working fine
@rounak1068 If you have a single top cell, you can use
layout.top_cell()
to get the Cell object of this cell. You don't need to know the top cell name. But it will fail, if your layout contains multiple top cells.Matthias