#+TITLE: Python CAD Tutorial 09 - Workspace class 
#+DATE: 2013-10-09 12:00:00 UTC
#+DESCRIPTION: Create a workspace class to hold our objects 
#+FILETAGS: GTK-3:python:cad:opengl
#+CATEGORY: python
#+SLUG: 09-cad-workspace-class
#+THUMBNAIL: ../../../images/cad/tut09-interface.png
#+BEGIN_COMMENT
.. title: Python CAD Tutorial 09 - Workspace class 
.. slug: 09-cad-workspace-class.org
.. date: 2013-10-09 12:00:00 UTC
.. tags: GTK-3, python, cad, opengl
.. category: python
.. description: Create a workspace class to hold our objects 
.. type: text
#+END_COMMENT

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


#+CAPTION: workspace class
[[../../../images/cad/tut09-interface.png]]

The workspace class below is a management class. It will contain various methods for working with the visible objects. It will also manage our layers. For now we will add simple functions, for example 'append' (which will add objects to a layer).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 cad.point import createpoint
from OpenGL.GL import *
from OpenGL.arrays import vbo
from numpy import array


#lets create a dict object we can name our layers from here
class workspace(dict):
    selected_shape = 'workspace'
    selected_part = None

    def reset(self):
        for k in self.keys():
            del(self[k])
        self['workspace'] = shape.createshape()

    def create(self, item, name=None):
        if name is not None:
            self.selected_part = name
        if self.selected_part is not None:
            if not self.get(self.selected_part):
                self[self.selected_part] = shape.createshape()
            self[self.selected_part].append(item)
            self.selected_shape = self[self.selected_part].count()

    def append(self, item, name=None):
        self[self.selected_part].primitives[self.selected_shape].append(item)

    def objects_iter(self):
        for layer in self.keys():
            for shape in self[layer]:
                yield shape

    def set(self, name, value):
        for layer in self.values():
            for item in layer.primitives:
                item.display_color = value
                self.color = value

    #handle drawing our shapes here
    def draw(self):
        for item in self.values():
            item.draw()
#+END_SRC
