chiark / gitweb /
Change how the engine is interfaced from the python code. Put the GCode viewer in...
[cura.git] / Cura / gui / util / previewTools.py
index 0de3bf67922cbe7323f93842698c4aeb0f4f24ef..372d9cf2ff039bc3a3f6d16d4812ba043257665a 100644 (file)
@@ -1,16 +1,36 @@
 from __future__ import absolute_import
+__copyright__ = "Copyright (C) 2013 David Braam - Released under terms of the AGPLv3 License"
 
 import math
 import wx
 import numpy
 
 import OpenGL
-OpenGL.ERROR_CHECKING = False
+#OpenGL.ERROR_CHECKING = False
 from OpenGL.GLU import *
 from OpenGL.GL import *
 
 from Cura.gui.util import opengl
 
+class toolNone(object):
+       def __init__(self, parent):
+               self.parent = parent
+
+       def OnMouseMove(self, p0, p1):
+               pass
+
+       def OnDragStart(self, p0, p1):
+               return False
+
+       def OnDrag(self, p0, p1):
+               pass
+
+       def OnDragEnd(self):
+               pass
+
+       def OnDraw(self):
+               pass
+
 class toolInfo(object):
        def __init__(self, parent):
                self.parent = parent
@@ -91,11 +111,9 @@ class toolRotate(object):
                self.dragEndAngle = None
 
        def _ProjectToPlanes(self, p0, p1):
-               pp0 = p0 - [0,0,self.parent.getObjectSize()[2]/2]
-               pp1 = p1 - [0,0,self.parent.getObjectSize()[2]/2]
-               cursorX0 = pp0 - (pp1 - pp0) * (pp0[0] / (pp1[0] - pp0[0]))
-               cursorY0 = pp0 - (pp1 - pp0) * (pp0[1] / (pp1[1] - pp0[1]))
-               cursorZ0 = pp0 - (pp1 - pp0) * (pp0[2] / (pp1[2] - pp0[2]))
+               cursorX0 = p0 - (p1 - p0) * (p0[0] / (p1[0] - p0[0]))
+               cursorY0 = p0 - (p1 - p0) * (p0[1] / (p1[1] - p0[1]))
+               cursorZ0 = p0 - (p1 - p0) * (p0[2] / (p1[2] - p0[2]))
                cursorYZ = math.sqrt((cursorX0[1] * cursorX0[1]) + (cursorX0[2] * cursorX0[2]))
                cursorXZ = math.sqrt((cursorY0[0] * cursorY0[0]) + (cursorY0[2] * cursorY0[2]))
                cursorXY = math.sqrt((cursorZ0[0] * cursorZ0[0]) + (cursorZ0[1] * cursorZ0[1]))
@@ -106,7 +124,7 @@ class toolRotate(object):
                cursorX0, cursorY0, cursorZ0, cursorYZ, cursorXZ, cursorXY = self._ProjectToPlanes(p0, p1)
                oldDragPlane = self.dragPlane
                if radius * self.rotateRingDistMin <= cursorXY <= radius * self.rotateRingDistMax or radius * self.rotateRingDistMin <= cursorYZ <= radius * self.rotateRingDistMax or radius * self.rotateRingDistMin <= cursorXZ <= radius * self.rotateRingDistMax:
-                       self.parent.SetCursor(wx.StockCursor(wx.CURSOR_SIZING))
+                       #self.parent.SetCursor(wx.StockCursor(wx.CURSOR_SIZING))
                        if self.dragStartAngle is None:
                                if radius * self.rotateRingDistMin <= cursorXY <= radius * self.rotateRingDistMax:
                                        self.dragPlane = 'XY'
@@ -117,7 +135,7 @@ class toolRotate(object):
                else:
                        if self.dragStartAngle is None:
                                self.dragPlane = ''
-                       self.parent.SetCursor(wx.StockCursor(wx.CURSOR_DEFAULT))
+                       #self.parent.SetCursor(wx.StockCursor(wx.CURSOR_DEFAULT))
 
        def OnDragStart(self, p0, p1):
                radius = self.parent.getObjectBoundaryCircle()
