From 06528f6d0f9d95499c3096143f569bdc9bd09f98 Mon Sep 17 00:00:00 2001 From: casperlamboo Date: Mon, 15 Jun 2015 10:21:05 +0200 Subject: [PATCH] cleaner code --- src/box.js | 18 ++--- src/gcode.js | 23 +++--- src/slicer.js | 71 +++++++++++-------- src/slicerworker.js | 8 +++ .../editor_slicer/js/Sidebar.Slicer.js | 43 ++++++----- 5 files changed, 87 insertions(+), 76 deletions(-) diff --git a/src/box.js b/src/box.js index 1dbe677..cbc450e 100644 --- a/src/box.js +++ b/src/box.js @@ -40,6 +40,7 @@ D3D.Box.prototype.init = function () { if (error) { if (scope.alive) { scope.alive = false; + if (scope.ondisconnect !== undefined) { scope.ondisconnect(); } @@ -64,7 +65,7 @@ D3D.Box.prototype.init = function () { scope.init(); } - scope.updateConfig(data); + scope.config = data; if (!scope.loaded) { scope.loaded = true; @@ -79,15 +80,6 @@ D3D.Box.prototype.init = function () { return this; }; -D3D.Box.prototype.updateConfig = function (config) { - "use strict"; - - for (var i in config) { - this.config[i] = config[i]; - } - - return this; -}; D3D.Box.prototype.updateLoop = function () { "use strict"; var scope = this; @@ -96,7 +88,7 @@ D3D.Box.prototype.updateLoop = function () { //Bij error wordt gelijk zelfde data opnieuw gestuurd //Als DoodleBox ontkoppeld wordt komt er een error in de loop waardoor pagina breekt en ververst moet worden - if (this.printBatches.length > 0 && (this.status["buffered_lines"] + this.batchSize) <= this.maxBufferedLines) { + if (this.printBatches.length > 0 && (this.status["buffered_lines"] + this.batches[0].length) <= this.maxBufferedLines) { //if (this.printBatches.length > 0 ) { this.printBatch(); } @@ -167,11 +159,10 @@ D3D.Box.prototype.printBatch = function () { console.log("batch sent: " + scope.currentBatch, data); if (scope.printBatches.length > 0) { - //sent new batch scope.currentBatch ++; } else { - //finish sending + console.log("Finish sending model to printer"); } scope.updateState(); @@ -200,6 +191,7 @@ D3D.Box.prototype.stopPrint = function (printer) { return this; }; + //COMMUNICATION SHELL //see http://www.doodle3d.com/help/api-documentation D3D.Box.prototype.getConfig = function (keys, callback) { diff --git a/src/gcode.js b/src/gcode.js index 5dba303..a36fa19 100644 --- a/src/gcode.js +++ b/src/gcode.js @@ -25,21 +25,22 @@ D3D.GCode.prototype.addGCode = function (command) { "use strict"; var str = []; + var first = true; for (var i in command) { - if (i === "G") { - str.push(i + command[i]); + if (first) { + str = i + command[i]; + + first = false; } else if (this.current[i] !== command[i]) { - str.push(i + command[i]); + str += " " + i + command[i]; + this.current[i] = command[i]; } } - str = str.join(" "); - if (str.length > 0) { - this.gcode += str + "\n"; - } + this.gcode += str + "\n"; }; D3D.GCode.prototype.setSettings = function (printer) { "use strict"; @@ -146,9 +147,9 @@ D3D.GCode.prototype.unRetract = function () { "F": speed.toFixed(3) }); } - - return this; } + + return this; }; D3D.GCode.prototype.retract = function () { "use strict"; @@ -170,9 +171,9 @@ D3D.GCode.prototype.retract = function () { "F": speed.toFixed(3) }); } - - return this; } + + return this; }; D3D.GCode.prototype.getGCode = function () { "use strict"; diff --git a/src/slicer.js b/src/slicer.js index e6d00bf..1d1ddf4 100644 --- a/src/slicer.js +++ b/src/slicer.js @@ -134,6 +134,7 @@ D3D.Slicer.prototype.slice = function (layerHeight, height) { for (var layer = 1; layer < layersIntersections.length; layer ++) { var layerIntersections = layersIntersections[layer]; + //why have a slice with only support? if (layerIntersections.length > 0) { var y = layer * layerHeight; @@ -300,10 +301,12 @@ D3D.Slicer.prototype.slicesToData = function (slices, printer) { console.log("generating infills"); for (var layer = 0; layer < slices.length; layer ++) { var slice = slices[layer]; - - var downSkin = (layer - bottomSkinCount >= 0) ? slices[layer - bottomSkinCount].getOutline() : new D3D.Paths([], true); - var upSkin = (layer + topSkinCount < slices.length) ? slices[layer + topSkinCount].getOutline() : new D3D.Paths([], true); - var surroundingLayer = (downSkin.length === 0 || upSkin.length === 0) ? new D3D.Paths([], true) : upSkin.intersect(downSkin); + + if (layer - bottomSkinCount >= 0 && layer + topSkinCount < slices.length) { + var downSkin = slices[layer - bottomSkinCount].getOutline(); + var upSkin = slices[layer + topSkinCount].getOutline(); + var surroundingLayer = upSkin.intersect(downSkin); + } for (var i = 0; i < slice.parts.length; i ++) { var part = slice.parts[i]; @@ -313,7 +316,7 @@ D3D.Slicer.prototype.slicesToData = function (slices, printer) { var inset = ((part.innerLines.length > 0) ? part.innerLines[part.innerLines.length - 1] : outerLine); var fillArea = inset.offset(-nozzleRadius); - var highFillArea = fillArea.difference(surroundingLayer); + var highFillArea = (surroundingLayer !== undefined) ? fillArea.difference(surroundingLayer) : fillArea; var lowFillArea = fillArea.difference(highFillArea); var fill = new D3D.Paths([], false); @@ -326,6 +329,7 @@ D3D.Slicer.prototype.slicesToData = function (slices, printer) { var bounds = highFillArea.bounds(); var even = (layer % 2 === 0); var highFillTemplate = this.getFillTemplate(bounds, nozzleDiameter, even, !even); + part.fill.join(highFillTemplate.intersect(highFillArea)); } } @@ -338,13 +342,6 @@ D3D.Slicer.prototype.slicesToData = function (slices, printer) { if (useSupport) { console.log("generating support"); - var supportTemplate = this.getFillTemplate({ - left: this.geometry.boundingBox.min.z * scale, - top: this.geometry.boundingBox.min.x * scale, - right: this.geometry.boundingBox.max.z * scale, - bottom: this.geometry.boundingBox.max.x * scale - }, supportGridSize, true, true); - var supportAreas = new D3D.Paths([], true); for (var layer = slices.length - 1 - supportDistanceLayers; layer >= 0; layer --) { @@ -367,6 +364,8 @@ D3D.Slicer.prototype.slicesToData = function (slices, printer) { currentSlice.support = template.intersect(supportAreas); } else { + var supportTemplate = this.getFillTemplate(supportAreas.bounds(), supportGridSize, true, true); + currentSlice.support = supportTemplate.intersect(supportAreas).join(supportAreas.clone()); } } @@ -382,11 +381,6 @@ D3D.Slicer.prototype.slicesToData = function (slices, printer) { var overhang = outerLine.difference(overlap); if (overlap.length === 0 || overhang.length > 0) { - //var supportArea = outerLine.difference(supportSkin.intersect(outerLine)); - //supportAreas = supportAreas.union(supportArea); - - //supportAreas = supportAreas.union(overhang); - supportAreas = supportAreas.union(overhang.offset(supportAcceptanceMargin).intersect(outerLine)); } } @@ -433,17 +427,28 @@ D3D.Slicer.prototype.getFillTemplate = function (bounds, size, even, uneven) { var paths = new D3D.Paths([], false); if (even) { - for (var length = Math.floor(bounds.left/size)*size; length <= Math.ceil(bounds.right/size)*size; length += size) { - paths.push([{X: length, Y: bounds.top}, {X: length, Y: bounds.bottom}]); + var left = Math.floor(bounds.left/size)*size; + var right = Math.ceil(bounds.right/size)*size; + + for (var length = left; length <= right; length += size) { + paths.push([ + {X: length, Y: bounds.top}, + {X: length, Y: bounds.bottom} + ]); } } if (uneven) { - for (var length = Math.floor(bounds.top/size)*size; length <= Math.floor(bounds.bottom/size)*size; length += size) { - paths.push([{X: bounds.left, Y: length}, {X: bounds.right, Y: length}]); + var top = Math.floor(bounds.top/size)*size; + var bottom = Math.floor(bounds.bottom/size)*size; + + for (var length = top; length <= bottom; length += size) { + paths.push([ + {X: bounds.left, Y: length}, + {X: bounds.right, Y: length} + ]); } } - //return paths; return paths; }; D3D.Slicer.prototype.dataToGCode = function (data, printer) { @@ -451,7 +456,7 @@ D3D.Slicer.prototype.dataToGCode = function (data, printer) { var gcode = new D3D.GCode().setSettings(printer); - function sliceToGCode (path, retract, unRetract, type) { + function pathToGCode (path, retract, unRetract, type) { for (var i = 0; i < path.length; i ++) { var shape = path[i]; @@ -488,24 +493,24 @@ D3D.Slicer.prototype.dataToGCode = function (data, printer) { } if (slice.brim !== undefined) { - sliceToGCode(slice.brim, true, true, "brim"); + pathToGCode(slice.brim, true, true, "brim"); } for (var i = 0; i < slice.parts.length; i ++) { var part = slice.parts[i]; - sliceToGCode(part.outerLine, false, true, "outerLine"); + pathToGCode(part.outerLine, false, true, "outerLine"); for (var j = 0; j < part.innerLines.length; j ++) { var innerLine = part.innerLines[j]; - sliceToGCode(innerLine, false, false, "innerLine"); + pathToGCode(innerLine, false, false, "innerLine"); } - sliceToGCode(part.fill, true, false, "fill"); + pathToGCode(part.fill, true, false, "fill"); } if (slice.support !== undefined) { - sliceToGCode(slice.support, true, true, "support"); + pathToGCode(slice.support, true, true, "support"); } this.progress.gcodeLayer = layer; @@ -525,7 +530,7 @@ D3D.Slicer.prototype.getGCode = function (printer) { this.progress.dataLayer = 0; this.progress.gcodeLayer = 0; - var start = new Date().getTime(); + /*var start = new Date().getTime(); var slices = this.slice(layerHeight, dimensionsZ); var end = new Date().getTime(); console.log("Slicing: " + (end - start) + "ms"); @@ -538,7 +543,13 @@ D3D.Slicer.prototype.getGCode = function (printer) { start = new Date().getTime(); var gcode = this.dataToGCode(slices, printer); end = new Date().getTime(); - console.log("Gcode: " + (end - start) + "ms"); + console.log("Gcode: " + (end - start) + "ms");*/ + + var slices = this.slice(layerHeight, dimensionsZ); + + this.slicesToData(slices, printer); + + var gcode = this.dataToGCode(slices, printer); return gcode; }; \ No newline at end of file diff --git a/src/slicerworker.js b/src/slicerworker.js index 78efc1c..81e633b 100644 --- a/src/slicerworker.js +++ b/src/slicerworker.js @@ -40,6 +40,8 @@ D3D.SlicerWorker.prototype.setSettings = function (USER_SETTINGS, PRINTER_SETTIN 'USER_SETTINGS': USER_SETTINGS, 'PRINTER_SETTINGS': PRINTER_SETTINGS }); + + return this; }; D3D.SlicerWorker.prototype.setMesh = function (mesh) { 'use strict'; @@ -68,6 +70,8 @@ D3D.SlicerWorker.prototype.setMesh = function (mesh) { }, 'matrix': mesh.matrix.toArray() }, buffers); + + return this; }; D3D.SlicerWorker.prototype.slice = function () { 'use strict'; @@ -75,6 +79,8 @@ D3D.SlicerWorker.prototype.slice = function () { this.worker.postMessage({ 'cmd': 'SLICE' }); + + return this; }; D3D.SlicerWorker.prototype.close = function () { 'use strict'; @@ -82,4 +88,6 @@ D3D.SlicerWorker.prototype.close = function () { this.worker.postMessage({ 'cmd': 'CLOSE' }); + + return this; }; \ No newline at end of file diff --git a/three.js-master/editor_slicer/js/Sidebar.Slicer.js b/three.js-master/editor_slicer/js/Sidebar.Slicer.js index 5291fb5..ba1c395 100644 --- a/three.js-master/editor_slicer/js/Sidebar.Slicer.js +++ b/three.js-master/editor_slicer/js/Sidebar.Slicer.js @@ -1,18 +1,37 @@ Sidebar.Slicer = function ( editor ) { - var USER_SETTINGS, PRINTER_SETTINGS, selectedPrinter, printer; + var USER_SETTINGS, PRINTER_SETTINGS, selectedPrinter; function settingsLoaded () { + printer.updateConfig(USER_SETTINGS); + var options = {}; for (var i in PRINTER_SETTINGS) { options[i] = i; } + printerType.setOptions(options); } + var container = new UI.CollapsiblePanel(); + container.setCollapsed( editor.config.getKey( 'ui/sidebar/slicer/collapsed' ) ); + container.onCollapsedChange( function ( boolean ) { + + editor.config.setKey( 'ui/sidebar/slicer/collapsed', boolean ); + + } ); + + var printer = new D3D.Printer(); + + var localIp = location.hash.substring(1); + var doodleBox = new D3D.Box(localIp).init(); + + container.addStatic( new UI.Text( 'SLICER' ) ); + container.add( new UI.Break() ); + function createRow (name) { var row = new UI.Panel(); @@ -28,22 +47,6 @@ Sidebar.Slicer = function ( editor ) { } - var localIp = location.hash.substring(1); - var doodleBox = new D3D.Box(localIp).init(); - - var signals = editor.signals; - - var container = new UI.CollapsiblePanel(); - container.setCollapsed( editor.config.getKey( 'ui/sidebar/slicer/collapsed' ) ); - container.onCollapsedChange( function ( boolean ) { - - editor.config.setKey( 'ui/sidebar/slicer/collapsed', boolean ); - - } ); - - container.addStatic( new UI.Text( 'SLICER' ) ); - container.add( new UI.Break() ); - var state = createRow('State'); var bedTemperature = createRow('Bed Temperature'); var bedTargetTemperature = createRow('Bed Target Temperature'); @@ -66,8 +69,6 @@ Sidebar.Slicer = function ( editor ) { printBatches.setValue(doodleBox.printBatches.length); }; - var ignoreObjectSelectedSignal = false; - var printerTypeRow = new UI.Panel(); var printerType = new UI.Select().setWidth( '150px' ); printerType.onChange( function () { @@ -75,7 +76,7 @@ Sidebar.Slicer = function ( editor ) { var type = printerType.getValue(); selectedPrinter = type; - printer = new D3D.Printer().updateConfig(USER_SETTINGS).updateConfig(PRINTER_SETTINGS[selectedPrinter]); + printer.updateConfig(PRINTER_SETTINGS[selectedPrinter]); } ); @@ -84,9 +85,7 @@ Sidebar.Slicer = function ( editor ) { container.add( printerTypeRow ); - var progress = createRow("Progress"); - var slice = new UI.Button( 'Slice' ); slice.onClick( function () {