Library cells and replace function

edited October 2014 in KLayout Support
Hi Matthias,

I am currently struggling with a problem for library cell replacement in a layout.
Let's me expose the problem so that it is clear enough:
I have a gds library called foo and a 2nd one called foo1
in this library i have cells
bar
bar1
bar2
and in foo1 i have
bar
bar4

Now i use this library in a new layout, my layout is like this:
top cell
child cell
foo.bar
foo.bar1

now i want to replace foo.bar1 by foo.bar2 and foo.bar by foo1.bar
in klayout, two ways are provided to replace cells:
the replace cell command and the replace with other. To use replace cell you have to have in your layout the cell with which you want to do the replacement. For this i create an instance, for example foo.bar2 in the layout. then i use the replace cell command. this is not working, klayout telling me "not a valid cell name" whatever i choose from shallow replace to complete replace.
If i use the replace cell with other (ruby script) (for example foo.bar and foo1.bar) things are working better, since all the cells belonging to the selected library are imported in the layout (and the cell is properly replaced if there is a cell with the smae name in the library)
However the library link is lost during the operation (foo1.bar becomes bar, not anymore linked to the library foo1...)which is not that nice.
I tried to implement a new macro based on this replace cell with other but i am struggling at doing a working code !!!
Any help ?

Regards
Joël

