From 797471dc22536271766a9127163e42163be71dc3 Mon Sep 17 00:00:00 2001
From: amandaghassaei <amandaghassaei@gmail.com>
Date: Wed, 18 Mar 2015 15:04:45 -0400
Subject: [PATCH] support for gcode sim

---
 js/cam/GCodeExporter.js       | 46 +++++++++++++++++++++++++++++------
 js/cam/ShopbotExporter.js     |  4 +--
 js/menus/AnimationMenuView.js |  9 ++++---
 3 files changed, 45 insertions(+), 14 deletions(-)

diff --git a/js/cam/GCodeExporter.js b/js/cam/GCodeExporter.js
index dc724ab8..ceba96c6 100644
--- a/js/cam/GCodeExporter.js
+++ b/js/cam/GCodeExporter.js
@@ -29,11 +29,7 @@ GCodeExporter.prototype.addLine = function(command, params, comment){
     data += command + " ";
     _.each(params, function(param){
         if (!param) return;
-        if (isNaN(parseFloat(param))) {
-            data += param + " ";
-            return;
-        }
-        data += param.toFixed(3) + " ";
+        data += param + " ";
     });
     if (comment) data += "(" + comment + ")";
     data += "\n";
@@ -57,9 +53,9 @@ GCodeExporter.prototype.rapidZ = function(z){
 };
 
 GCodeExporter.prototype.moveXYZ = function(x, y, z){
-    if (x !== null) x = "X"+x;
-    if (y !== null) y = "Y"+y;
-    if (z !== null) z = "Z"+z;
+    if (x !== null) x = "X"+parseFloat(x).toFixed(3);
+    if (y !== null) y = "Y"+parseFloat(y).toFixed(3);
+    if (z !== null) z = "Z"+parseFloat(z).toFixed(3);
     return this.addLine("G01", [x,y,z]);
 };
 
@@ -87,3 +83,37 @@ GCodeExporter.prototype.save = function(data){
     var blob = new Blob([data], {type: "text/plain;charset=utf-8"});
     saveAs(blob, "GCodeExport" + ".g");
 };
+
+GCodeExporter.prototype.simulate = function(line, machine, wcs,  callback){
+    if (line == "(get stock)"){
+        machine.pickUpStock();
+        return callback();
+    }
+    if (line.substr(0,2) == "({"){
+        machine.releaseStock(line.substr(1,line.length-2));
+        return callback();
+    }
+    if (line == "" || line[0] == "(" || line.substr(0,3) != "G01"){
+        return callback();
+    }
+    if (line.substr(0,3) == "G01"){
+        //return this._simulateGetPosition(line, dmaGlobals.assembler.get("feedRate"), machine, wcs, callback);
+        return this._simulateGetPosition(line, dmaGlobals.assembler.get("rapidSpeeds"), machine, wcs, callback);
+    } else {
+        console.warn("problem parsing gcode: " + line);
+        return callback();
+    }
+};
+
+GCodeExporter.prototype._simulateGetPosition = function(line, speed, machine, wcs, callback){
+    var data = line.split(" ");
+    var position = {x:"",y:"",z:""};
+    if (data.length<2) console.warn("problem parsing gcode " + line);
+    for (var i=1;i<data.length;i++){
+        var item = data[i];
+        if (item[0] == "X") position.x = item.substr(1);
+        if (item[0] == "Y") position.y = item.substr(1);
+        if (item[0] == "Z") position.z = item.substr(1);
+    }
+    machine.moveTo(position.x, position.y, position.z, speed, wcs, callback);
+};
diff --git a/js/cam/ShopbotExporter.js b/js/cam/ShopbotExporter.js
index 4af77eb8..681461dd 100644
--- a/js/cam/ShopbotExporter.js
+++ b/js/cam/ShopbotExporter.js
@@ -100,7 +100,7 @@ ShopbotExporter.prototype.simulate = function(line, machine, wcs,  callback){
     } else if (line[0] == "M"){
         return this._simulateGetPosition(line, dmaGlobals.assembler.get("feedRate"), machine, wcs, callback);
     } else {
-        console.warn("problem parsing sbp");
+        console.warn("problem parsing sbp " + line);
         return callback();
     }
 };
@@ -121,7 +121,7 @@ ShopbotExporter.prototype._simulateGetPosition = function(line, speed, machine,
     } else if (line[1] == "S"){
         return callback();
     } else {
-        console.warn("problem parsing sbp");
+        console.warn("problem parsing sbp " + line);
         return callback();
     }
 };
diff --git a/js/menus/AnimationMenuView.js b/js/menus/AnimationMenuView.js
index 006cf83f..6158eed3 100644
--- a/js/menus/AnimationMenuView.js
+++ b/js/menus/AnimationMenuView.js
@@ -78,14 +78,16 @@ AnimationMenuView = Backbone.View.extend({
         var newText = code.join("\n");
         var $editor = $('#gcodeEditor');
         $editor.html(newText);
-        var highlighterHeight = $("#gcodeHighlighter").position().top - $editor.position().top;
+        var $highlighter = $("#gcodeHighlighter");
+        if (!$editor.position() || !$highlighter.position()) return;//todo weird bug
+        var highlighterHeight = $highlighter.position().top - $editor.position().top;
         var desiredHeight = $editor.height()/2;
         if (highlighterHeight > desiredHeight) $editor.scrollTop($editor.scrollTop()+highlighterHeight-desiredHeight);
     },
 
     _setEditorHeight: function(){
         var $editor = $('#gcodeEditor');
-        var height = this.$el.height()-$editor.position().top-50
+        var height = this.$el.height()-$editor.position().top-50;
         height = Math.max(height, 250);
         $editor.css({height:height +"px"});
     },
@@ -94,9 +96,8 @@ AnimationMenuView = Backbone.View.extend({
         if (this.model.get("currentTab") != "animate") return;
         if (dmaGlobals.assembler.get("needsPostProcessing") && !dmaGlobals.assembler.get("editsMadeToProgram")) dmaGlobals.assembler.postProcess();
         this.$el.html(this.template(_.extend(this.model.toJSON(), dmaGlobals.assembler.toJSON())));
-        if (!dmaGlobals.appState.get("stockSimulationPlaying")) this._drawGcodeHighlighter();//in case of code pause
-
         this._setEditorHeight();
+        this._drawGcodeHighlighter();//in case of code pause
 
         $('#speedSlider').slider({
             formatter: function(value) {
-- 
GitLab