From c7e5d631f947982c1b2fad570bc9c3a65cc0d3d3 Mon Sep 17 00:00:00 2001 From: daid303 Date: Wed, 3 Apr 2013 08:54:39 +0200 Subject: [PATCH] Add option to save GCode file. Re-center the objects on the bed when you add a new object. --- Cura/gui/sceneView.py | 70 +++++++++++++++++++++++++---------- Cura/gui/util/openglGui.py | 28 +++++++------- Cura/util/meshLoader.py | 4 +- Cura/util/meshLoaders/obj.py | 72 +++++++++++++++++------------------- Cura/util/meshLoaders/stl.py | 2 +- Cura/util/objectScene.py | 24 ++++++++++-- 6 files changed, 121 insertions(+), 79 deletions(-) diff --git a/Cura/gui/sceneView.py b/Cura/gui/sceneView.py index c1a91d62..5796e135 100644 --- a/Cura/gui/sceneView.py +++ b/Cura/gui/sceneView.py @@ -4,6 +4,8 @@ import wx import numpy import time import os +import traceback +import shutil import OpenGL OpenGL.ERROR_CHECKING = False @@ -74,23 +76,42 @@ class SceneView(openglGui.glGuiPanel): self.updateProfileToControls() wx.EVT_IDLE(self, self.OnIdle) - def ShowLoadModel(self): - dlg=wx.FileDialog(self, 'Open 3D model', os.path.split(profile.getPreference('lastFile'))[0], style=wx.FD_OPEN|wx.FD_FILE_MUST_EXIST) - dlg.SetWildcard(meshLoader.wildcardFilter()) - if dlg.ShowModal() != wx.ID_OK: + def ShowLoadModel(self, button): + if button == 1: + dlg=wx.FileDialog(self, 'Open 3D model', os.path.split(profile.getPreference('lastFile'))[0], style=wx.FD_OPEN|wx.FD_FILE_MUST_EXIST) + dlg.SetWildcard(meshLoader.wildcardFilter()) + if dlg.ShowModal() != wx.ID_OK: + dlg.Destroy() + return + filename = dlg.GetPath() dlg.Destroy() - return - filename = dlg.GetPath() - dlg.Destroy() - if not(os.path.exists(filename)): - return False - profile.putPreference('lastFile', filename) - self.GetParent().GetParent().GetParent().addToModelMRU(filename) - self.loadScene([filename]) - - def ShowPrintWindow(self): - if machineCom.machineIsConnected(): - printWindow.printFile(self._slicer.getGCodeFilename()) + if not(os.path.exists(filename)): + return False + profile.putPreference('lastFile', filename) + self.GetParent().GetParent().GetParent().addToModelMRU(filename) + self.loadScene([filename]) + + def ShowPrintWindow(self, button): + if button == 1: + if machineCom.machineIsConnected(): + printWindow.printFile(self._slicer.getGCodeFilename()) + elif len(removableStorage.getPossibleSDcardDrives()) > 0: + drives = removableStorage.getPossibleSDcardDrives() + if len(drives) > 1: + pass + else: + defPath = profile.getPreference('lastFile') + defPath = defPath[0:defPath.rfind('.')] + '.gcode' + dlg=wx.FileDialog(self, 'Save toolpath', defPath, style=wx.FD_SAVE) + dlg.SetFilename(defPath) + dlg.SetWildcard('Toolpath (*.gcode)|*.gcode;*.g') + if dlg.ShowModal() != wx.ID_OK: + dlg.Destroy() + return + filename = dlg.GetPath() + dlg.Destroy() + + shutil.copy(self._slicer.getGCodeFilename(), filename) def OnIdle(self, e): if self._animView is not None or self._animZoom is not None: @@ -113,10 +134,16 @@ class SceneView(openglGui.glGuiPanel): def loadScene(self, fileList): for filename in fileList: - for obj in meshLoader.loadMeshes(filename): - obj._loadAnim = anim(1, 0, 1.5) - self._scene.add(obj) - self._selectObject(obj) + try: + objList = meshLoader.loadMeshes(filename) + except: + traceback.print_exc() + else: + for obj in objList: + obj._loadAnim = anim(1, 0, 1.5) + self._scene.add(obj) + self._scene.centerAll() + self._selectObject(obj) self.sceneUpdated() def _deleteObject(self, obj): @@ -276,10 +303,13 @@ class SceneView(openglGui.glGuiPanel): def OnPaint(self,e): if machineCom.machineIsConnected(): self.printButton._imageID = 6 + self.printButton._tooltip = 'Print' elif len(removableStorage.getPossibleSDcardDrives()) > 0: self.printButton._imageID = 2 + self.printButton._tooltip = 'Toolpath to SD' else: self.printButton._imageID = 3 + self.printButton._tooltip = 'Save toolpath' if self._animView is not None: self._viewTarget = self._animView.getPosition() diff --git a/Cura/gui/util/openglGui.py b/Cura/gui/util/openglGui.py index 72da49d2..f4fd3707 100644 --- a/Cura/gui/util/openglGui.py +++ b/Cura/gui/util/openglGui.py @@ -71,9 +71,9 @@ class glGuiContainer(glGuiControl): self._glGuiControlList.append(ctrl) self.updateLayout() - def OnMouseDown(self, x, y): + def OnMouseDown(self, x, y, button): for ctrl in self._glGuiControlList: - if ctrl.OnMouseDown(x, y): + if ctrl.OnMouseDown(x, y, button): return True return False @@ -101,7 +101,7 @@ class glGuiContainer(glGuiControl): class glGuiPanel(glcanvas.GLCanvas): def __init__(self, parent): - attribList = (glcanvas.WX_GL_RGBA, glcanvas.WX_GL_DOUBLEBUFFER, glcanvas.WX_GL_DEPTH_SIZE, 24, glcanvas.WX_GL_STENCIL_SIZE, 8) + attribList = (glcanvas.WX_GL_RGBA, glcanvas.WX_GL_DOUBLEBUFFER, glcanvas.WX_GL_DEPTH_SIZE, 32, glcanvas.WX_GL_STENCIL_SIZE, 8) glcanvas.GLCanvas.__init__(self, parent, style=wx.WANTS_CHARS, attribList = attribList) self._base = self self._focus = None @@ -145,7 +145,7 @@ class glGuiPanel(glcanvas.GLCanvas): def _OnGuiMouseDown(self,e): self.SetFocus() - if self._container.OnMouseDown(e.GetX(), e.GetY()): + if self._container.OnMouseDown(e.GetX(), e.GetY(), e.Button): self.Refresh() return self.OnMouseDown(e) @@ -442,9 +442,9 @@ class glButton(glGuiControl): self._focus = False return False - def OnMouseDown(self, x, y): + def OnMouseDown(self, x, y, button): if self._checkHit(x, y): - self._callback() + self._callback(button) return True return False @@ -533,7 +533,7 @@ class glComboButton(glButton): self._imageID = self._imageIDs[self._selection] self._comboCallback() - def OnMouseDown(self, x, y): + def OnMouseDown(self, x, y, button): if self._hidden or self._disabled: return False if self.hasFocus(): @@ -545,7 +545,7 @@ class glComboButton(glButton): self._base._focus = None self._comboCallback() return True - return super(glComboButton, self).OnMouseDown(x, y) + return super(glComboButton, self).OnMouseDown(x, y, button) class glFrame(glGuiContainer): def __init__(self, parent, pos): @@ -704,9 +704,9 @@ class glFrame(glGuiContainer): self._focus = False return False - def OnMouseDown(self, x, y): + def OnMouseDown(self, x, y, button): if self._checkHit(x, y): - super(glFrame, self).OnMouseDown(x, y) + super(glFrame, self).OnMouseDown(x, y, button) return True return False @@ -752,7 +752,7 @@ class glLabel(glGuiControl): def OnMouseMotion(self, x, y): return False - def OnMouseDown(self, x, y): + def OnMouseDown(self, x, y, button): return False class glNumberCtrl(glGuiControl): @@ -813,7 +813,7 @@ class glNumberCtrl(glGuiControl): def OnMouseMotion(self, x, y): return False - def OnMouseDown(self, x, y): + def OnMouseDown(self, x, y, button): if self._checkHit(x, y): self.setFocus() return True @@ -914,7 +914,7 @@ class glCheckbox(glGuiControl): def OnMouseMotion(self, x, y): return False - def OnMouseDown(self, x, y): + def OnMouseDown(self, x, y, button): if self._checkHit(x, y): self._value = not self._value return True @@ -1032,7 +1032,7 @@ class glSlider(glGuiControl): self._focus = False return False - def OnMouseDown(self, x, y): + def OnMouseDown(self, x, y, button): if self._checkHit(x, y): self.setFocus() self.OnMouseMotion(x, y) diff --git a/Cura/util/meshLoader.py b/Cura/util/meshLoader.py index f533a056..bc514150 100644 --- a/Cura/util/meshLoader.py +++ b/Cura/util/meshLoader.py @@ -21,9 +21,9 @@ def wildcardFilter(): def loadMeshes(filename): ext = filename[filename.rfind('.'):].lower() if ext == '.stl': - return stl.loadSTLscene(filename) + return stl.loadScene(filename) if ext == '.obj': - return obj.objModel().load(filename) + return obj.loadScene(filename) if ext == '.dae': return dae.daeModel().load(filename) if ext == '.amf': diff --git a/Cura/util/meshLoaders/obj.py b/Cura/util/meshLoaders/obj.py index e8c7dfa7..dab9ddc2 100644 --- a/Cura/util/meshLoaders/obj.py +++ b/Cura/util/meshLoaders/obj.py @@ -2,42 +2,38 @@ from __future__ import absolute_import from Cura.util import mesh -class objModel(mesh.mesh): - def __init__(self): - super(objModel, self).__init__() +def loadScene(filename): + obj = mesh.printableObject() + m = obj._addMesh() - def load(self, filename): - vertexList = [] - faceList = [] - - f = open(filename, "r") - for line in f: - parts = line.split() - if len(parts) < 1: - continue - if parts[0] == 'v': - vertexList.append([float(parts[1]), float(parts[2]), float(parts[3])]) - if parts[0] == 'f': - parts = map(lambda p: p.split('/')[0], parts) - for idx in xrange(1, len(parts)-2): - faceList.append([int(parts[1]), int(parts[idx+1]), int(parts[idx+2])]) - f.close() - - self._prepareVertexCount(len(faceList) * 3) - for f in faceList: - i = f[0] - 1 - if i < 0 or i >= len(vertexList): - i = 0 - self.addVertex(vertexList[i][0], vertexList[i][1], vertexList[i][2]) - i = f[1] - 1 - if i < 0 or i >= len(vertexList): - i = 0 - self.addVertex(vertexList[i][0], vertexList[i][1], vertexList[i][2]) - i = f[2] - 1 - if i < 0 or i >= len(vertexList): - i = 0 - self.addVertex(vertexList[i][0], vertexList[i][1], vertexList[i][2]) - - self._postProcessAfterLoad() - return self - + vertexList = [] + faceList = [] + + f = open(filename, "r") + for line in f: + parts = line.split() + if len(parts) < 1: + continue + if parts[0] == 'v': + vertexList.append([float(parts[1]), float(parts[2]), float(parts[3])]) + if parts[0] == 'f': + parts = map(lambda p: p.split('/')[0], parts) + for idx in xrange(1, len(parts)-2): + faceList.append([int(parts[1]), int(parts[idx+1]), int(parts[idx+2])]) + f.close() + + m._prepareFaceCount(len(faceList)) + for f in faceList: + i = f[0] - 1 + j = f[1] - 1 + k = f[2] - 1 + if i < 0 or i >= len(vertexList): + i = 0 + if j < 0 or j >= len(vertexList): + j = 0 + if k < 0 or k >= len(vertexList): + k = 0 + m._addFace(vertexList[i][0], vertexList[i][1], vertexList[i][2], vertexList[j][0], vertexList[j][1], vertexList[j][2], vertexList[k][0], vertexList[k][1], vertexList[k][2]) + + obj._postProcessAfterLoad() + return [obj] diff --git a/Cura/util/meshLoaders/stl.py b/Cura/util/meshLoaders/stl.py index 068154ec..ab9c9354 100644 --- a/Cura/util/meshLoaders/stl.py +++ b/Cura/util/meshLoaders/stl.py @@ -35,7 +35,7 @@ def _loadBinary(m, f): data = struct.unpack("