Search and Replace of instance in a boundary

edited December 2021 in KLayout Support

Hi,
I am trying the following custom queries to replace 'SUB1' instances with the 'SUB2' instances only if it is located in the left x-axis. It does not show any error but it also does not replace the instance. Also, can I also do these queries in the macro python script, if so can you please let me know how.
Thanks
with instances of cell Main.* where (cell.bbox.left==-70000 && cell.bbox.right==0 && cell_name ~ "SUB1") do inst.cell_index = <<"SUB2">>

Comments

  • The query needs to use "inst" instead of "cell" for the coordinate check.

    Here is a sample for a test case of mine. Please also note I'm using "dbbox" which is like "bbox", but returns the micron-unit bounding box (instead of DBU):

    with instances of cell TOP.* where (inst.dbbox.left==0 && inst.dbbox.right==10.0 && cell.name == "A") do inst.cell_index = <<B>>
    

    You can run a query through the LayoutQuery class (see https://www.klayout.de/doc-qt5/code/class_LayoutQuery.html) in Python. However, I think it's easier and safer to directly code the query as a Python loop over the instances inside the cell.

    Matthias

  • Hi Matthias,
    Thank you so much. In the meantime, I figured out how to do it with python scripting -

    import pya
    layout = pya.Application.instance().main_window().current_view().active_cellview().layout()
    
    topcell=layout.cell("Main")
    dbu=layout.dbu
    change_from="SUB1"
    change_to="SUB2"
    
    left    =-60
    right   =-30
    bottom  =-25
    top     =30
    bb=pya.DBox(left,bottom,right,top)
    for i in topcell.each_overlapping_inst(bb):
      if i.cell.name==change_from:
        i.cell_index=layout.cell(change_to).cell_index()
    
  • Very good :)

    BTW: the lengthy second line can be simplified to:

    layout = pya.CellView.active().layout()
    

    Regards,

    Matthias

  • @Matthias
    Could you please help to check how to do "replace" in Ruby?
    I knew that some explain in your document.
    Signature: Instance replace (const Instance instance, const CellInstArray cell_inst_array)

    But that code below is not workable...
    instance.replace(RBA::CellInstArray::new(placeme, RBA::Trans::new(RBA::Vector::new(0, 0))))
    here was my full code.

    main_window = RBA::Application::instance.main_window
    layout = RBA::CellView::active.layout
    layout_view = main_window.current_view
    out_cell = layout_view.active_cellview.cell
    layoutView = RBA::Application::instance.main_window.current_view.active_cellview.cell_name
    getcells=layout.each_cell
    ############################################
    shotcell="Shot"
    diecell="DIE"
    layers=["PI1","CapRD","PI2","UBM"]
    #####shot######
    layers.each do |layername|
    cellname="#{shotcell}_#{layername}"
      if !layout.has_cell?(cellname) then
      layout.create_cell(cellname)
      end
      viewed_cell = layout.cell(cellname)
      subcell_index = layout.cell(shotcell).cell_index()
      locationx=0
      locationy=0
      viewed_cell.insert(RBA::DCellInstArray::new(subcell_index, RBA::DTrans::new(locationx , locationy)))
      viewed_cell.each_inst do |instance|
              instance.flatten(1)
        end #each _ins
    
      placeme = layout.cell("DIE_CapRD").cell_index()    
       viewed_cell.each_inst do |instance|
            if instance.cell.name==diecell then
            instance.replace(RBA::CellInstArray::new(placeme, RBA::Trans::new(RBA::Vector::new(0, 0))))
            end
       end #each _ins
    end
    
Sign In or Register to comment.