Comments

  • edited November -1
    Hi Matthias,

    You can close this item, i think i found the solution by myself.
    sorry for the disturbance...

    Joël
  • edited November -1

    Very good :-)

    I usually don't delete discussions. If you like you can state your solution here - this might be useful for other users too.

    Matthias

  • edited November -1
    Will do, for sure !!
    Thanks
    Joël
  • edited November -1
    Replacing a cell with a library cell still gives a "not a valid cell name" error in the latest version of KLayout 0.24.8. Unfortunately gwondaleya never posted the solution so I am at a loss how to do this.
  • edited November -1

    Hi,

    I am not aware of the solution either. But I guess the general misconception is to replace "cells". In fact you can only replace "instances".

    So the solution is

    • Manually: go through the layout and change the target cells of the instances (tedious - support through search & replace would be desirable)
    • By Script: create a new library proxy, look up the instances of the cell you want to replace (through the parent instances) and replace the target cell of these instances

    A general note on library cells: KLayout's concept of libraries is not that of a "link" as the question suggests. Instead, a library cell is a weakly linked copy of the original library cell. The advantage is that the layout can live independently from the library. But once the link is broken it's hard to recover. The names "LIB.NAME" are just visual clues - they don't imply a link to the library.

    Matthias

  • edited November -1
    OH So Sorry guys.. looks like forgot to do so .time is sometimes flying so fast.Here is the macro


    Again, maybe not very clean coding style...
    How to use:
    put the cell to be replaced as active (cell name in bold)
    then right clock and select Replace Lib and Cell.

    use the drop down menu to select the library/cell

    should do the trick

    ----
    # $description: Lib-and-cell-replace
    # $version: 1.0
    # $autorun

    # replace lib & cell script

    # define a new Menuaction class
    include RBA
    class MenuAction < RBA::Action
    def initialize( title, shortcut, &action )
    self.title = title
    self.shortcut = shortcut
    @action = action
    end
    def triggered
    @action.call( self )
    end
    private
    @action
    end

    #define replace function
    $replace_lib_cell = MenuAction.new("Replace lib and cell", "") do
    # new dialog class for library and cell selection
    class ReplaceDialog < QDialog
    include RBA
    def initialize(parent = nil)
    super
    setWindowTitle("Lib/Cell Exchange dialog")
    winlayout = QGridLayout.new(self)
    setLayout(winlayout)
    # gds layout/cell view
    app = RBA::Application.instance
    mw = app.main_window
    # work on gds current view
    lv = mw.current_view
    if lv == nil
    raise "Shape Statistics: No view selected"
    end
    #cellview is the active one
    cv=lv.active_cellview
    if !cv.is_valid?
    raise "No cell or no layout found" # non: on s'arrête
    end
    layout=lv.cellview(0).layout
    cell_to_rep = lv.active_cellview.cell.name # cell to be replaced is the active/selected cell (Ctrl S)
    cell_to_rep_id = layout.cell_by_name(cell_to_rep) # index value of the cell to be replaced
    if lv.active_cellview.cell.is_library_cell?
    library_to_rep = lv.active_cellview.cell.library.name # library of the cell to be replaced (if any)
    else
    library_to_rep ="local"
    end
    cellindex = "" #declare variable first , so that they are "global"
    libindex = ""

    # dialog construction
    l = QLabel.new(self)
    l.text = "Origin Library"
    winlayout.addWidget(l, 0, 0, 1, 1)

    lib_edit = QLineEdit.new(self)
    lib_edit.text = library_to_rep
    winlayout.addWidget(lib_edit, 0, 1, 1, 1)

    c = QLabel.new(self)
    c.text = "Origin Cell"
    winlayout.addWidget(c, 1, 0, 1, 1)

    cell_edit = QLineEdit.new(self)
    cell_edit.text = cell_to_rep
    winlayout.addWidget(cell_edit, 1, 1, 1, 1)

    nlibl = QLabel.new(self)
    nlibl.text = "New library"
    winlayout.addWidget(nlibl, 2, 0, 1, 1)

    ncelll = QLabel.new(self)
    ncelll.text = "New Cell"
    winlayout.addWidget(ncelll, 3, 0, 1, 1)

    libnames ={}
    libnames = RBA::Library.library_names
    nlib = QComboBox.new(self)
    nlib.addItems(libnames) #populate menu with lib names
    winlayout.addWidget(nlib, 2, 1, 1, 1)
    nlib.activated do
    begin
    libindex = nlib.currentText # replacement library selected
    end
    librar=RBA::Library.library_by_name(libindex)
    libcell = librar.layout
    libcellname = []
    i=1
    libcell.each_top_cell do |t|
    libcellname[i] = libcell.cell_name(t).to_s # extract lib cell names
    i=i+1
    end
    libcellname[0]=""
    ncell = QComboBox.new(self)
    ncell.addItems(libcellname)#populate menu with cell lib names
    winlayout.addWidget(ncell, 3, 1, 1, 1)
    ncell.activated do
    begin
    cellindex = ncell.currentText # replacement cell
    end
    end
    end #of the library/cell selection
    do_ok = QPushButton.new(self) # do button
    do_ok.text = "OK"
    winlayout.addWidget(do_ok, 4, 4, 1, 1)
    do_ok.clicked do # we do the replacement
    begin
    lib=RBA::Library.library_by_name(libindex)
    new_cell_id = layout.add_lib_cell(lib,lib.layout.cell_by_name(cellindex)) # create a new cell id with the replacement cell
    cv.layout.each_cell do |ctr| # scan eall the cell in the layout
    ctr.each_inst do |inst| # scan each instance of theses cells
    if (inst.cell_index == cell_to_rep_id) # scanned cell match the cell to be replaced??
    i = inst.cell_inst # yes
    trans = i.is_complex? ? i.cplx_trans : i.trans # find & copy which type of geometrical transformation is done on this cell
    if i.is_complex?
    instancebis=RBA::CellInstArray::new_inst_cplx(new_cell_id,trans) # create an instance of the repalcement cell with the correct geometrical trans
    else
    instancebis=RBA::CellInstArray::new_inst(new_cell_id,trans)
    end
    ctr.replace(inst,instancebis) # replace the "old" cell instance with the replacement cell instance
    end

    end
    end

    rescue
    end
    end



    end
    end

    $dialog13 = ReplaceDialog.new(Application::instance.main_window)

    $dialog13.exec

    end

    # implementation of contextual menu
    app = RBA::Application.instance
    mw = app.main_window

    menu = mw.menu
    menu.insert_separator("@hcp_context_menu.end", "replace_lib_and_cell")
    menu.insert_item("@hcp_context_menu.end", "replace_lib_and_cell", $replace_lib_cell)


    -----

    Regards
    Joël
Sign In or Register to comment.