Intro to Python Scripting: 21 Polygon Class Pt2

Software: 
Video Duration: 
10 minutes
Author: 
Zach Downey

In this tutorial we clean up our polygon python class a bit and make it a little more user friendly by changing the initialization/constructor method to include a user provided centerpoint. This means we need to rewrite the code a bit and we also need to pass a new origin parameter when we instantiate our class. 

import rhinoscriptsyntax as rs
import math
 
#class definition
class MyPolygon:
    #polygon initialization or constructor method
    def __init__(self,radius,sides,origin):
        self.radius = radius
        self.sides = sides
        self.origin = origin
        origin = self.origin
        theta = (2*math.pi)/self.sides
        x = origin[0] + self.radius
        y = origin[1]
        z = origin[2]
        pt01 = rs.AddPoint(x,y,z);
        pts = []
        pts.append(pt01)
        degrees = theta*(180/math.pi)
        for i in range(1,self.sides):
            tempPt = pts[-1]
            newPt = rs.RotateObject(tempPt,origin,degrees,None,True)
            pts.append(newPt)
        pts.append(pt01)
        self.polygon = rs.AddPolyline(pts);
 
 
    def fillPolygon(self):
        return rs.AddPlanarSrf(self.polygon)
 
    def extrudePolygon(self,height):
        startPt = self.origin;
        newZ = self.origin[2]+height
        endPt = [self.origin[0],self.origin[1],newZ]
        return rs.ExtrudeCurveStraight(self.polygon, startPt, endPt)
 
 
 
userpt = rs.GetPoint("Pick a centerpoint")
polygon0 = MyPolygon(6,6,userpt)
polygon0.fillPolygon()
polygon0.extrudePolygon(5)
 
 
userpt = rs.GetPoint("Pick a centerpoint")
polygon1 = MyPolygon(8,12,userpt)
polygon1.fillPolygon()
polygon1.extrudePolygon(10)

Rating

Please rate this tutorial below. Thanks.

5
Average: 4.9 (9 votes)

Comments

Can anyone tell me why the self is removed from self.origin at 3:35 minutes of the video? The script seems to work fine if you keep self.origin (this makes the most sense to me). I am still trying to understand Class and the self function. Thanks!!!

Want to Contribute?

Want to be an author? Drop us a line here we'd love to have you.

Already have a video you'd like to post? Send us a link and we'll get you going.

:)