@@ -132,6 +150,7 @@ class toolRotate(object):
                        else:
                                self.dragPlane = 'YZ'
                                self.dragStartAngle = math.atan2(cursorX0[2], cursorX0[1]) * 180 / math.pi
+                       self.dragEndAngle = self.dragStartAngle
                        return True
                return False
 
@@ -190,7 +209,7 @@ class toolRotate(object):
                                glEnd()
                                glTranslatef(1.1,0,0)
                                glColor4ub(0,0,0,255)
-                               opengl.glDrawStringCenter("%d" % (abs(self.dragEndAngle - self.dragStartAngle)))
+                               opengl.glDrawStringCenter("%d" % (abs(self.dragEndAngle - self.dragStartAngle) + 0.5))
                                glColor4ub(255,64,64,255)
                                glPopMatrix()
                else:
@@ -270,16 +289,14 @@ class toolScale(object):
                return numpy.linalg.norm(numpy.cross((p0 - p1), (p0 - p2))) / numpy.linalg.norm(p2 - p1)
 
        def _traceNodes(self, p0, p1):
-               pp0 = p0 - [0,0,self.parent.getObjectSize()[2]/2]
-               pp1 = p1 - [0,0,self.parent.getObjectSize()[2]/2]
                s = self._nodeSize()
-               if self._pointDist(numpy.array([0,0,0],numpy.float32), pp0, pp1) < s * 2:
+               if self._pointDist(numpy.array([0,0,0],numpy.float32), p0, p1) < s * 2:
                        return 1
-               if self._pointDist(numpy.array([s*15,0,0],numpy.float32), pp0, pp1) < s * 2:
+               if self._pointDist(numpy.array([s*15,0,0],numpy.float32), p0, p1) < s * 2:
                        return 2
-               if self._pointDist(numpy.array([0,s*15,0],numpy.float32), pp0, pp1) < s * 2:
+               if self._pointDist(numpy.array([0,s*15,0],numpy.float32), p0, p1) < s * 2:
                        return 3
-               if self._pointDist(numpy.array([0,0,s*15],numpy.float32), pp0, pp1) < s * 2:
+               if self._pointDist(numpy.array([0,0,s*15],numpy.float32), p0, p1) < s * 2:
                        return 4
                return None
 
@@ -300,7 +317,7 @@ class toolScale(object):
                return t
 
        def _nodeSize(self):
-               return self.parent.zoom / self.parent.GetSize().GetWidth() * 6
+               return float(self.parent._zoom) / float(self.parent.GetSize().GetWidth()) * 6.0
 
        def OnMouseMove(self, p0, p1):
                self.node = self._traceNodes(p0, p1)
@@ -312,8 +329,6 @@ class toolScale(object):
 
        def OnDrag(self, p0, p1):
                s = self._nodeSize()
-               pp0 = p0 - [0,0,self.parent.getObjectSize()[2]/2]
-               pp1 = p1 - [0,0,self.parent.getObjectSize()[2]/2]
                endPoint = [1,1,1]
                if self.node == 2:
                        endPoint = [1,0,0]
@@ -321,9 +336,21 @@ class toolScale(object):
                        endPoint = [0,1,0]
                elif self.node == 4:
                        endPoint = [0,0,1]
-               scale = self._lineLineCrossingDistOnLine(pp0, pp1, numpy.array([0,0,0], numpy.float32), numpy.array(endPoint, numpy.float32)) / 15.0 / s
+               scale = self._lineLineCrossingDistOnLine(p0, p1, numpy.array([0,0,0], numpy.float32), numpy.array(endPoint, numpy.float32)) / 15.0 / s
                if not wx.GetKeyState(wx.WXK_SHIFT):
