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)
    
Sign In or Register to comment.