Merging multiple GDS containing the same cell name

edited November 2023 in Python scripting

Hi,

I would like to merge multiple GDS that contain the same cell name by running .tpl file using Python.
But once I merged, the cell will have the same layer overlapped with number of the cells.
Can I avoid this? Even if you have the same cell name, merge them with only one cell not ovelapping.

I use this code for merging cells.

for cell in final_layout.top_cells():
    if cell.name != "{{top_level[0]}}":
        new_instance = pya.CellInstArray(cell.cell_index(), pya.Trans(x, y) * rotation)
        top_cell.insert(new_instance)

Comments

  • Could you give some context ?
    Any specific reason why you want to do that inside klayout using a script ?
    This can also be done with klayout's buddy tools strm2gds/strm2oas, which will take care of uniquification.

    strm2oas a.gds,b.gds.gz,c.oas collected.oas
    

    I can't answer your original questions, but I'm pretty sure it can be done with python scripting too.

  • I believe that File>Import>Other_Files_Into_Current from
    another GDS file" will "uniquify" duplicate names encountered
    during the import, if you use the menu command.

  • Thank you guys,
    I think Stefan's suggestion make sense, maybe I can use some python package for this function

  • In Python, the solution is simply

    ly = pya.Layout()
    ly.read("file1.gds")
    ly.read("file2.gds")
    ...
    

    Cells with the same name in "file1.gds" and "file2.gds" will be merged into one cell. If that happens for the top level cell, this is usually fine and to my understanding this is what you need. If it happens for child cells however, the result is usually not useful. I would recommend this way only for flat layouts or when it is made sure by other ways that child cells carry different names in the different files.

    Matthias

  • Thank @Matthias
    It is apparently for the case the sub childs, so your recommendation was not the perfect solution.
    Do we have any other solution?

  • The more elaborate solution is to load into two separate layouts and merge:

    ly1 = pya.Layout()
    ly1.read("file1.gds")
    
    ly2 = pya.Layout()
    ly2.read("file2.gds")
    
    # merge ly2's top cell into ly1's top cell
    # NOTE: "move_tree" will empty ly2, but is more memory efficient
    ly1.top_cell().move_tree(ly2.top_cell())
    
    ly1.write("output.gds")
    

    Matthias

Sign In or Register to comment.