Open .gds file in KLayout by using Python

edited December 2024 in Python scripting

Once I write my structure(a lot of PCells) into a .gds file. How can I use Python code to open this .gds file in KLayout? I think this will be much eaiser for me to do visualization.

Is there a function in pya module can realise this.

Comments

  • That is simple:

    pya.MainWindow.instance().load_layout("your.gds", 1)
    

    The "1" is for "open in a new panel". You can use 0 to replace the current panel with the new layout or 2 to add the layout to the current panel.

    Matthias

  • edited December 2024

    Thank you for your reply. But when I use this code, I got a tip message

    AttributeError: module 'pya' has no attribute 'MainWindow'
    
  • How are you running this code?

  • edited December 2024

    Normally, I write them directly in PyCharm and run them to write layout into KLayout.

    Today I tried your code in KLaout Macro tab. It works well, but with a small problem. I find that I need to use absolute path rather tham relative path. I mean I put my .gds file with my path file in same directory. Once I run the code like:

    import pya
    
    pya.MainWindow.instance().load_layout(r"PolygonWithHoles.gds", 1)
    

    It raise an error like below:

    What happens here? Is this a bug? I think sometimes by using relative path can make thing become easier.

  • Inside the application, your current directory is the one that Windows gave to the application when it started it. That may be the installation path or whatever.

    Just use a full path when opening the file.

    Matthias

  • edited January 6

    OK, I get your idea. But you know if we use python macro in KLayout. We need to add a location. Maybe you can consider to make this location also as the default searching directory. Then we do not need to use sys.path.add() to add the searching directory?

    I will leave a solution here for opening file in Pycharm and VS code by python. Currently I use os module.

    import os
    
    os.startfile(path)
    
  • This last offering, "os.startfile(path)", works, but does not open a file in a layout when it is already running. This creates a bunch of confusing problems with multiples. Is there a way to open from the macro? I get the error: No overload with matching arguments in MainWindow.load_layout. This occurs from the basic Klayout opening screen, with no cells, layouts, layers or files loaded.

  • @double0darbo I think if you want to open in macro, because when you use macro you need to open KLayout first, so whey not directly open the file without using codes? What I did is I use compiler like Pycharm, VS code or something. This can be easier for me to do visualization.

  • "load_layout" simply takes a string which ideally is an absolute path. Otherwise it will simply read that file relative to the current directory and that is usually something defined by the operating system. There is nothing special here. Just like any other application or Python function would behave - for example "open". If you want to read a file from a specific location, specify a full absolute path.

    How you get this path is up to you. "os.startfile" does not establish a specific start directory AFAIK. "os.path.abspath" maybe will help you.

    Frankly, that is all somewhat outside the scope of the KLayout application.

    Matthias

Sign In or Register to comment.