Moving set of layers

What is proper way to move set of layers (change their order) in Python? Or re-arrange cellviews order because in I need to move all layers belonging to particular cellview.

Comments

  • Hi,

    the solution depends on what you want to achieve.

    In any case I'd recommend to use the LayerIterator concept. "LayoutView#each_layer" isn't entirely stable for iterating while deleting (see https://github.com/KLayout/klayout/issues/368).

    Here is some sample code that moves all layer with even GDS layer numbers to the end:

    # Example: shuffle the layers so that even-numbered layers
    # move to the end.
    
    lv = pya.LayoutView.current()
    
    to_insert = []
    
    # Delete all layers with even layer numbers and keep their properties
    li = lv.begin_layers()
    while not li.at_end():
      if li.current().source_layer % 2 == 0:
        # keep the layer properties (dup LayerPropertiesNode) and 
        # delete the layer. li.next() isn't required as li will point
        # to the next entry anyway.
        to_insert.append(li.current().dup())
        lv.delete_layer(li)
      else:
        li.next()
    
    # Finally insert the deleted layers at the end of the layer list
    for l in to_insert:
      lv.insert_layer(lv.end_layers(), l)
    

    Kind regards,

    Matthias

  • Hi, Matthias!

    Thank you for help! Similar code pattern (I used active_cellview.index() as criteria) solved my problem!

Sign In or Register to comment.