Parray

edited October 2022 in Python scripting

i have version 0.27.6 and follow an online tutorial have the code below: no error to run the script and the Parray appears in my first library after clicking instance. a popup window titled "except caught" but no content in the window. and the program closes afterward.

please help show where it could be wrong.

code starts from next line.
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

  • First of all, you can format code by placing a line with three backticks before and after the code (I change it for you, otherwise it's very difficult to read).

    The error message is this:

    I don't know why it does not show up in your case, maybe you should try the latest version.

    So the fix is simple: just add "self" in front of rownum and colnum:

    ...
        for j in range(self.rownum):
         for k in range(self.colnum):
    ...
    

    Matthias

  • Matthias,
    thank you very much for helping fix the bugs. it works right now.

  • edited December 2022

    Hi I am using version 0.27.12 and trying to do Parray. I can't change the shape with partial. Can you please help me with that? Please check the code below.

  • @soumeniit Could you explain in more detail what you are doing and what is your expected outcome?

    Thanks,

    Matthias

  • @Matthias, I am just trying to make a custom Parray. I want to do partial change in one macros but can't change.. Can you please help?

  • You have to first create "variants". Use "Edit/Selection/Make Cell Variants". This will break the array and create a new cell specific for the location where your selected shape is in. After that you can edit this cell without affecting the other instances.

    Matthias

Sign In or Register to comment.