From 021f413c127ef79e2a318c6e0acd7bba4dd39fc5 Mon Sep 17 00:00:00 2001 From: Dale Price Date: Tue, 26 Mar 2013 00:09:36 -0500 Subject: [PATCH] add "Load and Print" button which automatically starts printing when a gcode file is done loading. Solves foosel/OctoPrint#91 --- octoprint/printer.py | 27 +++++++++++++++++++++++++++ octoprint/server.py | 8 ++++++++ octoprint/static/css/octoprint.less | 2 +- octoprint/static/js/ui.js | 9 +++++++++ octoprint/templates/index.html | 2 +- 5 files changed, 46 insertions(+), 2 deletions(-) diff --git a/octoprint/printer.py b/octoprint/printer.py index 14edf8b..72cb531 100644 --- a/octoprint/printer.py +++ b/octoprint/printer.py @@ -172,6 +172,22 @@ class Printer(): self._gcodeLoader.start() self._stateMonitor.setState({"state": self._state, "stateString": self.getStateString(), "flags": self._getStateFlags()}) + + def loadAndPrintGcode(self, file): + """ + Loads the gcode from the given file as the new print job and starts the print. + Aborts if the printer is currently printing or another gcode file is currently being loaded, or if loading the gcode file fails. + """ + if (self._comm is not None and self._comm.isPrinting()) or (self._gcodeLoader is not None): + return + + self._setJobData(None, None) + + self._gcodeLoader = GcodeLoader(file, self._onGcodeLoadingProgress, self._onGcodeLoadedToPrint) + self._gcodeLoader.start() + + self._stateMonitor.setState({"state": self._state, "stateString": self.getStateString(), "flags": self._getStateFlags()}) + def startPrint(self): """ @@ -416,6 +432,17 @@ class Printer(): self._stateMonitor.setGcodeData({"filename": None, "progress": None}) self._stateMonitor.setState({"state": self._state, "stateString": self.getStateString(), "flags": self._getStateFlags()}) + def _onGcodeLoadedToPrint(self, filename, gcodeList): + self._setJobData(filename, gcodeList) + self._setCurrentZ(None) + self._setProgressData(None, None, None) + self._gcodeLoader = None + + self._stateMonitor.setGcodeData({"filename": None, "progress": None}) + self._stateMonitor.setState({"state": self._state, "stateString": self.getStateString(), "flags": self._getStateFlags()}) + + self.startPrint(); + #~~ state reports def feedrateState(self): diff --git a/octoprint/server.py b/octoprint/server.py index 63c096c..6af1e58 100644 --- a/octoprint/server.py +++ b/octoprint/server.py @@ -266,6 +266,14 @@ def loadGcodeFile(): printer.loadGcode(filename) return jsonify(SUCCESS) +@app.route(BASEURL + "gcodefiles/loadandprint", methods=["POST"]) +def loadAndPrintGcodeFile(): + if "filename" in request.values.keys(): + filename = gcodeManager.getAbsolutePath(request.values["filename"]) + if filename is not None: + printer.loadAndPrintGcode(filename) + return jsonify(SUCCESS) + @app.route(BASEURL + "gcodefiles/delete", methods=["POST"]) def deleteGcodeFile(): if "filename" in request.values.keys(): diff --git a/octoprint/static/css/octoprint.less b/octoprint/static/css/octoprint.less index ce1be6c..31a233c 100644 --- a/octoprint/static/css/octoprint.less +++ b/octoprint/static/css/octoprint.less @@ -145,7 +145,7 @@ table { &.gcode_files_action { text-align: center; - width: 45px; + width: 70px; } // timelapse files diff --git a/octoprint/static/js/ui.js b/octoprint/static/js/ui.js index 48df520..8027d51 100644 --- a/octoprint/static/js/ui.js +++ b/octoprint/static/js/ui.js @@ -719,6 +719,15 @@ function GcodeFilesViewModel() { }) } + self.loadAndPrintFile = function(filename) { + $.ajax({ + url: AJAX_BASEURL + "gcodefiles/loadandprint", + type: "POST", + dataType: "json", + data: {filename: filename} + }) + } + self.removeFile = function(filename) { $.ajax({ url: AJAX_BASEURL + "gcodefiles/delete", diff --git a/octoprint/templates/index.html b/octoprint/templates/index.html index ba96af2..8bf411c 100644 --- a/octoprint/templates/index.html +++ b/octoprint/templates/index.html @@ -127,7 +127,7 @@ -  |  +  |  |