Need for a klayout shared library option that exposes the drc engine functions

edited July 25 in KLayout Development

Hello Matthias, Thank you for your work on this great project. I was wondering if there was a way to use klayout code base as a shared library (preferably a python module even) to run the drc engine functionality(preferably from python) natively. I am not aware of a way to do that. Is there a documented way to do that ? Or is that not supported currently?
If my question is not very clear. I mean I would like to do something like this

in c/c++ : #include <libklayout>
in python: import klayout

and then I will be able to mayb do something like this:

gds_input = klayout.drc_engine.read_gds('path/to/file')


And perform boolean operations on the layers the same way I would be able to if I had written a script and passed it to klayout via the -roption (for drc verification for instance)


  • @nofal There is a klayout module on PyPI already.

    It does not provide the DRC engine as this is a Ruby wrapper around the Region, Edges, EdgePairs and Texts classes. I can't wrap Ruby into a Python module and the DRC engine has more integration into the application framework that is good for a Python module.

    But everything you can do with DRC you can also do with the klayout module. It's just somewhat more low level.


  • edited August 2

    Thank you @Matthias for the quick response. I am not sure where to look for documentation on how to use the klayout pypi module. Can you point me in the right direction or mayb include a code snippet that reads a layout; loads a couple of layers and performs an AND operation ?

  • @nofal Here is some sample code

    ly =
    # equivalent of "a = input(7, 0)"
    layer_a = ly.layer(7, 0)
    a = pya.Region(ly.top_cell().begin_shapes_rec(layer_a))
    # equivalent of "b = input(16, 0)"
    layer_b = ly.layer(16, 0)
    b = pya.Region(ly.top_cell().begin_shapes_rec(layer_b))
    # same than in DRC
    a_and_b = a & b
    # equivalent of a_and_b.output(100, 0)
    layer_out = ly.layer(100, 0)


  • Hello @Matthias thank you for your response. I tried to import klayout but could not find pya anywhere.
    How can I read a gds file. I am sorry to be asking so many questions. I am not sure where to look for documentation on this specific topic.

    Thank you.

  • @nofal You can start reading a file this way:

    ly = pya.Layout()

    There is some documentation you should read for an introduction: and the subchapters.


  • I am afraid you are not using the pypi module here. I need to use the pypi module without invoking the klayout binary and passing it the python script. I am not sure how to get pya

    Is it possible to use the klayout pypi module (the one installable via pip : pip install klayout) to do what can be done in a python script that is PASSED to klayout EXECUTABLE ?

    essentially I would like to have a file called

    and then call it this way python

    and in

    import klayout
    # do some stuff using klayout module to achieve drc checks

    I DO NOT want to call it this way klayout -b -r

  • The Python module does not provide "pya", but "klayout.db" for most classes. Some special classes may be found in "klayout.lib" or "".

    Usually you can do:

    import klayout.db as pya

    I skipped that part because I was assuming you're familiar with the Python module concept. Please read "pya" as "the appropriate module the way you have imported it".

    But frankly, saying "I do not want to ..." is not the smoothest way to solve problems. There are easy paths and thorny ones. You are choosing the second type and you need to be prepared for a some more learning cycles. When I say, that klayout.db is capable of emulating the DRC features I'm not saying "easily". If you want to learn about the details, you can study the sources of the DRC layer which are found here:


  • Sorry if I sounded demanding, English is not my first language. I am very thankful for your effort and response. @Matthias

  • edited August 23

    Mine either :)

    I did not want to imply you sound demanding. I just wanted to give an advice: try adapting to the tools you have instead of focussing on a specific path which may be difficult to take. Engineering is mostly making the best from the material available. Software is a little easier to take - it can be modified, specifically if it is open source. Still that is an effort and not always someone wants to pay for that.


Sign In or Register to comment.