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 set_unit(text):
unit_field.set_active((text == "mm") and 0 or 1)
self.settings.add_item("unit", unit_field.get_active_text, set_unit)
# define the limit callback functions
for limit in ["minx", "miny", "minz", "maxx", "maxy", "maxz"]:
obj = self.gui.get_object(limit)
self.settings.add_item(limit, obj.get_value, obj.set_value)
# connect the "Bounds" action
self.gui.get_object("Minimize bounds").connect("clicked", self.minimize_bounds)
self.gui.get_object("Reset bounds").connect("clicked", self.reset_bounds)
def draw_toolpath(self): def minimize_bounds(self, widget, data=None):
if self.toolpath: for limit in ["minx", "miny", "minz", "maxx", "maxy", "maxz"]:
last = None self.settings.set(limit, getattr(self.model, limit))
for path in self.toolpath:
if last: def reset_bounds(self, widget, data=None):
glColor3f(.5,1,.5) xwidth = self.model.maxx - self.model.minx
glBegin(GL_LINES) ywidth = self.model.maxy - self.model.miny
glVertex3f(last.x,last.y,last.z) zwidth = self.model.maxz - self.model.minz
last = path.points[0] self.settings.set("minx", self.model.minx - 0.1 * xwidth)
glVertex3f(last.x,last.y,last.z) self.settings.set("miny", self.model.miny - 0.1 * ywidth)
glEnd() self.settings.set("minz", self.model.minz - 0.1 * zwidth)
glColor3f(1,.5,.5) self.settings.set("maxx", self.model.maxx + 0.1 * xwidth)
glBegin(GL_LINE_STRIP) self.settings.set("maxy", self.model.maxy + 0.1 * ywidth)
for point in path.points: self.settings.set("maxz", self.model.maxz + 0.1 * zwidth)
glVertex3f(point.x,point.y,point.z)
glEnd()
last = path.points[-1]
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)
...@@ -317,10 +317,22 @@ ...@@ -317,10 +317,22 @@
<property name="top_padding">4</property> <property name="top_padding">4</property>
<property name="left_padding">8</property> <property name="left_padding">8</property>
<child> <child>
<object class="GtkVBox" id="vbox1"> <object class="GtkVBox" id="scale_box">
<property name="visible">True</property> <property name="visible">True</property>
<property name="orientation">vertical</property> <property name="orientation">vertical</property>
<property name="spacing">3</property> <property name="spacing">3</property>
<child>
<placeholder/>
</child>
<child>
<object class="GtkHSeparator" id="hseparator3">
<property name="visible">True</property>
</object>
<packing>
<property name="expand">False</property>
<property name="position">1</property>
</packing>
</child>
<child> <child>
<object class="GtkSpinButton" id="Scale factor"> <object class="GtkSpinButton" id="Scale factor">
<property name="visible">True</property> <property name="visible">True</property>
...@@ -334,32 +346,32 @@ ...@@ -334,32 +346,32 @@
</object> </object>
<packing> <packing>
<property name="expand">False</property> <property name="expand">False</property>
<property name="position">0</property> <property name="position">2</property>
</packing> </packing>
</child> </child>
<child> <child>
<object class="GtkButton" id="scale up"> <object class="GtkButton" id="scale down">
<property name="label" translatable="yes">Enlage</property> <property name="label" translatable="yes">Shrink</property>
<property name="visible">True</property> <property name="visible">True</property>
<property name="can_focus">True</property> <property name="can_focus">True</property>
<property name="receives_default">True</property> <property name="receives_default">True</property>
</object> </object>
<packing> <packing>
<property name="expand">False</property> <property name="expand">False</property>
<property name="fill">False</property> <property name="position">3</property>
<property name="position">1</property>
</packing> </packing>
</child> </child>
<child> <child>
<object class="GtkButton" id="scale down"> <object class="GtkButton" id="scale up">
<property name="label" translatable="yes">Shrink</property> <property name="label" translatable="yes">Enlage</property>
<property name="visible">True</property> <property name="visible">True</property>
<property name="can_focus">True</property> <property name="can_focus">True</property>
<property name="receives_default">True</property> <property name="receives_default">True</property>
</object> </object>
<packing> <packing>
<property name="expand">False</property> <property name="expand">False</property>
<property name="position">2</property> <property name="fill">False</property>
<property name="position">4</property>
</packing> </packing>
</child> </child>
</object> </object>
...@@ -548,12 +560,14 @@ ...@@ -548,12 +560,14 @@
<property name="column_spacing">1</property> <property name="column_spacing">1</property>
<property name="row_spacing">1</property> <property name="row_spacing">1</property>
<child> <child>
<object class="GtkSpinButton" id="spinbutton1"> <object class="GtkSpinButton" id="minx">
<property name="visible">True</property> <property name="visible">True</property>
<property name="can_focus">True</property> <property name="can_focus">True</property>
<property name="invisible_char">&#x2022;</property> <property name="invisible_char">&#x2022;</property>
<property name="width_chars">4</property> <property name="width_chars">4</property>
<property name="adjustment">min-x</property>
<property name="digits">2</property> <property name="digits">2</property>
<property name="numeric">True</property>
</object> </object>
<packing> <packing>
<property name="left_attach">1</property> <property name="left_attach">1</property>
...@@ -565,12 +579,14 @@ ...@@ -565,12 +579,14 @@
</packing> </packing>
</child> </child>
<child> <child>
<object class="GtkSpinButton" id="spinbutton5"> <object class="GtkSpinButton" id="maxy">
<property name="visible">True</property> <property name="visible">True</property>
<property name="can_focus">True</property> <property name="can_focus">True</property>
<property name="invisible_char">&#x2022;</property> <property name="invisible_char">&#x2022;</property>
<property name="width_chars">4</property> <property name="width_chars">4</property>
<property name="adjustment">max-y</property>
<property name="digits">2</property> <property name="digits">2</property>
<property name="numeric">True</property>
</object> </object>
<packing> <packing>
<property name="left_attach">2</property> <property name="left_attach">2</property>
...@@ -582,12 +598,14 @@ ...@@ -582,12 +598,14 @@
</packing> </packing>
</child> </child>
<child> <child>
<object class="GtkSpinButton" id="spinbutton4"> <object class="GtkSpinButton" id="minz">
<property name="visible">True</property> <property name="visible">True</property>
<property name="can_focus">True</property> <property name="can_focus">True</property>
<property name="invisible_char">&#x2022;</property> <property name="invisible_char">&#x2022;</property>
<property name="width_chars">4</property> <property name="width_chars">4</property>
<property name="adjustment">min-z</property>
<property name="digits">2</property> <property name="digits">2</property>
<property name="numeric">True</property>
</object> </object>
<packing> <packing>
<property name="left_attach">1</property> <property name="left_attach">1</property>
...@@ -599,12 +617,14 @@ ...@@ -599,12 +617,14 @@
</packing> </packing>
</child> </child>
<child> <child>
<object class="GtkSpinButton" id="spinbutton6"> <object class="GtkSpinButton" id="maxz">
<property name="visible">True</property> <property name="visible">True</property>
<property name="can_focus">True</property> <property name="can_focus">True</property>
<property name="invisible_char">&#x2022;</property> <property name="invisible_char">&#x2022;</property>
<property name="width_chars">4</property> <property name="width_chars">4</property>
<property name="adjustment">max-z</property>
<property name="digits">2</property> <property name="digits">2</property>
<property name="numeric">True</property>
</object> </object>
<packing> <packing>
<property name="left_attach">2</property> <property name="left_attach">2</property>
...@@ -616,12 +636,14 @@ ...@@ -616,12 +636,14 @@
</packing> </packing>
</child> </child>
<child> <child>
<object class="GtkSpinButton" id="spinbutton3"> <object class="GtkSpinButton" id="miny">
<property name="visible">True</property> <property name="visible">True</property>
<property name="can_focus">True</property> <property name="can_focus">True</property>
<property name="invisible_char">&#x2022;</property> <property name="invisible_char">&#x2022;</property>
<property name="width_chars">4</property> <property name="width_chars">4</property>
<property name="adjustment">min-y</property>
<property name="digits">2</property> <property name="digits">2</property>
<property name="numeric">True</property>
</object> </object>
<packing> <packing>
<property name="left_attach">1</property> <property name="left_attach">1</property>
...@@ -633,12 +655,14 @@ ...@@ -633,12 +655,14 @@
</packing> </packing>
</child> </child>
<child> <child>
<object class="GtkSpinButton" id="spinbutton2"> <object class="GtkSpinButton" id="maxx">
<property name="visible">True</property> <property name="visible">True</property>
<property name="can_focus">True</property> <property name="can_focus">True</property>
<property name="invisible_char">&#x2022;</property> <property name="invisible_char">&#x2022;</property>
<property name="width_chars">4</property> <property name="width_chars">4</property>
<property name="adjustment">max-x</property>
<property name="digits">2</property> <property name="digits">2</property>
<property name="numeric">True</property>
</object> </object>
<packing> <packing>
<property name="left_attach">2</property> <property name="left_attach">2</property>
...@@ -730,8 +754,8 @@ ...@@ -730,8 +754,8 @@
</packing> </packing>
</child> </child>
<child> <child>
<object class="GtkButton" id="Apply bounds"> <object class="GtkButton" id="Minimize bounds">
<property name="label" translatable="yes">Apply</property> <property name="label" translatable="yes">Minimize</property>
<property name="visible">True</property> <property name="visible">True</property>
<property name="can_focus">True</property> <property name="can_focus">True</property>
<property name="receives_default">True</property> <property name="receives_default">True</property>
...@@ -982,4 +1006,40 @@ ...@@ -982,4 +1006,40 @@
</object> </object>
</child> </child>
</object> </object>
<object class="GtkAdjustment" id="min-x">
<property name="upper">100</property>
<property name="step_increment">1</property>
<property name="page_increment">10</property>
<property name="page_size">10</property>
</object>
<object class="GtkAdjustment" id="min-y">
<property name="upper">100</property>
<property name="step_increment">1</property>
<property name="page_increment">10</property>
<property name="page_size">10</property>
</object>
<object class="GtkAdjustment" id="min-z">
<property name="upper">100</property>
<property name="step_increment">1</property>
<property name="page_increment">10</property>
<property name="page_size">10</property>
</object>
<object class="GtkAdjustment" id="max-x">
<property name="upper">100</property>
<property name="step_increment">1</property>
<property name="page_increment">10</property>
<property name="page_size">10</property>
</object>
<object class="GtkAdjustment" id="max-y">
<property name="upper">100</property>
<property name="step_increment">1</property>
<property name="page_increment">10</property>
<property name="page_size">10</property>
</object>
<object class="GtkAdjustment" id="max-z">
<property name="upper">100</property>
<property name="step_increment">1</property>
<property name="page_increment">10</property>
<property name="page_size">10</property>
</object>
</interface> </interface>
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