From dc374af538771ef12fb660e5c21b6f8372c1b4e8 Mon Sep 17 00:00:00 2001
From: Amanda Ghassaei <amandaghassaei@gmail.com>
Date: Wed, 11 Mar 2015 01:14:57 -0400
Subject: [PATCH] working on sbp exporter

---
 js/cam/Assembler.js       |  7 ++++++
 js/cam/ShopbotExporter.js | 50 ++++++++++++++++++++++++++++++++++++++-
 js/menus/CamMenuView.js   |  9 ++++++-
 3 files changed, 64 insertions(+), 2 deletions(-)

diff --git a/js/cam/Assembler.js b/js/cam/Assembler.js
index 44964c5a..4b35ba64 100644
--- a/js/cam/Assembler.js
+++ b/js/cam/Assembler.js
@@ -15,6 +15,13 @@ Assembler = Backbone.Model.extend({
 
     },
 
+    processAndSave: function(){
+        var exporter;
+        if (this.get("camProcess") == "shopbot") exporter = new ShopbotExporter();
+        if (exporter) exporter.processAndSave();
+        else console.warn("cam process not supported");
+    },
+
     destroy: function(){
     }
 
diff --git a/js/cam/ShopbotExporter.js b/js/cam/ShopbotExporter.js
index f25bf3a3..8c85400c 100644
--- a/js/cam/ShopbotExporter.js
+++ b/js/cam/ShopbotExporter.js
@@ -6,6 +6,54 @@ function ShopbotExporter() {
 
 }
 
-ShopbotExporter.prototype.exportAndSave = function(){
+ShopbotExporter.prototype.processAndSave = function(){
 
+    var data = "";
+
+    data = this.makeHeader(data);
+    data = this.moveZ(data, 3);
+    data = this.jog3(data, 1, 4, 5);
+
+    this.save(data);
+};
+
+ShopbotExporter.prototype.makeHeader = function(data){
+    data = this.addLine(data, "FG", [], "single step mode");
+    data = this.goHome(data);
+    data = this.addLine(data, "SM", [4, 1], "set to move/cut mode");
+    data = this.addLine(data, "JS", [4, 1], "jog speed- xy, z inches per sec");
+    data = this.addLine(data, "MS", [4, 1], "move speed- xy, z inches per sec");
+    return data;
+};
+
+ShopbotExporter.prototype.addLine = function(data, command, params, comment){
+    data += command + " ";
+    _.each(params, function(param){
+        data += param + ", ";
+    });
+    if (comment) data += "'" +comment;
+    data += "\n";
+    return data;
+};
+
+ShopbotExporter.prototype.jog3 = function(data, x, y, z){
+    return this.addLine(data, "J3", [x,y,z]);
+};
+
+ShopbotExporter.prototype.move3 = function(data, x, y, z){
+    return this.addLine(data, "M3", [x,y,z]);
+};
+
+ShopbotExporter.prototype.goHome = function(data){
+    return this.addLine(data, "JH", [], "go home");
+};
+
+ShopbotExporter.prototype.moveZ = function(data, z){
+    return this.move3(data, "", "", z);
+};
+
+
+ShopbotExporter.prototype.save = function(data){
+    var blob = new Blob([data], {type: "text/plain;charset=utf-8"});
+    saveAs(blob, "ShopbotExport" + ".sbp");
 };
diff --git a/js/menus/CamMenuView.js b/js/menus/CamMenuView.js
index d08343d0..8d06bb3c 100644
--- a/js/menus/CamMenuView.js
+++ b/js/menus/CamMenuView.js
@@ -8,7 +8,8 @@ CamMenuView = Backbone.View.extend({
     el: "#menuContent",
 
     events: {
-        "click .camProcess":                            "_selectCamProcess"
+        "click .camProcess":                            "_selectCamProcess",
+        "click #saveCam":                               "_processAndSave"
     },
 
 
@@ -26,6 +27,11 @@ CamMenuView = Backbone.View.extend({
         this.assembler.set("camProcess", $(e.target).data("type"));
     },
 
+    _processAndSave: function(e){
+        e.preventDefault();
+        this.assembler.processAndSave();
+    },
+
     render: function(){
         if (this.model.get("currentTab") != "cam") return;
         this.$el.html(this.template(_.extend(this.model.toJSON(), this.assembler.toJSON())));
@@ -41,6 +47,7 @@ CamMenuView = Backbone.View.extend({
                     <% }); %>\
                 </ul>\
             </div><br/><br/>\
+            <a href="#" id="saveCam" class=" btn btn-block btn-lg btn-default">Process and Save</a><br/>\
         ')
 
 });
\ No newline at end of file
-- 
GitLab