Copy and Pasting Layout Contents/Merging

edited June 2023 in Python scripting

Hello!
I know similar questions have been posted concerning this topic, but for some reason they seem to not be working for me.
I have a large script that calls a toolbox in Python to create specific shapes (that are too complex to be done by hand through KLayout and Python), then compiles everything into a GDS file that I can open through KLayout. This toolbox, however, only creates the shapes, but I need to do more and Python through KLayout can help.
After compiling the large code into a GDS file I started writing below it (same file) in Python using KLayout documentation to copy contents from one file, called 'logo.gds' and paste it into the compiled file, called 'outfile.gds,' creating a new, combined file called 'final.gds.' However, when pasting the contents, it inserts shape already made in the 'outfile.gds' file (completely unrelated to the contents of 'logo.gds') and it's not in the specified point that I noted in the code.

Here is my code currently. I am not entirely sure what I am doing incorrectly. I am new to KLayout and would appreciate any and all help!
Thank you in advance!

import pya
import klayout.db as kdb

# Load the 'outfile.gds' file
outfile_path = 'outfile.gds'
outfile_layout = kdb.Layout()
outfile_layout.read(outfile_path)

# Load the 'logo.gds' file
logo_path = 'logo.gds'
logo_layout = kdb.Layout()
logo_layout.read(logo_path)

# Get the top cell from each layout
outfile_top_cell = outfile_layout.cell('TopCell')
logo_top_cell = logo_layout.top_cell()

# Get the cell reference of the 'TOP' cell in the 'logo.gds' file
logo_top_cell_ref = logo_layout.cell('TOP')

# Get the transformation to apply for merging
merge_transform = kdb.DTrans(-22, 3800)

# Create a new instance of CellInstArray for the logo cell
logo_cell_inst_array = kdb.CellInstArray(logo_top_cell_ref, merge_transform)

# Insert the logo cell into the outfile top cell
outfile_top_cell.insert(logo_cell_inst_array)

# Save the merged layout to a new GDS file
merged_filename = 'final.gds'
outfile_layout.write(merged_filename)

Comments

  • Hello @bmebu,

    you cannot directly reference a cell from a foreign layout. For technical reasons (cell references are integer cell indexes), KLayout can't warn in that case, but the mistake is trying insert a cell from "logo_layout" into "outfile_layout". KLayout isn't OpenAccess.

    The solution is to first copy the new cell intp the target layout. You will receive a new cell index valid for the target layout:

    ...
    logo_top_cell_in_target = outfile_layout.create_cell(logo_top_cell.name)
    logo_top_cell_in_target.copy_tree(logo_top_cell)
    logo_cell_inst_array = kdb.CellInstArray(logo_top_cell_in_target, merge_transform)
    ...
    

    BTW: you do not need "import pya". That is only provided for compatibility with the KLayout-internal dialect.

    Matthias

  • edited June 2023

    Hi Matthias!
    Thank you for your reply. I was actually able to sort of figure it out with the code below based on another discussion you had! :

        import klayout.db as kdb
        layout = pya.Layout()
        lmap = layout.read("outfile.gds")
        TOP = layout.cell("TopCell") #TopCell is the name of the top cell of "outfile.gds"
        layout.read("logo.gds")
        tiles = layout.cell('TOP') #TOP is the name of the top cell of "logo.gds"
        logol = 482 #The length of the contents of logo.gds
        xpoint = xcor1 + 2*crossl + logol - (padl + padgapx - logol)/2 #Finding the specific x-coordinate
        TOP.insert(pya.DCellInstArray(tiles.cell_index(), pya.DTrans(pya.DTrans.R0, pya.DPoint(xpoint, 3550))))
    
        # Write the layout to "outfile.gds" with the specified layer
        layout.write("outfile.gds")
    
        f.close()
    

    However, this automatically inserts the contents of "logo.gds" into the 1st layer of "outfile.gds" regardless of what layer the contents of "logo.gds" are in. How can I alter this so that it can go into another layer, perhaps 3/0 for example?

    Thanks!

  • edited July 2023

    This method bears the risk of cell name conflicts. If the original layout contains a cell called "TOP" it will be spoiled by reading another layout with "TOP" in it. This risk is not present if you load the logo layout into a different layout first.

    For mapping the layers, you can use a layer map in the "read" step of the logo layout. Here is an example:

    lm = pya.LayerMap()
    # maps layer 100/0 to 3/0
    lm.map("100/0 : 3/0")
    
    opt = pya.LoadLayoutOptions()
    opt.create_other_layers = False    # disables all other layers
    opt.layer_map = lm
    layout.read("logo.gds", opt)
    

    Matthias

Sign In or Register to comment.