Renaming of cells through command line

edited April 2015 in Ruby Scripting
Hi,

I am new to Ruby scripting and I am trying to rename all cells in input gds by a prefix "hello_".
I am able to do it by inserting a menu item as shown below:

File klayout_test.rbm :

class MenuAction < RBA::Action
def initialize( title, shortcut, &action )
self.title = title
self.shortcut = shortcut
@action = action
end
def triggered
string = "hello_*"


app = RBA::Application.instance

mw = app.main_window
view = mw.current_view

layout = view.cellview(view.active_cellview_index).layout

layout.each_cell do |cell|
cell_name = layout.cell_name(cell.cell_index)
new_cell_name = string.gsub(/#/, cell.cell_index.to_s).gsub(/\*/, cell_name)
if cell_name != new_cell_name
layout.rename_cell(cell.cell_index, new_cell_name)
end
end



opt = RBA::SaveLayoutOptions.new

view.save_as(0, # cell index, for some reason it needs to be "0"
"output.gds", # file name
0, # do not gzip output
opt)

end
private
@action
end


$rename_cells_handler = MenuAction.new( "Rename Cells", "" )


app = RBA::Application.instance
mw = app.main_window

menu = mw.menu
menu.insert_separator("tools_menu.end", "sep_rename_cells")
menu.insert_item("tools_menu.end", "rename_cells", $rename_cells_handler)


When I read the above file in klayout by :

klayout input.gds -klayout_test.rbm

klayout window opens up with "Rename Cells" option in tools. On selecting the option in GUI, all cells are renamed by a prefix "hello_" and the resulatant gds is saved as output.gds. However I want to use it in non GUI mode. For that I wanted to trigger the above procedure through command line. If I use the following command :

$rename_cells_handler.triggered

I get an error saying "Undefined method `active_cellview_index' for nil:NilClass"

Please help me out on this.

Comments

  • edited November -1

    Hi,

    when you execute the script directly when loading it, you will not find a layout since that is loaded after the script.

    But there is a far easier solution: you can instantiate a layout without user interface and directly manipulate the content. This is your sample written that way:

    string = "hello_*"
    
    layout = RBA::Layout::new
    layout.read($input)
    
    layout.each_cell do |cell|
        cell_name = layout.cell_name(cell.cell_index)
        new_cell_name = string.gsub(/#/, cell.cell_index.to_s).gsub(/\*/, cell_name)
        if cell_name != new_cell_name
            layout.rename_cell(cell.cell_index, new_cell_name)
        end
    end
    
    layout.write($output)
    

    You can save this script to a file, for example "test.rb" and run it this way:

    klayout -b -r test.rb -rd input=input.gds -rd output=output.gds
    

    "-b" will run KLayout in batch mode which switches off several features which you don't require in this script. "-rd x=y" will define a global variable $x with a value "y", so you can access input and output as "$input" and "$output" in the script.

    Matthias

  • edited November -1
    Thanks a lot Matthias :)
Sign In or Register to comment.