#+TITLE: Python CAD Tutorial 08 - Drawing a polygon & add generic shape class 
#+DATE: 2013-10-08 12:00:00 UTC 
#+DESCRIPTION: Draw a polygon and create a generic shape base class 
#+FILETAGS: GTK-3:python:cad:opengl 
#+CATEGORY: python
#+SLUG: 08-cad-drawing-a-polygon-adding-shape-class
#+THUMBNAIL: ../../../images/cad/tut08-interface.png
#+BEGIN_COMMENT
.. title: Python CAD Tutorial 08 - Drawing a polygon & add generic shape class 
.. slug: 08-cad-drawing-a-polygon-adding-shape-class.org
.. date: 2013-10-08 12:00:00 UTC
.. tags: GTK-3, python, cad, opengl
.. category: python
.. description: Draw a polygon and create a generic shape base class 
.. type: text
#+END_COMMENT

[[https://code.launchpad.net/~oly/fabricad/tut08][View/Download Code]]


#+CAPTION: Polygon
[[../../../images/cad/tut08-interface.png]]

We created a grid in the previous tutorial. Now let's add a polygon class and a shape object to contain our primitives.The polygon class is basically an extension of the line class. It works in the same way, however we have added an append method which keeps adding points to the polygon on each click. The draw method has then been adjusted to loop over and draw the polygon from the point list.

#+BEGIN_SRC python
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[0].x, self.points[0].y, self.points[0].z)
                glVertex3f(self.points[-1].x, self.points[-1].y, self.points[-1].z)
            glEnd()

        for item in self.points:
            item.draw()
#+END_SRC

The shape class below is basically just a container object for storing our various primitives like lines, circles and polygons. This class has methods to loop over and add the primitives store,. This will be useful later on for changing properties on mass.In the main mycad.py file you can see that our new polygon is contained and updated through the shape class. Later on we will attach this to our GUI so we can dynamically add primitives as we need them.

#+BEGIN_SRC python
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()
#+END_SRC

