Extract layer list and top cell name from gds file

edited May 2018 in Ruby Scripting
Hello,

Is it possible extract the layer list and top cell name from a gds file using a Ruby script?

Many thanks in advance for any suggestion and support.

Best regards

Luciano

Comments

  • edited November -1

    Hi Luciano,

    there is a similar post here: https://www.klayout.de/forum/comments.php?DiscussionID=1062&page=1#Item_5.

    Here is a similar example in Ruby:

    layout = RBA::Layout::new
    layout.read("/home/matthias/x.gds")
    
    puts "Top cell: " + layout.top_cell.name
    
    puts "Layers:"
    layout.layer_indices.each do |layer_id|
      layer_info = layout.get_info(layer_id)
      puts layer_info.to_s
    end
    

    Regards,

    Matthias

  • edited May 2018

    If you save the following script in a file called "cell_layers.rbm" in the same directory as the KLayout installation with the program klayout.exe, you will get it int the tools menu. It also save the layers in a file called "cell_layers.txt" in the same directory as your GDS2 file.

    Laurent

    #
    # DESCRIPTION: Compute and output the layers list of a cell
    #
    # Run the script with
    #   klayout -rm cell_layers.rbm ...
    # or put the script as "cell_layers.rbm" into the installation path
    #
    
    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
    
    $cell_layers_handler = MenuAction.new( "Cell Layers List", "" ) {
        app = RBA::Application.instance
        mw = app.main_window
        view = mw.current_view
        cv = view.cellview(view.active_cellview_index)
    
        if !cv.is_valid?
            return
        end
    
        cell = cv.cell
        dbu = cv.layout.dbu
    
        text = "Cell : #{cv.layout.cell_name(cv.cell_index)}\n"
        text += "Layers List :\n\n"
    
        cv.layout.layer_indices.each do |layer_id|
            layer_info = cv.layout.get_info(layer_id)
    
            lp_found = nil
            iter = view.begin_layers
            while !iter.at_end?
                lp = iter.current
                if lp.layer_index == layer_id
                    lp_found = lp 
                end
                iter.next
            end
    
            if lp_found.nil?
                text += "#{layer_info.to_s}\n"
            else
                text += "#{layer_info.to_s}  :  #{lp_found.name}\n"
            end
        end
    
        RBA::MessageBox::info("Cell Layers", text, RBA::MessageBox::b_ok)
    
    # save the data in the file :
        File.open(File.join(File.dirname(RBA::CellView::active.filename), "cell_layers.txt"), "w") do |file|
            file.puts(text)
        end
    }
    
    app = RBA::Application.instance
    mw = app.main_window
    
    menu = mw.menu
    menu.insert_item("tools_menu.end", "cell_layers", $cell_layers_handler)
    
  • edited May 2018
    Thanks You both.

    Laurent, but if I would like run script on this way

    klayout -e mygdsfile -r cell_layers.rbm

    and automatically create the file cell_layers.txt

    how can modify the rbm script?

    Many thanks in advance

    Regards

    Luciano
  • edited May 2018
    No sorry, I always run KLayout in GUI.

    Laurent
  • edited November -1

    Hi,

    first, please use the ".rb" extension for your script - ".rbm" is deprecated.

    You can use this script ("script.rb"):

    layout = RBA::Layout::new
    layout.read($infile)
    
    File.open($outfile) do |file|
      file.puts "Top cell: " + layout.top_cell.name
      file.puts "Layers:"
      layout.layer_indices.each do |layer_id|
        layer_info = layout.get_info(layer_id)
        file.puts layer_info.to_s
      end
    end
    

    And use it like this:

    klayout -b -r script.rb -rd infile=input.gds -rd outfile=layers.txt
    

    Matthias

Sign In or Register to comment.