I want to get currently selected polygons

Trying to make a tool that will merge polygons horizontally or vertically, so the first step I wanted to take is to get the currently selected polygons, and their information (coordinates of points/corners, layername/data/type).

I've tried reading, but since I don't know Ruby, and most of the documentation is written for Ruby with no Python substitute... I'm left relatively confused.

Here's as far as I've got... able to print the list of layers available... but don't know how to discriminate which are selected and which are not (even though this doesn't matter, it seems like a simple API feature that /should/ exist... but I just couldn't figure it out.

Later I try to print the current layer list, but it shows empty... as does the selected_cells_paths.

import pya
import pprint
import inspect

print('\n\nStarting KLayout Script')

c=pya.CellView.active().layout()
lv = pya.LayoutView.current()
sl = lv.selected_layers()
cll = lv.current_layer_list

if sl:
  sll = []
  while not sl[0].at_end():
    sll.append(sl[0].current())
    sl[0].next()
  print(f'selected layer(s): {[(s.name, s.visible, s.marked) for s in sll]}')
else:
  print('Nothing in "Layers" list is currently selected')

print(f'current layer list: {cll}')
print(f'selected_cells_paths: {lv.selected_cells_paths(0)}')
print(f'selection bounding-box: {lv.selection_bbox()}')

Comments

  • I made some progress, I can print the coordinates of each rectangular polygon:

    import pya
    
    # Enter your Python code here ..
    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')
    
    for o in currently_selected_polygons:
      if (o.shape.polygon.is_box()):
        bb = o.shape.polygon.bbox()
        ll, ur = bb.p1, bb.p2
        print(f'Selected rect lower left ({ll.x}, {ll.y}) , upper-right ({ur.x}, {ur.y})')
    
  • edited September 2022

    Have you tried this: https://www.klayout.de/doc-qt5/programming/python.html ?

    Traversing the selected layers is complicated by the potential presence of multiple layouts. So this is a way to traverse the layers in the layer list in the general case:

    lv = pya.LayoutView.current()
    
    for layer_iter in lv.selected_layers():
    
      # layer_iter is a LayerPropertiesIterator
      # layer_node is a LayerPropertiesNode object
      layer_node = layer_iter.current()
    
      # cv_index is the index of the layout loaded
      # layer_index the logical layer therein
      cv_index = layer_node.cellview()
      layer_index = layer_node.layer_index()
    
      # layers may be invalid, so we have to skip them
      layout = lv.cellview(cv_index).layout()
      if layout.is_valid_layer(layer_index):
    
        # print layer information
        print(str(layout.get_info(layer_index)) + f"@{cv_index + 1}")
    

    If you're looking for selected objects on selected layers you need to check if o.cv_index is one of the selected layer's cellview values and o.layer is one of the selected layer's layer_index values.

    So as you're basically able to retrieve the polygons, what are you trying to implement next?

    Matthias

Sign In or Register to comment.