It looks like you're new here. If you want to get involved, click one of these buttons!
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
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
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.
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_144Matthias
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.
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)
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.
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":
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