From 420a3874abe1e70988cdb8b77adf29563cc062b0 Mon Sep 17 00:00:00 2001
From: Amanda Ghassaei <amandaghassaei@gmail.com>
Date: Fri, 21 Aug 2015 00:40:20 -0400
Subject: [PATCH] load custom scripts

---
 dependencies/jsonfn.js                    | 84 +++++++++++++++++++++++
 js/cam/assemblers/Assembler.js            | 14 +++-
 js/cam/assemblers/AssemblerPost.js        | 29 +++++++-
 js/cam/assemblers/Component.js            |  1 -
 js/cells/DMACell.js                       |  4 +-
 js/main.js                                |  5 +-
 js/menus/ScriptView.js                    |  4 +-
 js/menus/templates/AssemblerMenuView.html |  3 +-
 js/models/FileSaver.js                    |  6 +-
 js/plists/CamPList.js                     |  3 +-
 10 files changed, 137 insertions(+), 16 deletions(-)
 create mode 100755 dependencies/jsonfn.js

diff --git a/dependencies/jsonfn.js b/dependencies/jsonfn.js
new file mode 100755
index 00000000..bad4bbeb
--- /dev/null
+++ b/dependencies/jsonfn.js
@@ -0,0 +1,84 @@
+/**
+* JSONfn - javascript (both node.js and browser) plugin to stringify, 
+*          parse and clone objects with Functions, Regexp and Date.
+*  
+* Version - 0.60.00
+* Copyright (c) 2012 - 2014 Vadim Kiryukhin
+* vkiryukhin @ gmail.com
+* http://www.eslinstructor.net/jsonfn/
+* 
+* Licensed under the MIT license ( http://www.opensource.org/licenses/mit-license.php )
+*
+*   USAGE:
+*     browser:
+*         JSONfn.stringify(obj);
+*         JSONfn.parse(str[, date2obj]);
+*         JSONfn.clone(obj[, date2obj]);
+*
+*     nodejs:
+*       var JSONfn = require('path/to/json-fn');
+*       JSONfn.stringify(obj);
+*       JSONfn.parse(str[, date2obj]);
+*       JSONfn.clone(obj[, date2obj]);
+*
+*
+*     @obj      -  Object;
+*     @str      -  String, which is returned by JSONfn.stringify() function; 
+*     @date2obj - Boolean (optional); if true, date string in ISO8061 format
+*                 is converted into a Date object; otherwise, it is left as a String.
+*/
+
+"use strict";
+
+(function (exports) {
+
+  exports.stringify = function (obj) {
+
+    return JSON.stringify(obj, function (key, value) {
+      if (value instanceof Function || typeof value == 'function') {
+        return value.toString();
+      }
+      if (value instanceof RegExp) {
+        return '_PxEgEr_' + value;
+      }
+      return value;
+    });
+  };
+
+  exports.parse = function (str, date2obj) {
+
+    var iso8061 = date2obj ? /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}(?:\.\d*)?)Z$/ : false;
+
+    return JSON.parse(str, function (key, value) {
+      var prefix;
+
+      if (typeof value != 'string') {
+        return value;
+      }
+      if (value.length < 8) {
+        return value;
+      }
+
+      prefix = value.substring(0, 8);
+
+      if (iso8061 && value.match(iso8061)) {
+        return new Date(value);
+      }
+      if (prefix === 'function') {
+        return eval('(' + value + ')');
+      }
+      if (prefix === '_PxEgEr_') {
+        return eval(value.slice(8));
+      }
+
+      return value;
+    });
+  };
+
+  exports.clone = function (obj, date2obj) {
+    return exports.parse(exports.stringify(obj), date2obj);
+  };
+
+}(typeof exports === 'undefined' ? (window.JSONfn = {}) : exports));
+
+
diff --git a/js/cam/assemblers/Assembler.js b/js/cam/assemblers/Assembler.js
index 4bc3e4d1..8c69af97 100644
--- a/js/cam/assemblers/Assembler.js
+++ b/js/cam/assemblers/Assembler.js
@@ -310,6 +310,17 @@ define(['underscore', 'appState', 'lattice', 'stlLoader', 'threeModel', 'cam', '
             rapidSpeeds: cam.get("rapidSpeeds"),
             feedRate: cam.get("feedRate")
         };
+        json.customPost = {
+            customFunctionsContext: this.customFunctionsContext,
+            customHeader: this.customHeader.toString(),
+            customFooter: this.customFooter.toString(),
+            customHome: this.customHome.toString(),
+            customPickUpStock: this.customPickUpStock.toString(),
+            customChangeZLayer: this.customChangeZLayer.toString(),
+            customMoveXY: this.customMoveXY.toString(),
+            customPlacePart: this.customPlacePart.toString(),
+            customCalcPositionOffsets: this.customCalcPositionOffsets.toString()
+        };
         return {assembler: json};
     };
 
