swap Datatype-number vs. Layer-number

edited May 1 in Python scripting

Hi all, below you can find a python function for swapping Datatype vs. Layer-number in companion with some diagnostic print messages.

As it happend I got some GDS files were I need to swap the layer. The project partner statement was: "we have done it always that way we can't change" :(.

The function takes a pya.Layout object as argument and returns a new one.
I am wondering if this could have been done much simpler (of course with pya) ?

###############################################################
def swap(source_layout):
  dbu = round(source_layout.dbu, 9)
  print("DBU:", dbu)
  print("All Cells:", [source_cell.name for source_cell in source_layout.each_cell()])
  print("Top Cells:", [c.name for c in source_layout.top_cells()])
  print("All Layers:", source_layout.layer_infos())
  print()  
  target_layout = pya.Layout()
  target_layout.dbu = dbu
  for source_cell in source_layout.each_cell():
    print('Cell:', source_cell.name)
    print(2*'  ', 'Childs:', source_cell.child_instances())
    print(2*'  ', 'Children:', list(set([inst.cell.name for inst in source_cell.each_inst()])))

    target_cell = target_layout.create_cell(source_cell.name)
    for inst in source_cell.each_inst():
      print(4*'  ', 'Instance:', inst.cell.name, inst.cplx_trans, inst.a, inst.b, inst.na, inst.na)
      target_cell.insert(inst)

    # in every layer after swapping copy shapes from source_layout to target_layout
    nr_shapes = 0
    for source_lay in source_layout.layer_indexes():
      # get source_shapes of layer
      source_shapes = source_cell.shapes(source_lay)
      nr_shapes += source_shapes.size()

      # original layer_no, datatype_no
      layer_no = source_layout.layer_infos()[source_lay].layer
      datatype_no = source_layout.layer_infos()[source_lay].datatype

      # retrieve (create if not existing) target_layer with swapped datatype_no vs. layer_no
      target_layer = target_layout.layer(datatype_no, layer_no)
      # prepare empty target_shapes
      target_shapes = target_cell.shapes(target_layer)
      # copy all source_shapes to target_shapes
      target_shapes.insert(source_shapes)
    print(2*'  ', 'Shapes found', nr_shapes)
  return target_layout
###############################################################

Regards Robert

Comments

  • edited May 6

    Hi Robert,

    thanks for sharing this.

    You can also modify the layer info to achieve this results if you want to swap layer/datatype for all cells globally:

    for li in ly.layer_indexes():
      info = ly.get_info(li)
      ( info.layer, info.datatype ) = ( info.datatype, info.layer )
      ly.set_info(li, info)
    

    Matthias

  • edited May 4

    Hi Matthias

    Well, life can be so easy if you know what you are doing :).
    On the other hand doing it the "hard way" I learned much about traversing the Layout structure.
    One small typo in your code: you must either use "ly" or "source_layout" (not intermixing both)

    for li in source_layout.layer_indexes():
      info = source_layout.get_info(li)
      ( info.layer, info.datatype ) = ( info.datatype, info.layer )
      source_layout.set_info(li, info)
    

    Thank you so much for your reply.
    Congratulations for excellent KLAYOUT.

  • Sure, thanks (for the kudos and the typo note) :) I have edited the code above.

    Regards,

    Matthias

Sign In or Register to comment.