import pya import math """ This sample PCell implements a library called "MyLib" with a single PCell that draws a circle. It demonstrates the basic implementation techniques for a PCell and how to use the "guiding shape" feature to implement a handle for the circle radius. 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 circle """ 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.TypeInt, "Row X", default = 0) self.param("rowy", self.TypeInt, "Row Y", default = 5) self.param("rowm", self.TypeInt, "Row Mag", default = 1) self.param("rowa", self.TypeInt, "Row Ang", default = 0) self.param("colnum", self.TypeInt, "Col Number", default = 4) self.param("colx", self.TypeInt, "Col X", default = 5) self.param("coly", self.TypeInt, "Col Y", default = 0) self.param("colm", self.TypeInt, "Col Mag", default = 0) self.param("cola", self.TypeInt, "Col Ang", 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: we can use any shape which # has a finite bounding box return self.shape.is_polygon() def parameters_from_shape_impl(self): # Implement the "Create PCell from shape" protocol: we set r and l from the shape's # bounding box width and layer 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 use the center of the shape's # bounding box to determine the transformation return pya.Trans(pya.Point(0,0)) def produce_impl(self): # This is the main part of the implementation: create the layout # fetch the parameters 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()