cleaner code

This commit is contained in:
casperlamboo 2015-06-15 10:21:05 +02:00 committed by Simon Voordouw
parent 2ffb6eb848
commit bdb98fbfff
5 changed files with 87 additions and 76 deletions

View File

@ -40,6 +40,7 @@ D3D.Box.prototype.init = function () {
if (error) { if (error) {
if (scope.alive) { if (scope.alive) {
scope.alive = false; scope.alive = false;
if (scope.ondisconnect !== undefined) { if (scope.ondisconnect !== undefined) {
scope.ondisconnect(); scope.ondisconnect();
} }
@ -64,7 +65,7 @@ D3D.Box.prototype.init = function () {
scope.init(); scope.init();
} }
scope.updateConfig(data); scope.config = data;
if (!scope.loaded) { if (!scope.loaded) {
scope.loaded = true; scope.loaded = true;
@ -79,15 +80,6 @@ D3D.Box.prototype.init = function () {
return this; 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 () { D3D.Box.prototype.updateLoop = function () {
"use strict"; "use strict";
var scope = this; var scope = this;
@ -96,7 +88,7 @@ D3D.Box.prototype.updateLoop = function () {
//Bij error wordt gelijk zelfde data opnieuw gestuurd //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 //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 ) { //if (this.printBatches.length > 0 ) {
this.printBatch(); this.printBatch();
} }
@ -167,11 +159,10 @@ D3D.Box.prototype.printBatch = function () {
console.log("batch sent: " + scope.currentBatch, data); console.log("batch sent: " + scope.currentBatch, data);
if (scope.printBatches.length > 0) { if (scope.printBatches.length > 0) {
//sent new batch
scope.currentBatch ++; scope.currentBatch ++;
} }
else { else {
//finish sending console.log("Finish sending model to printer");
} }
scope.updateState(); scope.updateState();
@ -200,6 +191,7 @@ D3D.Box.prototype.stopPrint = function (printer) {
return this; return this;
}; };
//COMMUNICATION SHELL //COMMUNICATION SHELL
//see http://www.doodle3d.com/help/api-documentation //see http://www.doodle3d.com/help/api-documentation
D3D.Box.prototype.getConfig = function (keys, callback) { D3D.Box.prototype.getConfig = function (keys, callback) {

View File

@ -25,21 +25,22 @@ D3D.GCode.prototype.addGCode = function (command) {
"use strict"; "use strict";
var str = []; var str = [];
var first = true;
for (var i in command) { for (var i in command) {
if (i === "G") { if (first) {
str.push(i + command[i]); str = i + command[i];
first = false;
} }
else if (this.current[i] !== command[i]) { else if (this.current[i] !== command[i]) {
str.push(i + command[i]); str += " " + i + command[i];
this.current[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) { D3D.GCode.prototype.setSettings = function (printer) {
"use strict"; "use strict";
@ -146,9 +147,9 @@ D3D.GCode.prototype.unRetract = function () {
"F": speed.toFixed(3) "F": speed.toFixed(3)
}); });
} }
}
return this; return this;
}
}; };
D3D.GCode.prototype.retract = function () { D3D.GCode.prototype.retract = function () {
"use strict"; "use strict";
@ -170,9 +171,9 @@ D3D.GCode.prototype.retract = function () {
"F": speed.toFixed(3) "F": speed.toFixed(3)
}); });
} }
}
return this; return this;
}
}; };
D3D.GCode.prototype.getGCode = function () { D3D.GCode.prototype.getGCode = function () {
"use strict"; "use strict";

View File

@ -134,6 +134,7 @@ D3D.Slicer.prototype.slice = function (layerHeight, height) {
for (var layer = 1; layer < layersIntersections.length; layer ++) { for (var layer = 1; layer < layersIntersections.length; layer ++) {
var layerIntersections = layersIntersections[layer]; var layerIntersections = layersIntersections[layer];
//why have a slice with only support?
if (layerIntersections.length > 0) { if (layerIntersections.length > 0) {
var y = layer * layerHeight; var y = layer * layerHeight;
@ -301,9 +302,11 @@ D3D.Slicer.prototype.slicesToData = function (slices, printer) {
for (var layer = 0; layer < slices.length; layer ++) { for (var layer = 0; layer < slices.length; layer ++) {
var slice = slices[layer]; var slice = slices[layer];
var downSkin = (layer - bottomSkinCount >= 0) ? slices[layer - bottomSkinCount].getOutline() : new D3D.Paths([], true); if (layer - bottomSkinCount >= 0 && layer + topSkinCount < slices.length) {
var upSkin = (layer + topSkinCount < slices.length) ? slices[layer + topSkinCount].getOutline() : new D3D.Paths([], true); var downSkin = slices[layer - bottomSkinCount].getOutline();
var surroundingLayer = (downSkin.length === 0 || upSkin.length === 0) ? new D3D.Paths([], true) : upSkin.intersect(downSkin); var upSkin = slices[layer + topSkinCount].getOutline();
var surroundingLayer = upSkin.intersect(downSkin);
}
for (var i = 0; i < slice.parts.length; i ++) { for (var i = 0; i < slice.parts.length; i ++) {
var part = slice.parts[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 inset = ((part.innerLines.length > 0) ? part.innerLines[part.innerLines.length - 1] : outerLine);
var fillArea = inset.offset(-nozzleRadius); var fillArea = inset.offset(-nozzleRadius);
var highFillArea = fillArea.difference(surroundingLayer); var highFillArea = (surroundingLayer !== undefined) ? fillArea.difference(surroundingLayer) : fillArea;
var lowFillArea = fillArea.difference(highFillArea); var lowFillArea = fillArea.difference(highFillArea);
var fill = new D3D.Paths([], false); var fill = new D3D.Paths([], false);
@ -326,6 +329,7 @@ D3D.Slicer.prototype.slicesToData = function (slices, printer) {
var bounds = highFillArea.bounds(); var bounds = highFillArea.bounds();
var even = (layer % 2 === 0); var even = (layer % 2 === 0);
var highFillTemplate = this.getFillTemplate(bounds, nozzleDiameter, even, !even); var highFillTemplate = this.getFillTemplate(bounds, nozzleDiameter, even, !even);
part.fill.join(highFillTemplate.intersect(highFillArea)); part.fill.join(highFillTemplate.intersect(highFillArea));
} }
} }
@ -338,13 +342,6 @@ D3D.Slicer.prototype.slicesToData = function (slices, printer) {
if (useSupport) { if (useSupport) {
console.log("generating support"); 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); var supportAreas = new D3D.Paths([], true);
for (var layer = slices.length - 1 - supportDistanceLayers; layer >= 0; layer --) { 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); currentSlice.support = template.intersect(supportAreas);
} }
else { else {
var supportTemplate = this.getFillTemplate(supportAreas.bounds(), supportGridSize, true, true);
currentSlice.support = supportTemplate.intersect(supportAreas).join(supportAreas.clone()); currentSlice.support = supportTemplate.intersect(supportAreas).join(supportAreas.clone());
} }
} }
@ -382,11 +381,6 @@ D3D.Slicer.prototype.slicesToData = function (slices, printer) {
var overhang = outerLine.difference(overlap); var overhang = outerLine.difference(overlap);
if (overlap.length === 0 || overhang.length > 0) { 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)); 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); var paths = new D3D.Paths([], false);
if (even) { if (even) {
for (var length = Math.floor(bounds.left/size)*size; length <= Math.ceil(bounds.right/size)*size; length += size) { var left = Math.floor(bounds.left/size)*size;
paths.push([{X: length, Y: bounds.top}, {X: length, Y: bounds.bottom}]); 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) { if (uneven) {
for (var length = Math.floor(bounds.top/size)*size; length <= Math.floor(bounds.bottom/size)*size; length += size) { var top = Math.floor(bounds.top/size)*size;
paths.push([{X: bounds.left, Y: length}, {X: bounds.right, Y: length}]); 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; return paths;
}; };
D3D.Slicer.prototype.dataToGCode = function (data, printer) { 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); 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 ++) { for (var i = 0; i < path.length; i ++) {
var shape = path[i]; var shape = path[i];
@ -488,24 +493,24 @@ D3D.Slicer.prototype.dataToGCode = function (data, printer) {
} }
if (slice.brim !== undefined) { 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 ++) { for (var i = 0; i < slice.parts.length; i ++) {
var part = slice.parts[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 ++) { for (var j = 0; j < part.innerLines.length; j ++) {
var innerLine = part.innerLines[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) { if (slice.support !== undefined) {
sliceToGCode(slice.support, true, true, "support"); pathToGCode(slice.support, true, true, "support");
} }
this.progress.gcodeLayer = layer; this.progress.gcodeLayer = layer;
@ -525,7 +530,7 @@ D3D.Slicer.prototype.getGCode = function (printer) {
this.progress.dataLayer = 0; this.progress.dataLayer = 0;
this.progress.gcodeLayer = 0; this.progress.gcodeLayer = 0;
var start = new Date().getTime(); /*var start = new Date().getTime();
var slices = this.slice(layerHeight, dimensionsZ); var slices = this.slice(layerHeight, dimensionsZ);
var end = new Date().getTime(); var end = new Date().getTime();
console.log("Slicing: " + (end - start) + "ms"); console.log("Slicing: " + (end - start) + "ms");
@ -538,7 +543,13 @@ D3D.Slicer.prototype.getGCode = function (printer) {
start = new Date().getTime(); start = new Date().getTime();
var gcode = this.dataToGCode(slices, printer); var gcode = this.dataToGCode(slices, printer);
end = new Date().getTime(); 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; return gcode;
}; };

View File

@ -40,6 +40,8 @@ D3D.SlicerWorker.prototype.setSettings = function (USER_SETTINGS, PRINTER_SETTIN
'USER_SETTINGS': USER_SETTINGS, 'USER_SETTINGS': USER_SETTINGS,
'PRINTER_SETTINGS': PRINTER_SETTINGS 'PRINTER_SETTINGS': PRINTER_SETTINGS
}); });
return this;
}; };
D3D.SlicerWorker.prototype.setMesh = function (mesh) { D3D.SlicerWorker.prototype.setMesh = function (mesh) {
'use strict'; 'use strict';
@ -68,6 +70,8 @@ D3D.SlicerWorker.prototype.setMesh = function (mesh) {
}, },
'matrix': mesh.matrix.toArray() 'matrix': mesh.matrix.toArray()
}, buffers); }, buffers);
return this;
}; };
D3D.SlicerWorker.prototype.slice = function () { D3D.SlicerWorker.prototype.slice = function () {
'use strict'; 'use strict';
@ -75,6 +79,8 @@ D3D.SlicerWorker.prototype.slice = function () {
this.worker.postMessage({ this.worker.postMessage({
'cmd': 'SLICE' 'cmd': 'SLICE'
}); });
return this;
}; };
D3D.SlicerWorker.prototype.close = function () { D3D.SlicerWorker.prototype.close = function () {
'use strict'; 'use strict';
@ -82,4 +88,6 @@ D3D.SlicerWorker.prototype.close = function () {
this.worker.postMessage({ this.worker.postMessage({
'cmd': 'CLOSE' 'cmd': 'CLOSE'
}); });
return this;
}; };

View File

@ -1,18 +1,37 @@
Sidebar.Slicer = function ( editor ) { Sidebar.Slicer = function ( editor ) {
var USER_SETTINGS, PRINTER_SETTINGS, selectedPrinter, printer; var USER_SETTINGS, PRINTER_SETTINGS, selectedPrinter;
function settingsLoaded () { function settingsLoaded () {
printer.updateConfig(USER_SETTINGS);
var options = {}; var options = {};
for (var i in PRINTER_SETTINGS) { for (var i in PRINTER_SETTINGS) {
options[i] = i; options[i] = i;
} }
printerType.setOptions(options); 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) { function createRow (name) {
var row = new UI.Panel(); 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 state = createRow('State');
var bedTemperature = createRow('Bed Temperature'); var bedTemperature = createRow('Bed Temperature');
var bedTargetTemperature = createRow('Bed Target Temperature'); var bedTargetTemperature = createRow('Bed Target Temperature');
@ -66,8 +69,6 @@ Sidebar.Slicer = function ( editor ) {
printBatches.setValue(doodleBox.printBatches.length); printBatches.setValue(doodleBox.printBatches.length);
}; };
var ignoreObjectSelectedSignal = false;
var printerTypeRow = new UI.Panel(); var printerTypeRow = new UI.Panel();
var printerType = new UI.Select().setWidth( '150px' ); var printerType = new UI.Select().setWidth( '150px' );
printerType.onChange( function () { printerType.onChange( function () {
@ -75,7 +76,7 @@ Sidebar.Slicer = function ( editor ) {
var type = printerType.getValue(); var type = printerType.getValue();
selectedPrinter = type; selectedPrinter = type;
printer = new D3D.Printer().updateConfig(USER_SETTINGS).updateConfig(PRINTER_SETTINGS[selectedPrinter]); printer.updateConfig(PRINTER_SETTINGS[selectedPrinter]);
} ); } );
@ -84,10 +85,8 @@ Sidebar.Slicer = function ( editor ) {
container.add( printerTypeRow ); container.add( printerTypeRow );
var progress = createRow("Progress"); var progress = createRow("Progress");
var slice = new UI.Button( 'Slice' ); var slice = new UI.Button( 'Slice' );
slice.onClick( function () { slice.onClick( function () {