From 28c179aa776309b5f23272a3a6e2dfb41198c432 Mon Sep 17 00:00:00 2001 From: Wouter R Date: Mon, 21 Oct 2013 22:37:10 +0200 Subject: [PATCH] Hacky implementation of an SVG generator + api call to save generated data. --- .gitignore | 2 +- www/js/buttonbehaviors.js | 57 ++++++++++++++++++++++++++++----------- www/js/canvasDrawing.js | 43 +++++++++++++++++++++++++++++ 3 files changed, 85 insertions(+), 17 deletions(-) diff --git a/.gitignore b/.gitignore index a9b723e..6e829d9 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ - +node_modules .project diff --git a/www/js/buttonbehaviors.js b/www/js/buttonbehaviors.js index 1cb2fdd..eb7db6b 100644 --- a/www/js/buttonbehaviors.js +++ b/www/js/buttonbehaviors.js @@ -7,7 +7,7 @@ var twistIncrement = Math.PI/1800; var btnOopsInterval; -var btnNew, btnPrevious, btnNext; +var btnNew, btnSave, btnPrevious, btnNext; var btnOops, btnStop, btnClear; var btnMoveUp, btnMoveDown, btnTwistLeft, btnTwistRight; var btnInfo, btnSettings; @@ -32,6 +32,7 @@ function initButtonBehavior() { btnInfo = $(".btnInfo"); btnSettings = $(".btnSettings"); btnNew = $(".btnNew"); + btnSave = $(".btnSave"); btnPrint= $(".btnPrint"); btnStop = $(".btnStop"); @@ -186,6 +187,31 @@ function initButtonBehavior() { //*/ //btnStop.on('touchstart mousedown',stopPrint); + + btnSave.mouseup(function(e) { + svg = saveToSvg(); + console.log("generated SVG [" + _points.length + " points, " + svg.length + " bytes]:\n" + svg); + + wifiboxURL = "http://192.168.5.1/d3dapi"; + $.ajax({ + url: wifiboxURL + "/sketch", + dataType: 'json', + type: 'POST', + data: { data: svg }, + //timeout: this.timeoutTime, + success: function(response) { + if (response.status == 'error' || response.status == 'fail') + console.log("saveSketch fail/error: ", response); + else + console.log("saveSketch success: saved with id #" + response.data.id, response); + } + }).fail(function() { + console.log("saveSketch failed: ", response); + }); + }); + + btnPrevious.mouseup(function(e) { prevDoodle(); }); + btnNext.mouseup(function(e) { nextDoodle(); }); } function stopPrint() { console.log("f:stopPrint() >> sendPrintCommands = " + sendPrintCommands); @@ -198,7 +224,6 @@ function stopPrint() { function prevDoodle(e) { console.log("f:prevDoodle()"); - console.log("f:prevDoodle()"); } function nextDoodle(e) { console.log("f:nextDoodle()"); @@ -213,14 +238,14 @@ function print(e) { //setState(Printer.BUFFERING_STATE,printer.hasControl); printer.overruleState(Printer.BUFFERING_STATE); - + btnStop.css("display","none"); // hack - - // we put the gcode generation in a little delay + + // we put the gcode generation in a little delay // so that for example the print button is disabled right away clearTimeout(gcodeGenerateDelayer); - gcodeGenerateDelayer = setTimeout(function() { - + gcodeGenerateDelayer = setTimeout(function() { + var gcode = generate_gcode(); if (sendPrintCommands) { if(gcode.length > 0) { @@ -237,7 +262,7 @@ function print(e) { $("#textdump").text(""); $("#textdump").text(gcode.join("\n")); } - + }, gcodeGenerateDelay); } else { console.log("f:print >> not enough points!"); @@ -317,11 +342,11 @@ function update() { progressbar.update(printer.currentLine, printer.totalLines); } -function setState(newState,newHasControl) { +function setState(newState,newHasControl) { if(newState == state && newHasControl == hasControl) return; prevState = state; - + console.log("setState: ",prevState," > ",newState," ( ",newHasControl,")"); setDebugText("State: "+newState); @@ -353,34 +378,34 @@ function setState(newState,newHasControl) { case Printer.BUFFERING_STATE: case Printer.PRINTING_STATE: case Printer.STOPPING_STATE: - thermometer.show(); + thermometer.show(); break; default: - thermometer.hide(); + thermometer.hide(); break; } // progress indicator switch(newState) { case Printer.PRINTING_STATE: - progressbar.show(); + progressbar.show(); break; default: progressbar.hide(); break; } - + if(newState == Printer.WIFIBOX_DISCONNECTED_STATE) { message.set("Lost connection to WiFi box",Message.ERROR); } else if(prevState == Printer.WIFIBOX_DISCONNECTED_STATE) { message.set("Connected to WiFi box",Message.INFO,true); } else if(newState == Printer.DISCONNECTED_STATE) { message.set("Printer disconnected",Message.WARNING,true); - } else if(prevState == Printer.DISCONNECTED_STATE && newState == Printer.IDLE_STATE || + } else if(prevState == Printer.DISCONNECTED_STATE && newState == Printer.IDLE_STATE || prevState == Printer.UNKNOWN_STATE && newState == Printer.IDLE_STATE) { message.set("Printer connected",Message.INFO,true); } - + state = newState; hasControl = newHasControl; } diff --git a/www/js/canvasDrawing.js b/www/js/canvasDrawing.js index 27350e3..649f004 100644 --- a/www/js/canvasDrawing.js +++ b/www/js/canvasDrawing.js @@ -300,6 +300,49 @@ function onCanvasMouseDown(e) { draw(x, y, 0.5); } +function saveToSvg() { + var lastX = 0, lastY = 0, lastIsMove; + var data = ''; //TODO: change data to an array which is collapsed after the loop? + var svg = ''; + + var boundsWidth = doodleBounds[3] - doodleBounds[1]; + var boundsHeight = doodleBounds[2] - doodleBounds[0]; + + svg += '\n'; + svg += '\n'; + svg += '\n'; + svg += '\tDoodle 3D sketch\n'; + + for (var i = 0; i < _points.length; ++i) { + var x = _points[i][0], y = _points[i][1], isMove = _points[i][2]; + var dx = x - lastX, dy = y - lastY; + + if (i == 0) + data += 'M'; //emit absolute move on first pair of coordinates + else if (isMove != lastIsMove) + data += isMove ? 'm' : 'l'; + + data += dx + ',' + dy + ' '; + + lastX = x; + lastY = y; + lastIsMove = isMove; + } + + svg += '\t\n'; + + var field = 'height', value = numLayers; + svg += '\t\n'; + field = 'outlineShape', value = VERTICALSHAPE; + svg += '\t\n'; + field = 'twist', value = rStep; + svg += '\t\n'; + + svg += '\n'; + + return svg; +} + var prevPoint = {x:-1, y:-1}; function onCanvasMouseMove(e) { // console.log("f:onCanvasMouseMove()");