Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Show as new top model in python

Sounds simple but I'm struggling with the following issue:
Running klayout in batch mode:
1) I create a new cell - layout.create_cell('abc')
2) switch into this cell - similar show as new top in the GUI mode
3) add some shapes right into the cell
4) switch back to the top level

My question is how to model the show as new top command in python?

def createLayout(topCellName):
        topcell = layout.top_cell()
    print('Layout created: '

def switchTop(top):
    cell =
    cv.cell_name = top
    print('Change to cell: '+top)

def newCell(top,cellName,x,y):
    dummy = layout.create_cell(cellName)
    for cell in layout.each_cell():
      if ( == top):
        print('new cell generated: '+cellName+' in Hierarchy '+top)



### Insert shapes into hierarchy cell
#TOP=layout.top_cell() # insert shapoes in top cell
#TOP.shapes(myLayer).insert(pya.Box(x1*dbu, y1*dbu, x2*dbu, y2*dbu))

# work but need to provide cell name
cell.shapes(myLayer).insert(pya.Box(x1*dbu, y1*dbu, x2*dbu, y2*dbu))

Best Regards,


  • Hi Andy,

    some things are really easy: = layout.cell("abc")



  • Hello Matthias,
    sometimes it's the obvious we don't see ... Thanks a lot.
    My main target is to create a bunch of 'super' commands to ease the shape generation.
    Those macro language is stored in the python directory and imported as a module into python. I finally run klayout -b- r to generate the shapes.
    Up to now I defined:

    • createLayout(...) : Initialize the layout view
    • newCell(...) : create a new hierarchy
    • loadCell(...): load an existing gds file into a new generated hierarchy (newCell())
    • placeInst(...) : place an existing cell x-times
    • donut(...) : create a donut in a new generated hierarchy
    • rectangle(...) : create a box
    • adjustOrigin(): Cosmetic correction
    • addtext(...): create a text label
    • saveLayout(...): save the gds/oasis/txt file


    #usage: klayout -b -r
    from pya import *
    from util import *   # utility functions
    layout = pya.Layout()
    layout.dbu = 0.001
    dbu = 1/layout.dbu
    layer22 = 22
    topCellName = 'myTop' 
    createLayout(topCellName)       # first we need to create a layout view
    newCell(topCellName,'abc',0,0) # create abc hierarchy in TOP 
    loadCell('myTop','/home/lib/toto.oas',100,50)  # load existing gds in myTop
    donut('abc',layer22,1,3,0,10)  # create a donut in cell abc
    rectangle('abc',layer22,100,100,30,50) # create a rect in abc
    addText('abc',layer22,'mySampleText ')  # create a text pcell below abc

    In future I want to use your new development in PyPi to import the Klayout module into python. This way I could more easy access other python modules. Looking forward to the first trials.

    Best Regards,

  • Hi Andy,

    that the beauty of a dynamic language - everyone can tailor it to his or her needs :-)

    I personally like Ruby better for this kind of thing. You can essentially turn it into something entirely different (look at Rails or KLayout's DRC or XS script). But that's of course a matter of taste.



Sign In or Register to comment.