Python automation of KLayout in Windows 10

edited April 2020 in Python scripting

I am new to Klayout and even newer to Python, but not new to programming in general. Using KLayout WIndows GUI I have learnt to create layers and cells and can create multi layouts layouts. However, I would prefer to automate the entire layout design process. I have got the most recent KLayout (v0.26.4) and Python (3.8) installed. However, I cannot get my head around how to use Python to generate a layer or cell in KLayout.

Typing in the Python script from here
https://klayout.de/doc/programming/python.html
in Macro Development box with Python selected does not seem to do anything when I run the script (F5). Running the inbuilt templates are also doing nothing.

I suspected there is an installation problem, so I tried the following.
https://klayout.de/forum/discussion/1428/pypi-install-version to install Klayout from PyPI, but I get
SyntaxError: unexpected character after line continuation character
when using (in CMD)
C:\Users\my_user_name\AppData\Local\Programs\Python\Python38\Scripts> pip install klayout<
May be the install directory is not correct?
Attempt to install via
C:\Users\my_user_name\AppData\Local\Programs\Python\Python38\Scripts> pip install C:\klayout-0.26.4-cp35-cp35m-win_amd64.whl<
also leads to the aforementioned syntax error.

Ideally I would like to run a script written in Python and run it view/open the result in Kayout GUI.

Could not find much help in the forum post on this so any help to get me started is much appreciated.

Comments

  • Hi,

    First of all, if you just install KLayout there is not much else to do. You can run Python scripts inside KLayout because KLayout comes with it's own Python interpreter embedded. Note that in this case, the module is "pya".

    You should check from inside KLayout if a simply Python script such as 'print("Hello, world!")' runs.

    If you want to use the PyPI module that's an entirely different topic. This is for using KLayout's algorithms inside an external Python installation. Windows does not come with a nice package manager such as Linux, so Python distributions such as Anaconda exist entirely independent from KLayout and they don't know of each other. A PyPI module is called "klayout.db" for example. "pya" was too generic for wide deployment.

    On Windows there is not a single Python binary flavour (the build environment matters) and so far there is only support for Python up to 3.7 with MSVC 2017. The combination I am testing is Anaconda with Python 3.7. I can't speak for any other distribution, version or ABI variant.

    Regards,

    Matthias

  • edited April 2020

    Thanks for the response. Typing print ('hello world'') does work and gives the expected output in the console.
    All I needed to understand the mistakes I was making was to watch this video.

    I then tried the following example from the documentation

    import pya
    layout = pya.Layout()
    top = layout.create_cell("TOP")
    l1 = layout.layer(1, 0)
    top.shapes(l1).insert(pya.Box(0, 0, 1000, 2000))
    layout.write("t.gds")
    

    but it took me a bit of work to find where t.gds got saved. It gets saved in the Klayout installation folder which was tricky to find. In my case it was

    C:\Users\my_user_name\AppData\Roaming\KLayout

    Two questions before I get myself back to some more python and KLayout fun.

    1. How to I set a specific path in layout.write() for the gds to get saved in? For example if I want to save the gds file to myFolder in My Docments?
    2. What is the command (if there is one) to programmatically open the saved GDS file in KLayout (to put at the end of above script)?

    Thanks.

  • Hi,

    for 1. just specify the full path (e.g. layout.write("c:\\users\\yourself\\documents\\t.gds")).

    For 2. use pya.MainWindow.instance().load_layout("/home/matthias/x2.gds", 1). For more details see here: https://www.klayout.de/doc-qt5/code/class_MainWindow.html#k_144

    Matthias

  • Many thanks. All working.
    I have created a multi-layer layout fully via drawing in the GUI, can I open the gds from within the macro development,if so how?

  • Hi,

    I don't understand your question. Isn't that 2.) from the answers above?

    Matthias

  • As I understand, (2) is for opening a gds file created by a macro (created in Python in my case). If I create a layout, say consisting of layers, cells and instances completely via the GUI (i.e not via a script but drawing by hand) and save it as a gds, can I open this gds file as a Python (or Ruby) script - if so how?
    Thanks.

  • edited April 2020

    While I await answer to my previous query, I am wondering if I have stumbled on a bug! Please see if you can reproduce this.
    In the Macro Development, I have the following piece of code (saved under Local)

        import pya
        layout = pya.Layout()  
        top = layout.create_cell("TOP1") 
        layer1 = layout.layer(1, 0) 
        top.shapes(layer1).insert(pya.Box(0, 0, 1000, 2000))
        layout.write("C:\\Users\\my_user_name\\Documents\\KL\\my.gds")
        pya.MainWindow.instance().load_layout("C:\\Users\\my_user_name\\Documents\\KL\\my.gds", 1) 
    

    Running this (F5) loads the my.gds file in the KLayout GUI, as intended.

    Then I (1) close the my.gds layout tab in KLayout GUI, (2) close the above code tab [under Macro Development] and then (3) open the inbuilt pcell_declaration_helper script. Running this (F5) pcell_declaration_helper script loads the my.gds layout again in the KLayout GUI. This should not be happening?

    Shift+F5 does what is expected. But F5 appears to run a script which is not open (but was open) in a tab. Is this the expected behavior?

    I also notice that each time I press F5, my.gds keeps loading in the GUI with my.gds, my.gds[1], my.gds[2] and so on.

  • edited April 2020

    Hi,

    you can't open a GDS as "macro" (I guess you expect to see some script which generates a layout). There is no such "script dump of a layout file" yet.

    Regarding "Shift+F5" and "F5":

    • Shift+F5 will run the script from the current tab. It will also make this script the "current script".
    • F5 along will run the "current script" (the one you ran last). If you close the tab with the current script, it will still run this script.

    The "current script" is indicated by a green triangle symbol in the tab's header and a "white on green" icon in the script list. As "0.25api" in this example:

    Matthias

Sign In or Register to comment.