Write Child Cells
false
false
true
write_child_cells
@hcp_context_menu.end
ruby
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# DESCRIPTION: Write all child cells of the current cell to individual files
#
# Install the script with
# klayout -rm write_childcells.lym ...
# or put the script as "write_childcells.lym" into the installation path
# (on Unix for version <=0.21: set $KLAYOUTPATH to the installation folder).
#
# The script installs a new menu entry at the end of the cell list context
# menu: "Write Child Cells". This function asks for the hierarchy level and
# writes all cells at this level (below the current cell) to files called
# "{cellname}.gds".
# Collect all cells at hiearchy level "level" below the cell "from_cell"
# into the array "cells" which is used as a set.
# The keys of "cells" will be the cell indexes of the cells collected.
def get_cells_at_hierarchy_level(layout, from_cell, level, cells)
from_cell.each_child_cell do |cc|
if level == 1
cells[cc] = 1
else
get_cells_at_hierarchy_level(layout, layout.cell(cc), level - 1, cells)
end
end
end
# Main functionality
app = RBA::Application.instance
mw = app.main_window
# get the current layout view
lv = mw.current_view
if lv == nil
raise "No view selected"
end
# get the current cell view (the one selected in the hierarchy browser)
cv = lv.cellview(lv.active_cellview_index)
if !cv.is_valid?
raise "No layout selected"
end
# fetch the hierarchy level from which to write the cells
level = RBA::InputDialog.get_int_ex("Hierachy Level", "Specify the hierarchy level below cell #{cv.layout.cell_name(cv.cell_index)}, layout @#{lv.active_cellview_index+1} from which to write the cells", 1, 1, 1000, 1)
if level.has_value?
# gather the cells to write
cells_to_write = {}
get_cells_at_hierarchy_level(cv.layout, cv.cell, level.value, cells_to_write)
# loop over all child cells of the current cell
cells_to_write.each do |cc,dummy|
# make a cell object reference from the cell index
child_cell = cv.layout.cell(cc)
# get the cell's name
cell_name = cv.layout.cell_name(cc)
# write the cell as "{cellname}.gds"
file_name = "#{cell_name}.gds"
options = RBA::SaveLayoutOptions.new
options.add_cell(cc) # select this cell for writing
puts "Writing #{file_name} .."
cv.layout.write(file_name, false, options)
end
end