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 *
from OpenGL.GLUT import *
from OpenGL.GLU import *
import pycam.Importers.STLImporter
import pycam.Gui.Settings
import pygtk
import gtk
import os
......@@ -114,11 +115,10 @@ class GLView:
def paint(self):
glTranslatef(0,0,-2)
if self.settings["Unit"]() == "mm":
if self.settings.get("unit") == "mm":
size = 100
else:
size = 5
bounds = self.settings["bounds"]
# axes
glBegin(GL_LINES)
glColor3f(1,0,0)
......@@ -139,12 +139,12 @@ class GLView:
glEnd()
self.draw_string(0,0,size,'xz',"Z")
# stock model
minx = float(bounds["minx"])
maxx = float(bounds["maxx"])
miny = float(bounds["miny"])
maxy = float(bounds["maxy"])
minz = float(bounds["minz"])
maxz = float(bounds["maxz"])
minx = float(self.settings.get("minx"))
miny = float(self.settings.get("miny"))
minz = float(self.settings.get("minz"))
maxx = float(self.settings.get("maxx"))
maxy = float(self.settings.get("maxy"))
maxz = float(self.settings.get("maxz"))
glBegin(GL_LINES)
glColor3f(0.3,0.3,0.3)
glVertex3f(minx, miny, minz)
......@@ -176,7 +176,7 @@ class GLView:
glColor3f(0.5,.5,1)
self.model_paint_func()
# draw the toolpath
self.settings["toolpath_repaint_func"]()
self.draw_toolpath(self.settings.get("toolpath"))
def _gl_clear(self):
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
......@@ -218,6 +218,24 @@ class GLView:
def _resize_window(self, widget, data=None):
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:
......@@ -234,40 +252,52 @@ class ProjectGui:
self.model = None
self.toolpath = None
# add some dummies - to be implemented later ...
self.settings = {
"bounds": {
"minx": 0,
"miny": 0,
"minz": 0,
"maxx": 7,
"maxy": 7,
"maxz": 2,
},
"Unit": lambda: "mm",
"toolpath_repaint_func": self.draw_toolpath,
}
self.settings = pycam.Gui.Settings.Settings()
self.settings.add_item("model", lambda: getattr(self, "model"))
self.settings.add_item("toolpath", lambda: getattr(self, "toolpath"))
# create the unit field (can't be defined via glade)
scale_box = self.gui.get_object("scale_box")
unit_field = gtk.combo_box_new_text()
unit_field.append_text("mm")
unit_field.append_text("inch")
unit_field.set_active(0)
unit_field.show()
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):
if self.toolpath:
last = None
for path in self.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]
def minimize_bounds(self, widget, data=None):
for limit in ["minx", "miny", "minz", "maxx", "maxy", "maxz"]:
self.settings.set(limit, getattr(self.model, limit))
def reset_bounds(self, widget, data=None):
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):
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):
if not filename:
return
......@@ -278,17 +308,18 @@ class ProjectGui:
# do the gl initialization
self.view3d = GLView(self.gui, self.settings)
if self.model and self.view3d.enabled:
self.reset_bounds(None)
# why "2.0"?
self.view3d.set_scale(2.0/self.model.maxsize())
self.view3d.set_model_paint_func(self.model.to_OpenGL)
self.view3d.reset_view()
def main(self):
def mainloop(self):
gtk.main()
if __name__ == "__main__":
gui = ProjectGui()
if len(sys.argv) > 1:
gui.load_model_file(None, sys.argv[1])
gui.main()
gui.open(None, sys.argv[1])
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 @@
<property name="top_padding">4</property>
<property name="left_padding">8</property>
<child>
<object class="GtkVBox" id="vbox1">
<object class="GtkVBox" id="scale_box">
<property name="visible">True</property>
<property name="orientation">vertical</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>
<object class="GtkSpinButton" id="Scale factor">
<property name="visible">True</property>
......@@ -334,32 +346,32 @@
</object>
<packing>
<property name="expand">False</property>
<property name="position">0</property>
<property name="position">2</property>
</packing>
</child>
<child>
<object class="GtkButton" id="scale up">
<property name="label" translatable="yes">Enlage</property>
<object class="GtkButton" id="scale down">
<property name="label" translatable="yes">Shrink</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="position">1</property>
<property name="position">3</property>
</packing>
</child>
<child>
<object class="GtkButton" id="scale down">
<property name="label" translatable="yes">Shrink</property>
<object class="GtkButton" id="scale up">
<property name="label" translatable="yes">Enlage</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
</object>
<packing>
<property name="expand">False</property>
<property name="position">2</property>
<property name="fill">False</property>
<property name="position">4</property>
</packing>
</child>
</object>
......@@ -548,12 +560,14 @@
<property name="column_spacing">1</property>
<property name="row_spacing">1</property>
<child>
<object class="GtkSpinButton" id="spinbutton1">
<object class="GtkSpinButton" id="minx">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="invisible_char">&#x2022;</property>
<property name="width_chars">4</property>
<property name="adjustment">min-x</property>
<property name="digits">2</property>
<property name="numeric">True</property>
</object>
<packing>
<property name="left_attach">1</property>
......@@ -565,12 +579,14 @@
</packing>
</child>
<child>
<object class="GtkSpinButton" id="spinbutton5">
<object class="GtkSpinButton" id="maxy">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="invisible_char">&#x2022;</property>
<property name="width_chars">4</property>
<property name="adjustment">max-y</property>
<property name="digits">2</property>
<property name="numeric">True</property>
</object>
<packing>
<property name="left_attach">2</property>
......@@ -582,12 +598,14 @@
</packing>
</child>
<child>
<object class="GtkSpinButton" id="spinbutton4">
<object class="GtkSpinButton" id="minz">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="invisible_char">&#x2022;</property>
<property name="width_chars">4</property>
<property name="adjustment">min-z</property>
<property name="digits">2</property>
<property name="numeric">True</property>
</object>
<packing>
<property name="left_attach">1</property>
......@@ -599,12 +617,14 @@
</packing>
</child>
<child>
<object class="GtkSpinButton" id="spinbutton6">
<object class="GtkSpinButton" id="maxz">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="invisible_char">&#x2022;</property>
<property name="width_chars">4</property>
<property name="adjustment">max-z</property>
<property name="digits">2</property>
<property name="numeric">True</property>
</object>
<packing>
<property name="left_attach">2</property>
......@@ -616,12 +636,14 @@
</packing>
</child>
<child>
<object class="GtkSpinButton" id="spinbutton3">
<object class="GtkSpinButton" id="miny">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="invisible_char">&#x2022;</property>
<property name="width_chars">4</property>
<property name="adjustment">min-y</property>
<property name="digits">2</property>
<property name="numeric">True</property>
</object>
<packing>
<property name="left_attach">1</property>
......@@ -633,12 +655,14 @@
</packing>
</child>
<child>
<object class="GtkSpinButton" id="spinbutton2">
<object class="GtkSpinButton" id="maxx">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="invisible_char">&#x2022;</property>
<property name="width_chars">4</property>
<property name="adjustment">max-x</property>
<property name="digits">2</property>
<property name="numeric">True</property>
</object>
<packing>
<property name="left_attach">2</property>
......@@ -730,8 +754,8 @@
</packing>
</child>
<child>
<object class="GtkButton" id="Apply bounds">
<property name="label" translatable="yes">Apply</property>
<object class="GtkButton" id="Minimize bounds">
<property name="label" translatable="yes">Minimize</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
......@@ -982,4 +1006,40 @@
</object>
</child>
</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>
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