From 5ce34f774ec3186d31eb256800d8ac52b43ad3c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Mon, 24 Jul 2017 12:55:39 +0200 Subject: [PATCH] GCODE Viewer: Fix file position calculation We were only adding one byte for "\n". That ignored that our regex could also match "\r\n" or "\r\r" or something like that. Our split regex now only matches one "\r" or "\n". Empty lines we'll simply ignore anyhow so no real harm done. Also, we no longer strip the comments in this step - leave that to the worker running in its own thread. Not only should that speed things up a bit, it will also allow us to better debug the worker in the future. --- src/octoprint/static/gcodeviewer/js/Worker.js | 5 +++++ src/octoprint/static/gcodeviewer/js/gCodeReader.js | 11 ++++------- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/octoprint/static/gcodeviewer/js/Worker.js b/src/octoprint/static/gcodeviewer/js/Worker.js index f96fa706b..6afb77d77 100644 --- a/src/octoprint/static/gcodeviewer/js/Worker.js +++ b/src/octoprint/static/gcodeviewer/js/Worker.js @@ -325,6 +325,11 @@ var doParse = function () { extrude = false; line = line.split(/[\(;]/)[0]; + if (!line || line.trim() === "") { + // empty line, skip entirely + continue; + } + var addToModel = false; var move = false; diff --git a/src/octoprint/static/gcodeviewer/js/gCodeReader.js b/src/octoprint/static/gcodeviewer/js/gCodeReader.js index abc567276..6f8a9e49a 100644 --- a/src/octoprint/static/gcodeviewer/js/gCodeReader.js +++ b/src/octoprint/static/gcodeviewer/js/gCodeReader.js @@ -44,15 +44,12 @@ GCODE.gCodeReader = (function(){ var prepareGCode = function(totalSize){ if(!lines)return; gcode = []; - var i, tmp, byteCount; + var i, byteCount; byteCount = 0; for(i=0;i 1 || tmp === -1) { - gcode.push({line: lines[i], percentage: byteCount * 100 / totalSize}); - } + byteCount += lines[i].length + 1; // line length + line ending + gcode.push({line: lines[i], percentage: byteCount * 100 / totalSize}); } lines = []; }; @@ -146,7 +143,7 @@ GCODE.gCodeReader = (function(){ this.clear(); var totalSize = reader.target.result.length; - lines = reader.target.result.split(/[\r\n]+/g); + lines = reader.target.result.split(/[\r\n]/g); reader.target.result = null; prepareGCode(totalSize);