-                       scale = round(scale * 10) / 10
+                       objMatrix = self.parent.getObjectMatrix()
+                       scaleX = numpy.linalg.norm(objMatrix[::,0].getA().flatten())
+                       scaleY = numpy.linalg.norm(objMatrix[::,1].getA().flatten())
+                       scaleZ = numpy.linalg.norm(objMatrix[::,2].getA().flatten())
+                       if self.node == 1 or not wx.GetKeyState(wx.WXK_CONTROL):
+                               matrixScale = (scaleX + scaleY + scaleZ) / 3
+                       elif self.node == 2:
+                               matrixScale = scaleX
+                       elif self.node == 3:
+                               matrixScale = scaleY
+                       elif self.node == 4:
+                               matrixScale = scaleZ
+                       scale = (round((matrixScale * scale) * 10) / 10) / matrixScale
                if scale < 0:
                        scale = -scale
                if scale < 0.1:
@@ -352,9 +379,10 @@ class toolScale(object):
                        sy *= self.scale
                if self.node == 4 and self.scale is not None:
                        sz *= self.scale
-               scaleX = numpy.linalg.norm(self.parent.parent.matrix[0].getA().flatten())
-               scaleY = numpy.linalg.norm(self.parent.parent.matrix[1].getA().flatten())
-               scaleZ = numpy.linalg.norm(self.parent.parent.matrix[2].getA().flatten())
+               objMatrix = self.parent.getObjectMatrix()
+               scaleX = numpy.linalg.norm(objMatrix[::,0].getA().flatten())
+               scaleY = numpy.linalg.norm(objMatrix[::,1].getA().flatten())
+               scaleZ = numpy.linalg.norm(objMatrix[::,2].getA().flatten())
                if self.scale is not None:
                        scaleX *= self.scale
                        scaleY *= self.scale
@@ -367,17 +395,21 @@ class toolScale(object):
 
                glColor3ub(0,0,0)
                size = self.parent.getObjectSize()
-               radius = self.parent.getObjectBoundaryCircle() * max(scaleX, scaleY, scaleZ)
+               radius = self.parent.getObjectBoundaryCircle()
+               if self.scale is not None:
+                       radius *= self.scale
                glPushMatrix()
                glTranslate(0,0,size[2]/2 + 5)
-               glRotate(-self.parent.yaw, 0,0,1)
-               if self.parent.pitch < 80:
+               glRotate(-self.parent._yaw, 0,0,1)
+               if self.parent._pitch < 80:
                        glTranslate(0, radius + 5,0)
-               elif self.parent.pitch < 100:
-                       glTranslate(0, (radius + 5) * (90 - self.parent.pitch) / 10,0)
+               elif self.parent._pitch < 100:
+                       glTranslate(0, (radius + 5) * (90 - self.parent._pitch) / 10,0)
                else:
                        glTranslate(0,-(radius + 5),0)
-               opengl.glDrawStringCenter("%dx%dx%d" % (size[0] * scaleX, size[1] * scaleY, size[2] * scaleZ))
+               if self.parent.tempMatrix is not None:
+                       size = (numpy.matrix([size]) * self.parent.tempMatrix).getA().flatten()
+               opengl.glDrawStringCenter("W, D, H: %0.1f, %0.1f, %0.1f mm" % (size[0], size[1], size[2]))
                glPopMatrix()
 
                glLineWidth(1)
@@ -385,10 +417,10 @@ class toolScale(object):
                glColor3ub(128,0,0)
                glVertex3f(0, 0, 0)
                glVertex3f(sx, 0, 0)
-               glColor3ub(128,128,0)
+               glColor3ub(0,128,0)
                glVertex3f(0, 0, 0)
                glVertex3f(0, sy, 0)
-               glColor3ub(0,128,0)
+               glColor3ub(0,0,128)
                glVertex3f(0, 0, 0)
                glVertex3f(0, 0, sz)
                glEnd()
@@ -415,9 +447,9 @@ class toolScale(object):
                        opengl.glDrawStringCenter("%0.2f" % (scaleX))
                glPopMatrix()
                if self.node == 3:
-                       glColor3ub(255,255,0)
+                       glColor3ub(64,255,64)
                else:
-                       glColor3ub(128,128,0)
+                       glColor3ub(0,128,0)
                glPushMatrix()
                glTranslatef(0,sy,0)
                opengl.DrawBox([-s,-s,-s], [s,s,s])
