Find and Delete the Empty cells in the GDS

edited August 2022 in Layout

Dear Support team,
May I know how can we delete the empty cells listed? the code can able to list the empty cells that need to be deleted. So can I have your guide thru .

here is the code to find the empty cell.

if pya.CellView.active() is not None:
    ly = pya.CellView.active().layout()
    layers = ly.layer_indexes()
    for cell in ly.each_cell():
        empty = True
        if cell.child_instances() > 0:
            empty = False
        for layer in layers:
            each_shape = list(cell.each_shape(layer))
            if len(each_shape) > 0:
                empty = False
        if empty:
            print(f"Cell {cell.name} is empty")

May I know how to continue deleting the empty cell?
Rgds
KumaranS

Comments

  • An easy way to remove empty cells is to save with empty cells disabled:

    opt = pya.SaveLayoutOptions()
    opt.no_empty_cells = True
    ly.write("layout.gds", opt)
    

    If you want to do that explicitly, why not using "is_empty" on the Cell?

    So eventually this problem is solved by this single line of code:

    ly.delete_cells([ c.cell_index() for c in ly.each_cell() if c.is_empty() ])
    

    Matthias

  • Hello Matthias ,
    It's working, Thanks for the suggestion and the 1 line code. That helps a lot.
    May I have a request to integrate the code, first we need to display the empty cells and the count then we need to delete the cell and display the deleted count.
    As we generally won't generate the GDs as we get from the customers and need to do the verification only. so Kindly can I have your guide thru.
    Rgds
    KumaranS

  • @Kumaran I'm sorry, but I don't fully understand. What exactly is your request? A new feature? Like "remove all empty cells"?

    Matthias

  • Hello Matthias,
    Thanks for the prompt reply,
    yes removal of empty cells, but prior to that we need to list down the empty cell names and their total count, then we remove the empty cells and display the count of the empty cell deleted which should tally before and after the removal of empty cells.
    Hope I made my statement clear, kindly let me know for any clarifications. Thanks for the support.

    Rgds
    KumaranS

  • Thanks for the explanation. But honestly such a feature can be scripted by yourself for your specific purpose. I think it is a bit too specialized to be considered a generic feature request.

    Matthias

  • edited October 2022

    Dear Matthias,
    May I have your suggestion on deleting the empty cell using the following command
    ly.delete_cells([ c.cell_index() for c in ly.each_cell() if c.is_empty() ])
    while applying this command in the code,

    if pya.CellView.active() is not None:
        ly = pya.CellView.active().layout()
        layers = ly.layer_indexes()
        for cell in ly.each_cell():
            empty = True
            if cell.child_instances() > 0:
                empty = False
            for layer in layers:
                each_shape = list(cell.each_shape(layer))
                if len(each_shape) > 0:
                    empty = False
            if empty:
                print(f"Cell {cell.name} is empty")
    ly.delete_cells([ c.cell_index() for c in ly.each_cell() if c.is_empty() ])
    

    The Klayout hangs while deleting the Empty cells.
    The GDS contains around 2k of empty cells.
    Kindly guide me thru.

    Rgds
    KumaranS

  • edited October 2022

    I took the liberty to format the above code with triple backticks so we all can see the indents.

    In the code above, why don't you simply use "is_empty" for printing empty cells? Specifically list(cell.each_shape(layer)) can be a slow operation when there are many shapes in the cell.

    But you're right, "delete_cells" appears to be very slow in some cases. I have created a ticket for this: https://github.com/KLayout/klayout/issues/1167

    Good news is there is a simply workaround which is to disable layout updates during "delete_cells":

      ly.start_changes()
      ly.delete_cells([ c.cell_index() for c in ly.each_cell() if c.is_empty() ])
      ly.end_changes()
    

    In a testcase on mine, deleting >1.5k cells took fractions of a second then. I'll still leave the ticket open as delete_cells should better disable the updates internally.

    Matthias

  • edited October 2022


    Dear Matthias ,
    Thanks, but I find some errors while executing the code. kindly let me know the issue pls

  • Hi Kumaran,

    Check out this link:

    https://www.klayout.de/forum/discussion/comment/8606#Comment_8606

    Note Matthias' comment about the definition of "emptiness"!

    You can also use custom queries from the "Search And Replace" tool (Edit Menu):

    "cells * where cell.is_empty" will display the empty cells
    "delete cells * where cell.is_empty" will physically delete the empty cells in your layout

    Not sure you can do counts inside a query...

    Cheers,

    Tomas

  • edited October 2022

    @kumaran Please use Python indentation. You need to delete the blanks in front of the three new lines.

  • Dear Matthias,
    Thanks for the suggestion. it works.
    Rgds
    KumaranS

Sign In or Register to comment.