Python CAD Tutorial 09 - Workspace class
rss_feed

Python CAD Tutorial 09 - Workspace class

homeHome
pagesGTK-3
pagespython
pagescad
pagesopengl

View/Download Code

workspace class

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.

 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
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()