It looks like you're new here. If you want to get involved, click one of these buttons!
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:
BTW: you do not need "import pya". That is only provided for compatibility with the KLayout-internal dialect.
Matthias
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! :
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!
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:
Matthias