Python CAD Tutorial 08 - Drawing a polygon & add generic shape class

# Python CAD Tutorial 08 - Drawing a polygon & add generic shape class

homeHome
pagesGTK-3
pagespython
View/Download Code ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44``` ```from point import createpoint class createpolygon: name = 'polygon' points = [] closed = True def __init__(self, point): self.points = [] self.points.append(createpoint(point)) self.color = 0, 1, 0 self.display_color = 0, 1, 0 self.newPoint = None def append(self, point): self.points.append(createpoint(point)) def glvertex(self): glVertex3f(self.p1.x, self.p1.y, self.p1.z) #vertexs to draw a line use with glbegin(GL_LINES) def glvertexlist(self): glVertex3f(self.p1.x, self.p1.y, self.p1.z) glVertex3f(self.p2.x, self.p2.y, self.p2.z) def get_points(self): for p in self.points: yield (p) def draw(self): glColor3f(*self.display_color) if len(self.points) > 1: glBegin(GL_LINES) for n in xrange(0, len(self.points) - 1): glVertex3f(self.points[n].x, self.points[n].y, self.points[n].z) glVertex3f(self.points[n + 1].x, self.points[n + 1].y, self.points[n + 1].z) if self.closed is True: glVertex3f(self.points.x, self.points.y, self.points.z) glVertex3f(self.points[-1].x, self.points[-1].y, self.points[-1].z) glEnd() for item in self.points: item.draw() ```
 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26``` ```from point import createpoint class createshape: location = createpoint((0, 0, 0)) primitives = [] def __init__(self, name=None): self.primitives = [] def append(self, item): self.primitives.append(item) def __getitem__(self, n): return self.primitives[n] #temporary to get last primitive added to the scene def update_last(self): return self.primitives[-1] def count(self): return len(self.primitives) - 1 def draw(self): for item in self.primitives: item.draw() ```