How to show and hide all polygons on a given layer?

Hoping someone might have some code to share. I will post my findings here.

Comments

  • This code manages to unselect all selected polygons... but I haven't figured out how to hide them yet:

    import pya
    print('\n\nStarting KLayout Script')
    lv = pya.LayoutView.current()
    currently_selected_polygons = lv.object_selection  # list of ObjectInstPath
    
    if not currently_selected_polygons :
      print('no polygons selected in KLayout window')
    else:
      #print(dir(currently_selected_polygons[0]))
      pass
    
    for o in currently_selected_polygons:
      if (o.shape.polygon.is_box()):
        # currently let us ignore non-box shapes
        bb = o.shape.polygon.bbox()
        ll, ur = bb.p1, bb.p2
        print(f'Selected rect width({(ur.x - ll.x)}, height {(ur.y - ll.y)})')
        lv.unselect_object(o)
        # lv.select_object(o)  # you can also immediately select it again too, just to prove the concept
    
  • This code manages to select all shapes on a given layer... I haven't figured out how to show them yet, though (and hide all others):

    import pya
    print('\n\nStarting KLayout Script')
    lv = pya.LayoutView.current()
    cv = pya.CellView.active()
    layout =pya.CellView.active().layout()
    
    shapes = {}
    layer_name_to_layer_index = {}
    for layer in lv.each_layer():
      l= layout.find_layer(None, None, layer.name)
      layer_index = layer.layer_index()
      layer_name_to_layer_index[layer.name] = layer_index
    
      # ignore/filter-out some layers based on name
      if 'm0' in layer.name and 'drawing' in layer.name:
        print(layer.name)
        shapes[layer.name] = []
        for c in layout.each_cell():
          iter = layout.begin_shapes(c, layer_index)
          while not iter.at_end():
            shapes[layer.name].append(iter.shape())
            if iter.shape().is_polygon():
              # I never seem to get in here... not sure why, as later on the code DOES successfully select these shapes
              polygon = iter.shape().polygon.transformed(iter.trans())
              print("In cell " + iter.cell().name + ": " + polygon.to_s())
            iter.next()
    
    
    selections = []
    for layer_name in shapes:
      print(f'about to select all shapes in layer ({layer_name})')
      layer_index = layer_name_to_layer_index[layer_name]
      for p in shapes[layer_name]:
        selection = pya.ObjectInstPath()
        selection.layer = layer_index
        selection.cv_index = cv.index()
        selection.top = cv.cell_index
        selection.shape = p
        selections.append(selection)
    
    # make the new shape list become selected
    lv.object_selection = selections
    
  • Hi!

    If you want to hide entire layer content, you could find layer properties for given layer/datatype iterating LayoutView.begin_layers()/end_layers() and than set LayerProperties.visible to False.

  • Hi Eugene is begin_layers / end_layers different/better than for layer in lv.each_layer(): ? The latter feels more Pythonic, which is preferable.

  • Hi!

    I think LayoutView.each_layer should work for you. I used LayoutView.begin_layers because of more complicated situations like layer properties deletion.

Sign In or Register to comment.