0
0
mirror of https://github.com/Doodle3D/doodle3d-client.git synced 2024-11-29 11:54:55 +01:00
doodle3d-client/js/gcodeGenerating.js

307 lines
11 KiB
JavaScript
Raw Normal View History

2013-12-20 16:31:41 +01:00
/*
* This file is part of the Doodle3D project (http://doodle3d.com).
*
* Copyright (c) 2013, Doodle3D
* This software is licensed under the terms of the GNU GPL v2 or later.
* See file LICENSE.txt or visit http://www.gnu.org/licenses/gpl.html for full license details.
*/
var MAX_POINTS_TO_PRINT = 200000
2013-07-26 22:39:28 +02:00
var gcode = [];
function generate_gcode() {
2013-07-26 22:39:28 +02:00
console.log("f:generategcode()");
gcode = [];
console.log("settings: ",settings);
var speed = settings["printer.speed"];
var normalSpeed = speed;
var bottomSpeed = settings["printer.bottomLayerSpeed"];
var firstLayerSlow = settings["printer.firstLayerSlow"];
var bottomFlowRate = settings["printer.bottomFlowRate"];
var travelSpeed = settings["printer.travelSpeed"]
var filamentThickness = settings["printer.filamentThickness"];
var wallThickness = settings["printer.wallThickness"];
var screenToMillimeterScale = settings["printer.screenToMillimeterScale"];
var layerHeight = settings["printer.layerHeight"];
var temperature = settings["printer.temperature"];
var bedTemperature = settings["printer.bed.temperature"];
var useSubLayers = settings["printer.useSubLayers"];
var enableTraveling = settings["printer.enableTraveling"];
var retractionEnabled = settings["printer.retraction.enabled"];
var retractionspeed = settings["printer.retraction.speed"];
var retractionminDistance = settings["printer.retraction.minDistance"];
var retractionamount = settings["printer.retraction.amount"];
var preheatTemperature = settings["printer.heatup.temperature"];
var preheatBedTemperature = settings["printer.heatup.bed.temperature"];
var printerDimensionsX = settings["printer.dimensions.x"];
var printerDimensionsY = settings["printer.dimensions.y"];
var printerDimensionsZ = settings["printer.dimensions.z"];
var gCodeOffsetX = printerDimensionsX/2;
var gCodeOffsetY = printerDimensionsY/2;
var startCode = generateStartCode();
var endCode = generateEndCode();
// max amount of real world layers
var layers = printerDimensionsZ / layerHeight; //maxObjectHeight instead of objectHeight
// translate numLayers in preview to objectHeight in real world
objectHeight = Math.round(numLayers/maxNumLayers*printerDimensionsZ);
// translate preview rotation (per layer) to real world rotation
var rStepGCode = rStep * maxNumLayers/layers; ///maxNumLayers*maxObjectHeight;
// correct direction
rStepGCode = -rStepGCode;
2013-07-26 22:39:28 +02:00
// copy array without reference -> http://stackoverflow.com/questions/9885821/copying-of-an-array-of-objects-to-another-array-without-object-reference-in-java
var points = JSON.parse(JSON.stringify(_points));
// add gcode begin commands
2013-10-28 01:11:13 +01:00
gcode = gcode.concat(startCode);
var layers = printerDimensionsZ / layerHeight; //maxObjectHeight instead of objectHeight
2013-07-26 22:39:28 +02:00
var extruder = 0.0;
var prev = new Point(); prev.set(0, 0);
2013-07-30 14:21:04 +02:00
// replacement (and improvement) for ofxGetCenterofMass
2013-07-26 22:39:28 +02:00
var centerOfDoodle = {
x: doodleBounds[0] + (doodleBounds[2]- doodleBounds[0])/2,
y: doodleBounds[1] + (doodleBounds[3] - doodleBounds[1])/2
}
2013-07-26 22:39:28 +02:00
console.log("f:generategcode() >> layers: " + layers);
if (layers == Infinity) return;
// check feasibility of design
var pointsToPrint = points.length * layers*(objectHeight/printerDimensionsZ)
2013-10-23 16:45:41 +02:00
console.log("pointsToPrint: ",pointsToPrint);
if(pointsToPrint > MAX_POINTS_TO_PRINT) {
2013-10-23 18:46:13 +02:00
alert("Sorry, your doodle is too complex or too high. Please try to simplify it.");
2013-10-23 18:25:05 +02:00
console.log("ERROR: to many points too convert to gcode");
return [];
}
2013-07-26 22:39:28 +02:00
for (var layer = 0; layer < layers; layer++) {
2013-12-06 15:03:10 +01:00
//gcode.push(";LAYER:"+layer); //this will be added in a next release to support GCODE previewing in CURA
2013-12-05 11:50:02 +01:00
2013-07-26 22:39:28 +02:00
var p = JSON.parse(JSON.stringify(points)); // [].concat(points);
if (p.length < 2) return;
var even = (layer % 2 == 0);
var progress = layer / layers;
2013-09-18 22:35:38 +02:00
var layerScale = scaleFunction(progress);
2013-07-26 22:39:28 +02:00
// if begin point this row and end point last row are close enough, isLoop is true
var isLoop = lineLength(points[0][0], points[0][1], points[points.length-1][0], points[points.length-1][1]) < 3;
// set center of doodle as middle (ie subtract to that)
pointsTranslate(p, -centerOfDoodle.x, -centerOfDoodle.y);
pointsScale(p, screenToMillimeterScale,-screenToMillimeterScale);
pointsScale(p, layerScale, layerScale);
pointsRotate(p, rStepGCode * layer);
2013-07-26 22:39:28 +02:00
if (layer == 0) {
//gcode.push("M107"); //fan off
if (firstLayerSlow) {
//gcode.push("M220 S20"); //slow speed
speed = bottomSpeed;
//console.log("> speed: ",speed);
}
2013-07-30 14:21:04 +02:00
} else if (layer == 2) { ////////LET OP, pas bij layer 2 weer op normale snelheid ipv layer 1
2013-07-26 22:39:28 +02:00
gcode.push("M106"); //fan on
//gcode.push("M220 S100"); //normal speed
speed = normalSpeed;
//console.log("> speed: ",speed);
2013-07-26 22:39:28 +02:00
}
var curLayerCommand = 0;
var totalLayerCommands = p.length;
2013-07-30 14:21:04 +02:00
var layerProgress = 0;
2013-07-26 22:39:28 +02:00
var paths = [];
var pathCounter = -1;
// var points = [];
for (var i = 0; i < p.length; i++) {
if (p[i][2] == true) {
pathCounter++;
paths.push([]);
paths[pathCounter].push([p[i][0], p[i][1]]);
} else {
paths[pathCounter].push([p[i][0], p[i][1]]);
}
}
2013-07-26 22:39:28 +02:00
// loop over the subpaths (the separately drawn lines)
2013-07-30 14:21:04 +02:00
for (var j = 0; j < paths.length; j++) { // TODO paths > subpaths
var commands = paths[j];
2013-07-26 22:39:28 +02:00
// loop over the coordinates of the subpath
for (var i = 0; i < commands.length; i++) {
var last = commands.length - 1;
var to = new Point(); to.set(commands[i][0], commands[i][1]);
to.x += gCodeOffsetX;
to.y += gCodeOffsetY;
2013-07-26 22:39:28 +02:00
var sublayer = (layer == 0) ? 0.0 : layer + (useSubLayers ? (curLayerCommand/totalLayerCommands) : 0);
var z = (sublayer + 1) * layerHeight; // 2013-09-06 removed zOffset (seemed to be useless)
2013-07-26 22:39:28 +02:00
var isTraveling = !isLoop && i==0;
var doRetract = retractionEnabled && prev.distance(to) > retractionminDistance;
2013-07-26 22:39:28 +02:00
var firstPointEver = (layer == 0 && i == 0 && j == 0);
if (firstPointEver || layer > 2 && enableTraveling && isTraveling) { //always travel to first point, then disable traveling for first two layers and use settings for remainder of print
if (!firstPointEver && doRetract) gcode.push("G0 E" + (extruder - retractionamount).toFixed(3) + " F" + (retractionspeed * 60).toFixed(3)); //retract
gcode.push("G0 X" + to.x.toFixed(3) + " Y" + to.y.toFixed(3) + " Z" + z.toFixed(3) + " F" + (travelSpeed * 60).toFixed(3));
if (!firstPointEver && doRetract) gcode.push("G0 E" + extruder.toFixed(3) + " F" + (retractionspeed * 60).toFixed(3)); // return to normal
2013-07-26 22:39:28 +02:00
} else {
var f = (layer < 2) ? bottomFlowRate : 1;
extruder += prev.distance(to) * wallThickness * layerHeight / (Math.pow((filamentThickness/2), 2) * Math.PI) * f;
2013-07-26 22:39:28 +02:00
gcode.push("G1 X" + to.x.toFixed(3) + " Y" + to.y.toFixed(3) + " Z" + z.toFixed(3) + " F" + (speed * 60).toFixed(3) + " E" + extruder.toFixed(3));
}
curLayerCommand++;
2013-07-30 14:21:04 +02:00
layerProgress = curLayerCommand/totalLayerCommands;
2013-07-26 22:39:28 +02:00
prev = to;
}
}
if ((layer/layers) > (objectHeight/printerDimensionsZ)) {
console.log("f:generategcode() >> (layer/layers) > (objectHeight/printerDimensionsZ) is true -> breaking at layer " + (layer + 1));
2013-07-26 22:39:28 +02:00
break;
}
}
// add gcode end commands
2013-10-28 01:11:13 +01:00
gcode = gcode.concat(endCode);
return gcode;
2013-07-26 22:39:28 +02:00
}
function generateStartCode() {
var printerType = settings["printer.type"];
2013-12-01 18:38:02 +01:00
var startCode = settings["printer.startcode"];
startCode = subsituteVariables(startCode);
startCode = startCode.split("\n");
return startCode;
}
function generateEndCode() {
var printerType = settings["printer.type"];
2013-12-01 18:38:02 +01:00
var endCode = settings["printer.endcode"];
endCode = subsituteVariables(endCode);
endCode = endCode.split("\n");
return endCode;
}
function subsituteVariables(gcode) {
//,temperature,bedTemperature,preheatTemperature,preheatBedTemperature
var temperature = settings["printer.temperature"];
var bedTemperature = settings["printer.bed.temperature"];
var preheatTemperature = settings["printer.heatup.temperature"];
var preheatBedTemperature = settings["printer.heatup.bed.temperature"];
var printerType = settings["printer.type"];
2013-12-18 17:43:57 +01:00
var heatedbed = settings["printer.heatedbed"];
switch (printerType) {
2013-12-05 16:37:10 +01:00
case "makerbot_replicator2": printerType = "r2"; break;
case "makerbot_replicator2x": printerType = "r2x"; break;
case "makerbot_thingomatic": printerType = "t6"; break;
2013-12-01 18:38:02 +01:00
case "makerbot_generic": printerType = "r2"; break;
}
2013-12-18 17:43:57 +01:00
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);
2013-12-18 17:43:57 +01:00
gcode = gcode.replace(/{if heatedBed}/gi ,heatedBedReplacement);
return gcode;
}
function scaleFunction(percent) {
2013-09-18 22:35:38 +02:00
var r = 1.0;
switch (VERTICALSHAPE) {
case verticalShapes.NONE:
r = 1.0;
break;
case verticalShapes.DIVERGING:
r = .5 + (percent * .5);
break;
case verticalShapes.CONVERGING:
r = 1.0 - (percent * .8);
break;
case verticalShapes.SINUS:
r = (Math.cos(percent * Math.PI * 4) * .25) + .75;
break;
}
2013-09-18 22:35:38 +02:00
// return 1.0 - (percent *.8);
return r;
}
2013-07-26 22:39:28 +02:00
pointsTranslate = function(p, x, y) {
for (var i = 0; i < p.length; i++) {
p[i][0] += x;
p[i][1] += y;
}
}
pointsScale = function(p, sx, sy) {
for (var i = 0; i < p.length; i++) {
p[i][0] *= sx;
p[i][1] *= sy;
}
}
// rotates around point 0,0 (origin).
// Not the prettiest kind of rotation solution but in our case we're assuming that the points have just been translated to origin
pointsRotate = function(p, ang) {
var _ang, dist;
for (var i = 0; i < p.length; i++) {
dist = Math.sqrt(p[i][0] * p[i][0] + p[i][1] * p[i][1]);
_ang = Math.atan2(p[i][1], p[i][0]);
p[i][0] = Math.cos(_ang + ang) * dist;
p[i][1] = Math.sin(_ang + ang) * dist;
}
}
//+ Jonas Raoni Soares Silva
//@ http://jsfromhell.com/math/line-length [rev. #1]
lineLength = function(x, y, x0, y0){
return Math.sqrt((x -= x0) * x + (y -= y0) * y);
};
var Point = function() {};
Point.prototype = {
x: 0,
y: 0,
set: function(_x, _y) {
this.x = _x;
this.y = _y;
},
distance: function(p) {
var d = -1;
if (p instanceof Point) {
d = Math.sqrt((p.x - this.x) * (p.x - this.x) + (p.y - this.y) * (p.y - this.y));
}
return d;
},
toString: function() {
console.log("x:" + this.x + ", y:" + this.y);
}
}