@@ -426,9 +458,9 @@ class toolScale(object):
                        opengl.glDrawStringCenter("%0.2f" % (scaleY))
                glPopMatrix()
                if self.node == 4:
-                       glColor3ub(64,255,64)
+                       glColor3ub(64,64,255)
                else:
-                       glColor3ub(0,128,0)
+                       glColor3ub(0,0,128)
                glPushMatrix()
                glTranslatef(0,0,sz)
                opengl.DrawBox([-s,-s,-s], [s,s,s])
@@ -438,3 +470,66 @@ class toolScale(object):
                glPopMatrix()
 
                glEnable(GL_DEPTH_TEST)
+               glColor(255,255,255)
+               size = size / 2
+               size += 0.01
+               glLineWidth(1)
+               glBegin(GL_LINES)
+               glVertex3f(size[0], size[1], size[2])
+               glVertex3f(size[0], size[1], size[2]/4*3)
+               glVertex3f(size[0], size[1], size[2])
+               glVertex3f(size[0], size[1]/4*3, size[2])
+               glVertex3f(size[0], size[1], size[2])
+               glVertex3f(size[0]/4*3, size[1], size[2])
+
+               glVertex3f(-size[0], size[1], size[2])
+               glVertex3f(-size[0], size[1], size[2]/4*3)
+               glVertex3f(-size[0], size[1], size[2])
+               glVertex3f(-size[0], size[1]/4*3, size[2])
+               glVertex3f(-size[0], size[1], size[2])
+               glVertex3f(-size[0]/4*3, size[1], size[2])
+
+               glVertex3f(size[0], -size[1], size[2])
+               glVertex3f(size[0], -size[1], size[2]/4*3)
+               glVertex3f(size[0], -size[1], size[2])
+               glVertex3f(size[0], -size[1]/4*3, size[2])
+               glVertex3f(size[0], -size[1], size[2])
+               glVertex3f(size[0]/4*3, -size[1], size[2])
+
+               glVertex3f(-size[0], -size[1], size[2])
+               glVertex3f(-size[0], -size[1], size[2]/4*3)
+               glVertex3f(-size[0], -size[1], size[2])
+               glVertex3f(-size[0], -size[1]/4*3, size[2])
+               glVertex3f(-size[0], -size[1], size[2])
+               glVertex3f(-size[0]/4*3, -size[1], size[2])
+
+               glVertex3f(size[0], size[1], -size[2])
+               glVertex3f(size[0], size[1], -size[2]/4*3)
+               glVertex3f(size[0], size[1], -size[2])
+               glVertex3f(size[0], size[1]/4*3, -size[2])
+               glVertex3f(size[0], size[1], -size[2])
+               glVertex3f(size[0]/4*3, size[1], -size[2])
+
+               glVertex3f(-size[0], size[1], -size[2])
+               glVertex3f(-size[0], size[1], -size[2]/4*3)
+               glVertex3f(-size[0], size[1], -size[2])
+               glVertex3f(-size[0], size[1]/4*3, -size[2])
+               glVertex3f(-size[0], size[1], -size[2])
+               glVertex3f(-size[0]/4*3, size[1], -size[2])
+
+               glVertex3f(size[0], -size[1], -size[2])
+               glVertex3f(size[0], -size[1], -size[2]/4*3)
+               glVertex3f(size[0], -size[1], -size[2])
+               glVertex3f(size[0], -size[1]/4*3, -size[2])
+               glVertex3f(size[0], -size[1], -size[2])
+               glVertex3f(size[0]/4*3, -size[1], -size[2])
+
+               glVertex3f(-size[0], -size[1], -size[2])
+               glVertex3f(-size[0], -size[1], -size[2]/4*3)
+               glVertex3f(-size[0], -size[1], -size[2])
+               glVertex3f(-size[0], -size[1]/4*3, -size[2])
+               glVertex3f(-size[0], -size[1], -size[2])
+               glVertex3f(-size[0]/4*3, -size[1], -size[2])
+               glEnd()
+
+               glEnable(GL_DEPTH_TEST)