Commit 8e044e9d authored by sumpfralle's avatar sumpfralle

r562@erker: lars | 2010-02-02 05:36:31 +0100

 added a "Settings" object that connects code and Gui
 implement "bounds" Gui settings


git-svn-id: https://pycam.svn.sourceforge.net/svnroot/pycam/trunk@85 bbaffbd6-741e-11dd-a85d-61de82d9cad9
parent 195d6200
...@@ -4,6 +4,7 @@ from OpenGL.GL import * ...@@ -4,6 +4,7 @@ from OpenGL.GL import *
from OpenGL.GLUT import * from OpenGL.GLUT import *
from OpenGL.GLU import * from OpenGL.GLU import *
import pycam.Importers.STLImporter import pycam.Importers.STLImporter
import pycam.Gui.Settings
import pygtk import pygtk
import gtk import gtk
import os import os
...@@ -114,11 +115,10 @@ class GLView: ...@@ -114,11 +115,10 @@ class GLView:
def paint(self): def paint(self):
glTranslatef(0,0,-2) glTranslatef(0,0,-2)
if self.settings["Unit"]() == "mm": if self.settings.get("unit") == "mm":
size = 100 size = 100
else: else:
size = 5 size = 5
bounds = self.settings["bounds"]
# axes # axes
glBegin(GL_LINES) glBegin(GL_LINES)
glColor3f(1,0,0) glColor3f(1,0,0)
...@@ -139,12 +139,12 @@ class GLView: ...@@ -139,12 +139,12 @@ class GLView:
glEnd() glEnd()
self.draw_string(0,0,size,'xz',"Z") self.draw_string(0,0,size,'xz',"Z")
# stock model # stock model
minx = float(bounds["minx"]) minx = float(self.settings.get("minx"))
maxx = float(bounds["maxx"]) miny = float(self.settings.get("miny"))
miny = float(bounds["miny"]) minz = float(self.settings.get("minz"))
maxy = float(bounds["maxy"]) maxx = float(self.settings.get("maxx"))
minz = float(bounds["minz"]) maxy = float(self.settings.get("maxy"))
maxz = float(bounds["maxz"]) maxz = float(self.settings.get("maxz"))
glBegin(GL_LINES) glBegin(GL_LINES)
glColor3f(0.3,0.3,0.3) glColor3f(0.3,0.3,0.3)
glVertex3f(minx, miny, minz) glVertex3f(minx, miny, minz)
...@@ -176,7 +176,7 @@ class GLView: ...@@ -176,7 +176,7 @@ class GLView:
glColor3f(0.5,.5,1) glColor3f(0.5,.5,1)
self.model_paint_func() self.model_paint_func()
# draw the toolpath # draw the toolpath
self.settings["toolpath_repaint_func"]() self.draw_toolpath(self.settings.get("toolpath"))
def _gl_clear(self): def _gl_clear(self):
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT) glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
...@@ -218,6 +218,24 @@ class GLView: ...@@ -218,6 +218,24 @@ class GLView:
def _resize_window(self, widget, data=None): def _resize_window(self, widget, data=None):
glViewport(0, 0, widget.allocation.width, widget.allocation.height) glViewport(0, 0, widget.allocation.width, widget.allocation.height)
def draw_toolpath(self, toolpath):
if toolpath:
last = None
for path in toolpath:
if last:
glColor3f(.5,1,.5)
glBegin(GL_LINES)
glVertex3f(last.x,last.y,last.z)
last = path.points[0]
glVertex3f(last.x,last.y,last.z)
glEnd()
glColor3f(1,.5,.5)
glBegin(GL_LINE_STRIP)
for point in path.points:
glVertex3f(point.x,point.y,point.z)
glEnd()
last = path.points[-1]
class ProjectGui: class ProjectGui:
...@@ -234,40 +252,52 @@ class ProjectGui: ...@@ -234,40 +252,52 @@ class ProjectGui:
self.model = None self.model = None
self.toolpath = None self.toolpath = None
# add some dummies - to be implemented later ... # add some dummies - to be implemented later ...
self.settings = { self.settings = pycam.Gui.Settings.Settings()
"bounds": { self.settings.add_item("model", lambda: getattr(self, "model"))
"minx": 0, self.settings.add_item("toolpath", lambda: getattr(self, "toolpath"))
"miny": 0, # create the unit field (can't be defined via glade)
"minz": 0, scale_box = self.gui.get_object("scale_box")
"maxx": 7, unit_field = gtk.combo_box_new_text()
"maxy": 7, unit_field.append_text("mm")
"maxz": 2, unit_field.append_text("inch")
}, unit_field.set_active(0)
"Unit": lambda: "mm", unit_field.show()
"toolpath_repaint_func": self.draw_toolpath, scale_box.add(unit_field)
} # move it to the top
scale_box.reorder_child(unit_field, 0)
def draw_toolpath(self): def set_unit(text):
if self.toolpath: unit_field.set_active((text == "mm") and 0 or 1)
last = None self.settings.add_item("unit", unit_field.get_active_text, set_unit)
for path in self.toolpath: # define the limit callback functions
if last: for limit in ["minx", "miny", "minz", "maxx", "maxy", "maxz"]:
glColor3f(.5,1,.5) obj = self.gui.get_object(limit)
glBegin(GL_LINES) self.settings.add_item(limit, obj.get_value, obj.set_value)
glVertex3f(last.x,last.y,last.z) # connect the "Bounds" action
last = path.points[0] self.gui.get_object("Minimize bounds").connect("clicked", self.minimize_bounds)
glVertex3f(last.x,last.y,last.z) self.gui.get_object("Reset bounds").connect("clicked", self.reset_bounds)
glEnd()
glColor3f(1,.5,.5) def minimize_bounds(self, widget, data=None):
glBegin(GL_LINE_STRIP) for limit in ["minx", "miny", "minz", "maxx", "maxy", "maxz"]:
for point in path.points: self.settings.set(limit, getattr(self.model, limit))
glVertex3f(point.x,point.y,point.z)
glEnd() def reset_bounds(self, widget, data=None):
last = path.points[-1] xwidth = self.model.maxx - self.model.minx
ywidth = self.model.maxy - self.model.miny
zwidth = self.model.maxz - self.model.minz
self.settings.set("minx", self.model.minx - 0.1 * xwidth)
self.settings.set("miny", self.model.miny - 0.1 * ywidth)
self.settings.set("minz", self.model.minz - 0.1 * zwidth)
self.settings.set("maxx", self.model.maxx + 0.1 * xwidth)
self.settings.set("maxy", self.model.maxy + 0.1 * ywidth)
self.settings.set("maxz", self.model.maxz + 0.1 * zwidth)
def destroy(self, widget, data=None): def destroy(self, widget, data=None):
gtk.main_quit() gtk.main_quit()
def open(self, filename):
self.file_selector.set_filename(filename)
self.load_model_file(filename=filename)
def load_model_file(self, widget=None, filename=None): def load_model_file(self, widget=None, filename=None):
if not filename: if not filename:
return return
...@@ -278,17 +308,18 @@ class ProjectGui: ...@@ -278,17 +308,18 @@ class ProjectGui:
# do the gl initialization # do the gl initialization
self.view3d = GLView(self.gui, self.settings) self.view3d = GLView(self.gui, self.settings)
if self.model and self.view3d.enabled: if self.model and self.view3d.enabled:
self.reset_bounds(None)
# why "2.0"? # why "2.0"?
self.view3d.set_scale(2.0/self.model.maxsize()) self.view3d.set_scale(2.0/self.model.maxsize())
self.view3d.set_model_paint_func(self.model.to_OpenGL) self.view3d.set_model_paint_func(self.model.to_OpenGL)
self.view3d.reset_view() self.view3d.reset_view()
def main(self): def mainloop(self):
gtk.main() gtk.main()
if __name__ == "__main__": if __name__ == "__main__":
gui = ProjectGui() gui = ProjectGui()
if len(sys.argv) > 1: if len(sys.argv) > 1:
gui.load_model_file(None, sys.argv[1]) gui.open(None, sys.argv[1])
gui.main() gui.mainloop()
GET_INDEX = 0
SET_INDEX = 1
VALUE_INDEX = 2
class Settings:
def __init__(self):
self.items = {}
self.values = {}
def add_item(self, key, get_func=None, set_func=None):
self.items[key] = [None, None, None]
self.define_get_func(key, get_func)
self.define_set_func(key, set_func)
self.items[key][VALUE_INDEX] = None
def define_get_func(self, key, get_func=None):
if not self.items.has_key(key):
return
if get_func is None:
get_func = lambda: self.items[key][VALUE_INDEX]
self.items[key][GET_INDEX] = get_func
def define_set_func(self, key, set_func=None):
if not self.items.has_key(key):
return
def default_set_func(value):
self.items[key][VALUE_INDEX] = value
if set_func is None:
set_func = default_set_func
self.items[key][SET_INDEX] = set_func
def get(self, key, default=None):
if self.items.has_key(key):
return self.items[key][GET_INDEX]()
else:
return default
def set(self, key, value):
if not self.items.has_key(key):
self.add_item(key)
self.items[key][SET_INDEX](value)
self.items[key][VALUE_INDEX] = value
def __str__(self):
result = {}
for key in self.items.keys():
result[key] = self.get(key)
return str(result)
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment