how to use script to run DRC?

Hi All,

I only know how to use GUI to run DRC, then save as the DRC report.
May I know how to use script to run DRC in terminal? for example, use for loop to check DRC for 10 GDS files and save the separate reports.
Then when finish, I can check one by one?
Another question is how to use script to check DRC for different CELL in one GDS, for example, one GDS contains 5 CELLs and I want to check them separately, and generate different report?




  • Hi Shawn,

    the way I do it is to use KLayout in batch mode and configure DRC with variables taken from the command line.

    For example, in your DRC deck (e.g. "run.drc") you specify a source and report this way:

    report("DRC report", $output)

    Then you run KLayout with this deck in batch mode and pass "input" and "output" by defining script variables:

    klayout -b -r run.drc -rd input=input_file.gds -rd output=drc_for_input_file.lyrdb

    If you want to pick a specific cell from the input layout, you can use one more variable - i.e. "cellname":

    source($input, $cellname)
    report("DRC report", $output)


  • This looks like it should be paragraph 1, chapter 1 of the Klayout DRC Developer's Cookbook.

    If we had one.

  • Hi @Matthias ,

    Thank you very much!

    One more question.
    In the GUI, I can use

        cv = RBA::CellView::active

    to get GDS file name without path, and the cell name in the GDS.

    When I
    How can I get the GDS file name without path, and the cell name in the GDS?

    Thank you very much!


  • Hi Shawn

    For the file without path use the Ruby toolbox:

    filename_without_path = File.basename($input)

    For the cell name use



  • Hi @Matthias ,

    Thank you very much!


  • Hi Matthias,
    Sorry , more question for this topic.
    As normally , we have many DRC command , such 1.lydrc / 2.lydrc
    Can we call another DRC command in DRC ?
    such as picture shown...

  • edited December 2020


    well, that's actually harder than it looks. There is a forum entry where I explain the topic:

    The problem is basically that DRC is based on Ruby which (like Python) is a modern language. It incorporates concepts such as AST compilation and compile unit-local namespaces. Although this is nice and good it makes partitioning of big serial chunks of code difficult.

    In 0.27 I am proposing a pre-processor extension which adds a special feature to DRC scripts. This feature allows including other files through a simple pseudo-comment like this:

    # %include file_to_include.drc

    This will however only work for scripts executed within KLayout and debugging will be tedious outside KLayout because external debuggers will only see the expanded file.


  • Hi Matthias,
    firstly , I want to try to paste my code in the below , and change the format as a code ,
    hope it is workable to let you see what my code...

    # %include file_to_include.drc
    include_file = File.join(File.dirname(__FILE__), "DRC_width_loop.lydrc")
    instance_eval(, include_file)

    second, as the attached file , I want to include (or "call" ) my DRC file .
    that file was named "DRC_width_loop.lydrc"
    but , I get a message as picture and didn't know what the issue happen
    Could you please help ?

  • Hi Matthias,
    please let me explain more detail for the request...
    this is our DRC command file for 1 of part.(the DRC named "psv1.lydrc)
    and I also shown "PKG-8.lydrc " which is the DRC command we have to "call " or "include"
    Cause we have to run "PGK-8.lydrc" in first ,to output layer 1000/1.... then go to check other rule in the first DRC command .(psv1.lydrc)
    we also need to call (include) other DRC command , that is why I asking for the function.
    and as the picture below , I got some error message , please help it...
    Thanks very much~

    # %include file_to_include.drc
    include_file = File.join(File.dirname(__FILE__), "PKG-8.lydrc")
    instance_eval(, include_file)

    this is what the DRC command in PKG-8.lydrc


  • @jiunnweiyeh

    "lydrc", "lym" ... are XML formats which KLayout understands but not Ruby or Python. KLayout will use the XML attributes to store meta information such as where and how to show the script in the menu.

    In order to "load" a file, it needs to be in plain text (.drc).

    And please remove the "# %include ..." comment - it will confuse KLayout in the next release.


  • Hi Matthias
    Got it , it is work , Thanks very much for your help.

  • Hi Matthias,
    one more question , if the text file location at network , such as below.
    how to make file.join to link it?

    include_file = File.join(File.dirname(__FILE__), "PKG-8.lydrc")

  • edited December 2020


    I usually don't see issues with "File.join" or "File.dirname" with proper paths, even on Windows network drives. The network path should be a valid UNC path however which is starting with a double backslash, like \\nas\Personal\....


  • Hi @Matthias ,

    May I know is it possible that we run drc check in windows system batch mode?
    currently in Windows CMD, I can use following command to run
    C:\Users\user1\Downloads>C:\Klayout\klayout-0.26.9-win64\klayout_app.exe -r C:\Users\user1\KLayout\drc\DRC.lydrc test.gds

    The KLayout GUI will start and check the GDS.
    However if I add -b option, it will not run. is it possible we add -b option to make it run at background?

    Thank you very much!

  • @TryAndTry In batch mode you cannot give a file as an argument because there is no GUI to load it into.

    You also need to specify where to write the report or output layout to.

    You can pass this information from the command line using variables. This is how I do it:

    In the DRC:

    if $input && $output
      report($output)   # or target($output) for writing layouts 
    ... your DRC code ...

    This is how you call this script in batch mode:

    klayout -b -r your_drc.drc -rd input=input_file.gds -rd output=output_file


Sign In or Register to comment.