Python script example to merge gds files

edited February 2016 in Ruby Scripting

I prefer to use Python instead of Ruby. Could a forum category be added "Python Scripting"? It would be helpful to have all examples/information grouped instead of mixed under Ruby scripting (or rename the category to Scripting).

Anyway I wanted to post my example Python script that reads in a number of gds files and writes them out as subcells under a new top level. It took me some time to get it to work, even with a Ruby example of a script that did the same. Especially to get a new instance under a topcell I found tricky.
klayout keeps impressing me every time I use it :-)

import pya

# Simple script to merge three top level gds streams
#usage
# klayout -b -r make_TOP.py

layout = pya.Layout()
layout.dbu = 0.001
TOP = layout.create_cell("TOP")
gdsFiles=["A.gds","B.gds","C.gds"]
for i in gdsFiles:
  layout.read(i)

  for i in layout.top_cells():
  # we don't want to insert the topcell itself
    if (i.name != "TOP"):
      print "Adding "+i.name
      cell_index=i.cell_index()
      new_instance=pya.CellInstArray(cell_index,pya.Trans(pya.Point(0,0)))
      TOP.insert(new_instance)

layout.write("TOP.gds")

Comments

  • edited November -1

    Hi Theo,

    sure: "Python scripting" is available now.

    Thanks for the sample. The script looks nice :-)

    Let me just comment on one detail: loading multiple GDS files into one works. But there is one potential pitfall: if the GDS files contain cells with identical names, these will be merged together. That's not an issue if each GDS file contains a single cell with a different name each. But beware of the implications of a name clash - this might entirely mess up your layout.

    The safer (but slower) way is to read each layout into a separate pya.Layout object and copy the content over from this local layout to the target one using Cell#copy_tree. This will assign unique cell names if necessary.

    Regards,

    Matthias

  • edited November -1

    Hi Matthias

    Good point. I have to consider that indeed. I'm merging three top level cells. One from the P&R team, so standard cells and one from the analogue RF team. A third one is the ring of IO cells. I'm pretty sure I will get a clash and I actually use that on purpose. The digital gds contains standard cells in LEF format and a ROM with null pattern. So I read in the digital gds (P&R) and after that I read in the "full" gds standard cell libraries including poly/diffusion etc and the actual ROM gds. That seems to work fine.
    But if an analogue engineer would happen to use a block name exactly equal to a standard cell name..... I hadn't thought of that scenario actually. Would have found it though (LVS).

    I do need to make a small loop to remove all but the actual wanted toplevel. Our foundary does not accept gds with more than one toplevel. I taped out last week with klayout and library cells. First I thought it worked like a charm, but an extra toplevel is present next to the visible toplevel. I learned that this stores the references to the external gds files. But in the mean time I learned how to use Python macros, so I just create my own toplevel with each run.

    Cheers,
    Theo

Sign In or Register to comment.