heitzmann / gdstk

Gdstk (GDSII Tool Kit) is a C++/Python library for creation and manipulation of GDSII and OASIS files.
https://heitzmann.github.io/gdstk/
Boost Software License 1.0
324 stars 78 forks source link

How to create nested cells using gdstk #237

Closed GaN-T closed 4 months ago

GaN-T commented 5 months ago

How do I create a heirarchial cell structure in gdstk when viewing the file in Klayout? The example of cell references in the documentation results in independent cells appearing in the viewer cell tree.

import gdstk
import numpy as np

lib = gdstk.Library()
# Layer/datatype definitions for each step in the fabrication
ld = {
    "full etch": {"layer": 1},
    "partial etch": {"layer": 2},
    "lift-off": {"layer": 0},
}

p1 = gdstk.rectangle((-3, -3), (3, 3), **ld["full etch"])
p2 = gdstk.rectangle((-5, -3), (-3, 3), **ld["partial etch"])
p3 = gdstk.rectangle((5, -3), (3, 3), **ld["partial etch"])
p4 = gdstk.regular_polygon((0, 0), 2, 6, **ld["lift-off"])

# Create a cell with a component that is used repeatedly
contact = lib.new_cell("CONTACT")
contact.add(p1, p2, p3, p4)

cutout = gdstk.Polygon(
    [(0, 0), (5, 0), (5, 5), (0, 5), (0, 0), (2, 2), (2, 3), (3, 3), (3, 2), (2, 2)]
)
# Create a cell with the complete device
device = lib.new_cell("DEVICE")
device.add(cutout)
# Add 2 references to the component changing size and orientation
ref1 = gdstk.Reference(contact, (3.5, 1), magnification=0.25)
ref2 = gdstk.Reference(contact, (1, 3.5), magnification=0.25, rotation=np.pi / 2)
device.add(ref1, ref2)

# The final layout has several repetitions of the complete device
main = lib.new_cell("MAIN")
main.add(gdstk.Reference(device, (0, 0), columns=3, rows=2, spacing=(6, 7)))

# lib.add(main)
lib.write_gds("test.gds")

How do we achieve a nested structure e.g. shown here

Rishabhgoyal07 commented 5 months ago

@GaN-T, you have to reference the cells in the main cell to create nested cell.

GaN-T commented 5 months ago

Could you please show how the example code I gave in my post would be modified to achieve this?

Rishabhgoyal07 commented 4 months ago

in the *lib.add(main,main.dependencies(True)**), this will add all the dependencies of the referenced cell, and all the cells will be shown with their hierarchy

GaN-T commented 4 months ago

this didn't change the output from the example I initially provided referencing the "Getting started" documentation. The heirarchy is there, it is just not shown in the KLayout Cell viewer. Please see this link for an example of what I am trying to do

Rishabhgoyal07 commented 4 months ago

paste your klayout screenshot

GaN-T commented 4 months ago

It doesn't appear as a nested tree like I want even though the hierarchy is present in the layout. When I flatten cell list I can see the components in the list too, but as individual cells, not nested below main.


import gdstk
import numpy as np

lib = gdstk.Library()
# Layer/datatype definitions for each step in the fabrication
ld = {
    "full etch": {"layer": 1},
    "partial etch": {"layer": 2},
    "lift-off": {"layer": 0},
}

p1 = gdstk.rectangle((-3, -3), (3, 3), **ld["full etch"])
p2 = gdstk.rectangle((-5, -3), (-3, 3), **ld["partial etch"])
p3 = gdstk.rectangle((5, -3), (3, 3), **ld["partial etch"])
p4 = gdstk.regular_polygon((0, 0), 2, 6, **ld["lift-off"])

# Create a cell with a component that is used repeatedly
contact = lib.new_cell("CONTACT")
contact.add(p1, p2, p3, p4)

cutout = gdstk.Polygon(
    [(0, 0), (5, 0), (5, 5), (0, 5), (0, 0), (2, 2), (2, 3), (3, 3), (3, 2), (2, 2)]
)
# Create a cell with the complete device
device = lib.new_cell("DEVICE")
device.add(cutout)
# Add 2 references to the component changing size and orientation
ref1 = gdstk.Reference(contact, (3.5, 1), magnification=0.25)
ref2 = gdstk.Reference(contact, (1, 3.5), magnification=0.25, rotation=np.pi / 2)
device.add(ref1, ref2)

# The final layout has several repetitions of the complete device
main = lib.new_cell("MAIN")
main.add(gdstk.Reference(device, (0, 0), columns=3, rows=2, spacing=(6, 7)))

lib.add(main, *main.dependencies(True))
lib.write_gds("test.gds")

Could you post your output?

nmz787-intel commented 4 months ago

Using the original/first post code, I get this output image

You may need to right-click the cell list, and then uncheck "Flat Cell List" image

You may also notice that you can select which cell you want to start viewing the hierarchy from, with the so-called "Show As New Top"

GaN-T commented 4 months ago

Thanks this is exactly it!