Quick Question

Hi, I'm new to all this so mind the dumb question. I'm trying to have a library of functions in a .py file that I call freely from an .lym file.

Let's say I have,

import pya
import math

def circleMake(numPts, radius):
  pts = []
  numPts = 500
  radius = 10000
  angle = 2 * math.pi / numPts

  for incAngle in range(0, numPts): 
    pts.append(pya.Point.from_dpoint(pya.DPoint(radius * math.cos(incAngle * angle), radius * math.sin(incAngle * angle))))

  poly = pya.Polygon(pts)

layout = pya.Layout()
top = layout.create_cell("TOP"
l1 = layout.layer(1, 0)

circleMake(100, 50)


and I try to put circleMake into a different file. It says that layout/top/etc is not defined obviously. I'm not sure what the common solution is to this problem?

Thank you for your time!


  • Hi,

    welcome to the new style, BTW :)

    "layout" can't be accessed from a function inside a module. But of course you can add this to the list of parameters:

    def circleMake(layout, top, layer, numPts, radius):
    circleMake(layout, top, layer, 100, 50)

    In an object-oriented world you might consider using an object for this purpose:

    class CirclePainter(object):
      layout = None
      top = None
      def __init__(self, layout, top):
         self.layout = layout
         self.top = top
      def drawCircle(self, layer, numPts, radius):
         ... use self.layout and self.top in your code instead of layout and top ...
    circlePainter = CirclePainter(layout, top)
    circlePainter.drawCircle(l1, 100, 50)

    Using an object will make you think in terms of required information, status and services. The class provides the interface by which your outside code can make use of these services.


Sign In or Register to comment.