How to set a layer to active

edited September 2023 in Python scripting

This gets my workspace made now I want to have "RootShapes" be the active layer so that it's ready for the user to begin drawing shapes. I can draw shapes on the layer using python code but how do I select this layer so the user can just click the box tool and get to work drawing?

I investigated layer properties looking for a "selected" or active" I can change colors and "mark" a layer but still haven't found a way to set it to active.

import pya
app = pya.Application.instance()
mw = app.main_window()
view = mw.current_view()
cv = view.cellview(0)
layout = cv.layout()
# layout.dbu=1
# cv_index = cv.cell_index
cell_workspace = layout.create_cell("WorkSpace")
layer_root_shapes = layout.layer(1, 0, "RootShapes")
layer_array_shapes = layout.layer(1, 1, "ArrayShapes")

#changes color of layers
lv  = mw.current_view()
lp_found = None
iter = lv.begin_layers()
while not iter.at_end():
  lp = iter.current()
  if lp.source == 'RootShapes 1/0@1':
    print("found layer 1/0")
    lp.fill_color = 4278190335
    lp.frame_color = 4278190335
  if lp.source == 'ArrayShapes 1/1@1':
    print("found layer 1/1")
    lp.fill_color = 4294901760
    lp.frame_color = 4294901760
    # print("marking the array shape layer")
    # lp.marked=True puts a cross on the layer but doesn't select it


  • Hi rrzzxx22,

    The active layer is set using LayoutView.current_layer
    following example sets active to layer (4/0) if this layer exist.

    import pya
    def setActiveLayer(layerNo, layerDt):
        layoutView = pya.Application.instance().main_window().current_view()
        if layoutView:
            itr = layoutView.begin_layers()
            while not itr.at_end():
                lyp = itr.current()
                if (lyp.source_layer, lyp.source_datatype) == (layerNo, layerDt):
                    layoutView.current_layer = itr
    setActiveLayer(4, 0)
  • edited September 2023

    @RawrRanger Thank you! Works great!

    Edit: Actually it doesn't seem to work when run as part of other code. It works fine when running in the interpreter directly, but when run as part of a larger script even as the final line, I'm not arriving for the user in a state where they can proceed with the layer set to active.

    What I'm doing is loading a big image into klayout and asking the user to draw some boxes on features so I can make an array for them. I need their shapes, a number of rows/columns and step distance.

    (I know there's existing array features for instances but I don't think they're flexible enough for making a nominal array and then 'stretching it' nonlinearly when it doesn't match up to the picture.

    Still a useful function to have.

Sign In or Register to comment.