Create a clip of GDS at (30,30) to (110,110). Write out clip GDS via script.

Here I have to create a clip of GDS from specific location and save to clip as a new GDS via Script.
kindly provide me the correct way.

Comments

  • edited April 2023

    @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:

    layout = RBA::CellView::active.layout
    
    # Box in micron units:
    clip_rect = RBA::DBox::new(30.0, 30.0, 110.0, 110.0)
    
    # create a clip cell and write it:
    clip_cell = layout.clip(layout.cell("TOP"), clip_rect)
    clip_cell.write("clip.gds")
    

    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.

  • edited April 2023

    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)

  • edited April 2023

    @rounak1068

    The code from Matthias belongs to " Ruby "
    if you are fan of Python, please try below

    import pya
    
    layout = pya.CellView.active().layout()
    
    # Box in micron units:
    clip_rect = pya.DBox(30.0, 30.0, 110.0, 110.0)
    
    # create a clip cell and write it:
    clip_cell = layout.clip(layout.cell("TOP"), clip_rect)
    clip_cell.write("clip.gds")
    

    Just remind, your top cell naming should be " TOP "

    Vincent

  • edited April 2023

    @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

Sign In or Register to comment.