Python CAD Tutorial 09 - Workspace class



View/Download Code

tut09-interface.png

Figure 1: 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.

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

Comments