Merge files from csv list using layer offset

Hello, I'm trying to merge gds files together using python or ruby scripting and found the forum super helpful to find bits and pieces of scripting.
Ideally, this is what I'd like to do:
1) I have a master gds with layers 1-50 where I want to import new gds layouts (with layer offset) at specific coordinates
2) I have a csv file that includes a list of gds file names and the center coordinates where they should be placed into the master gds. Some of those gds files are copied multiple times, but at different coordinates. Sometimes, they must be rotated. - I can also add a "layer offset" column (e.g., gds files 1-3 are offset by 100/0, gds files 4-5 are offset by 200/0, ...) Ideally, I'd like a script that can look up the csv file, find the gds files, apply the layer offset, and copy them at the given coordinates listed in CSV into the Master gds.
The files to be imported are unique top cells, which I think simplifies things. I'd like to keep their names as is into the master.

What I've successfully done so far is to import multiple files at given coordinates, but I didn't know how to implement layer offset or the csv lookup table.


  • Hi @plic_user_1983,

    In order to do that, you will need to first read the imported layout into it's own Layout object and then copy it over to the destination layout. While you do that you can specify a layer mapping which allows you to implement the offset.

    Here is some sample code:

    import pya
    target = pya.Layout()
    # first input
    imported = pya.Layout()"to_import.gds")
    imported_cell = imported.top_cell()
    target_cell = target.create_cell(
    offset = 100
    # prepare a layer map with offset
    lm = pya.LayerMapping()
    for lindex in imported.layer_indexes():
      linfo = imported.get_info(lindex)
      linfo.layer += offset
      lindex_target = target.layer(linfo), lindex_target)
    # prepare a cell map (copy whole tree)
    cm = pya.CellMapping()
    cm.for_single_cell_full(target_cell, imported_cell)
    # copy the shapes and cell tree
    target_cell.copy_tree_shapes(imported_cell, cm, lm)
    # repeat for more inputs ...

    Hope this helps.


  • edited May 8

    @Matthias - Thank you - that helps a lot. and if I want to read those inputs from a table (e.g., csv) and call those out in the script, can I do this too? Instead of manually copy-paste the commands 10 times, can I create a loop that looks into the table ?

  • @plic_user_1983 The above code was meant as a sketch. It shows how to use KLayout API to merge a layout into another one with a layer offset. I'll leave it to you to combine that with a CSV reader (e.g. using the pangas module) and put that code into a loop.


Sign In or Register to comment.