Macros don't work

edited July 30 in Python scripting

Hi,

I'm having trouble getting started with writing macros. I started with the the basic example from https://www.klayout.de/doc/programming/python.html. I pasted the code into a new python macro and was able to run it and have it draw the rectangle, but then, I maybe accidentally changed a setting that's made macros stop working. In the console it says the macro has been run, but it isn't drawing anything. Here's a screenshot of my macro IDE (link removed).

Sorry if this is a really silly question.

Comments

  • Hi,

    Not a silly question :-)

    # https://www.klayout.de/forum/discussion/2759
    #
    # File: forum2759a.py
    import os
    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))
    
    outfile = os.path.join( os.path.expanduser("~"), "ta.gds" )
    layout.write(outfile)
    

    In the console it says the macro has been run, but it isn't drawing anything.

    Assuming your code is similar to the one above, nothing is displayed in my environment either.
    But it generates the GDS file.
    Since the code does not control the layout view, this behavior is normal.

    I pasted the code into a new python macro and was able to run it and have it draw the rectangle,

    Could you immediately see the rectangle in the KLayout's main canvas?
    I guess you opened the generated GDS file.


    A sample code below will behave as you expected.

    # https://www.klayout.de/forum/discussion/2759
    #
    # File: forum2759b.py
    import os
    import pya
    
    cell_view = pya.MainWindow.instance().create_layout(1)
    layout = cell_view.layout()
    
    top = layout.create_cell("TOP")
    cell_view.cell = top
    l1 = layout.layer(1, 0)
    top.shapes(l1).insert(pya.Box(0, 0, 1000, 2000))
    
    layoutView = cell_view.view()
    layoutView.add_missing_layers()
    layoutView.max_hier_levels = layout.cell(top.cell_index()).hierarchy_levels() + 1
    layoutView.zoom_fit()
    
    outfile = os.path.join( os.path.expanduser("~"), "tb.gds" )
    layout.write(outfile)
    

    Kazzz-S

  • edited July 30

    Hi @DGillam,

    I have removed the external link in your original post as it pointed to a suspicious page heavy in advertising links. Please paste the image directly.

    Matthias

Sign In or Register to comment.