Modifying default parameters is invalid

edited May 2023 in Python scripting
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 = 50)     
    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 = 50)     
    self.param("coly", self.TypeInt, "Col Y", default = 0)     
    self.param("colm", self.TypeInt, "Col Mag", default = 1)     
    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.bbox().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(rownum):
     for k in range(colnum):
      tt=pya.DCplxTrans(self.rowx*j+self.colx*k, self.rowy*j+self.coly*k)
      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()

Comments

  • edited May 2023

    Why is changing the default parameters invalid here

    # Enter your Python code here
    
    import pya
    import math
    # import klayout.db as db
    """
    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.
    """
    ly = pya.Layout()
    top_cell = ly.create_cell("TOP")
    
    class Pnumber(pya.PCellDeclarationHelper):
      """
      The PCell declaration for the circle
      """
    
      def __init__(self):
    
        # Important: initialize the super class
       # global nstart,nend,nstep,nmag,ngap 
        super(Pnumber, self).__init__()
        self.param("l", self.TypeLayer, "Layer", default = pya.LayerInfo(1, 0))
        self.param("nstart", self.TypeInt, "Number_Start", default = 1)     
        self.param("nend", self.TypeInt, "Number_End", default = 10) 
        self.param("nstep", self.TypeInt, "Number_Step", default = 3) 
        self.param("nmag", self.TypeInt, "Number_Mag", default = 100) 
        self.param("ngap", self.TypeDouble, "Number_Gap", default = 20.0) 
    
    
    
    
      def display_text_impl(self):
        # Provide a descriptive text for the cell
        return "Pnumber(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.bbox().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
    
        i = self.nstart
        n_now = self.nstart
    
        for self.n_now in range(self.nstart,self.nend):
          parameters = {
          "layer": pya.LayerInfo(1,0),
          "mag": self.nmag,
          "text": n_now
          }
    
    
          n_now = i * self.nstep + 1
          print(n_now)
          cell = ly.create_cell("TEXT", "Basic", parameters)#TEXT表示创建的是文本类
          y = 0.0
          x = self.n_now * self.ngap
          print(x)
          top_cell.insert(pya.CellInstArray(cell.cell_index(), pya.DTrans(x,y)))
          # DTrans创建的一定是浮点型
          i += 1
          print(i)
    
    
    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("Pnumber", Pnumber())
        # 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()
    
  • edited May 2023

    @zhenganqi Sorry, I don't understand. Please help me. What is the problem?

    And please mark Python code with a triple backtick line before and after so it gets formatted as code. Like on github. I took the liberty to edit your post.

  • Maybe some things are "baked in" and if you want to change
    the recipe, you have to go all the way back to making dough?

  • If you want to make a cake that is different from the one you just got - than that's the right approach :)

Sign In or Register to comment.