Useful Ruby Modules

This is a collection of useful ruby modules. These scripts may also serve as a starting point for custom developments. Please also check these resources:

All .lym macros are installed by one of these ways:

  • By copying the files into the local macro installation folder. For the location of the folder open the macro development environment (Tools/Macro Development) and point with the mouse over the "Local" folder.

    The "Import" function from the macro tree's context menu will do exactly that if you select the "Local" branch and choose "Import" from the context menu (right mouse click).

    On Linux, the local macro installation folder is "~/.klayout/macros", on Windows it often is "%HOME%\KLayout\macros". This is the preferred way to install macros for the current user only.
  • Or: By copying the file to the "macros" folder in installation path of KLayout. If this folder does not exist yet, create it. The installatio path is the folder where the klayout.exe or klayout binary is located. This usually is "C:/Program Files/KLayout" on Windows or some path like "/usr/bin/klayout" on Linux. This is the way to globally install a macro.
  • Or: by copying the file to an arbitrary folder and setting $KLAYOUT_PATH to this path.

These scripts are made available here:

Anisotropic scaling

Download link: scale_anisotropic.lym

This script installs a new menu entry in the "Edit" menu in the "Selection" submenu called "Anisotropic Scaling" before the "Scale" entry. It asks for two scaling factors for x and y (separated by comma) and applies a different scaling in both directions on the selected shapes. This is a simple implementation which does not check for rounding issues (gaps) and does not support scaling of instances. Paths are converted to polygons because in the general case a constant width cannot be maintained.

Compute the total area of all selected shapes

Download link: calc_area.lym

This script installs a new menu entry in the "Tools" menu called "Compute total area of selected shapes". It sums up the area of all shapes selected. Caution: this is a simple sum of areas. Areas where the shapes overlap are counted twice.

Compute the total area of all selected layers (hierarchical)

Download link: calc_area_hier.lym

This script installs a new menu entry in the "Tools" menu called "Compute layer area". It computes the total area of all layers selected. Caution: this is a simple sum of shape areas, weighted with the cell's instant counts. Areas where the shapes overlap are counted twice.

Search for odd-width paths

Download link: search_odd_width_paths.lym

This script installs a menu entry in the "Tools" menu called "Find Paths With Odd Width". It will find all paths with an odd width in database units in the current layout and report them. Such paths cannot be saved to OASIS, hence it's important to remove them before a layout can be written to OASIS.

Replace cells with others from another file

Download link: replace_cells.lym

The script installs a new menu entry at the end of the cell list context menu: "Replace Cells With Others". This function asks for a file containing a couple of other (top) cells, even with their own hierarchy. It will copy these cells into the existing layout and replace the corresponding cells in the current layout with the ones from the replacement library.

Hint: the script requires the database unit of the replacement and original file to be identical.

Write all child cells of the current cell to new files

Download link: write_childcells.lym

The script installs a new menu entry at the end of the cell list context menu: "Write Child Cells". This function asks for the hierarchy level and writes all cells at this level (below the current cell) to files called "{cellname}.gds".

Dump all shapes of the current cell recursively to a XML file

Download link: dump_flat_shapes.lym

This script installs a menu entry in the "Tools" menu called "Dump Flat Shapes". It asks for a file name and writes a flat dump of the current cell to this file. This dump contains all shapes of the cell and their chip cells projected into the top cell.

The format of the dump file is XML with that layout:

<shape_dump cell="{cell name}" dbu="{database unit}">
  <layer source="{layer}">
    .. shapes on that layer using these XML elements: ..
    <box>{box description}</box>
    <path>{path description}</path>
    <polygon>{polygon description}</polygon>
    <text>{text description}</text>
    .. more shapes ..
  </layer>
  .. more layers ..
</shape_dump>

List all layers under a ruler

Download link: list_layers.lym

This script will install a new entry in the "Tools" menu called "List Layers". Before this function can be used, a single ruler must be drawn. The script looks for shapes that are crossed by this ruler and reports the layers of those shapes. The script can operate on multiple layouts as well.

Rename all cells

Download link: rename_cells.lym

This script will install a new entry in the "Tools" menu called "Rename Cells". It will ask for a rename expression and rename all cells of the current layout. In the expression, "*" is a placeholder for the current cell name and "#" a placeholder for the cell index. Hence it is possible for example to add an "A" prefix by using an expression of "A*". Also it's possible to remove all traces of macro names by using "CELL#" as the expression.

Compute the bounding box of a cell

Download link: cell_bbox.lym

This script will install a new entry in the "Tools" menu called "Cell Bounding Box". It will compute and output the bounding box over all layers of the current cell (the one that is shown in the layout view and which is in the active cell tree). The ouput will include the corner coordinates as well as width and height.

Take a screenshot with options

Download link: screenshot.lym

This macro will provide a screenshot function with a dialog to enter the pixel dimensions. This script will install a new entry in the "File" menu called "Screenshot With Options".