Creating a whole mask only with Ruby

edited June 2013 in Ruby Scripting
I'm new to klayout.
I need to draw a mask for a semiconductor.
I prefer coding than drawing and I know all the parameters of the mask (lengths, angles etc.).
Is it possible to draw all the mask only by coding it in Ruby?

Thank you,


  • edited June 2013

    Hi Sveta,

    it is in fact. But you should make yourself familiar with Ruby and the basics of the programming API.

    Here is a very brief example. It creates one cell, one layer and one rectangle on that layer and writes the whole to a file called "x.gds":

    layout = RBA::Layout::new
    # database unit 1nm:
    layout.dbu = 0.001
    # create a top cell
    top = layout.cell(layout.add_cell("TOP"))
    # create a layer: layer number 1, datatype 0
    layer = layout.insert_layer(RBA::LayerInfo::new(1, 0))
    # create one rectangle with lower-left coordinates at 0,0
    # and width of 1um and heigth of 2um (coordinates are in
    # database units)
    top.shapes(layer).insert(RBA::Box::new(0, 0, 1000, 2000))
    # write to x.gds

    You can run the script using KLayout in batch mode with this command line:

    klayout -z -r path_to_script



  • edited November -1
    Thank you Matthias!
  • edited November -1
    Hi Mathias -

    I see two possible ways of scripting/programming the layout/geometry - using Ruby scripting (as you explained above in this thread), or generating an ASCII/text version of GDS file (using klayout text gds format - and using any scripting or programming language), and then converting it (through klayout GUI or through script) to binary GDS file.

    Are these two approaches completely equivalent, in terms of functionality, or are there some some hidden differences?
    Are there any other approaches possible, within klayout framework?
    You comments on advantages or disadvantages of these approached would be highly appreciated.


  • edited June 2013

    Hi Max,

    thank you for mentioning the text file approach. You're right, that is also a valid option.

    I personally prefer the Ruby-based approach but maybe that is because I am quite familiar with it :-)

    Let me list some advantages which come into my mind for every approach.


    • Object model with the capabilities for work with geometrical objects (i.e. transformations)
    • Correctness ensured by engine (i.e. proper forming of GDS hierarchy)
    • Higher level methods are available (i.e. boolean operations)
    • Not restricted to generation of files, allows modification/manipulation of layout
    • Can be embedded into KLayout's UI, i.e. linked to menu items

    Generation of text files + Conversion:

    • No restriction to Ruby (any programming language can be used)
    • Not confined to KLayout as an engine (other tools also support a GDS text format)
    • No need to learn "KLayout API"
    • Better performance, depending on the choice of language


Sign In or Register to comment.