grid-apps-cmms/src/kiri/print.js

894 lines
31 KiB
JavaScript
Raw Normal View History

2020-06-03 22:39:30 -04:00
/** Copyright Stewart Allen <sa@grid.space> -- All Rights Reserved */
2017-04-18 17:09:33 -04:00
"use strict";
(function() {
2020-06-03 22:39:30 -04:00
if (self.kiri.newPrint) return;
2017-04-18 17:09:33 -04:00
2020-06-03 22:39:30 -04:00
const KIRI = self.kiri,
2017-04-18 17:09:33 -04:00
DRIVERS = KIRI.driver,
LASER = DRIVERS.LASER,
2017-04-18 17:09:33 -04:00
CAM = DRIVERS.CAM,
FDM = DRIVERS.FDM,
BASE = self.base,
UTIL = BASE.util,
DBUG = BASE.debug,
POLY = BASE.polygons,
SQRT = Math.sqrt,
SQR = UTIL.sqr,
2017-04-18 17:09:33 -04:00
PI = Math.PI,
2017-10-28 13:30:08 -04:00
PRO = Print.prototype,
2017-04-18 17:09:33 -04:00
Polygon = BASE.Polygon,
2020-06-03 22:39:30 -04:00
newPoint = BASE.newPoint;
let lastPoint = null,
2017-11-11 15:37:59 -05:00
lastEmit = null;
2017-04-18 17:09:33 -04:00
KIRI.Print = Print;
2020-05-06 21:02:13 -04:00
KIRI.newPrint = function(settings, widgets, id) {
return new Print(settings, widgets, id);
};
2017-04-18 17:09:33 -04:00
/**
* @param {Object} settings
* @param {Widget[]} widgets
* @constructor
*/
function Print(settings, widgets, id) {
this.id = id || new Date().getTime().toString(36);
this.settings = settings;
this.widgets = widgets;
}
2017-11-11 15:37:59 -05:00
PRO.addOutput = addOutput;
2017-10-28 13:30:08 -04:00
PRO.extrudePerMM = extrudePerMM;
PRO.constReplace = constReplace;
PRO.poly2polyEmit = poly2polyEmit;
PRO.tip2tipEmit = tip2tipEmit;
2017-10-28 13:30:08 -04:00
PRO.addPrintPoints = addPrintPoints;
2017-04-18 17:09:33 -04:00
2019-09-17 08:36:20 -04:00
PRO.parseSVG = function(code, offset) {
let scope = this,
svg = new DOMParser().parseFromString(code, 'text/xml'),
lines = [...svg.getElementsByTagName('polyline')],
2019-09-17 08:36:20 -04:00
output = scope.output = [],
bounds = scope.bounds = {
max: { x:-Infinity, y:-Infinity, z:-Infinity},
min: { x:Infinity, y:Infinity, z:Infinity}
};
lines.forEach(line => {
let seq = [];
let points = [...line.points];
points.forEach(point => {
if (offset) {
point.x += offset.x;
point.y += offset.y;
}
if (point.x) bounds.min.x = Math.min(bounds.min.x, point.x);
if (point.x) bounds.max.x = Math.max(bounds.max.x, point.x);
if (point.y) bounds.min.y = Math.min(bounds.min.y, point.y);
if (point.y) bounds.max.y = Math.max(bounds.max.y, point.y);
if (point.z) bounds.min.z = Math.min(bounds.min.z, point.z);
if (point.z) bounds.max.z = Math.max(bounds.max.z, point.z);
2020-11-09 23:01:41 -05:00
const { x, y, z } = point; // SVGPoint is not serializable
addOutput(seq, { x, y, z }, seq.length > 0);
2019-09-17 08:36:20 -04:00
});
output.push(seq);
});
scope.imported = code;
2019-09-17 08:36:20 -04:00
scope.lines = lines.length;
scope.bytes = code.length;
2020-11-09 23:01:41 -05:00
return scope.output;
2019-09-17 08:36:20 -04:00
};
2020-11-09 18:21:59 -05:00
PRO.parseGCode = function(gcode, offset, progress, done, options) {
const opts = options || {};
const fdm = opts.fdm;
const lines = gcode
2017-04-18 17:09:33 -04:00
.toUpperCase()
.replace("X", " X")
.replace("Y", " Y")
.replace("Z", " Z")
.replace("E", " E")
.replace("F", " F")
.replace(" ", " ")
.split("\n");
2020-11-09 18:21:59 -05:00
const scope = this,
2017-04-18 17:09:33 -04:00
bounds = scope.bounds = {
max: { x:-Infinity, y:-Infinity, z:-Infinity},
min: { x:Infinity, y:Infinity, z:Infinity}
},
pos = {
X: 0.0,
Y: 0.0,
Z: 0.0,
F: 0.0,
E: 0.0
},
off = {
X: offset ? offset.x || 0 : 0,
Y: offset ? offset.y || 0 : 0,
Z: offset ? offset.z || 0 : 0
},
xoff = {
X: 0,
Y: 0,
Z: 0
2017-04-18 17:09:33 -04:00
};
2020-11-09 18:21:59 -05:00
let dz = 0,
abs = true,
defh = 0,
height = 0,
factor = 1,
tool = 0,
2020-11-13 09:27:42 -05:00
maxf = 0,
2020-11-09 18:21:59 -05:00
seq = [];
const output = scope.output = [ seq ];
2020-11-12 12:15:39 -05:00
function G0G1(g0, line) {
2020-11-11 18:07:53 -05:00
const mov = {};
line.forEach(function(tok) {
if (abs) {
pos[tok.charAt(0)] = parseFloat(tok.substring(1));
} else {
mov[tok.charAt(0)] = parseFloat(tok.substring(1));
}
});
if (!abs) {
for (let [k,v] of Object.entries(mov)) {
pos[k] += v;
}
}
2020-11-09 18:21:59 -05:00
if (pos.X) bounds.min.x = Math.min(bounds.min.x, pos.X * factor);
if (pos.X) bounds.max.x = Math.max(bounds.max.x, pos.X * factor);
if (pos.Y) bounds.min.y = Math.min(bounds.min.y, pos.Y * factor);
if (pos.Y) bounds.max.y = Math.max(bounds.max.y, pos.Y * factor);
if (pos.Z) bounds.min.z = Math.min(bounds.min.z, pos.Z * factor);
if (pos.Z) bounds.max.z = Math.max(bounds.max.z, pos.Z * factor);
const point = newPoint(
factor * pos.X + off.X + xoff.X,
factor * pos.Y + off.Y + xoff.Y,
factor * pos.Z + off.Z + xoff.Z + dz
);
2020-11-12 12:15:39 -05:00
const moving = g0 || (fdm && pos.E <= 0);
2020-11-13 09:27:42 -05:00
// update max speed
maxf = Math.max(maxf, pos.F);
2020-11-09 18:21:59 -05:00
// always add moves to the current sequence
if (moving) {
addOutput(seq, point, false, pos.F, tool);
return;
}
if (seq.Z === undefined) {
seq.Z = pos.Z;
}
// non-move in a new plane means burp out
// the old sequence and start a new one
if (seq.Z != pos.Z) {
seq = [];
seq.height = height = (defh || pos.Z - seq.Z);
if (fdm) dz = -height / 2;
output.push(seq);
}
// add point to current sequence
addOutput(seq, point, true, pos.F, tool);
}
2017-04-18 17:09:33 -04:00
lines.forEach(function(line) {
2020-11-09 16:11:02 -05:00
if (line.indexOf('- LAYER ') > 0) {
2020-11-11 16:33:56 -05:00
seq.height = defh;
2020-11-09 16:11:02 -05:00
const hd = line.replace('(','').replace(')','').split(' ');
defh = parseFloat(hd[4]);
2020-11-11 16:33:56 -05:00
if (fdm) dz = -defh / 2;
2020-11-09 16:11:02 -05:00
}
2020-11-11 16:33:56 -05:00
line = line.split(";")[0].split(" ").filter(v => v);
let cmd = line.shift();
2020-11-11 16:33:56 -05:00
if (!cmd) return;
if (cmd.charAt(0) === 'T') {
let ext = scope.settings.device.extruders;
let pos = parseInt(cmd.charAt(1));
if (ext && ext[pos]) {
xoff.X = -ext[pos].extOffsetX;
xoff.Y = -ext[pos].extOffsetY;
}
}
2020-11-09 18:21:59 -05:00
pos.E = 0.0;
switch (cmd) {
case 'G20':
factor = 25.4;
break;
case 'G21':
factor = 1;
break;
case 'G90':
// absolute positioning
abs = true;
break;
case 'G91':
// relative positioning
abs = false;
break;
2017-04-18 17:09:33 -04:00
case 'G0':
2020-11-11 18:07:53 -05:00
G0G1(true, line);
2020-11-09 18:21:59 -05:00
break;
2017-04-18 17:09:33 -04:00
case 'G1':
2020-11-12 12:15:39 -05:00
G0G1(false, line);
2017-04-18 17:09:33 -04:00
break;
case 'M6':
break;
}
});
scope.imported = gcode;
2017-04-18 17:09:33 -04:00
scope.lines = lines.length;
scope.bytes = gcode.length;
2020-11-13 09:27:42 -05:00
scope.maxSpeed = Math.floor(maxf / 60);
2020-11-09 13:57:18 -05:00
done({ output: scope.output });
2017-04-18 17:09:33 -04:00
};
function pref(a,b) {
return a !== undefined ? a : b;
}
2017-04-18 17:09:33 -04:00
/**
* @constructor
*/
function Output(point, emit, speed, tool) {
this.point = point; // point to emit
this.emit = emit; // emit (feed for printers, power for lasers, cut for cam)
this.speed = speed;
this.tool = tool;
}
/**
2017-11-11 15:37:59 -05:00
* @param {Point[]} array of points
2017-04-18 17:09:33 -04:00
* @param {Point} point
* @param {number} emit (0=move, !0=filament emit/laser on/cut mode)
* @param {number} [speed] speed
* @param {number} [tool] tool # or nozzle #
2017-04-18 17:09:33 -04:00
*/
2017-11-11 15:37:59 -05:00
function addOutput(array, point, emit, speed, tool) {
2017-12-13 08:15:37 -05:00
// drop duplicates (usually intruced by FDM bisections)
if (lastPoint && point) {
// nested due to uglify confusing browser
if (point.x == lastPoint.x && point.y == lastPoint.y && point.z == lastPoint.z && lastEmit == emit) {
return;
}
2017-11-11 15:37:59 -05:00
}
lastPoint = point;
lastEmit = emit;
array.push(new Output(point, emit, speed, tool));
2017-04-18 17:09:33 -04:00
}
/**
2018-03-25 19:21:07 -04:00
* FDM & Laser. add points in polygon to an output array (print path)
2017-04-18 17:09:33 -04:00
*
* @param {Polygon} poly
* @param {Point} startPoint
* @param {Array} output
* @param {number} [extrude] multiplier
* @param {Function} [onfirst] optional fn to call on first point
2017-04-18 17:09:33 -04:00
* @return {Point} last output point
*/
PRO.polyPrintPath = function(poly, startPoint, output, opt) {
2017-04-18 17:09:33 -04:00
poly.setClockwise();
let options = opt || {},
process = this.settings.process,
shortDist = process.outputShortDistance,
shellMult = pref(options.extrude, process.outputShellMult),
printSpeed = options.rate || process.outputFeedrate,
moveSpeed = process.outputSeekrate,
2019-05-04 09:56:21 -04:00
minSpeed = process.outputMinSpeed,
closest = options.simple ? poly.first() : poly.findClosestPointTo(startPoint),
2018-01-11 08:57:46 -05:00
perimeter = poly.perimeter(),
2017-04-18 17:09:33 -04:00
first = true,
close = !options.open,
last = startPoint,
wipeDist = options.wipe || 0,
2020-04-09 22:42:40 -04:00
coastDist = options.coast || 0,
tool = options.tool;
2017-04-18 17:09:33 -04:00
// if short, use calculated print speed based on sliding scale
if (perimeter < process.outputShortPoly) {
2019-05-04 09:56:21 -04:00
printSpeed = minSpeed + (printSpeed - minSpeed) * (perimeter / process.outputShortPoly);
2018-01-11 08:57:46 -05:00
}
poly.forEachPoint(function(point, pos, points, count) {
2017-04-18 17:09:33 -04:00
if (first) {
if (options.onfirst) {
options.onfirst(point);
}
2017-04-18 17:09:33 -04:00
// move from startPoint to point
2020-04-09 22:42:40 -04:00
addOutput(output, point, 0, moveSpeed, tool);
2017-04-18 17:09:33 -04:00
first = false;
} else {
let seglen = last.distTo2D(point);
if (coastDist && shellMult && perimeter - seglen <= coastDist) {
let delta = perimeter - coastDist;
let offset = seglen - delta;
let offPoint = last.offsetPointFrom(point, offset)
2020-04-09 22:42:40 -04:00
addOutput(output, offPoint, shellMult, printSpeed, tool);
shellMult = 0;
}
perimeter -= seglen;
2020-04-09 22:42:40 -04:00
addOutput(output, point, shellMult, printSpeed, tool);
2017-04-18 17:09:33 -04:00
}
last = point;
}, close, closest.index);
2017-04-18 17:09:33 -04:00
return output.last().point;
2017-04-18 17:09:33 -04:00
};
/**
2017-12-13 08:15:37 -05:00
* FDM only. create 3d print output path for this slice
2017-04-18 17:09:33 -04:00
*
* @parma {Slice} slice
* @param {Point} startPoint start as close as possible to startPoint
* @param {THREE.Vector3} offset
* @param {Point[]} output points
2018-01-14 18:43:43 -05:00
* @param {Object} [options] object
2017-04-18 17:09:33 -04:00
* @return {Point} last output point
*/
2018-01-14 18:43:43 -05:00
PRO.slicePrintPath = function(slice, startPoint, offset, output, options) {
2020-04-09 22:42:40 -04:00
// console.log({slicePrintPath: slice.index, ext:slice.extruder});
let i,
2018-01-14 18:43:43 -05:00
opt = options || {},
2017-04-18 17:09:33 -04:00
preout = [],
scope = this,
settings = this.settings,
process = settings.process,
2020-04-09 22:42:40 -04:00
extruder = slice.extruder || 0,
nozzleSize = settings.device.extruders[extruder].extNozzle,
2018-01-14 21:49:45 -05:00
firstLayer = opt.first || false,
2020-04-09 22:42:40 -04:00
minSeek = nozzleSize * (opt.minSeek || 1.5),
thinWall = nozzleSize * (opt.thinWall || 1.75),
2018-01-15 22:17:56 -05:00
retractDist = opt.retractOver || 2,
2018-01-14 18:43:43 -05:00
fillMult = opt.mult || process.outputFillMult,
shellMult = opt.mult || process.outputShellMult || (process.laserSliceHeight >= 0 ? 1 : 0),
sparseMult = process.outputSparseMult,
coastDist = process.outputCoastDist || 0,
2018-01-14 18:43:43 -05:00
finishSpeed = opt.speed || process.outputFinishrate,
2018-01-18 21:53:46 -05:00
firstShellSpeed = process.firstLayerRate,
firstFillSpeed = process.firstLayerFillRate,
firstPrintMult = process.firstLayerPrintMult,
2018-01-18 21:53:46 -05:00
printSpeed = opt.speed || (firstLayer ? firstShellSpeed : process.outputFeedrate),
fillSpeed = opt.speed || opt.fillSpeed || (firstLayer ? firstFillSpeed || firstShellSpeed : process.outputFeedrate),
moveSpeed = process.outputSeekrate,
2017-04-18 17:09:33 -04:00
origin = startPoint.add(offset),
2018-02-28 10:10:57 -05:00
zhop = process.zHopDistance || 0,
antiBacklash = process.antiBacklash,
doSupport = opt.support,
2017-04-18 17:09:33 -04:00
z = slice.z;
// apply first layer extrusion multipliers
if (firstLayer) {
fillMult *= firstPrintMult;
shellMult *= firstPrintMult;
sparseMult *= firstPrintMult;
}
2017-12-09 21:55:25 -05:00
function retract() {
if (preout.length) preout.last().retract = true;
}
2018-01-16 16:01:53 -05:00
function intersectsTop(p1, p2) {
let int = false;
2020-11-08 08:19:05 -05:00
POLY.flatten(slice.topPolys().clone(true)).forEach(function(poly) {
2018-01-30 20:01:34 -05:00
if (!int) poly.forEachSegment(function(s1, s2) {
2018-01-16 16:01:53 -05:00
if (UTIL.intersect(p1,p2,s1,s2,BASE.key.SEGINT)) {
int = true;
return int;
}
});
});
return int;
}
function outputTraces(poly, extrude) {
2017-04-18 17:09:33 -04:00
if (!poly) return;
if (Array.isArray(poly)) {
outputOrderClosest(poly, function(next) {
outputTraces(next, extrude);
2018-01-15 22:17:56 -05:00
}, null);
2017-04-18 17:09:33 -04:00
} else {
let finishShell = poly.depth === 0 && !firstLayer;
startPoint = scope.polyPrintPath(poly, startPoint, preout, {
2020-04-09 22:42:40 -04:00
tool: extruder,
2018-01-14 18:43:43 -05:00
rate: finishShell ? finishSpeed : printSpeed,
accel: finishShell,
wipe: process.outputWipeDistance || 0,
coast: firstLayer ? 0 : coastDist,
extrude: pref(extrude, shellMult),
onfirst: function(firstPoint) {
2019-03-04 18:12:00 -05:00
if (startPoint.distTo2D(firstPoint) > retractDist) {
retract();
}
2017-12-09 21:55:25 -05:00
}
});
2017-04-18 17:09:33 -04:00
}
}
/**
* @param {Polygon[]} polys
*/
2020-03-05 22:26:33 -05:00
function outputSparse(polys, extrude, speed) {
2017-04-18 17:09:33 -04:00
if (!polys) return;
let proxy = polys.map(function(poly) {
return {poly: poly, first: poly.first(), last: poly.last()};
});
let lp = startPoint;
startPoint = tip2tipEmit(proxy, startPoint, function(el, point, count) {
let poly = el.poly;
if (poly.last() === point) {
poly.reverse();
}
poly.forEachPoint(function(p, i) {
2018-01-16 16:01:53 -05:00
// retract if dist trigger and crosses a slice top polygon
2018-01-30 20:01:34 -05:00
if (i === 0 && lp && lp.distTo2D(p) > retractDist && intersectsTop(lp,p)) {
2018-01-16 16:01:53 -05:00
retract();
}
2020-04-09 22:42:40 -04:00
addOutput(preout, p, i === 0 ? 0 : extrude, speed || printSpeed, extruder);
lp = p;
});
return lp;
2017-04-18 17:09:33 -04:00
});
}
function outputFills(lines, options) {
if (!lines || lines.length === 0) {
return;
}
let p, p1, p2, dist, len, found, group, mindist, t1, t2,
2018-02-11 16:18:46 -05:00
marked = 0,
2018-02-11 22:59:15 -05:00
start = 0,
skip = false,
lastIndex = -1,
opt = options || {},
near = opt.near || false,
fast = opt.fast || false,
fill = opt.fill >= 0 ? opt.fill : fillMult,
thinDist = near ? thinWall : thinWall;
2018-02-11 22:59:15 -05:00
while (lines && marked < lines.length) {
group = null;
found = false;
2017-04-18 17:09:33 -04:00
mindist = Infinity;
2018-02-11 22:59:15 -05:00
// use next nearest line strategy
if (near)
for (i=0; i<lines.length; i += 2) {
t1 = lines[i];
if (t1.del) {
continue;
}
t2 = lines[i+1];
let d1 = t1.distToSq2D(startPoint);
let d2 = t2.distToSq2D(startPoint);
if (d1 < mindist || d2 < mindist) {
if (d2 < d1) {
p2 = t1;
p1 = t2;
} else {
p1 = t1;
p2 = t2;
}
mindist = Math.min(d1, d2);
lastIndex = i;
}
}
// use next index line strategy
2017-12-09 21:55:25 -05:00
// order all points by distance to last point
if (!near)
2018-02-11 22:59:15 -05:00
for (i=start; i<lines.length; i += 2) {
p = lines[i];
if (p.del) {
continue;
}
2018-02-11 22:59:15 -05:00
if (group === null && p.index > lastIndex) {
group = p.index;
}
if (group !== null) {
if (p.index !== group) {
break;
}
2018-02-11 22:59:15 -05:00
if (p.index % 2 === 0) {
t1 = lines[i];
t2 = lines[i+1];
} else {
t2 = lines[i];
t1 = lines[i+1];
}
dist = Math.min(t1.distTo2D(startPoint), t2.distTo2D(startPoint));
if (dist < mindist) {
p1 = t1;
p2 = t2;
mindist = dist;
}
start = i;
found = true;
}
2018-02-11 16:18:46 -05:00
}
2018-02-11 22:59:15 -05:00
// go back to start and try again
if (!near && !found) {
if (start === 0 && lastIndex === -1) {
console.log('infinite loop', lines, {
marked, options, i, group, start, lastIndex,
points: lines.map(p => p.index).join(', ')
});
break;
}
2018-02-11 22:59:15 -05:00
start = 0;
lastIndex = -1;
2018-02-11 16:18:46 -05:00
continue;
2017-04-18 17:09:33 -04:00
}
2017-12-08 13:27:15 -05:00
dist = startPoint.distToSq2D(p1);
len = p1.distToSq2D(p2);
// go back to start when dist > retractDist
if (!near && !fast && !skip && dist > retractDist) {
skip = true;
start = 0;
lastIndex = -1;
continue;
}
skip = false;
2018-02-13 14:46:37 -05:00
// mark as used (temporarily)
p1.del = true;
p2.del = true;
marked += 2;
lastIndex = p1.index;
2018-02-11 22:59:15 -05:00
// if dist to new segment is less than thinWall
// and segment length is less than thinWall then
// just extrude to midpoint of next segment. this is
// to avoid shaking the printer to death.
if (dist <= thinDist && len <= thinDist) {
2018-02-11 22:59:15 -05:00
p2 = p1.midPointTo(p2);
2020-04-09 22:42:40 -04:00
addOutput(preout, p2, fill * (dist / thinWall), fillSpeed, extruder);
2018-02-11 22:59:15 -05:00
} else {
// retract if dist trigger or crosses a slice top polygon
if (!fast && dist > retractDist && (zhop || intersectsTop(startPoint, p1))) {
2018-02-11 22:59:15 -05:00
retract();
}
2017-10-29 16:51:32 -04:00
2018-02-11 22:59:15 -05:00
// anti-backlash on longer move
if (!fast && antiBacklash && dist > retractDist) {
2020-04-09 22:42:40 -04:00
addOutput(preout, p1.add({x:antiBacklash,y:-antiBacklash,z:0}), 0, moveSpeed, extruder);
2018-02-11 22:59:15 -05:00
}
// bridge ends of fill when they're close together
if (dist < thinDist) {
2020-04-09 22:42:40 -04:00
addOutput(preout, p1, fill, fillSpeed, extruder);
2018-02-11 22:59:15 -05:00
} else {
2020-04-09 22:42:40 -04:00
addOutput(preout, p1, 0, moveSpeed, extruder);
2018-02-11 22:59:15 -05:00
}
2020-04-09 22:42:40 -04:00
addOutput(preout, p2, fill, fillSpeed, extruder);
2017-04-18 17:09:33 -04:00
}
2018-02-11 22:59:15 -05:00
startPoint = p2;
2017-04-18 17:09:33 -04:00
}
2017-04-18 17:09:33 -04:00
// clear delete marks so we can re-print later
if (lines) lines.forEach(function(p) { p.del = false });
}
/**
* given array of polygons, emit them in next closest order with
* the special exception that depth is considered into distance
* so that inner polygons are emitted first.
*
2017-04-18 17:09:33 -04:00
* @param {Array} array of Polygon or Polygon wrappers
* @param {Function} fn
* @param {Function} fnp convert 'next' object into a Polygon
*/
2017-12-15 09:31:53 -05:00
function outputOrderClosest(array, fn, fnp, newTop) {
2017-12-14 20:47:50 -05:00
if (array.length === 1) {
return fn(array[0]);
2017-12-14 20:47:50 -05:00
}
2017-04-18 17:09:33 -04:00
array = array.slice();
let closest, find, next, order, poly, lastDepth = 0;
2017-04-18 17:09:33 -04:00
for (;;) {
order = [];
2017-04-18 17:09:33 -04:00
closest = null;
for (i=0; i<array.length; i++) {
next = array[i];
if (!next) continue;
poly = fnp ? fnp(next) : next;
find = poly.findClosestPointTo(startPoint);
order.push({
i: i,
n: next,
d: find.distance - (poly.depth * thinWall),
});
2017-04-18 17:09:33 -04:00
}
2017-12-15 09:31:53 -05:00
newTop = false;
if (order.length === 0) {
return;
}
order.sort(function(a,b) {
return a.d - b.d;
});
array[order[0].i] = null;
fn(order[0].n);
2017-04-18 17:09:33 -04:00
}
}
let out = [];
if (slice.tops) {
out.appendAll(slice.tops);
};
if (opt.support && slice.supports) {
out.appendAll(slice.supports);
}
let lastTop = null;
outputOrderClosest(out, function(next) {
2017-04-18 17:09:33 -04:00
if (next instanceof Polygon) {
// support polygon
next.setZ(z);
2017-11-01 16:33:06 -04:00
outputTraces([next].appendAll(next.inner || []));
2017-04-18 17:09:33 -04:00
if (next.fills) {
next.fills.forEach(function(p) { p.z = z });
outputFills(next.fills, {fast: true});
2017-04-18 17:09:33 -04:00
}
} else {
// top object
2020-11-08 11:09:15 -05:00
let bounds = POLY.flatten(next.shellsAtDepth(0).clone(true));
2019-03-04 18:12:00 -05:00
let dir = -1; // 1 == inside out, -1 == outside-in
// output inner polygons
if (dir === 1)
2020-11-08 11:09:15 -05:00
outputTraces([].appendAll(next.innerShells() || []));
// sort perimeter polygon by length to go out-to-in or in-to-out
2020-11-08 08:19:05 -05:00
(next.shells || []).sort(function(a,b) {
return a.perimeter() > b.perimeter() ? dir : -dir;
2019-03-04 18:12:00 -05:00
}).forEach(function(poly, index) {
outputTraces(poly);
2019-03-04 18:12:00 -05:00
});
// output outer polygons
if (dir === -1)
2020-11-08 11:09:15 -05:00
outputTraces([].appendAll(next.innerShells() || []));
2019-03-04 18:12:00 -05:00
// output thin fill
outputFills(next.thin_fill, {near: true});
// then output solid and sparse fill
outputFills(next.fill_lines);
outputSparse(next.fill_sparse, sparseMult);
lastTop = next;
2017-04-18 17:09:33 -04:00
}
}, function(obj) {
return obj instanceof Polygon ? obj : obj.poly;
});
// produce polishing paths when present
2020-03-05 18:36:12 -05:00
if (slice.tops.length && slice.tops[0].polish) {
let {x,y} = slice.tops[0].polish;
if (x) {
2020-03-05 22:26:33 -05:00
outputSparse(x, 0, process.polishSpeed);
2020-03-05 18:36:12 -05:00
}
if (y) {
2020-03-05 22:26:33 -05:00
outputSparse(y, 0, process.polishSpeed);
}
}
2017-04-18 17:09:33 -04:00
// offset print points
for (i=0; i<preout.length; i++) {
preout[i].point = preout[i].point.add(offset);
}
// add offset points to total print
2020-04-09 22:42:40 -04:00
addPrintPoints(preout, output, origin, extruder);
2017-04-18 17:09:33 -04:00
return startPoint.add(offset);
};
/**
*
* @param {Output[]} input
* @param {Point[]} output
* @param {Point} [startPoint]
*/
2020-04-09 22:42:40 -04:00
function addPrintPoints(input, output, startPoint, tool) {
2017-04-18 17:09:33 -04:00
if (startPoint && input.length > 0) {
2020-04-09 22:42:40 -04:00
addOutput(output, startPoint, 0, undefined, tool);
2017-04-18 17:09:33 -04:00
}
output.appendAll(input);
}
/**
* emit each element in an array based on
* the next closest endpoint.
*/
function tip2tipEmit(array, startPoint, emitter) {
let mindist, dist, found, count = 0;
2017-04-18 17:09:33 -04:00
for (;;) {
found = null;
mindist = Infinity;
array.forEach(function(el) {
if (el.delete) return;
dist = startPoint.distTo3D(el.first);
if (dist < mindist) {
found = {el:el, first:el.first, last:el.last};
mindist = dist;
}
dist = startPoint.distTo3D(el.last);
if (dist < mindist) {
found = {el:el, first:el.last, last:el.first};
mindist = dist;
}
});
if (found) {
found.el.delete = true;
2020-03-11 13:24:09 -04:00
// startPoint = found.last;
startPoint = emitter(found.el, found.first, ++count);
2017-04-18 17:09:33 -04:00
} else {
break;
}
}
return startPoint;
}
/**
* like tip2tipEmit but accepts an array of polygons and the next closest
* point can be anywhere in the adjacent polygon. should be re-written
* to be more like outputOrderClosest() and have the option to account for
* depth in determining distance
2017-04-18 17:09:33 -04:00
*/
function poly2polyEmit(array, startPoint, emitter, options) {
let opt = options || {};
let marker = opt.mark || 'delete';
let mindist, dist, found, count = 0;
2017-04-18 17:09:33 -04:00
for (;;) {
found = null;
mindist = Infinity;
array.forEach(function(poly) {
if (poly[marker]) {
return;
}
2017-04-18 17:09:33 -04:00
if (poly.isOpen()) {
const d2f = startPoint.distTo2D(poly.first());
const d2l = startPoint.distTo2D(poly.last());
if (d2f > mindist && d2l > mindist) {
return;
}
2017-04-18 17:09:33 -04:00
if (d2l < mindist && d2l < d2f) {
poly.reverse();
2017-04-18 17:09:33 -04:00
found = {poly:poly, index:0, point:poly.first()};
} else if (d2f < mindist) {
found = {poly:poly, index:0, point:poly.first()};
}
return;
}
let area = poly.area();
2017-04-18 17:09:33 -04:00
poly.forEachPoint(function(point, index) {
dist = opt.weight ?
startPoint.distTo3D(point) * area * area :
startPoint.distTo2D(point);
2017-04-18 17:09:33 -04:00
if (dist < mindist) {
found = {poly:poly, index:index, point:point};
mindist = dist;
}
});
});
if (!found || opt.term) {
2017-04-18 17:09:33 -04:00
break;
}
found.poly[marker] = true;
startPoint = emitter(found.poly, found.index, ++count, startPoint) || found.point;
2017-04-18 17:09:33 -04:00
}
// undo delete marks
array.forEach(function(poly) { poly[marker] = false });
2017-04-18 17:09:33 -04:00
return startPoint;
}
/**
* flatten deeply nested polygons preserving inner arrays
*
* @param {Polygon | Polygon[]} poly or array to flatten
* @param {Polygon[]} to
* @returns {Polygon[]}
*/
function flattenPolygons(poly, to) {
if (!poly) return;
if (!to) to = [];
if (Array.isArray(poly)) {
poly.forEach(function(p) {
flattenPolygons(p, to);
})
} else {
to.push(poly);
flattenPolygons(poly.inner, to);
}
return to;
}
function polygonFitsIn(inside, outside, tolerance) {
return inside.isInside(outside, tolerance);
}
function polygonWithinOffset(poly1, poly2, offset) {
return polygonMinOffset(poly1, poly2, offset) <= offset;
}
function polygonMinOffset(poly1, poly2, offset) {
let mindist = Infinity;
2017-04-18 17:09:33 -04:00
poly1.forEachPoint(function(p) {
const nextdist = p.distToPolySegments(poly2, offset);
mindist = Math.min(mindist, nextdist);
// returning true terminates forEachPoint()
if (mindist <= offset) return true;
});
return mindist;
}
/**
* calculate mm of filament required for a given extrusion length and layer height.
*
2017-04-18 17:09:33 -04:00
* @param noz nozzle diameter
* @param fil filament diameter
* @param slice height in mm
* @returns mm of filament extruded per mm of length on the layer
2017-04-18 17:09:33 -04:00
*/
function extrudePerMM(noz, fil, slice) {
return ((PI * SQR(noz/2)) / (PI * SQR(fil/2))) * (slice / noz);
2017-04-18 17:09:33 -04:00
}
function constOp(tok, consts, opch, op) {
let pos, v1, v2;
2017-04-18 17:09:33 -04:00
if ((pos = tok.indexOf(opch)) > 0) {
v1 = consts[tok.substring(0,pos)] || 0;
v2 = parseInt(tok.substring(pos+1)) || 0;
return op(v1,v2);
} else {
return null;
}
}
function constReplace(str, consts, start) {
let cs = str.indexOf("{", start || 0),
2017-04-18 17:09:33 -04:00
ce = str.indexOf("}", cs),
tok, nutok, nustr;
if (cs >=0 && ce > cs) {
tok = str.substring(cs+1,ce);
nutok =
constOp(tok, consts, "-", function(v1,v2) { return v1-v2 }) ||
constOp(tok, consts, "+", function(v1,v2) { return v1+v2 }) ||
constOp(tok, consts, "/", function(v1,v2) { return v1/v2 }) ||
constOp(tok, consts, "*", function(v1,v2) { return v1*v2 }) ||
consts[tok] || 0;
nustr = str.replace("{"+tok+"}",nutok);
return constReplace(nustr, consts, ce+1+(nustr.length-str.length));
} else {
return str;
}
}
})();