Commit f423901d authored by sumpfralle's avatar sumpfralle

r569@erker: lars | 2010-02-03 03:38:14 +0100

 separated some more drawing functions to the "common" module


git-svn-id: https://pycam.svn.sourceforge.net/svnroot/pycam/trunk@92 bbaffbd6-741e-11dd-a85d-61de82d9cad9
parent 66b0eeb6
......@@ -12,21 +12,11 @@ import sys
GTKBUILD_FILE = os.path.join(os.path.dirname(__file__), "gtk-interface", "pycam-project.ui")
VIEW_ROTATIONS = {
"reset": [(110, 1.0, 0.0, 0.0), (180, 0.0, 1.0, 0.0), (160, 0.0, 0.0, 1.0)],
"front": [(-90, 1.0, 0, 0)],
"back": [(-90, 1.0, 0, 0), (180, 0, 0, 1.0)],
"left": [(-90, 1.0, 0, 0), (90, 0, 0, 1.0)],
"right": [(-90, 1.0, 0, 0), (-90, 0, 0, 1.0)],
"top": [],
"bottom": [(180, 1.0, 0, 0)],
}
def gtkgl_functionwrapper(function):
def decorated(self, widget, *args, **kwords):
gldrawable=widget.get_gl_drawable()
glcontext=widget.get_gl_context()
gldrawable=self.area.get_gl_drawable()
glcontext=self.area.get_gl_context()
if not gldrawable.gl_begin(glcontext):
return
function(self, widget, *args, **kwords)
......@@ -44,24 +34,22 @@ class GLView:
return
self.enabled = True
self.settings = settings
self.scale = 1
self.model_paint_func = None
self.gui = gui
self.window = self.gui.get_object("view3dwindow")
self.window.set_size_request(400,400)
self.window.connect("destroy", lambda widget, data=None: self.window.destroy())
self.container = self.gui.get_object("view3dbox")
self.gui.get_object("Reset View").connect("clicked", self.rotate_view, VIEW_ROTATIONS["reset"])
self.gui.get_object("Left View").connect("clicked", self.rotate_view, VIEW_ROTATIONS["left"])
self.gui.get_object("Right View").connect("clicked", self.rotate_view, VIEW_ROTATIONS["right"])
self.gui.get_object("Front View").connect("clicked", self.rotate_view, VIEW_ROTATIONS["front"])
self.gui.get_object("Back View").connect("clicked", self.rotate_view, VIEW_ROTATIONS["back"])
self.gui.get_object("Top View").connect("clicked", self.rotate_view, VIEW_ROTATIONS["top"])
self.gui.get_object("Bottom View").connect("clicked", self.rotate_view, VIEW_ROTATIONS["bottom"])
self.gui.get_object("Reset View").connect("clicked", self.rotate_view, GuiCommon.VIEW_ROTATIONS["reset"])
self.gui.get_object("Left View").connect("clicked", self.rotate_view, GuiCommon.VIEW_ROTATIONS["left"])
self.gui.get_object("Right View").connect("clicked", self.rotate_view, GuiCommon.VIEW_ROTATIONS["right"])
self.gui.get_object("Front View").connect("clicked", self.rotate_view, GuiCommon.VIEW_ROTATIONS["front"])
self.gui.get_object("Back View").connect("clicked", self.rotate_view, GuiCommon.VIEW_ROTATIONS["back"])
self.gui.get_object("Top View").connect("clicked", self.rotate_view, GuiCommon.VIEW_ROTATIONS["top"])
self.gui.get_object("Bottom View").connect("clicked", self.rotate_view, GuiCommon.VIEW_ROTATIONS["bottom"])
# OpenGL stuff
glconfig = gtk.gdkgl.Config(mode=gtk.gdkgl.MODE_RGB|gtk.gdkgl.MODE_DEPTH|gtk.gdkgl.MODE_DOUBLE)
self.area = gtk.gtkgl.DrawingArea(glconfig)
self.area.set_size_request(400,400)
self.area.set_size_request(400, 400)
# first run; might also be important when doing other fancy gtk/gdk stuff
self.area.connect_after('realize', self._realize)
# called when a part of the screen is uncovered
......@@ -74,10 +62,6 @@ class GLView:
self.window.show()
def glsetup(self):
#glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
#glEnable(GL_TEXTURE_2D)
#glEnable(GL_BLEND)
#glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
GLUT.glutInit()
GL.glShadeModel(GL.GL_FLAT)
GL.glMatrixMode(GL.GL_MODELVIEW)
......@@ -85,128 +69,38 @@ class GLView:
GL.glMaterial(GL.GL_FRONT_AND_BACK, GL.GL_SPECULAR, (0.1, 0.1, 0.1, 1.0))
GL.glMaterial(GL.GL_FRONT_AND_BACK, GL.GL_SHININESS, (0.5))
def paint(self):
GL.glTranslatef(0,0,-2)
if self.settings.get("unit") == "mm":
size = 100
else:
size = 5
# axes
GL.glBegin(GL.GL_LINES)
GL.glColor3f(1,0,0)
GL.glVertex3f(0,0,0)
GL.glVertex3f(size,0,0)
GL.glEnd()
GuiCommon.draw_string(size,0,0,'xy',"X")
GL.glBegin(GL.GL_LINES)
GL.glColor3f(0,1,0)
GL.glVertex3f(0,0,0)
GL.glVertex3f(0,size,0)
GL.glEnd()
GuiCommon.draw_string(0,size,0,'yz',"Y")
GL.glBegin(GL.GL_LINES)
GL.glColor3f(0,0,1)
GL.glVertex3f(0,0,0)
GL.glVertex3f(0,0,size)
GL.glEnd()
GuiCommon.draw_string(0,0,size,'xz',"Z")
# stock model
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"))
GL.glBegin(GL.GL_LINES)
GL.glColor3f(0.3,0.3,0.3)
GL.glVertex3f(minx, miny, minz)
GL.glVertex3f(maxx, miny, minz)
GL.glVertex3f(minx, maxy, minz)
GL.glVertex3f(maxx, maxy, minz)
GL.glVertex3f(minx, miny, maxz)
GL.glVertex3f(maxx, miny, maxz)
GL.glVertex3f(minx, maxy, maxz)
GL.glVertex3f(maxx, maxy, maxz)
GL.glVertex3f(minx, miny, minz)
GL.glVertex3f(minx, maxy, minz)
GL.glVertex3f(maxx, miny, minz)
GL.glVertex3f(maxx, maxy, minz)
GL.glVertex3f(minx, miny, maxz)
GL.glVertex3f(minx, maxy, maxz)
GL.glVertex3f(maxx, miny, maxz)
GL.glVertex3f(maxx, maxy, maxz)
GL.glVertex3f(minx, miny, minz)
GL.glVertex3f(minx, miny, maxz)
GL.glVertex3f(maxx, miny, minz)
GL.glVertex3f(maxx, miny, maxz)
GL.glVertex3f(minx, maxy, minz)
GL.glVertex3f(minx, maxy, maxz)
GL.glVertex3f(maxx, maxy, minz)
GL.glVertex3f(maxx, maxy, maxz)
GL.glEnd()
# draw the model
GL.glColor3f(0.5,.5,1)
self.model_paint_func()
# draw the toolpath
self.draw_toolpath(self.settings.get("toolpath"))
def _gl_clear(self):
GL.glClear(GL.GL_COLOR_BUFFER_BIT|GL.GL_DEPTH_BUFFER_BIT)
def _gl_finish(self):
self.area.get_gl_drawable().swap_buffers()
@gtkgl_functionwrapper
def _realize(self, widget):
self.glsetup()
@gtkgl_functionwrapper
def _expose_event(self, widget, event):
self._gl_clear()
self.paint()
self._gl_finish()
def rotate_view(self, widget=None, rotation=None):
def rotate_view(self, widget, data=None):
self._gl_clear()
GL.glMatrixMode(GL.GL_MODELVIEW)
GL.glLoadIdentity()
GL.glScalef(self.scale,self.scale,self.scale)
if rotation:
for one_rot in rotation:
GL.glRotatef(*one_rot)
GuiCommon.rotate_view(self.settings.get("scale"), data)
self.paint()
self._gl_finish()
def reset_view(self):
self.rotate_view(rotation=VIEW_ROTATIONS["reset"])
self.rotate_view(self.area, GuiCommon.VIEW_ROTATIONS["reset"])
def set_scale(self, value):
self.scale = value
@gtkgl_functionwrapper
def _realize(self, widget):
self.glsetup()
def set_model_paint_func(self, func):
self.model_paint_func = func
@gtkgl_functionwrapper
def _expose_event(self, widget, event):
self._gl_clear()
self.paint()
self._gl_finish()
@gtkgl_functionwrapper
def _resize_window(self, widget, data=None):
GL.glViewport(0, 0, widget.allocation.width, widget.allocation.height)
def draw_toolpath(self, toolpath):
if toolpath:
last = None
for path in toolpath:
if last:
GL.glColor3f(.5,1,.5)
GL.glBegin(GL.GL_LINES)
GL.glVertex3f(last.x,last.y,last.z)
last = path.points[0]
GL.glVertex3f(last.x,last.y,last.z)
GL.glEnd()
GL.glColor3f(1,.5,.5)
GL.glBegin(GL.GL_LINE_STRIP)
for point in path.points:
GL.glVertex3f(point.x,point.y,point.z)
GL.glEnd()
last = path.points[-1]
def paint(self):
GuiCommon.draw_complete_model_view(self.settings)
class ProjectGui:
......@@ -227,7 +121,7 @@ class ProjectGui:
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)
# create the unit field (the default content 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")
......@@ -249,6 +143,7 @@ class ProjectGui:
self.gui.get_object("Reset bounds").connect("clicked", self.reset_bounds)
def minimize_bounds(self, widget, data=None):
# be careful: this depends on equal names of "settings" keys and "model" variables
for limit in ["minx", "miny", "minz", "maxx", "maxy", "maxz"]:
self.settings.set(limit, getattr(self.model, limit))
......@@ -282,8 +177,7 @@ class ProjectGui:
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.settings.set("scale", 0.9/self.model.maxsize())
self.view3d.reset_view()
def mainloop(self):
......
This diff is collapsed.
......@@ -2,9 +2,20 @@ import OpenGL.GL as GL
import OpenGL.GLUT as GLUT
VIEW_ROTATIONS = {
"reset": [(110, 1.0, 0.0, 0.0), (180, 0.0, 1.0, 0.0), (160, 0.0, 0.0, 1.0)],
"front": [(-90, 1.0, 0, 0)],
"back": [(-90, 1.0, 0, 0), (180, 0, 0, 1.0)],
"left": [(-90, 1.0, 0, 0), (90, 0, 0, 1.0)],
"right": [(-90, 1.0, 0, 0), (-90, 0, 0, 1.0)],
"top": [],
"bottom": [(180, 1.0, 0, 0)],
}
def draw_string(x, y, z, p, s, scale=.01):
GL.glPushMatrix()
GL.glTranslatef(x,y,z)
GL.glTranslatef(x, y, z)
if p == 'xy':
pass
elif p == 'yz':
......@@ -19,3 +30,110 @@ def draw_string(x, y, z, p, s, scale=.01):
GLUT.glutStrokeCharacter(GLUT.GLUT_STROKE_ROMAN, ord(c))
GL.glPopMatrix()
def draw_axes(size):
GL.glBegin(GL.GL_LINES)
GL.glColor3f(1, 0, 0)
GL.glVertex3f(0, 0, 0)
GL.glVertex3f(size, 0, 0)
GL.glEnd()
draw_string(size, 0, 0, 'xy', "X")
GL.glBegin(GL.GL_LINES)
GL.glColor3f(0, 1, 0)
GL.glVertex3f(0, 0, 0)
GL.glVertex3f(0, size, 0)
GL.glEnd()
draw_string(0, size, 0, 'yz', "Y")
GL.glBegin(GL.GL_LINES)
GL.glColor3f(0, 0, 1)
GL.glVertex3f(0, 0, 0)
GL.glVertex3f(0, 0, size)
GL.glEnd()
draw_string(0, 0, size, 'xz', "Z")
def draw_bounding_box(minx, miny, minz, maxx, maxy, maxz):
color = [0.3, 0.3, 0.3]
p1 = [minx, miny, minz]
p2 = [minx, maxy, minz]
p3 = [maxx, maxy, minz]
p4 = [maxx, miny, minz]
p5 = [minx, miny, maxz]
p6 = [minx, maxy, maxz]
p7 = [maxx, maxy, maxz]
p8 = [maxx, miny, maxz]
# lower rectangle
GL.glBegin(GL.GL_LINES)
GL.glColor3f(*color)
# all combinations of neighbouring corners
for corner_pair in [(p1, p2), (p1, p5), (p1, p4), (p2, p3),
(p2, p6), (p3, p4), (p3, p7), (p4, p8), (p5, p6),
(p6, p7), (p7, p8), (p8, p5)]:
GL.glVertex3f(*(corner_pair[0]))
GL.glVertex3f(*(corner_pair[1]))
GL.glEnd()
def draw_complete_model_view(settings):
GL.glTranslatef(0, 0, -2)
if settings.get("unit") == "mm":
size = 100
else:
size = 5
# axes
draw_axes(size)
# stock model
draw_bounding_box(float(settings.get("minx")), float(settings.get("miny")),
float(settings.get("minz")), float(settings.get("maxx")),
float(settings.get("maxy")), float(settings.get("maxz")))
# draw the model
GL.glColor3f(0.5, 0.5, 1)
settings.get("model").to_OpenGL()
# draw the toolpath
draw_toolpath(settings.get("toolpath"))
def draw_toolpath(toolpath):
if toolpath:
last = None
for path in toolpath:
if last:
GL.glColor3f(0.5, 1, 0.5)
GL.glBegin(GL.GL_LINES)
GL.glVertex3f(last.x, last.y, last.z)
last = path.points[0]
GL.glVertex3f(last.x, last.y, last.z)
GL.glEnd()
GL.glColor3f(1, 0.5, 0.5)
GL.glBegin(GL.GL_LINE_STRIP)
for point in path.points:
GL.glVertex3f(point.x, point.y, point.z)
GL.glEnd()
last = path.points[-1]
def rotate_view(scale, rotation=None):
GL.glMatrixMode(GL.GL_MODELVIEW)
GL.glLoadIdentity()
GL.glScalef(scale, scale, scale)
if rotation:
for one_rot in rotation:
GL.glRotatef(*one_rot)
def reset_view(scale):
rotate_view(scale, rotation=VIEW_ROTATIONS["reset"])
def front_view(scale):
rotate_view(scale, rotation=VIEW_ROTATIONS["front"])
def back_view(scale):
rotate_view(scale, rotation=VIEW_ROTATIONS["back"])
def top_view(scale):
rotate_view(scale, rotation=VIEW_ROTATIONS["top"])
def bottom_view(scale):
rotate_view(scale, rotation=VIEW_ROTATIONS["bottom"])
def left_view(scale):
rotate_view(scale, rotation=VIEW_ROTATIONS["left"])
def right_view(scale):
rotate_view(scale, rotation=VIEW_ROTATIONS["right"])
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