@@ -328,9 +339,6 @@ define(['underscore', 'appState', 'lattice', 'stlLoader', 'threeModel', 'cam', '
             stock: stockJSON,
             tree: this.tree || this.buildComponentTree()
         });
-        json.customPost = {
-
-        };
         return json;
     };
 
diff --git a/js/cam/assemblers/AssemblerPost.js b/js/cam/assemblers/AssemblerPost.js
index 11dcec97..8928b0a7 100644
--- a/js/cam/assemblers/AssemblerPost.js
+++ b/js/cam/assemblers/AssemblerPost.js
@@ -20,7 +20,7 @@ define(['underscore', 'appState', 'lattice', 'cam'], function(_, appState, latti
 //        stockPosition: THREE.Vector3 - not used for your machine
 //        units: mm
 
-        this.customFunctionsContext = {
+        this.customFunctionsContext = json.customPost.customFunctionsContext || {
             zClearHeight: 8,//height above part to clear during assembly
             zPreload: 0.2,
             stockWait: 0.75,//seconds
@@ -33,6 +33,7 @@ define(['underscore', 'appState', 'lattice', 'cam'], function(_, appState, latti
     data += this.customHome(exporter, settings, context);
     return data;
 };
+        this._loadFunction(json.customPost, "customHeader");
 
         this.customFooter = function(exporter, settings, context){
     var data = "";
@@ -40,17 +41,23 @@ define(['underscore', 'appState', 'lattice', 'cam'], function(_, appState, latti
     return data;
 };
 
+        this._loadFunction(json.customPost, "customFooter");
+
         this.customHome = function(exporter, settings, context){
     var data = "";
     data += exporter.goHome(settings);
     return data;
 };
 
+        this._loadFunction(json.customPost, "customHome");
+
         this.customPickUpStock = function(exporter, settings, context){//not relevant for your assembler
     var data = "";
     return data;
 };
 
+        this._loadFunction(json.customPost, "customPickUpStock");
+
         this.customChangeZLayer = function(currentIndex, lastIndex, exporter, settings, context){
     var data = "";
     if (lastIndex === null || (currentIndex.z-lastIndex.z)%2 != 0){
@@ -60,6 +67,8 @@ define(['underscore', 'appState', 'lattice', 'cam'], function(_, appState, latti
     return data;
 };
 
+        this._loadFunction(json.customPost, "customChangeZLayer");
+
         this.customMoveXY = function(position, lastPosition, index, exporter, settings, context){//already offset for dual heads
     var data = "";
 
@@ -83,6 +92,8 @@ define(['underscore', 'appState', 'lattice', 'cam'], function(_, appState, latti
     return data;
 };
 
+        this._loadFunction(json.customPost, "customMoveXY");
+
         this.customPlacePart = function(position, index, material, exporter, settings, context){//already offset for dual heads
     var data = "";
     data += exporter.rapidZ(position.z + settings.safeHeight, settings);
@@ -103,6 +114,8 @@ define(['underscore', 'appState', 'lattice', 'cam'], function(_, appState, latti
     return data;
 };
 
+        this._loadFunction(json.customPost, "customPlacePart");
+
         this.customCalcPositionOffsets = function(index, position, material, settings, context){
     //this feeds into moveXY and placePart functions
 
@@ -123,13 +136,13 @@ define(['underscore', 'appState', 'lattice', 'cam'], function(_, appState, latti
         return null;
     }
 
+
     position.sub(stock.getPosition().multiplyScalar(settings.scale));
 
     return position;
 }
 
-
-
+        this._loadFunction(json.customPost, "customCalcPositionOffsets");
     }
 
 
@@ -140,6 +153,16 @@ define(['underscore', 'appState', 'lattice', 'cam'], function(_, appState, latti
 
 
 
+    AssemblerPost.prototype._loadFunction = function(json, name){
+        if (json[name] === undefined) return;
+        var js = "js = " + json[name];
+        try{
+            eval(js);
+            this[name] = js;
+        } catch(error){
+            console.log(error.message);
+        }
+    };
 
 
 
diff --git a/js/cam/assemblers/Component.js b/js/cam/assemblers/Component.js
index f8a418fb..0073567d 100644
--- a/js/cam/assemblers/Component.js
+++ b/js/cam/assemblers/Component.js
@@ -50,7 +50,6 @@ define(['underscore', 'cam', 'three'], function(_, cam, THREE){
             var wrapper = new THREE.Object3D();
             wrapper.add(child.getObject3D());
             wrapper.position.set(-this.centerOfRotation.x, -this.centerOfRotation.y, -this.centerOfRotation.z);
-            console.log("here");
             this.secondWrapper = new THREE.Object3D();
             this.secondWrapper.add(wrapper);
             this.object3D.add(this.secondWrapper);
diff --git a/js/cells/DMACell.js b/js/cells/DMACell.js
index 8fa62bd7..cd230940 100644
--- a/js/cells/DMACell.js
+++ b/js/cells/DMACell.js
@@ -90,7 +90,7 @@ define(['underscore', 'three', 'threeModel', 'lattice', 'appState', 'globals', '
 
     DMACell.prototype.getIndex = function(){
         if (!this.index) {
-            console.warn("no index for this cell");
+//            console.warn("no index for this cell");
             return null;
         }
         return this.index.clone();
@@ -98,7 +98,7 @@ define(['underscore', 'three', 'threeModel', 'lattice', 'appState', 'globals', '
 
     DMACell.prototype.getAbsoluteIndex = function(){
         if (!this.index) {
-            console.warn("no index for this cell");
+//            console.warn("no index for this cell");
             return null;
         }
         if (!this.superCell) return this.getIndex();
diff --git a/js/main.js b/js/main.js
index 3b139a8c..64597bf2 100644
--- a/js/main.js
+++ b/js/main.js
@@ -16,7 +16,7 @@ require.config({
         numeric: '../dependencies/numeric-1.2.6',
         codeMirrorJS: '../dependencies/codemirror/javascript',
         codeMirror: '../dependencies/codemirror/codemirror',
-        acorn: '../dependencies/acorn/acorn',
+        jsonFn: '../dependencies/jsonfn',
 
         //three
         three: '../dependencies/three',
@@ -235,6 +235,9 @@ require.config({
         },
         'numeric': {
             exports: 'numeric'
+        },
+        'jsonFn': {
+            exports: 'JSONfn'
         }
     }
 
diff --git a/js/menus/ScriptView.js b/js/menus/ScriptView.js
index 4b58be1c..de02d46f 100644
--- a/js/menus/ScriptView.js
+++ b/js/menus/ScriptView.js
@@ -4,8 +4,8 @@
 
 
 
-define(['jquery', 'underscore', 'backbone', 'appState', 'codeMirror', 'acorn', 'globals', 'text!menus/templates/ScriptView.html',  'codeMirrorJS'],
-    function($, _, Backbone, appState, CodeMirror, acorn, globals, template){
+define(['jquery', 'underscore', 'backbone', 'appState', 'codeMirror', 'globals', 'text!menus/templates/ScriptView.html',  'codeMirrorJS'],
+    function($, _, Backbone, appState, CodeMirror, globals, template){
     
 
     var ScriptView = Backbone.View.extend({
diff --git a/js/menus/templates/AssemblerMenuView.html b/js/menus/templates/AssemblerMenuView.html
index 06063640..9eac7f05 100644
--- a/js/menus/templates/AssemblerMenuView.html
+++ b/js/menus/templates/AssemblerMenuView.html
@@ -32,7 +32,8 @@ Strategy: &nbsp;&nbsp;
         </ul>
     </div><br/><br/>
 <% if (camStrategy == "raster"){ %>
-    Raster Order: &nbsp;&nbsp;<input value="<%= placementOrder %>" placeholder="Placement Order" class="form-control placementOrder halfWidth" type="text"><br/><br/>
+    Raster Order: &nbsp;&nbsp;<input value="<%= placementOrder %>" placeholder="Placement Order" class="form-control placementOrder halfWidth" type="text"><br/>
+    <label>"YXZ" or "X-YZ" etc</label><br/><br/>
 <% } %>
 <% if (thisAssembler.numMaterials > -1 && thisAssembler.numMaterials < allCAMMaterialTypes.length){ %>
     <div class="inlineWarning">Number of materials in assembly exceeds available materials ( <%= thisAssembler.numMaterials %> ) for assembler type.</div>
diff --git a/js/models/FileSaver.js b/js/models/FileSaver.js
index 70587b3b..4b589eac 100644
--- a/js/models/FileSaver.js
+++ b/js/models/FileSaver.js
@@ -6,8 +6,10 @@
 define(['underscore', 'fileSaverLib', 'lattice', 'materials', 'ribbon', 'menuWrapper'], function(_, saveAs, lattice, materials, ribbon, menuWrapper){
 
     function _saveFile(data, name, extension){
-        var blob = new Blob([JSON.stringify(data, null, '\t')], {type: "text/plain;charset=utf-8"});
-        saveAs(blob, name + extension);
+//        require(['jsonFn'], function(JSONfn){
+            var blob = new Blob([JSON.stringify(data, null, '\t')], {type: "text/plain;charset=utf-8"});
+            saveAs(blob, name + extension);
+//        });
     }
 
 //    function save(name){
diff --git a/js/plists/CamPList.js b/js/plists/CamPList.js
index 88c35637..a8f9271a 100644
--- a/js/plists/CamPList.js
+++ b/js/plists/CamPList.js
@@ -133,7 +133,8 @@ define(['three'], function(THREE){
                     originPosition: new THREE.Vector3(0,0,0),
                     rapidSpeeds:{xy: 250, z: 250},
                     feedRate:{xy: 6, z: 6}
-                }
+                },
+                customPost: {}
             }
         },
 
-- 
GitLab