Doodle3D-Slicer/build/d3d.js

1319 lines
32 KiB
JavaScript
Raw Normal View History

2015-05-01 14:09:45 +02:00
/******************************************************
*
* Utils
2015-05-07 11:04:48 +02:00
* requires jQuery, Three.js
2015-05-01 14:09:45 +02:00
*
******************************************************/
var D3D = {
2015-05-07 14:09:36 +02:00
"version": "0.1",
"website": "http://www.doodle3d.com/",
2015-05-01 14:09:45 +02:00
"contact": "develop@doodle3d.com"
};
function sendAPI (url, data, callback) {
"use strict";
$.ajax({
2015-05-07 14:09:36 +02:00
url: url,
type: "POST",
data: data,
dataType: "json",
timeout: 10000,
2015-05-01 14:09:45 +02:00
success: function (response) {
if (response.status === "success") {
if (callback !== undefined) {
callback(response.data);
}
}
else {
console.warn(response.msg);
}
}
}).fail(function () {
console.warn("Failed connecting to " + url);
2015-05-01 14:09:45 +02:00
sendAPI(url, data, callback);
});
}
function getAPI (url, callback) {
"use strict";
$.ajax({
2015-05-07 14:09:36 +02:00
url: url,
dataType: "json",
timeout: 5000,
2015-05-01 14:09:45 +02:00
success: function (response) {
if (response.status === "success") {
if (callback !== undefined) {
callback(response.data);
}
}
else {
console.warn(response.msg);
}
}
}).fail(function () {
console.warn("Failed connecting to " + url);
2015-05-01 14:09:45 +02:00
getAPI(url, callback);
});
}
2015-05-19 19:58:13 +02:00
function loadSettings (url, callback) {
"use strict";
$.ajax({
url: url,
dataType: "json",
success: function (response) {
if (callback !== undefined) {
callback(response);
}
}
});
}
2015-05-01 14:09:45 +02:00
function downloadFile (file, data) {
"use strict";
2015-05-19 19:58:13 +02:00
var blob = new Blob([data], {type:'text/plain'});
var button = document.createElement("a");
button.download = file;
button.href = window.URL.createObjectURL(blob);
button.click();
2015-05-01 14:09:45 +02:00
}
Array.prototype.clone = function () {
"use strict";
var array = [];
for (var i = 0; i < this.length; i ++) {
array[i] = this[i];
}
return array;
2015-05-19 19:58:13 +02:00
}
2015-05-01 14:09:45 +02:00
/******************************************************
*
* Box
* Representation of de Doodle3DBox
* Handles all communication with the doodle box
* JavaScript shell for api communication
* Check http://www.doodle3d.com/help/api-documentation
2015-05-01 14:09:45 +02:00
*
******************************************************/
//TODO
//Als meerdere clients met box zouden verbinden zal de api te veel requests krijgen waardoor hij crasht
//implimentatie van het veranderen van onder andere naam, netwerkverbinding etc
D3D.Box = function (localIp) {
"use strict";
var self = this;
this.batchSize = 512;
this.maxBufferedLines = 4096;
this.localIp = localIp;
this.api = "http://" + localIp + "/d3dapi/";
this.config = {};
2015-05-19 19:58:13 +02:00
this.status = {};
2015-05-01 14:09:45 +02:00
this.printBatches = [];
this.currentBatch = 0;
this.loaded = false;
2015-05-07 14:09:36 +02:00
this.getConfigAll(function (data) {
self.updateConfig(data);
2015-05-01 14:09:45 +02:00
self.update();
self.loaded = true;
if (self.onload !== undefined) {
self.onload();
}
});
};
2015-05-07 14:09:36 +02:00
D3D.Box.prototype.updateConfig = function (config) {
"use strict";
for (var i in config) {
2015-05-19 19:58:13 +02:00
this.config[i] = config[i];
2015-05-07 14:09:36 +02:00
}
return this;
};
2015-05-01 14:09:45 +02:00
D3D.Box.prototype.update = function () {
"use strict";
//TODO
//Code is zo op gezet dat maar api call te gelijk is
//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
2015-05-19 19:58:13 +02:00
if (this.printBatches.length > 0 && (this.status["buffered_lines"] + this.batchSize) <= this.maxBufferedLines) {
//if (this.printBatches.length > 0 ) {
2015-05-01 14:09:45 +02:00
this.printBatch();
}
else {
this.updateState();
}
};
D3D.Box.prototype.updateState = function () {
2015-05-07 14:09:36 +02:00
//que api calls so they don't overload the d3d box
2015-05-01 14:09:45 +02:00
"use strict";
var self = this;
2015-05-07 14:09:36 +02:00
this.getInfoStatus(function (data) {
2015-05-19 19:58:13 +02:00
self.status = data;
2015-05-01 14:09:45 +02:00
2015-05-07 14:09:36 +02:00
if (self.onupdate !== undefined) {
self.onupdate(data);
}
2015-05-01 14:09:45 +02:00
self.update();
});
};
D3D.Box.prototype.print = function (gcode) {
"use strict";
this.currentBatch = 0;
//clone gcode to remove array links
gcode = gcode.clone();
//gcode split in batches
while (gcode.length > 0) {
var gcodeBatch = gcode.splice(0, Math.min(this.batchSize, gcode.length));
this.printBatches.push(gcodeBatch);
}
2015-05-06 15:06:04 +02:00
return this;
2015-05-01 14:09:45 +02:00
};
D3D.Box.prototype.printBatch = function () {
"use strict";
var self = this;
var gcode = this.printBatches.shift();
2015-05-07 14:09:36 +02:00
this.setPrinterPrint({
"start": ((this.currentBatch === 0) ? true : false),
"first": ((this.currentBatch === 0) ? true : false),
2015-05-01 14:09:45 +02:00
"gcode": gcode.join("\n")
}, function (data) {
console.log("batch sent: " + self.currentBatch, data);
2015-05-01 14:09:45 +02:00
if (self.printBatches.length > 0) {
//sent new batch
self.currentBatch ++;
}
else {
2015-05-07 14:09:36 +02:00
//finish sending
2015-05-01 14:09:45 +02:00
}
self.updateState();
});
};
D3D.Box.prototype.stopPrint = function () {
2015-05-01 14:09:45 +02:00
"use strict";
this.printBatches = [];
this.currentBatch = 0;
var finishMove = [
"M107 ;fan off",
"G91 ;relative positioning",
"G1 E-1 F300 ;retract the filament a bit before lifting the nozzle, to release some of the pressure",
"G1 Z+0.5 E-5 X-20 Y-20 F9000 ;move Z up a bit and retract filament even more",
"G28 X0 Y0 ;move X/Y to min endstops, so the head is out of the way",
"M84 ;disable axes / steppers",
"G90 ;absolute positioning",
"M104 S180",
";M140 S70",
"M117 Done ;display message (20 characters to clear whole screen)"
];
2015-05-07 14:09:36 +02:00
this.setPrinterStop({
//"gcode": {}
2015-05-01 14:09:45 +02:00
"gcode": finishMove.join("\n")
}, function (data) {
console.log("Printer stop command sent");
});
2015-05-06 15:06:04 +02:00
return this;
};
2015-05-07 14:09:36 +02:00
//COMMUNICATION SHELL
//see http://www.doodle3d.com/help/api-documentation
D3D.Box.prototype.getConfig = function (keys, callback) {
"use strict";
getAPI(this.api + "config/?" + keys.join("=&") + "=", callback);
return this;
2015-05-07 14:09:36 +02:00
};
D3D.Box.prototype.getConfigAll = function (callback) {
"use strict";
getAPI(this.api + "config/all", callback);
return this;
2015-05-07 14:09:36 +02:00
};
D3D.Box.prototype.setConfig = function (data, callback) {
2015-05-06 15:06:04 +02:00
"use strict";
2015-05-07 14:09:36 +02:00
var self = this;
2015-05-06 15:06:04 +02:00
2015-05-07 14:09:36 +02:00
sendAPI(this.api + "config", data, function (response) {
for (var i in response.validation) {
2015-05-19 19:58:13 +02:00
if (response.validation[i] === "ok") {
self[i] = data[i];
2015-05-07 14:09:36 +02:00
}
}
if (callback !== undefined) {
callback(response);
}
});
2015-05-06 15:06:04 +02:00
return this;
};
D3D.Box.prototype.getInfo = function (callback) {
2015-05-06 15:06:04 +02:00
"use strict";
getAPI(this.api + "info", callback);
return this;
};
2015-05-07 14:09:36 +02:00
D3D.Box.prototype.getInfoStatus = function (callback) {
"use strict";
getAPI(this.api + "info/status", callback);
return this;
};
D3D.Box.prototype.downloadInfoLogFiles = function () {
//works in google chrome... not tested in other browsers
//some browsers may redirect using this code
"use strict";
2015-05-06 15:06:04 +02:00
window.location = this.api + "info/logfiles";
2015-05-06 15:06:04 +02:00
};
D3D.Box.prototype.getInfoAcces = function (callback) {
"use strict";
getAPI(this.api + "info/access", callback);
2015-05-06 15:06:04 +02:00
return this;
};
D3D.Box.prototype.getNetworkScan = function (callback) {
2015-05-06 15:06:04 +02:00
"use strict";
getAPI(this.api + "network/scan", callback);
2015-05-06 15:06:04 +02:00
return this;
};
D3D.Box.prototype.getNetworkKnown = function (callback) {
"use strict";
getAPI(this.api + "network/known", callback);
2015-05-06 15:06:04 +02:00
return this;
};
D3D.Box.prototype.getNetworkStatus = function (callback) {
"use strict";
getAPI(this.api + "network/status", callback);
2015-05-06 15:06:04 +02:00
return this;
};
D3D.Box.prototype.setNetworkAssosiate = function (data, callback) {
2015-05-06 15:06:04 +02:00
"use strict";
sendAPI(this.api + "network/associate", data, callback);
2015-05-06 15:06:04 +02:00
return this;
};
D3D.Box.prototype.setNetworkDisassosiate = function (callback) {
//not tested
2015-05-06 15:06:04 +02:00
"use strict";
sendAPI(this.api + "network/disassociate", {}, callback);
2015-05-06 15:06:04 +02:00
return this;
};
D3D.Box.prototype.setNetworkOpenAP = function (callback) {
//not tested
2015-05-06 15:06:04 +02:00
"use strict";
sendAPI(this.api + "network/openap", {}, callback);
2015-05-06 15:06:04 +02:00
return this;
};
D3D.Box.prototype.setNetworkRemove = function (ssid, callback) {
2015-05-06 15:06:04 +02:00
"use strict";
sendAPI(this.api + "network/remove", {
"ssid": ssid
}, callback);
2015-05-06 15:06:04 +02:00
return this;
};
2015-05-07 14:09:36 +02:00
D3D.Box.prototype.getNetworkSignin = function (callback) {
"use strict";
getAPI(this.api + "network/signin", callback);
return this;
};
2015-05-06 15:06:04 +02:00
D3D.Box.prototype.getNetworkAlive = function (callback) {
"use strict";
getAPI(this.api + "network/alive", callback);
2015-05-06 15:06:04 +02:00
return this;
};
2015-05-07 14:09:36 +02:00
D3D.Box.prototype.getPrinterTemperature = function (callback) {
"use strict";
getAPI(this.api + "printer/temperature", callback);
return this;
};
D3D.Box.prototype.getPrinterProgress = function (callback) {
"use strict";
getAPI(this.api + "printer/progress", callback);
return this;
};
D3D.Box.prototype.getPrinterState = function (callback) {
"use strict";
getAPI(this.api + "printer/state", callback);
return this;
};
2015-05-06 15:06:04 +02:00
D3D.Box.prototype.getPrinterListAll = function (callback) {
"use strict";
getAPI(this.api + "printer/listall", callback);
2015-05-06 15:06:04 +02:00
return this;
};
D3D.Box.prototype.setPrinterHeatup = function (callback) {
2015-05-06 15:06:04 +02:00
"use strict";
sendAPI(this.api + "printer/heatup", {}, callback);
2015-05-06 15:06:04 +02:00
return this;
};
2015-05-07 14:09:36 +02:00
D3D.Box.prototype.setPrinterPrint = function (data, callback) {
2015-05-06 15:06:04 +02:00
"use strict";
2015-05-07 14:09:36 +02:00
sendAPI(this.api + "printer/print", data, callback);
return this;
};
D3D.Box.prototype.setPrinterStop = function (data, callback) {
"use strict";
sendAPI(this.api + "printer/stop", data, callback);
return this;
};
2015-05-07 11:04:48 +02:00
D3D.Box.prototype.getSketch = function (id, callback) {
2015-05-07 14:09:36 +02:00
"use strict";
getAPI(this.api + "sketch/?id=" + id, callback);
return this;
};
D3D.Box.prototype.setSketch = function (data, callback) {
2015-05-07 11:04:48 +02:00
"use strict";
2015-05-07 14:09:36 +02:00
sendAPI(this.api + "sketch", {
"data": data
}, callback);
2015-05-07 11:04:48 +02:00
return this;
};
D3D.Box.prototype.getSketchStatus = function (callback) {
"use strict";
getAPI(this.api + "sketch/status", callback);
return this;
};
2015-05-07 14:09:36 +02:00
D3D.Box.prototype.setSketchClear = function (callback) {
"use strict";
2015-05-07 14:09:36 +02:00
sendAPI(this.api + "sketch/clear", callback);
return this;
};
2015-05-07 14:09:36 +02:00
D3D.Box.prototype.getSystemVersions = function (callback) {
2015-05-07 11:04:48 +02:00
"use strict";
2015-05-07 14:09:36 +02:00
getAPI(this.api + "system/fwversions", callback);
2015-05-07 11:04:48 +02:00
return this;
};
2015-05-07 14:09:36 +02:00
D3D.Box.prototype.getUpdateStatus = function (callback) {
2015-05-07 11:04:48 +02:00
"use strict";
2015-05-07 14:09:36 +02:00
getAPI(this.api + "update/status", callback);
2015-05-07 11:04:48 +02:00
return this;
};
D3D.Box.prototype.setUpdateDownload = function (callback) {
//not tested
"use strict";
sendAPI(this.api + "update/download", {}, callback);
return this;
};
D3D.Box.prototype.setUpdateInstall = function (callback) {
//not tested
"use strict";
sendAPI(this.api + "update/install", {}, callback);
return this;
};
D3D.Box.prototype.setUpdateClear = function (callback) {
//not tested
"use strict";
sendAPI(this.api + "update/clear", {}, callback);
2015-05-06 15:06:04 +02:00
return this;
2015-05-01 14:09:45 +02:00
};
/******************************************************
*
* Printer
* Representation of the connected printer
*
******************************************************/
2015-05-19 19:58:13 +02:00
D3D.Printer = function (printerSettings, userSettings) {
2015-05-01 14:09:45 +02:00
"use strict";
this.config = {};
2015-05-19 19:58:13 +02:00
this.updateConfig(printerSettings);
this.updateConfig(userSettings);
2015-05-07 14:09:36 +02:00
};
D3D.Printer.prototype.updateConfig = function (config) {
"use strict";
2015-05-01 14:09:45 +02:00
for (var i in config) {
if (i.indexOf("printer") === 0) {
this.config[i] = config[i];
}
}
2015-05-07 14:09:36 +02:00
return this;
2015-05-01 14:09:45 +02:00
};
D3D.Printer.prototype.getStartCode = function () {
"use strict";
var gcode = this.config["printer.startcode"];
gcode = this.subsituteVariables(gcode);
return gcode.split("\n");
};
D3D.Printer.prototype.getEndCode = function () {
"use strict";
var gcode = this.config["printer.endcode"];
gcode = this.subsituteVariables(gcode);
return gcode.split("\n");
};
D3D.Printer.prototype.subsituteVariables = function (gcode) {
"use strict";
var temperature = this.config["printer.temperature"];
var bedTemperature = this.config["printer.bed.temperature"];
var preheatTemperature = this.config["printer.heatup.temperature"];
var preheatBedTemperature = this.config["printer.heatup.bed.temperature"];
var printerType = this.config["printer.type"];
var heatedbed = this.config["printer.heatedbed"];
switch (printerType) {
case "makerbot_replicator2": printerType = "r2"; break;
case "makerbot_replicator2x": printerType = "r2x"; break;
case "makerbot_thingomatic": printerType = "t6"; break;
case "makerbot_generic": printerType = "r2"; break;
case "_3Dison_plus": printerType = "r2"; break;
}
var heatedBedReplacement = heatedbed ? "" : ";";
gcode = gcode.replace(/{printingTemp}/gi, temperature);
gcode = gcode.replace(/{printingBedTemp}/gi, bedTemperature);
gcode = gcode.replace(/{preheatTemp}/gi, preheatTemperature);
gcode = gcode.replace(/{preheatBedTemp}/gi, preheatBedTemperature);
gcode = gcode.replace(/{printerType}/gi, printerType);
gcode = gcode.replace(/{if heatedBed}/gi, heatedBedReplacement);
return gcode;
};
/******************************************************
*
2015-05-12 11:29:01 +02:00
* Path
*
* Abstraction layer for annoying clipper js
*
******************************************************/
2015-05-13 17:37:52 +02:00
D3D.Paths = function (paths, closed) {
2015-05-12 11:29:01 +02:00
"use strict";
2015-05-13 17:37:52 +02:00
Array.call(this);
this.setPaths(paths || []);
2015-05-12 11:29:01 +02:00
this.closed = (closed !== undefined) ? closed : true;
};
2015-05-13 17:37:52 +02:00
D3D.Paths.prototype = Object.create(Array.prototype);
D3D.Paths.prototype.setPaths = function (paths) {
2015-05-12 11:29:01 +02:00
"use strict";
2015-05-13 17:37:52 +02:00
for (var i = 0; i < paths.length; i ++) {
var path = paths[i];
if (path.length > 0) {
this.push(path);
}
2015-05-13 17:37:52 +02:00
}
2015-05-12 11:29:01 +02:00
return this;
};
2015-05-13 17:37:52 +02:00
D3D.Paths.prototype.clip = function (path, type) {
2015-05-12 11:29:01 +02:00
"use strict";
var solution = new ClipperLib.Paths();
var clipper = new ClipperLib.Clipper();
2015-05-13 17:37:52 +02:00
clipper.AddPaths(this, ClipperLib.PolyType.ptSubject, this.closed);
clipper.AddPaths(path, ClipperLib.PolyType.ptClip, path.closed);
clipper.Execute(type, solution);
2015-05-12 11:29:01 +02:00
return new D3D.Paths(solution, this.closed);
2015-05-12 11:29:01 +02:00
};
2015-05-13 17:37:52 +02:00
D3D.Paths.prototype.union = function (path) {
2015-05-12 11:29:01 +02:00
"use strict";
2015-05-13 17:37:52 +02:00
return this.clip(path, ClipperLib.ClipType.ctUnion);
2015-05-12 11:29:01 +02:00
};
2015-05-13 17:37:52 +02:00
D3D.Paths.prototype.difference = function (path) {
2015-05-12 11:29:01 +02:00
"use strict";
2015-05-13 17:37:52 +02:00
return this.clip(path, ClipperLib.ClipType.ctDifference);
2015-05-12 11:29:01 +02:00
};
2015-05-13 17:37:52 +02:00
D3D.Paths.prototype.intersect = function (path) {
2015-05-12 11:29:01 +02:00
"use strict";
2015-05-13 17:37:52 +02:00
return this.clip(path, ClipperLib.ClipType.ctIntersection);
};
D3D.Paths.prototype.xor = function () {
"use strict";
2015-05-12 11:29:01 +02:00
2015-05-13 17:37:52 +02:00
return this.clip(path, ClipperLib.ClipType.ctXor);
2015-05-12 11:29:01 +02:00
};
2015-05-13 17:37:52 +02:00
D3D.Paths.prototype.offset = function (offset) {
2015-05-12 11:29:01 +02:00
"use strict";
var solution = new ClipperLib.Paths();
var co = new ClipperLib.ClipperOffset(1, 1);
2015-05-13 17:37:52 +02:00
co.AddPaths(this, ClipperLib.JoinType.jtRound, ClipperLib.EndType.etClosedPolygon);
2015-05-12 11:29:01 +02:00
co.Execute(solution, offset);
2015-05-13 17:37:52 +02:00
return new D3D.Paths(solution);
2015-05-12 11:29:01 +02:00
};
2015-05-13 17:37:52 +02:00
D3D.Paths.prototype.scaleUp = function (factor) {
2015-05-12 11:29:01 +02:00
"use strict";
2015-05-13 17:37:52 +02:00
var path = ClipperLib.JS.ScaleUpPaths(this, factor);
2015-05-12 11:29:01 +02:00
return this;
};
2015-05-13 17:37:52 +02:00
D3D.Paths.prototype.scaleDown = function (factor) {
2015-05-12 11:29:01 +02:00
"use strict";
2015-05-13 17:37:52 +02:00
var path = ClipperLib.JS.ScaleDownPaths(this, factor);
2015-05-12 11:29:01 +02:00
return this;
};
2015-05-15 15:07:47 +02:00
D3D.Paths.prototype.lastPoint = function () {
"use strict";
var lastPath = this[this.length - 1];
var lastPoint = this.closed ? lastPath[0] : lastPath[lastPath.length - 1];
return new THREE.Vector2(lastPoint.X, lastPoint.Y);
};
2015-05-15 11:14:44 +02:00
D3D.Paths.prototype.optimizePath = function (start) {
"use strict";
2015-05-15 15:07:47 +02:00
var optimizedPaths = new D3D.Paths([], this.closed);
var donePaths = [];
2015-05-15 11:14:44 +02:00
while (optimizedPaths.length !== this.length) {
2015-05-15 15:07:47 +02:00
var minLength = false;
2015-05-15 11:14:44 +02:00
var reverse;
var minPath;
2015-05-15 15:07:47 +02:00
var offset;
var pathIndex;
2015-05-15 11:14:44 +02:00
for (var i = 0; i < this.length; i ++) {
var path = this[i];
2015-05-15 15:07:47 +02:00
if (donePaths.indexOf(i) === -1) {
if (this.closed) {
for (var j = 0; j < path.length; j ++) {
var point = new THREE.Vector2(path[j].X, path[j].Y);
var length = point.sub(start).length();
if (minLength === false || length < minLength) {
minPath = path;
minLength = length;
offset = j;
pathIndex = i;
}
}
2015-05-15 11:14:44 +02:00
}
2015-05-15 15:07:47 +02:00
else {
var startPoint = new THREE.Vector2(path[0].X, path[0].Y);
var length = startPoint.sub(start).length();
if (minLength === false || length < minLength) {
minPath = path;
minLength = length;
reverse = false;
pathIndex = i;
}
var endPoint = new THREE.Vector2(path[path.length - 1].X, path[path.length - 1].Y);
var length = endPoint.sub(start).length();
if (length < minLength) {
minPath = path;
minLength = length;
reverse = true;
pathIndex = i;
}
2015-05-15 11:14:44 +02:00
}
}
}
2015-05-15 15:07:47 +02:00
if (this.closed) {
minPath = minPath.concat(minPath.splice(0, offset));
var point = minPath[0];
}
else {
if (reverse) {
minPath.reverse();
}
var point = minPath[minPath.length - 1];
2015-05-15 11:14:44 +02:00
}
2015-05-15 15:07:47 +02:00
donePaths.push(pathIndex);
2015-05-15 11:14:44 +02:00
start = new THREE.Vector2(point.X, point.Y);
optimizedPaths.push(minPath);
}
return optimizedPaths;
};
2015-05-13 17:37:52 +02:00
D3D.Paths.prototype.tresholdArea = function (minArea) {
2015-05-15 11:14:44 +02:00
//code not tested yet
2015-05-12 11:29:01 +02:00
"use strict";
2015-05-13 17:37:52 +02:00
for (var i = 0; i < this.length; i ++) {
var shape = this[i];
2015-05-12 11:29:01 +02:00
var area = ClipperLib.Clipper.Area(shape);
if (area < minArea) {
2015-05-13 17:37:52 +02:00
this.splice(i, 1);
2015-05-12 11:29:01 +02:00
i --;
}
}
return areas;
};
2015-05-13 17:37:52 +02:00
D3D.Paths.prototype.join = function (path) {
"use strict";
2015-05-12 11:29:01 +02:00
2015-05-13 17:37:52 +02:00
for (var i = 0; i < path.length; i ++) {
this.push(path[i]);
2015-05-12 11:29:01 +02:00
}
2015-05-13 17:37:52 +02:00
return this;
2015-05-12 11:29:01 +02:00
};
2015-05-13 17:37:52 +02:00
D3D.Paths.prototype.clone = function () {
2015-05-12 11:29:01 +02:00
"use strict";
2015-05-13 17:37:52 +02:00
return new D3D.Paths(ClipperLib.JS.Clone(this), this.closed);
};
D3D.Paths.prototype.bounds = function () {
"use strict";
2015-05-12 11:29:01 +02:00
2015-05-13 17:37:52 +02:00
return ClipperLib.Clipper.GetBounds(this);
};
D3D.Paths.prototype.draw = function (context, color) {
2015-05-12 11:29:01 +02:00
"use strict";
context.strokeStyle = color;
2015-05-13 17:37:52 +02:00
for (var i = 0; i < this.length; i ++) {
var shape = this[i];
2015-05-12 11:29:01 +02:00
//var point = shape[0];
//context.fillText(i, point.X*2, point.Y*2);
2015-05-15 11:14:44 +02:00
2015-05-12 11:29:01 +02:00
context.beginPath();
var length = this.closed ? (shape.length + 1) : shape.length;
for (var j = 0; j < length; j ++) {
var point = shape[j % shape.length];
context.lineTo(point.X*2, point.Y*2);
}
context.stroke();
}
};
/******************************************************
*
2015-05-01 14:09:45 +02:00
* Slicer
*
* TODO (optimalisatie)
* sorteer lijnen op laagste hoogte -> stop loop wanneer hij een lijn zonder intersectie heeft gevonden
* verwijder lijnen die ooit interactie gehad hebben, maar nu niet meer
* helft van lijnen toevoegen omdat 4face altijd recht is, en 3face dus te veel data bevat
*
* omliggende lagen -> difference && sum omliggende lijnen
* voor laag 5 = 5 diff (3 && 4 && 6 && 7))
*
******************************************************/
D3D.Slicer = function () {
"use strict";
};
D3D.Slicer.prototype.setMesh = function (mesh) {
2015-05-01 14:09:45 +02:00
"use strict";
//convert buffergeometry to geometry;
var geometry = mesh.geometry.clone();
if (geometry instanceof THREE.BufferGeometry) {
geometry = new THREE.Geometry().fromBufferGeometry(geometry);
}
//remove duplicate vertices;
for (var i = 0; i < geometry.vertices.length; i ++) {
var vertexA = geometry.vertices[i];
for (var j = i + 1; j < geometry.vertices.length; j ++) {
var vertexB = geometry.vertices[j];
if (vertexA.equals(vertexB)) {
geometry.vertices[j] = vertexA;
}
}
}
2015-05-13 17:37:52 +02:00
geometry.mergeVertices();
//apply mesh matrix on geometry;
mesh.updateMatrix();
2015-05-13 17:37:52 +02:00
geometry.applyMatrix(mesh.matrix);
geometry.computeFaceNormals();
2015-05-19 19:58:13 +02:00
geometry.computeBoundingBox();
this.geometry = geometry;
2015-05-01 14:09:45 +02:00
//get unique lines from geometry;
2015-05-01 14:09:45 +02:00
this.createLines();
return this;
};
2015-05-06 15:06:04 +02:00
D3D.Slicer.prototype.createLines = function () {
2015-05-01 14:09:45 +02:00
"use strict";
2015-05-06 15:06:04 +02:00
this.lines = [];
var lineLookup = {};
2015-05-01 14:09:45 +02:00
2015-05-06 15:06:04 +02:00
var self = this;
function addLine (a, b) {
2015-05-01 14:09:45 +02:00
2015-05-06 15:06:04 +02:00
//think lookup can only be b_a, a_b is only possible when face is flipped
2015-05-19 19:58:13 +02:00
var index = lineLookup[b + "_" + a] || lineLookup[a + "_" + b];
2015-05-01 14:09:45 +02:00
2015-05-06 15:06:04 +02:00
if (index === undefined) {
index = self.lines.length;
lineLookup[a + "_" + b] = index;
2015-05-01 14:09:45 +02:00
2015-05-06 15:06:04 +02:00
self.lines.push({
2015-05-07 14:09:36 +02:00
line: new THREE.Line3(self.geometry.vertices[a], self.geometry.vertices[b]),
connects: [],
2015-05-13 17:37:52 +02:00
normals: [],
ignore: 0
2015-05-06 15:06:04 +02:00
});
}
return index;
2015-05-07 11:04:48 +02:00
}
2015-05-01 14:09:45 +02:00
for (var i = 0; i < this.geometry.faces.length; i ++) {
var face = this.geometry.faces[i];
2015-05-13 17:37:52 +02:00
2015-05-19 19:58:13 +02:00
if (face.normal.y !== 1 && face.normal.y !== -1) {
var normal = new THREE.Vector2().set(face.normal.z, face.normal.x).normalize();
2015-05-13 17:37:52 +02:00
//check for only adding unique lines
//returns index of said line
var a = addLine(face.a, face.b);
var b = addLine(face.b, face.c);
var c = addLine(face.c, face.a);
//set connecting lines (based on face)
this.lines[a].connects.push(b, c);
this.lines[b].connects.push(c, a);
this.lines[c].connects.push(a, b);
this.lines[a].normals.push(normal);
this.lines[b].normals.push(normal);
this.lines[c].normals.push(normal);
2015-05-19 19:58:13 +02:00
}
2015-05-01 14:09:45 +02:00
}
};
D3D.Slicer.prototype.slice = function (height, step) {
"use strict";
var layersIntersections = [];
2015-05-01 14:09:45 +02:00
for (var i = 0; i < this.lines.length; i ++) {
var line = this.lines[i];
2015-05-01 14:09:45 +02:00
var min = Math.ceil(Math.min(line.line.start.y, line.line.end.y) / step);
var max = Math.floor(Math.max(line.line.start.y, line.line.end.y) / step);
2015-05-01 14:09:45 +02:00
for (var layerIndex = min; layerIndex <= max; layerIndex ++) {
if (layerIndex >= 0) {
if (layersIntersections[layerIndex] === undefined) {
layersIntersections[layerIndex] = [];
}
layersIntersections[layerIndex].push(i);
}
}
}
2015-05-01 14:09:45 +02:00
var slices = [];
2015-05-01 14:09:45 +02:00
2015-05-19 19:58:13 +02:00
//still error in first layer, so remove first layer & last layer
//see https://github.com/Doodle3D/Doodle3D-Slicer/issues/1
2015-05-13 17:37:52 +02:00
for (var layer = 1; layer < layersIntersections.length-1; layer ++) {
2015-05-19 19:58:13 +02:00
//for (var layer = 0; layer < layersIntersections.length; layer ++) {
var layerIntersections = layersIntersections[layer];
var y = layer*step;
2015-05-01 14:09:45 +02:00
var intersections = [];
for (var i = 0; i < layerIntersections.length; i ++) {
var index = layerIntersections[i];
var line = this.lines[index].line;
2015-05-01 14:09:45 +02:00
var alpha = (y - line.start.y) / (line.end.y - line.start.y);
2015-05-12 11:29:01 +02:00
var x = line.end.x * alpha + line.start.x * (1 - alpha);
var z = line.end.z * alpha + line.start.z * (1 - alpha);
2015-05-01 14:09:45 +02:00
2015-05-19 19:58:13 +02:00
intersections[index] = new THREE.Vector2(z, x);
2015-05-01 14:09:45 +02:00
}
var done = [];
2015-05-19 19:58:13 +02:00
var slice = [];
for (var i = 0; i < layerIntersections.length; i ++) {
var index = layerIntersections[i];
2015-05-01 14:09:45 +02:00
if (done.indexOf(index) === -1) {
2015-05-01 14:09:45 +02:00
var shape = [];
while (index !== -1) {
var intersection = intersections[index];
shape.push({X: intersection.x, Y: intersection.y});
done.push(index);
var connects = this.lines[index].connects;
var faceNormals = this.lines[index].normals;
for (var j = 0; j < connects.length; j ++) {
index = connects[j];
if (intersections[index] && done.indexOf(index) === -1) {
2015-05-19 19:58:13 +02:00
var a = new THREE.Vector2(intersection.x, intersection.y);
2015-05-01 14:09:45 +02:00
var b = intersections[index];
var normal = a.sub(b).normal().normalize();
var faceNormal = faceNormals[Math.floor(j/2)];
if (normal.dot(faceNormal) > 0) {
break;
}
else {
index = -1;
}
}
else {
index = -1;
}
}
}
/*
for (var i = 0; i < shape.length; i ++) {
var point = shape[i];
var previousPoint = shape[(i + shape.length - 1) % shape.length];
var nextPoint = shape[(i + 1) % shape.length];
var point = new THREE.Vector2(point.X, point.Y);
var previousPoint = new THREE.Vector2(previousPoint.X, previousPoint.Y);
var nextPoint = new THREE.Vector2(nextPoint.X, nextPoint.Y);
//var lineLength = nextPoint.sub(previousPoint).length();
var normal = nextPoint.sub(previousPoint).normal().normalize();
var distance = Math.abs(normal.dot(point.sub(previousPoint)));
//something better for offset check
if (distance <= 0.01) {
shape.splice(i, 1);
i --;
}
}
*/
2015-05-01 14:09:45 +02:00
//think this check is not nescesary, always higher as 0
if (shape.length > 0) {
2015-05-13 17:37:52 +02:00
slice.push(new D3D.Paths([shape]));
2015-05-01 14:09:45 +02:00
}
}
}
2015-05-13 17:37:52 +02:00
var layerParts = [];
for (var i = 0; i < slice.length; i ++) {
var layerPart1 = slice[i];
var merge = false;
for (var j = 0; j < layerParts.length; j ++) {
var layerPart2 = layerParts[j];
if (layerPart2.intersect(layerPart1).length > 0) {
layerPart2.join(layerPart1);
merge = true;
break;
}
}
if (!merge) {
layerParts.push(layerPart1);
}
}
2015-05-01 14:09:45 +02:00
//stop when ther are no intersects
2015-05-13 17:37:52 +02:00
if (layerParts.length > 0) {
slices.push(layerParts);
2015-05-01 14:09:45 +02:00
}
else {
break;
}
}
return slices;
};
D3D.Slicer.prototype.slicesToData = function (slices, printer) {
"use strict";
var scale = 100;
var layerHeight = printer.config["printer.layerHeight"] * scale;
var dimensionsZ = printer.config["printer.dimensions.z"] * scale;
2015-05-19 19:58:13 +02:00
var wallThickness = printer.config["printer.wallThickness"] * scale / 2;
2015-05-01 14:09:45 +02:00
var shellThickness = printer.config["printer.shellThickness"] * scale;
var fillSize = printer.config["printer.fillSize"] * scale;
var brimOffset = printer.config["printer.brimOffset"] * scale;
2015-05-19 19:58:13 +02:00
var bottomThickness = printer.config["printer.bottomThickness"] * scale;
var topThickness = printer.config["printer.topThickness"] * scale;
var bottomSkinCount = Math.ceil(bottomThickness/layerHeight);
var topSkinCount = Math.ceil(topThickness/layerHeight);
2015-05-01 14:09:45 +02:00
2015-05-15 15:07:47 +02:00
var start = new THREE.Vector2(0, 0);
2015-05-01 14:09:45 +02:00
var data = [];
2015-05-15 11:14:44 +02:00
var lowFillTemplate = this.getFillTemplate({
2015-05-19 19:58:13 +02:00
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
2015-05-15 11:14:44 +02:00
}, fillSize, true, true);
2015-05-01 14:09:45 +02:00
for (var layer = 0; layer < slices.length; layer ++) {
var slice = slices[layer];
2015-05-13 17:37:52 +02:00
var layerData = [];
data.push(layerData);
2015-05-15 11:14:44 +02:00
var downSkin = new D3D.Paths([], true);
2015-05-19 19:58:13 +02:00
if (layer - bottomSkinCount >= 0) {
var downLayer = slices[layer - bottomSkinCount];
2015-05-13 17:37:52 +02:00
for (var i = 0; i < downLayer.length; i ++) {
downSkin.join(downLayer[i]);
}
}
2015-05-15 11:14:44 +02:00
var upSkin = new D3D.Paths([], true);
2015-05-19 19:58:13 +02:00
if (layer + topSkinCount < slices.length) {
var downLayer = slices[layer + topSkinCount];
2015-05-13 17:37:52 +02:00
for (var i = 0; i < downLayer.length; i ++) {
upSkin.join(downLayer[i]);
}
}
2015-05-19 19:58:13 +02:00
var surroundingLayer = upSkin.intersect(downSkin).scaleUp(scale);
2015-05-13 17:37:52 +02:00
var sliceData = [];
2015-05-01 14:09:45 +02:00
2015-05-13 17:37:52 +02:00
for (var i = 0; i < slice.length; i ++) {
var part = slice[i];
2015-05-01 14:09:45 +02:00
2015-05-19 19:58:13 +02:00
//var outerLayer = part.clone();
var outerLayer = part.clone().scaleUp(scale).offset(-wallThickness/2);
2015-05-01 14:09:45 +02:00
2015-05-19 19:58:13 +02:00
if (outerLayer.length > 0) {
var insets = new D3D.Paths([], true);
for (var offset = wallThickness; offset <= shellThickness; offset += wallThickness) {
var inset = outerLayer.offset(-offset);
2015-05-01 14:09:45 +02:00
2015-05-19 19:58:13 +02:00
insets.join(inset);
}
2015-05-01 14:09:45 +02:00
2015-05-19 19:58:13 +02:00
var fillArea = (inset || outerLayer).offset(-wallThickness/2);
//var fillArea = (inset || outerLayer).clone();
2015-05-01 14:09:45 +02:00
2015-05-19 19:58:13 +02:00
var highFillArea = fillArea.difference(surroundingLayer);
2015-05-13 17:37:52 +02:00
2015-05-19 19:58:13 +02:00
var lowFillArea = fillArea.difference(highFillArea);
2015-05-13 17:37:52 +02:00
2015-05-19 19:58:13 +02:00
var fill = new D3D.Paths([], false);
2015-05-13 17:37:52 +02:00
2015-05-19 19:58:13 +02:00
if (lowFillTemplate.length > 0) {
fill.join(lowFillTemplate.intersect(lowFillArea));
}
2015-05-01 14:09:45 +02:00
2015-05-19 19:58:13 +02:00
if (highFillArea.length > 0) {
var bounds = highFillArea.bounds();
var even = (layer % 2 === 0);
var highFillTemplate = this.getFillTemplate(bounds, wallThickness, even, !even);
fill.join(highFillTemplate.intersect(highFillArea));
}
outerLayer = outerLayer.optimizePath(start);
if (insets.length > 0) {
insets = insets.optimizePath(outerLayer.lastPoint());
fill = fill.optimizePath(insets.lastPoint());
}
else {
fill = fill.optimizePath(outerLayer.lastPoint());
}
if (fill.length > 0) {
start = fill.lastPoint();
}
else if (insets.length > 0) {
start = insets.lastPoint();
}
else {
start = outerLayer.lastPoint();
}
layerData.push({
outerLayer: outerLayer.scaleDown(scale),
fill: fill.scaleDown(scale),
insets: insets.scaleDown(scale)
});
}
2015-05-13 17:37:52 +02:00
}
2015-05-01 14:09:45 +02:00
}
return data;
};
2015-05-15 11:14:44 +02:00
D3D.Slicer.prototype.getFillTemplate = function (bounds, size, even, uneven) {
2015-05-12 11:29:01 +02:00
"use strict";
2015-05-13 17:37:52 +02:00
var paths = new D3D.Paths([], false);
2015-05-12 11:29:01 +02:00
if (even) {
2015-05-19 19:58:13 +02:00
for (var length = Math.floor(bounds.left/size)*size; length <= Math.ceil(bounds.right/size)*size; length += size) {
2015-05-15 11:14:44 +02:00
paths.push([{X: length, Y: bounds.top}, {X: length, Y: bounds.bottom}]);
2015-05-12 11:29:01 +02:00
}
}
if (uneven) {
2015-05-19 19:58:13 +02:00
for (var length = Math.floor(bounds.top/size)*size; length <= Math.floor(bounds.bottom/size)*size; length += size) {
2015-05-15 11:14:44 +02:00
paths.push([{X: bounds.left, Y: length}, {X: bounds.right, Y: length}]);
2015-05-12 11:29:01 +02:00
}
}
//return paths;
2015-05-13 17:37:52 +02:00
return paths;
2015-05-12 11:29:01 +02:00
};
2015-05-01 14:09:45 +02:00
D3D.Slicer.prototype.dataToGcode = function (data, printer) {
"use strict";
var layerHeight = printer.config["printer.layerHeight"];
var normalSpeed = printer.config["printer.speed"];
var bottomSpeed = printer.config["printer.bottomLayerSpeed"];
var firstLayerSlow = printer.config["printer.firstLayerSlow"];
var bottomFlowRate = printer.config["printer.bottomFlowRate"];
var travelSpeed = printer.config["printer.travelSpeed"];
var filamentThickness = printer.config["printer.filamentThickness"];
var wallThickness = printer.config["printer.wallThickness"];
var enableTraveling = printer.config["printer.enableTraveling"];
var retractionEnabled = printer.config["printer.retraction.enabled"];
var retractionSpeed = printer.config["printer.retraction.speed"];
var retractionMinDistance = printer.config["printer.retraction.minDistance"];
var retractionAmount = printer.config["printer.retraction.amount"];
2015-05-06 15:06:04 +02:00
2015-05-01 14:09:45 +02:00
function sliceToGcode (slice) {
var gcode = [];
2015-05-13 17:37:52 +02:00
for (var i = 0; i < slice.length; i ++) {
var shape = slice[i];
2015-05-01 14:09:45 +02:00
var previousPoint;
2015-05-13 17:37:52 +02:00
var length = slice.closed ? (shape.length + 1) : shape.length;
for (var j = 0; j < length; j ++) {
var point = shape[j % shape.length];
2015-05-01 14:09:45 +02:00
if (j === 0) {
//TODO
//add retraction
if (extruder > retractionMinDistance && retractionEnabled) {
gcode.push([
"G0",
2015-05-07 14:09:36 +02:00
"E" + (extruder - retractionAmount).toFixed(3),
2015-05-01 14:09:45 +02:00
"F" + (retractionSpeed * 60).toFixed(3)
].join(" "));
}
gcode.push([
"G0",
"X" + point.X.toFixed(3) + " Y" + point.Y.toFixed(3) + " Z" + z,
2015-05-15 15:07:47 +02:00
"F" + (travelSpeed * 60)
2015-05-01 14:09:45 +02:00
].join(" "));
if (extruder > retractionMinDistance && retractionEnabled) {
gcode.push([
"G0",
2015-05-07 14:09:36 +02:00
"E" + extruder.toFixed(3),
2015-05-01 14:09:45 +02:00
"F" + (retractionSpeed * 60).toFixed(3)
].join(" "));
}
}
else {
2015-05-19 19:58:13 +02:00
var a = new THREE.Vector2(point.X, point.Y);
var b = new THREE.Vector2(previousPoint.X, previousPoint.Y);
2015-05-01 14:09:45 +02:00
var lineLength = a.distanceTo(b);
extruder += lineLength * wallThickness * layerHeight / filamentSurfaceArea * flowRate;
gcode.push([
"G1",
"X" + point.X.toFixed(3) + " Y" + point.Y.toFixed(3) + " Z" + z,
"F" + speed,
"E" + extruder.toFixed(3)
].join(" "));
}
previousPoint = point;
}
}
return gcode;
}
var gcode = printer.getStartCode();
var extruder = 0.0;
var speed = firstLayerSlow ? (bottomSpeed*60).toFixed(3) : (normalSpeed*60).toFixed(3);
var filamentSurfaceArea = Math.pow((filamentThickness/2), 2) * Math.PI;
var flowRate = bottomFlowRate;
for (var layer = 0; layer < data.length; layer ++) {
var slice = data[layer];
//turn on fan on layer 2
if (layer === 2) {
gcode.push("M106");
speed = (normalSpeed*60).toFixed(3);
flowRate = 1;
}
var z = ((layer + 1) * layerHeight).toFixed(3);
2015-05-13 17:37:52 +02:00
for (var i = 0; i < slice.length; i ++) {
var layerPart = slice[i];
gcode = gcode.concat(sliceToGcode(layerPart.outerLayer));
gcode = gcode.concat(sliceToGcode(layerPart.insets));
gcode = gcode.concat(sliceToGcode(layerPart.fill));
}
2015-05-01 14:09:45 +02:00
}
gcode = gcode.concat(printer.getEndCode());
2015-05-19 19:58:13 +02:00
2015-05-01 14:09:45 +02:00
return gcode;
};
2015-05-12 11:29:01 +02:00
//only for debug purposes
2015-05-01 14:09:45 +02:00
D3D.Slicer.prototype.drawPaths = function (printer, min, max) {
"use strict";
var layerHeight = printer.config["printer.layerHeight"];
var dimensionsZ = printer.config["printer.dimensions.z"];
var slices = this.slice(dimensionsZ, layerHeight);
var data = this.slicesToData(slices, printer);
var canvas = document.createElement("canvas");
canvas.width = 400;
canvas.height = 400;
var context = canvas.getContext("2d");
for (var layer = min; layer < max; layer ++) {
var slice = data[layer % data.length];
2015-05-13 17:37:52 +02:00
for (var i = 0; i < slice.length; i ++) {
var layerPart = slice[i];
layerPart.insets.draw(context, "blue");
layerPart.outerLayer.draw(context, "green");
layerPart.fill.draw(context, "red");
}
2015-05-01 14:09:45 +02:00
}
return canvas;
};
D3D.Slicer.prototype.getGcode = function (printer) {
"use strict";
var layerHeight = printer.config["printer.layerHeight"];
var dimensionsZ = printer.config["printer.dimensions.z"];
var start = new Date().getTime();
2015-05-01 14:09:45 +02:00
var slices = this.slice(dimensionsZ, layerHeight);
var end = new Date().getTime();
console.log("Slicing: " + (end - start) + "ms");
var start = new Date().getTime();
2015-05-01 14:09:45 +02:00
var data = this.slicesToData(slices, printer);
var end = new Date().getTime();
console.log("Data: " + (end - start) + "ms");
var start = new Date().getTime();
2015-05-01 14:09:45 +02:00
var gcode = this.dataToGcode(data, printer);
var end = new Date().getTime();
console.log("Gcode: " + (end - start) + "ms");
2015-05-01 14:09:45 +02:00
return gcode;
};