Merge and delete the specific layer shapes within the subcell

edited September 25 in Python scripting

Hi, I want to merge the layer shapes and delete the original ones in the subcell over python scripting. Could you please help me with this?
The script is in the following, but it doesn't work:

` + 'test.gds')

merge_layers = [ly0.layer(51, 4), ly0.layer(51, 5), ly0.layer(88, 0)]
merge_cells = []
for i in range(ly0.cells()):
merge_cell = ly0.cell('Waveguide' + str(i))
if merge_cell != None:
for idx, merge_layer in enumerate(merge_layers):
for merge_cell in merge_cells:
region =

ly0.write(layoutFilePath + 'test.gds')`



  • It doesn't work and I can't read it. Please use triple backticks on a single line to start and end code. Python without indentation reads badly.

    I don't get how you look for "WaveguideN". "ly0.cells()" will give you the number of cells, so what if there is a cell called "Waveguide427" if there are only 10 cells for example? Why not just checking for "name starts with Waveguide" or using a RegEx?

    Apart from that (and apart from missing indentation), the script should basically work. But there is one asymmetry: while you are pulling all shapes from a cell including the child cells (using begin_shapes_rec), you're just deleting the shapes of the cell, but not it's child cells (in clear).

    So the child cells will retain it's shapes and I assume that is what isn't working.

    The solution is tricky. If you just clear the shapes of all child cells - who says this one isn't used somewhere else where you don't want this cell to be empty? The general solution is complex and involves hierarchy variant building. If you can ignore this case, you may want to clear the shapes from merge_cell plus all it's children (merge_cell.called_cells).

    There is also some optimization potential here: if you use "begin_shapes_rec" inside a loop that modifies the layout, you can use "ly0.start_changes()" before and "ly0.end_changes()" after the loop. This will prevent KLayout from recomputing the geometry hashes and similar things over and over.


Sign In or Register to comment.