Is it possible to place many instances of a cell to a text list of x,y coordinates?

edited October 2013 in General
I wish to make a simple dot feature (small square) and then repeat that same square at thousands (or tens of thousands) of computer-generated x,y locations. Other software will provide a text file with the x,y coordinates of each feature, and I'd like to use this list to place the origin of a cell with the feature to be replicated. Is this possible in Klayout, and if so, how?


  • edited 11:40AM
  • edited October 2013

    Hi bunday,

    There is not built-in feature for that, but
    this problem can be solved through scripting. There is some sample code provide at Here is some slightly modified version of that:

    # create a new view (mode 1) with an empty layout
    main_window =  RBA::Application::instance.main_window
    layout = main_window.create_layout(1).layout
    layout_view = main_window.current_view
    # set the database unit (shown as an example, the default is 0.001)
    layout.dbu = 0.001
    # add a cell (in this case "TOP")
    cell_index = layout.add_cell("TOP")
    cell = layout.cell(cell_index)
    # create a layer (in this sample layer 10, datatype 0)
    layer_index = layout.insert_layer(RBA::LayerInfo::new(10, 0))
    # add shapes now
    # TODO: read the text file and create one shape on each location read
    # (in this sample a single box with lower left corner being a 0,0 and
    # width of 1um and height of 2um is created)
    cell.shapes(layer_index).insert(RBA::Box::new(0, 0, 1000, 2000))
    # select the top cell in the view, set up the view's layer list and
    # fit the viewport to the extensions of our layout
    layout_view.select_cell(cell_index, 0)

    Maybe you can give some details I can modify the sample accordingly.


  • edited October 2013

    I am new and was struggling to look for the solution, here is one that can help:

    Make sure you have an object in cell on a specific layer. Just select the layer of that object, then..
    Tools>VErification>Shapes to Marker>Flat
    IN the new window it should select all by default (if it doesnt select all then it might just select 1000 so tweak this in 'configure' option at bottom)
    Then on the top right you will see File>Save as>
    Save it and you should have a file (.lyrdb) with a lot of stuff including the coordinates positions..(open with notepad or any text editor)
    Maybe then you just have to use Matlab or C or some programing langauage to extract these from the text file.
    DONE...but the hard way...
    Hope it helps. Let me know if you come up with a script ;)
  • edited 11:40AM
    Thank you Matthias; What I want to do is, into a fresh layout, place a cell for each line in a textfile named "dotcoordxy.txt". This textfile is placed in the root of some known folder (for simplicity, say "C:\coords"). An example of this very simple textfile, in microns, for 12 sets of x,y coordinates, is at the end of this post. The real final goal is to allow for placing a cell (containing maybe a 0.015 micron square or circle, defined in the cell) at each of these coordinates, although with tens of thousands of them. None of them overlap, as these coordinates have already been generated elsewhere with design rules of dot size and minimum distance to neighbors.


    ***********text file example starts here, "dotcoordxy"**************

    0.000 0.000
    1.648 1.366
    2.288 0.392
    0.185 0.831
    1.787 1.331
    2.184 1.014
    0.825 1.084
    0.444 1.798
    2.403 0.184
    2.278 2.028
    0.147 2.278
    0.645 1.105
  • edited October 2013


    Ok, placing a CELL is something different. Here is the sample code (not tested). It assumes you already have created a cell called "SUBJECT" and it creates a new top cell called "TOP" with all these instances:

    # create a new view (mode 1) with an empty layout
    main_window =  RBA::Application::instance.main_window
    layout = main_window.current_view.active_cellview.layout
    # find "SUBJECT"
    subject = layout.cell_by_name("SUBJECT")
    # add a cell (in this case "TOP")
    cell_index = layout.add_cell("TOP")
    cell = layout.cell(cell_index)
    # read lines and create instances"coordinates.txt", "r") do |file|
      file.each_line do |line|
        xy = line.split(/\s+/)
        if xy.size >= 2
          x = (0.5 + xy[0].to_f / layout.dbu).to_i
          y = (0.5 + xy[1].to_f / layout.dbu).to_i
          cell.insert(RBA::CellInstArray::new(subject, RBA::Trans::new(RBA::Point::new(x, y))))


  • edited 11:40AM
    Thanks Matthias, I've put it in the macro interface and tried to run, seems to be on the brink of working, but gives the error: "undefined method 'CellInstArray' for RBA:Module", likely something wrong with line 19. If I backtrace, I get:

    NoMethodError: undefined method `CellInstArray' for RBA:Module
    C:/Users/bundayb/KLayout/macros/macrotest12new.rb:19:in `block (2 levels) in <main>'
    C:/Users/bundayb/KLayout/macros/macrotest12new.rb:14:in `each_line'
    C:/Users/bundayb/KLayout/macros/macrotest12new.rb:14:in `block in <main>'
    C:/Users/bundayb/KLayout/macros/macrotest12new.rb:13:in `open'
    C:/Users/bundayb/KLayout/macros/macrotest12new.rb:13:in `<main>

    Any idea what is wrong? Gotta say I'm not a programmer, I'm a microscopist, so I'm probalby missing something trivial in the syntax...
  • edited 11:40AM

    Sorry, there was a typo. It should say RBA::CellInstArray::new. I edited the script above.

    As I said, it's not tested.


  • Hi all,

    I have tried the above script and it nicely works when one wants to instance a cell based on a list of coordinates.

    I have a follow on question regarding this type of instancing. Is is possible to create discrete paths with varying lengths based on a set of coordinates? For instance if I have the following coordinates;

    x1, y1
    x2, y2
    x3, y3
    x4, y4

    and if I want to create discrete paths with start and end coordinates like;


    where essentially two paths are created which are not connected. Would it be possible to achieve this with scripting?

    Any help is much appreciated.



Sign In or Register to comment.