Clip utility

edited August 2010 in KLayout Support

Great tool by the way, very fast and reliable.

I want to use the clip utility to extract a small section of the design for further analysis. When I tried this on a small design, the "clipped" design that I saved out is 200x bigger than the source gds, despite only representing about 5% of the design.

Any ideas why this is?


  • edited November -1
    hello again,

    It seems that if I flatten the hierarchy before running clip, then I see this issue. However, if I don't flatten the hierarchy then the resulting clipped gds is still bigger, but only by a fraction of a %, not 200x.

    Is klayout saving the original gds + the clipped section?

  • edited August 2010

    Hi Rich,

    yes, you are right, by default KLayout is saving both the clip and the original.

    What KLayout actually does is do create a new top cell (by default called CLIP) containing the clip. The original top cell will still exist and show the whole layout (that way, another clip can still be made). KLayout will do the clip hierarchically by creating cell variants with just the subset required. Where a cell is completely inside the clip region, the original cell is used.

    In the cell tree you will find the clip cell and the original one. Both are top cells, i.e. appear as root level nodes in the tree.

    You can save just the clip by selecting the clip cell in the cell tree and choose "Save Current Cell As" from the context menu (right mouse click). Then just this cell is saved which is usually just a fraction of the original layout.

    If you want a flat clip, I would recommend to flatten just the clip cell (after the clip) using "Flatten" from the context menu. That produces a much smaller data volume as you have noticed ... :-)

    Best regards,


  • edited November -1
    Hi Matthias,

    Thanks for your help and advice, that's exactly what I needed. I was only using the "show current cell as top" not "save current cell as".

    Best regards,
  • edited November -1
    I need to modify the clip tool so that it automatically cuts the whole gds into various smaller square (of desired size) gds and save them as separate gds files. Can you please guide me on how to proceed for this ?
  • edited November -1


    I would suggest to create a ruby script that does that - however, right now, the clip function is not provided as a ruby method and it's pretty tedious to emulate it. I'll consider that as a suggestion for the next release (which hopefully will come soon).

    Right now, you would have to modify the code - the place to look for is, around line 200. The basic function is db::clip_layout which does the work. It already provides a way to create multiple clips in separate cells. You'll need to save the cells in separate files using db::Writer (dbWriter.h) and provide a individual cell in the db::SaveLayoutOptions (method add_cell).

    There is also a manual solution. The clip tool already allows to build multiple clips into separate cells. A separate cell is not the same than a separate GDS file, but maybe you flow allows to use the same file with a different top cell, so you can extract the clip from a single GDS file, provided each clip is contained in it's own cell.

    The manual procedure is that: create a regular arrangement of boxes covering your layout on a new layer. I did so by creating a new cell with a single box and changing the boxes properties to (0,0) for the lower left and (100,100) for the upper right corner for example. This box now represents one clip tile. You can create a regular arrangement of boxes easily by placing a array instance of that cell over your layout: place an instance and check the "array instance" box, enter the desired box size as displacement vectors - i.e. (0,100) and (100,0).

    Now your layout is prepared to be clipped into pieces. Just select the clip tool and choose "Shapes on layer .." for the clip box specification. Enter the layer that you placed the boxes on previously.

    After having ran the clip tool, you will see a new cell called "CLIP" containing a sub-hierarchy of individual clips. If the original cell for called "TOP" for example, the clip cells will be called "TOP$1" for the lower left clip, "TOP$2" for the next clip right of that one and so on.

    Maybe this information is helpful.

    Best regards,


  • edited November -1

    Thanks a lot and I tried the manual selection method, its pretty useful. I was also interested in using Klayout in a batch ( non-interactive) mode so that the cutting of gds can be done through a script. Can you suggest me ways for this ?

  • edited December 2010

    Hi Acku,

    With the next release (0.21) it will be possible to write a ruby script that creates the desired clips and saves them to individual GDS files using the integrated ruby interpreter (RBA).

    This will be far easier than modifying the C++ code and more flexible.

    This is basically how the script might look like in upcoming version 0.21:

    # HINT: this simple implementation does not account 
    # for variant building - the shapes and instances are simply taken
    # out from the original shapes and therefore might disappear somewhere else.
    $multiclip =
    $multiclip.title = "Multiclip"
    $multiclip.on_triggered do 
        app = RBA::Application.instance
        mw = app.main_window
        lv = mw.current_view
        if !lv || !lv.active_cellview
          raise "Multiclip: No view or no layout active"
        lay = lv.active_cellview.layout
        top = lv.active_cellview.cell_index
        top_name = lay.cell_name(top)
        # Prepare a new layout to receive the clips
        cl =
        cl.dbu = lay.dbu
        lay.layer_indices.each do |li|
          cl.insert_layer_at(li, lay.get_info(li))
        # b = the top cell's bounding box
        b = lv.active_cellview.cell.bbox
        # Compute a box size and a box count in x and y direction
        s = (b.width / 10.0).ceil
        nh = (b.height / s).ceil
        nw = 10
        # Prepare the boxes and assign file names
        boxes = []
        filenames = []
        (1..nw).each do |iw|
          (1..nh).each do |ih|
            boxes.push( + s * (iw - 1), b.bottom + s * (ih - 1),
                                    b.left + s * iw, b.bottom + s * ih))
        # Actually do the clip
        cells = lay.multi_clip_into(top, cl, boxes)
        # Write the cells to individual files do |c,fn|
          # Rename the clip's top cell to the original top
          cl.rename_cell(c, top_name)
          # Save this cell and it's child cell tree
          opt = RBA::SaveLayoutOptions::new
          opt.format = "GDS2";
          cl.write(fn, false, opt)
          puts "Written #{fn} with top cell #{top_name} .."
    app = RBA::Application.instance
    mw = app.main_window
    menu =
    menu.insert_item("@toolbar.end", "multiclip", $multiclip)

    Best regards,


  • edited November -1
    This sounds great !!
    The code above would be very helpful to me in writing ruby scripts.
    Thanks a lot


  • edited November -1
    Hi Matthias
    I saw in release notes that version 0.21.1 is out, so is the clip method now available through a ruby method ?
  • edited December 2010


    Sorry for not mentioning that here. Yes it is. I changed to code above so it is a full Ruby module. Copy the code into some file, i.e. "multiclip.rbm" and run KLayout (version 0.21.1) like this:

    klayout -rm multiclip.rbm ...

    If you copy multiclip.rbm to the installation folder on Windows or to a folder pointed to be $KLAYOUTPATH on Linux, it will be loaded automatically without needing the -rm option.

    Please adapt the script as you require. It provides a new entry in the toolsbar called "Multiclip", which creates the clips.

    For all users familiar with the RBA scheme, this script also employs a new feature (events) which simplify binding of code to actions.

    Best regards,


  • edited November -1

    sure i'll try to do it. Probably I want to use this multi clip feature in a non interactive mode so that things could be automated. So as it is a ruby method, I can do some experiments :)


  • Updating these instructions, thanks for the nice clipping utility Matthias (please correct any mistakes here).

    1. With your layout to be clipped, go to "Edit -> Utilities -> Clip Tool"
    2. Select the location and size of the clip (I like to use the "Box center and dimensions" option because then I can right click on the center of the clip, then "CTRL + g" to see the center and input those numbers)
    3. Find your clip on the "Cells" menu, probably on the left of your KLayout window, right click on it and select "Save Selected Cells As"
    4. Pick path to save new clip
    5. Fill out the fields in the "Save Layout Options" window

    If you have saved a small clip it should be done in less than a second.



Sign In or Register to comment.