How to make layout view active or visible?

I am generating a layout with a Python script. I have a couple questions that I have not been able to sort out.

  1. Is it possible to save the layer properties (.lyp) file via Python?
  2. Does the .lyp file get automatically loaded when KLayout loads a .gds file?
  3. How can I make the layout I've generated active in the Editor automatically? My script runs and creates a GDS file without any issues - I'd like to also have it visible in the Editor once I'm done.

This is the beginining of my code:

    import pya

    def SetupLayer(num, dt, name):
        if pya.Application.instance().main_window().current_view() is not None:
            pya.Application.instance().main_window().current_view().add_missing_layers()
            li = pya.Application.instance().main_window().current_view().begin_layers()
            while not li.at_end():
                lp = li.current()
                if ((lp.source_layer == num) and (lp.source_datatype == dt)):
                    lp.name = name
                    lp.visible = True
                lp = li.next()

    L = pya.Layout()
    L.visible = True
    TOP = L.create_cell("TOP")

    PartOutline = L.layer(100, 0)
    SetupLayer(100, 0, "PartOutline")
    ScribeOutline = L.layer(200, 0)
    SetupLayer(200, 0, "ScribeOutline")
    Pads = L.layer(300, 0)
    SetupLayer(300, 0, "Pads")
    Pin_Number = L.layer(400, 0)
    SetupLayer(400, 0, "Pin_Number")
    Functional_Signal = L.layer(500, 0)
    SetupLayer(500, 0, "Functional_Signal")
    Net_Name = L.layer(600, 0)
    SetupLayer(600, 0, "Net_Name")

If I run the same code with a blank layout window created, it seems to run the block of code where I set the layer name but as near as I can tell, it is not the correct layout view that it is operating against. I can't seem to make the layout view I have created visible.

Mike

Comments

  • Hi Mike,

    The core idea is to work with layout views rather than layout objects (pya.Layout). Layout objects are standalone and not connected with the views.

    1. To create a layout within a view use
    pya.MainWindow.instance().create_layout()
    view = pya.LayoutView.current()
    L = pya.CellView.active().layout()
    
    1. To save the layer properties use
    view.save_layer_props(filename)
    

    Your SetupLayer should then use "view" to set the layer properties.

    Regards,

    Matthias

  • Thanks for looking at my problem. I've made some progress but it seems like I've taken one step forward and two steps back. Now my GDS loads and the layer properties save correctly but now I don't have any shapes in my layout. I am expecting a polygon on 100/0 but I have no layers at all.

    import pya
    
    pya.MainWindow.instance().create_layout(0)
    view = pya.LayoutView.current()
    lp = pya.LayerProperties()
    lp.visible = True
    L = pya.CellView.active().layout()
    TOP = L.create_cell("TOP")
    
    li = pya.LayerInfo(100, 0, "PartOutline")
    lp.source_layer = 100
    lp.datatype = 0
    lp.name = "PartOutline"
    lp.source_name = "PartOutline"
    view.insert_layer(view.end_layers(), lp)
    
    po = []
    po.append(pya.Point(0, 0))
    po.append(pya.Point(720990, 0))
    po.append(pya.Point(720990, 723456))
    po.append(pya.Point(0, 723456))
    TOP.shapes(li.layer).insert(pya.Polygon(po))
    
    opt = pya.SaveLayoutOptions()
    opt.set_format_from_filename("test.gds")
    L.write("test.gds")
    view.save_layer_props("test.lyp")
    view.load_layout("test.gds", 0)
    view.load_layer_props("test.lyp")
    

    I am clearly struggling with comprhending the layout vs view models and I am sure my mistake is probably pretty simple but I can't see it. ;-)

    What am I missing?

  • I've made some further progress this evening. In fact, for the most part what I am trying to do is working. The only issue I am running into now is when I try to run my script as batch script from the command line it doesn't generate a GDS file. If I run the same script from the Macro Development GUI it runs to completion and I end up with the GDS file.

    This is my current solution:

    import pya
    
    class GDSLayer:
    
        li = None
        lp = None
        lyr = None
    
        def __init__(self, num, dt, name):
            self.num = num
            self.dt = dt
            self.name = name
    
        def Add(self, layout):
            self.lyr = layout.layer(self.num, self.dt)
    
        def View(self, view):
            if view is not None:
                view.add_missing_layers()
                self.li = view.begin_layers()
                while not self.li.at_end():
                    self.lp = self.li.current()
                    if ((self.lp.source_layer == self.num) and (self.lp.source_datatype == self.dt)):
                        self.lp.name = self.name
                        self.lp.visible = True
                    self.lp = self.li.next()
    
    L = pya.Layout()
    TOP = L.create_cell("TOP")
    
    Part_Outline = GDSLayer(100, 0, "Part_Outline")
    Part_Outline.Add(L)
    
    po = []
    po.append(pya.Point(0, 0))
    po.append(pya.Point(2700000, 0))
    po.append(pya.Point(2700000, 2700000))
    po.append(pya.Point(0, 2700000))
    TOP.shapes(Part_Outline.lyr).insert(pya.Polygon(po))
    
    L.write("t.gds")
    
    if pya.MainWindow.instance() is not None:
        pya.MainWindow.instance().create_layout(0)
        view = pya.LayoutView.current()
        view.load_layout("t.gds", 0)
        Part_Outline.View(view)
        view.save_layer_props("t.lyp")
    

    I am getting close. I don't see anything obvious as to why this wouldn't run in -b mode.

  • There are different batch modes available. Some have a main window, some don't. "klayout -b" will enter "bare metal" batch mode where there is no main window and "pya.MainWindow.instance()" will be None.

    I tried your script in this mode:

    klayout -z -r yourscript.py
    

    and for me I got "t.gds" and "t.lyp" files. You can also add "-nc -rx" which avoids side effects such as updating the configuration file.

    Matthias

  • Thanks, I can replicate the results using the same switches so that works for me!

    Last question (I hope) - can I do the same on Windows? When I use the same command I don't see any results and "klayout_app -h" on the Windows command line doesn't return anything like it does on Linux.

  • KLayout isn't compiled as a console application on Windows (as then an ugly command terminal pops up), so it won't print anything to the console.

    But if you use a bash from cygwin or MSYS it will do. Also redirection to a file should work.

    Matthias

  • Hi all,
    I am trying to use klayout python module.
    First thing I would like to load a dxf file and visualize it from a python script.
    I have tried with the following code (inspired by this thread):

    import pya
    pya.MainWindow.instance().create_layout(0)
    view=pya.LayoutView.current()
    L =pya.CellView.active().layout()
    filePath=...
    L.read(filePath)
    view.show()

    Unfortunately it breaks at the second line with following error message:

    AttributeError: module 'pya' has no attribute 'MainWindow'

Sign In or Register to comment.