Sorry for dumb question - How I can made my layout from previously saved cells?

SiaSia
edited August 2015 in General
For example, I already have enough number of GDS cells (my own and other designs)
Now I want to merge some of them together, to create new layout.
But I don't know, how I may do such work with KLayout :-(
I can open and edit each cell, but I couldn't understand, how I can add saved cell (from file) to actual layout.
Also I couldn't find "Library Builder/Browser" or similar tool.
Could anybody help me?

Thanks!

Comments

  • edited August 2015

    Not a dumb question! The short answer is to use Layout#read method.

    Here is some code. It takes an array of folder names (in 'paths' variable) and looks inside these folders. For every gds and oas file it finds, it instances this once at 0,0. You may instead want to instance them as top cells rather than as children of an existing top cell, so you would just delete the whole "top_cells.each" block.

    # Assembles a layout from files.
    #
    # To use, open new layout with one empty top cell. Modify relevant lines below 
    # and then run. For every gds or oas file it finds inside any of the folders 
    # listed in the 'paths' variable, it will instance them once at 0,0 under the 
    # top cell.
    
    include RBA
    
    paths = ['C:\folder1','C:\folder2'] # The local folder(s) with all the separate oas files in
    
    save_output = false # false means just assemble layout, true means assemble and then save to out_filename
    out_filepath = 'C:\out_filepath\\' # Put two \\'s on the end
    out_filename = 'filename.oas.gz'
    opt = SaveLayoutOptions::new
    opt.gds2_libname = "IL1"
    opt.format = "OASIS"
    
    lv = Application.instance.main_window.current_view
    if lv == nil; raise "No view selected"; end
    master_layout = lv.active_cellview.layout
    top_cell_idx = lv.active_cellview.cell.cell_index
    top_cell_name = master_layout.cell(top_cell_idx).name
    
    begin
      # Convert all to correct slash/backslash etc format
      paths.map! { |p| File.expand_path(p) }
    
      # In each supplied path, loop through the array of filenames, and select each one that ends in gds or oas
      files = []
      paths.each { |p|
        a = Dir.entries(p).select { |f| ext = f[-3..-1]; ext == "gds" || ext == "GDS" || ext == "oas" || ext == "OAS" } # Find the ones with correct extension
        a.map! { |v| p + '/' + v } # Concatenate the path p with the filename v
        files << a
      }
      files = files.flatten
      len = files.length
      p "Found #{len} files:"
      p files
      p "Adding these to: #{File.expand_path(out_filepath + out_filename)}"
    
      progress = RelativeProgress::new("Loading...", files.length)
    
      files.each_with_index { |f,i|
        p "Reading #{f}"
        master_layout.read(f) # This reads it in but instantiates it as new top cells rather than child cells under "TOP". That is not my desired behavior, but it's what we'll do first.
    
        # Next we find the just-read-in layout cells, and instance them at 0,0 under the top cell
        top_cells = master_layout.top_cells
        top_cells.each { |c|
    
          # Skip the original top cell
          next if c.name == top_cell_name 
    
          # Instantiate the other cells under the top cell
          cell_index = c.cell_index
          t = Trans.new
          master_layout.cell(top_cell_idx).insert(CellInstArray.new(cell_index, t))
    
        }
    
        progress.inc
      }
      lv.add_missing_layers
      lv.zoom_fit
    
      if save_output
        p "Writing output file..."
        master_layout.write(File.expand_path(out_filepath + out_filename), opt)
        p "Done!"
      end
    
    ensure
      progress.destroy
    end
    

    I've been meaning to write a nicer version of that with a multi-file chooser dialog and an option for recalling transformation setpoints for each file (rather than always having it instance at 0,0). When that's done I'll add it to The Red Toolbox.

  • SiaSia
    edited August 2015
    Thank You, David!
    I try this script tomorrow and will write results.
    Thanks again for fast responce.
  • edited August 2015

    Hi all,

    @David: thanks again for providing the script :-)

    The manual way is to use File/Import/Other File into Current. There are multiple options available and the feature is documented here: http://klayout.de/doc/manual/import_layout.html.

    The scripted solution using Layout#read is very efficient and easy to use but it bears a certain risk: if the layouts you import contain cells with the same name they will be merged into a single cell. If the cell names are guaranteed to be different (including child cells), there won't be any issue.

    The safe option is to read the individual layouts into a new layout object and copy the contents over into the master layout using Cell#copy_tree.

    Matthias

  • SiaSia
    edited November -1
    Thanks!

    Sorry for delay.
    I already had imported files manually, but I definitely will try your script.

    Overall, as topological editor - your KLayout, by my opinion, even more comfortable, than the Tanner L-edit 15. Thanks, Matias, it's The Great tool!

    But now I have another problem with hierarchical design.
    For example, I have some layout, which contained an array of one cell, named, say, ACell.
    I need replace all instances of ACell on BCell, with exactly same coordinates and angles.
    How I can do it easy?
    Also, is any other "tips and hints" about "right" creation library of cells ?

    Thanks again!
  • edited August 2015

    Edit > Search and Replace, Instance cell name ~ ACell, Replace with BCell (note: BCell must already exist in the layout, and I don't think you can't use library cells using this function. If you want to replace with lib cells you will need to do a scripted solution). ACell will become a new top-level cell in this case.

    Second option: You can also right-click on "ACell" in the cell tree on the left, choose "Replace Cell", then type "BCell". All ACell instances will be replaced with BCell instances. Except, then your ACells will be deleted entirely from the layout (no matter which of Shallow/Deep/All Replace option you choose. But, it is sometimes what you want.

  • SiaSia
    edited August 2015
    Thanks again!
    Last advice - is exactly that, what i need. And it works OK.
Sign In or Register to comment.