Skip to content

Instantly share code, notes, and snippets.

@g2hollow
Created August 29, 2020 18:14
Show Gist options
  • Save g2hollow/dab01263576988c2e3d0a849cad204af to your computer and use it in GitHub Desktop.
Save g2hollow/dab01263576988c2e3d0a849cad204af to your computer and use it in GitHub Desktop.
klayout marco pcell parray
import pya
import math
"""
This sample PCell implements a library called "MyLib" with a single PCell that
draws a Parray. This parametric array allows the user to modify the angle and
magnification of a shape along the rows and columns independently.
NOTE: after changing the code, the macro needs to be rerun to install the new
implementation. The macro is also set to "auto run" to install the PCell
when KLayout is run.
"""
class Parray(pya.PCellDeclarationHelper):
"""
The PCell declaration for the Parray
"""
def __init__(self):
# Important: initialize the super class
super(Parray, self).__init__()
# declare the parameters
self.param("l", self.TypeLayer, "Layer", default = pya.LayerInfo(1, 0))
self.param("ref", self.TypeShape, "", default = pya.DPoint(0, 0))
self.param("shp",self.TypeShape,"",default = pya.Polygon([pya.DPoint(-500, -500), pya.DPoint(1000, 0), pya.DPoint(0, 1000)]))
self.param("rownum", self.TypeInt, "Row Number", default = 4)
self.param("rowx", self.TypeDouble, "Row X", default = 0)
self.param("rowy", self.TypeDouble, "Row Y", default = 5)
self.param("rowm", self.TypeDouble, "Row Mag", default = 0)
self.param("rowa", self.TypeDouble, "Row Angle", default = 0)
self.param("colnum", self.TypeInt, "Col Number", default = 4)
self.param("colx", self.TypeDouble, "Col X", default = 5)
self.param("coly", self.TypeDouble, "Col Y", default = 0)
self.param("colm", self.TypeDouble, "Col Mag", default = 0)
self.param("cola", self.TypeDouble, "Col Angle", default = 0)
def display_text_impl(self):
# Provide a descriptive text for the cell
return "Parray(L=" + str(self.l) + ")"
def coerce_parameters_impl(self):
pass
def can_create_from_shape_impl(self):
# Implement the "Create PCell from shape" protocol: this particular script is
# only designed to handle polygons
return self.shape.is_polygon()
def parameters_from_shape_impl(self):
self.shp = self.shape.dpolygon
self.ref = self.shape.dbbox().center()
self.l = self.layout.get_info(self.layer)
def transformation_from_shape_impl(self):
# Implement the "Create PCell from shape" protocol: we don't apply any
# transformation in this case, since we're pulling the coordinates of
# the verticies of the polygon.
return pya.Trans(pya.Point(0,0))
def produce_impl(self):
# This is the main part of the implementation: create the layout
# hack to get the correct units. We first add a copy of the shape
# to the layout and return its coordinates in um.
self.cell.shapes(self.l_layer).insert(self.shp)
for j in self.cell.shapes(self.l_layer).each():
self.shp = j.dpolygon
for j in range(self.rownum):
for k in range(self.colnum):
tt = pya.DCplxTrans(self.ref.x+self.rowx*j+self.colx*k,self.ref.y+self.rowy*j+self.coly*k)
tt = tt*pya.DCplxTrans(1+self.rowm*j+self.colm*k,self.rowa*j+self.cola*k,False,0,0)
tt = tt*pya.DCplxTrans(-self.ref.x,-self.ref.y)
self.cell.shapes(self.l_layer).insert(tt.trans(self.shp))
class MyLib(pya.Library):
"""
The library where we will put the PCell into
"""
def __init__(self):
# Set the description
self.description = "My First Library"
# Create the PCell declarations
self.layout().register_pcell("Parray", Parray())
# That would be the place to put in more PCells ...
# Register us with the name "MyLib".
# If a library with that name already existed, it will be replaced then.
self.register("MyLib")
# Instantiate and register the library
MyLib()
@ashkanr65
Copy link

Hi,

Imagine I have a Pcell (like a transistor with W, L, and Number of fingers variables) I want to use this Pcell:
1- To create an array of transistors with different features but static L
2- Use this Pcell inside another Pcell.
How should I do that?
Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment