2020-11-05 18:29:04 -05:00
|
|
|
/** Copyright Stewart Allen <sa@grid.space> -- All Rights Reserved */
|
|
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
|
|
(function() {
|
|
|
|
|
|
|
|
|
|
let KIRI = self.kiri,
|
|
|
|
|
BASE = self.base,
|
|
|
|
|
UTIL = BASE.util,
|
2020-11-08 08:19:05 -05:00
|
|
|
POLY = BASE.polygons,
|
2020-11-05 18:29:04 -05:00
|
|
|
FDM = KIRI.driver.FDM,
|
2020-11-08 10:55:36 -05:00
|
|
|
newPoint = BASE.newPoint,
|
|
|
|
|
newPolygon = BASE.newPolygon;
|
2020-11-05 18:29:04 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* DRIVER PRINT CONTRACT
|
|
|
|
|
*
|
2020-11-08 13:04:54 -05:00
|
|
|
* @param {Function} update progress callback
|
|
|
|
|
* @returns {Object[]} returns array of render objects
|
2020-11-05 18:29:04 -05:00
|
|
|
*/
|
2020-11-08 13:04:54 -05:00
|
|
|
FDM.prepare = function(widgets, settings, update) {
|
|
|
|
|
settings = FDM.fixExtruders(settings);
|
|
|
|
|
|
2021-01-01 12:40:18 -05:00
|
|
|
let render = settings.render !== false,
|
|
|
|
|
device = settings.device,
|
2020-11-05 18:29:04 -05:00
|
|
|
nozzle = device.extruders[0].extNozzle,
|
|
|
|
|
process = settings.process,
|
2020-11-09 00:00:57 -05:00
|
|
|
bounds = settings.bounds,
|
2020-11-05 18:29:04 -05:00
|
|
|
mode = settings.mode,
|
2020-11-08 10:55:36 -05:00
|
|
|
output = [],
|
2020-11-05 18:29:04 -05:00
|
|
|
printPoint = newPoint(0,0,0),
|
|
|
|
|
firstLayerHeight = process.firstSliceHeight || process.sliceHeight,
|
2021-01-09 23:06:03 -05:00
|
|
|
firstLayerSeek = process.outputSeekrate,
|
|
|
|
|
firstLayerRate = process.firstLayerRate,
|
|
|
|
|
firstLayerMult = process.firstLayerPrintMult,
|
|
|
|
|
firstLayerBrim = process.firstLayerBrim,
|
2020-11-05 18:29:04 -05:00
|
|
|
maxLayers = 0,
|
|
|
|
|
layer = 0,
|
|
|
|
|
zoff = 0,
|
|
|
|
|
meshIndex,
|
|
|
|
|
lastIndex,
|
|
|
|
|
closest,
|
|
|
|
|
mindist,
|
|
|
|
|
minidx,
|
|
|
|
|
find,
|
|
|
|
|
layerout = [],
|
|
|
|
|
slices = [],
|
2020-11-08 13:04:54 -05:00
|
|
|
sliceEntry,
|
2020-11-08 16:44:32 -05:00
|
|
|
print = self.worker.print = KIRI.newPrint(settings, widgets),
|
2020-11-12 12:15:39 -05:00
|
|
|
isThin = settings.controller.lineType === "line",
|
2021-01-07 22:46:16 -05:00
|
|
|
isFlat = settings.controller.lineType === "flat",
|
2021-01-09 11:51:46 -05:00
|
|
|
isBelt = device.bedBelt,
|
2021-01-09 19:10:39 -05:00
|
|
|
beltYoff = device.bedDepth / 2,
|
2021-01-09 23:06:03 -05:00
|
|
|
beltfact = Math.cos(Math.PI/4),
|
|
|
|
|
invbfact = 1 / beltfact,
|
|
|
|
|
bfactor = invbfact * beltfact;
|
2020-11-05 18:29:04 -05:00
|
|
|
|
2020-12-31 23:22:39 -05:00
|
|
|
// compute bounds if missing
|
|
|
|
|
if (!bounds) {
|
|
|
|
|
bounds = new THREE.Box3();
|
|
|
|
|
for (let widget of widgets) {
|
|
|
|
|
let wp = widget.track.pos;
|
|
|
|
|
let wb = widget.bounds.clone();
|
|
|
|
|
wb.min.x += wp.x;
|
|
|
|
|
wb.max.x += wp.x;
|
|
|
|
|
wb.min.y += wp.y;
|
|
|
|
|
wb.max.y += wp.y;
|
|
|
|
|
bounds.union(wb);
|
|
|
|
|
}
|
|
|
|
|
settings.bounds = bounds;
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-05 18:29:04 -05:00
|
|
|
// TODO pick a widget with a slice on the first layer and use that nozzle
|
|
|
|
|
// create brim, skirt, raft if specificed in FDM mode (code shared by laser)
|
2021-01-07 22:46:16 -05:00
|
|
|
if (!isBelt && (process.outputBrimCount || process.outputRaft)) {
|
2020-11-05 18:29:04 -05:00
|
|
|
let brims = [],
|
|
|
|
|
offset = process.outputBrimOffset || (process.outputRaft ? 4 : 0);
|
|
|
|
|
|
|
|
|
|
// compute first brim
|
|
|
|
|
widgets.forEach(function(widget) {
|
|
|
|
|
let tops = [];
|
|
|
|
|
// collect top outer polygons
|
|
|
|
|
widget.slices[0].tops.forEach(function(top) {
|
|
|
|
|
tops.push(top.poly.clone());
|
|
|
|
|
});
|
|
|
|
|
// collect support polygons
|
|
|
|
|
if (widget.slices[0].supports)
|
|
|
|
|
widget.slices[0].supports.forEach(function(support) {
|
|
|
|
|
tops.push(support.clone());
|
|
|
|
|
});
|
|
|
|
|
// nest and offset tops
|
|
|
|
|
POLY.nest(tops).forEach(function(poly) {
|
|
|
|
|
poly.offset(-offset + nozzle / 2).forEach(function(brim) {
|
|
|
|
|
brim.move(widget.mesh.position);
|
|
|
|
|
brims.push(brim);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// merge brims
|
|
|
|
|
brims = POLY.union(brims);
|
|
|
|
|
|
|
|
|
|
// if brim is offset, over-expand then shrink to induce brims to merge
|
|
|
|
|
if (offset && brims.length) {
|
|
|
|
|
let extra = process.sliceSupportExtra + 2;
|
|
|
|
|
let zheight = brims[0].getZ();
|
|
|
|
|
brims = POLY.expand(brims, extra, zheight, null, 1);
|
|
|
|
|
brims = POLY.expand(brims, -extra, zheight, null, 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// if raft is specified
|
|
|
|
|
if (process.outputRaft) {
|
|
|
|
|
let offset = newPoint(0,0,0),
|
|
|
|
|
height = nozzle;
|
|
|
|
|
|
|
|
|
|
// cause first point of raft to be used
|
|
|
|
|
printPoint = null;
|
|
|
|
|
|
|
|
|
|
let raft = function(height, angle, spacing, speed, extrude) {
|
|
|
|
|
let slice = kiri.newSlice(zoff + height / 2);
|
|
|
|
|
brims.forEach(function(brim) {
|
|
|
|
|
// use first point of first brim as start point
|
|
|
|
|
if (printPoint === null) printPoint = brim.first();
|
|
|
|
|
let t = slice.addTop(brim);
|
|
|
|
|
t.traces = [ brim ];
|
|
|
|
|
t.inner = POLY.expand(t.traces, -nozzle * 0.5, 0, null, 1);
|
|
|
|
|
// tweak bounds for fill to induce an offset
|
|
|
|
|
t.inner[0].bounds.minx -= nozzle/2;
|
|
|
|
|
t.inner[0].bounds.maxx += nozzle/2;
|
|
|
|
|
t.fill_lines = POLY.fillArea(t.inner, angle, spacing, []);
|
|
|
|
|
})
|
|
|
|
|
offset.z = slice.z;
|
|
|
|
|
printPoint = print.slicePrintPath(slice, printPoint, offset, layerout, {
|
|
|
|
|
speed: speed,
|
|
|
|
|
mult: extrude,
|
|
|
|
|
});
|
|
|
|
|
layerout.height = height;
|
|
|
|
|
output.append(layerout);
|
|
|
|
|
|
|
|
|
|
layerout = [];
|
|
|
|
|
zoff += height;
|
|
|
|
|
};
|
|
|
|
|
|
2021-01-09 23:06:03 -05:00
|
|
|
raft(nozzle/1, process.sliceFillAngle + 0 , nozzle * 6.0, firstLayerRate / 3, 4);
|
|
|
|
|
raft(nozzle/1, process.sliceFillAngle + 0 , nozzle * 6.0, firstLayerRate / 2, 4);
|
2020-11-05 18:29:04 -05:00
|
|
|
raft(nozzle/2, process.sliceFillAngle + 90, nozzle * 3.0, process.outputFeedrate, 2.5);
|
|
|
|
|
raft(nozzle/2, process.sliceFillAngle + 0 , nozzle * 1.0, process.outputFeedrate, 1.5);
|
|
|
|
|
raft(nozzle/2, process.sliceFillAngle + 0 , nozzle * 1.0, process.outputFeedrate, 1.0);
|
|
|
|
|
|
|
|
|
|
// raise first layer off raft slightly to lessen adhesion
|
|
|
|
|
firstLayerHeight += process.outputRaftSpacing || 0;
|
|
|
|
|
|
|
|
|
|
// retract after last raft layer
|
|
|
|
|
output.last().last().retract = true;
|
|
|
|
|
}
|
|
|
|
|
// raft excludes brims
|
|
|
|
|
else
|
|
|
|
|
// if using brim vs raft
|
|
|
|
|
if (process.outputBrimCount) {
|
|
|
|
|
let polys = [],
|
|
|
|
|
preout = [];
|
|
|
|
|
|
|
|
|
|
// expand specified # of brims
|
|
|
|
|
brims.forEach(function(brim) {
|
2020-11-19 10:38:13 -05:00
|
|
|
POLY.offset([brim], nozzle, {
|
|
|
|
|
outs: polys,
|
|
|
|
|
flat: true,
|
|
|
|
|
count: process.outputBrimCount,
|
|
|
|
|
z: firstLayerHeight / 2
|
|
|
|
|
});
|
2020-11-05 18:29:04 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// output brim points
|
|
|
|
|
printPoint = print.poly2polyEmit(polys, printPoint, function(poly, index, count, startPoint) {
|
|
|
|
|
return print.polyPrintPath(poly, startPoint, preout, {
|
2021-01-09 23:06:03 -05:00
|
|
|
rate: firstLayerRate,
|
2020-11-05 18:29:04 -05:00
|
|
|
onfirst: function(point) {
|
|
|
|
|
if (preout.length && point.distTo2D(startPoint) > 2) {
|
|
|
|
|
// retract between brims
|
|
|
|
|
preout.last().retract = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
2020-11-06 14:27:26 -05:00
|
|
|
});
|
2020-11-05 18:29:04 -05:00
|
|
|
|
|
|
|
|
print.addPrintPoints(preout, layerout, null);
|
|
|
|
|
|
|
|
|
|
if (preout.length) {
|
|
|
|
|
// retract between brims and print
|
|
|
|
|
preout.last().retract = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// recompute bounds for purge block offsets
|
|
|
|
|
let bbounds = BASE.newBounds();
|
|
|
|
|
brims.forEach(brim => {
|
|
|
|
|
bbounds.merge(brim.bounds);
|
|
|
|
|
});
|
|
|
|
|
bounds.min.x = Math.min(bounds.min.x, bbounds.minx);
|
|
|
|
|
bounds.min.y = Math.min(bounds.min.y, bbounds.miny);
|
|
|
|
|
bounds.max.x = Math.max(bounds.max.x, bbounds.maxx);
|
|
|
|
|
bounds.max.y = Math.max(bounds.max.y, bbounds.maxy);
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-09 21:08:24 -05:00
|
|
|
// synthesize support widgets when needed
|
|
|
|
|
// so that they can use a separate extruder
|
|
|
|
|
for (let widget of widgets.slice()) {
|
|
|
|
|
let sslices = [];
|
|
|
|
|
for (let slice of widget.slices) {
|
|
|
|
|
if (!slice.supports) {
|
|
|
|
|
continue;
|
2020-11-05 18:29:04 -05:00
|
|
|
}
|
2021-01-09 21:08:24 -05:00
|
|
|
let sslice = KIRI.newSlice(slice.z);
|
|
|
|
|
sslice.extruder = process.sliceSupportNozzle;
|
|
|
|
|
sslice.supports = slice.supports.slice();
|
|
|
|
|
sslices.push(sslice);
|
|
|
|
|
}
|
|
|
|
|
if (sslices.length) {
|
|
|
|
|
let swidget = KIRI.newWidget();
|
|
|
|
|
swidget.slices = sslices;
|
|
|
|
|
swidget.support = true;
|
|
|
|
|
swidget.mesh = { widget: swidget, position: widget.mesh.position };
|
|
|
|
|
settings.widget[swidget.id] = { extruder: process.sliceSupportNozzle };
|
|
|
|
|
widgets.push(swidget);
|
|
|
|
|
}
|
2020-11-05 18:29:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let extruders = [];
|
|
|
|
|
let extcount = 0;
|
|
|
|
|
|
|
|
|
|
// find max layers (for updates)
|
|
|
|
|
// generate list of used extruders for purge blocks
|
2021-01-09 21:08:24 -05:00
|
|
|
for (let widget of widgets) {
|
2020-11-05 18:29:04 -05:00
|
|
|
maxLayers = Math.max(maxLayers, widget.slices.length);
|
|
|
|
|
let extruder = (settings.widget[widget.id] || {}).extruder || 0;
|
|
|
|
|
if (!extruders[extruder]) {
|
|
|
|
|
extruders[extruder] = {};
|
|
|
|
|
extcount++;
|
|
|
|
|
}
|
2021-01-09 21:08:24 -05:00
|
|
|
}
|
2020-11-05 18:29:04 -05:00
|
|
|
|
|
|
|
|
let blokpos, walkpos, blok;
|
|
|
|
|
if (bounds.min.x < bounds.min.y) {
|
|
|
|
|
let dx = ((bounds.max.x - bounds.min.x) - (extcount * 10)) / 2 + 5;
|
|
|
|
|
blokpos = { x:bounds.min.x + dx, y: bounds.max.y + 5};
|
|
|
|
|
walkpos = { x:10, y:0 };
|
|
|
|
|
blok = { x:9, y:4 };
|
|
|
|
|
} else {
|
|
|
|
|
let dy = ((bounds.max.y - bounds.min.y) - (extcount * 10)) / 2 + 5;
|
|
|
|
|
blokpos = { x:bounds.max.x + 5, y: bounds.min.y + dy};
|
|
|
|
|
walkpos = { x:0, y:10 };
|
|
|
|
|
blok = { x:4, y:9 };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// compute purge blocks
|
|
|
|
|
extruders = extruders.map((ext,i) => {
|
|
|
|
|
if (!ext) return ext;
|
|
|
|
|
let noz = device.extruders[i].extNozzle,
|
|
|
|
|
pos = {x:blokpos.x, y:blokpos.y, z:0},
|
|
|
|
|
rec = {
|
|
|
|
|
extruder: i,
|
2020-11-08 10:55:36 -05:00
|
|
|
poly: newPolygon().centerSpiral(pos, blok.x, blok.y, noz*2, 3)
|
2020-11-05 18:29:04 -05:00
|
|
|
};
|
|
|
|
|
blokpos.x += walkpos.x;
|
|
|
|
|
blokpos.y += walkpos.y;
|
|
|
|
|
return rec;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// generate purge block for given nozzle
|
|
|
|
|
function purge(nozzle, track, layer, start, z, using) {
|
|
|
|
|
if (extcount < 2) {
|
|
|
|
|
return start;
|
|
|
|
|
}
|
|
|
|
|
let rec = track[nozzle];
|
|
|
|
|
if (rec) {
|
|
|
|
|
track[nozzle] = null;
|
|
|
|
|
if (layer.last()) layer.last().retract = true;
|
|
|
|
|
start = print.polyPrintPath(rec.poly.clone().setZ(z), start, layer, {
|
|
|
|
|
tool: using || nozzle,
|
|
|
|
|
open: true,
|
|
|
|
|
simple: true
|
|
|
|
|
});
|
|
|
|
|
layer.last().retract = true;
|
|
|
|
|
return start;
|
|
|
|
|
} else {
|
|
|
|
|
console.log({already_purged: nozzle, from: track, layer});
|
|
|
|
|
return start;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-09 19:10:39 -05:00
|
|
|
// establish offsets
|
|
|
|
|
for (let widget of widgets) {
|
|
|
|
|
let offset = widget.mesh ? Object.clone(widget.mesh.position) : {x:0, y:0, z:0};
|
|
|
|
|
if (isBelt) {
|
|
|
|
|
offset.x = widget.rotinfo.xpos;
|
2021-01-10 22:22:36 -05:00
|
|
|
offset.y = -widget.belt.midy;
|
2021-01-11 20:25:10 -05:00
|
|
|
offset.y += widget.rotinfo.ypos * beltfact - 0.5;
|
|
|
|
|
offset.z = widget.rotinfo.ypos * beltfact;
|
2021-01-09 19:10:39 -05:00
|
|
|
} else {
|
|
|
|
|
// when rafts used this is non-zero
|
|
|
|
|
offset.z = zoff;
|
2020-11-05 18:29:04 -05:00
|
|
|
}
|
2021-01-09 19:10:39 -05:00
|
|
|
widget.offset = offset;
|
|
|
|
|
}
|
2020-11-05 18:29:04 -05:00
|
|
|
|
2021-01-09 19:10:39 -05:00
|
|
|
// create shuffled slice cake by z offset (slice.z + offset.z)
|
|
|
|
|
let cake = [];
|
|
|
|
|
let zrec = {};
|
|
|
|
|
for (let widget of widgets) {
|
|
|
|
|
// skip synthesized support widget(s)
|
|
|
|
|
if (!widget.mesh) {
|
|
|
|
|
continue;
|
2020-11-05 18:29:04 -05:00
|
|
|
}
|
2021-01-09 19:10:39 -05:00
|
|
|
for (let slice of widget.slices) {
|
|
|
|
|
slice.widget = widget;
|
|
|
|
|
let z = slice.z + widget.offset.z;
|
|
|
|
|
let rec = zrec[z] = zrec[z] || {z, slices:[]};
|
|
|
|
|
if (rec.slices.length === 0) {
|
|
|
|
|
cake.push(rec);
|
|
|
|
|
}
|
|
|
|
|
rec.slices.push(slice);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
cake.sort((a, b) => {
|
|
|
|
|
return a.z - b.z;
|
|
|
|
|
});
|
2020-11-05 18:29:04 -05:00
|
|
|
|
2021-01-09 19:10:39 -05:00
|
|
|
// walk cake layers bottom up
|
|
|
|
|
for (let layer of cake) {
|
2020-11-05 18:29:04 -05:00
|
|
|
// track purge blocks generated for each layer
|
|
|
|
|
let track = extruders.slice();
|
|
|
|
|
let lastOut;
|
|
|
|
|
let lastExt;
|
|
|
|
|
|
|
|
|
|
// iterate over layer slices, find closest widget, print, eliminate
|
|
|
|
|
for (;;) {
|
|
|
|
|
let order = [];
|
|
|
|
|
// select slices of the same extruder type first then distance
|
2021-01-09 19:10:39 -05:00
|
|
|
for (let slice of layer.slices) {
|
|
|
|
|
if (slice.prep) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
let offset = slice.widget.offset;
|
|
|
|
|
let find = slice.findClosestPointTo(printPoint.sub(offset));
|
|
|
|
|
if (find) {
|
|
|
|
|
let ext = slice.extruder;
|
|
|
|
|
let lex = lastOut ? lastOut.extruder : ext;
|
|
|
|
|
let dst = Math.abs(find.distance);
|
|
|
|
|
// penalize extruder swaps
|
|
|
|
|
if (ext !== lex) {
|
|
|
|
|
dst *= 10000;
|
2020-11-05 18:29:04 -05:00
|
|
|
}
|
2021-01-09 19:10:39 -05:00
|
|
|
order.push({dst, slice, offset, z: layer.z});
|
2020-11-05 18:29:04 -05:00
|
|
|
}
|
|
|
|
|
}
|
2021-01-09 19:10:39 -05:00
|
|
|
if (order.length === 0) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
2020-11-05 18:29:04 -05:00
|
|
|
order.sort((a,b) => {
|
|
|
|
|
return a.dst - b.dst;
|
|
|
|
|
});
|
2021-01-09 19:10:39 -05:00
|
|
|
let { z, slice, offset } = order[0];
|
|
|
|
|
slice.prep = true;
|
2020-11-05 18:29:04 -05:00
|
|
|
// retract between widgets
|
|
|
|
|
if (layerout.length && minidx !== lastIndex) {
|
|
|
|
|
layerout.last().retract = true;
|
|
|
|
|
}
|
2021-01-09 19:10:39 -05:00
|
|
|
layerout.z = z;
|
|
|
|
|
layerout.height = layerout.height || slice.height;
|
2020-11-05 18:29:04 -05:00
|
|
|
// detect extruder change and print purge block
|
2021-01-09 19:10:39 -05:00
|
|
|
if (!lastOut || lastOut.extruder !== slice.extruder) {
|
|
|
|
|
printPoint = purge(slice.extruder, track, layerout, printPoint, slice.z);
|
2020-11-05 18:29:04 -05:00
|
|
|
}
|
2021-01-09 23:06:03 -05:00
|
|
|
let tmpout = [];
|
2021-01-11 19:14:07 -05:00
|
|
|
let wtb = slice.widget.track.box;
|
2020-11-05 18:29:04 -05:00
|
|
|
// output seek to start point between mesh slices if previous data
|
|
|
|
|
printPoint = print.slicePrintPath(
|
2021-01-09 19:10:39 -05:00
|
|
|
slice,
|
2021-01-11 19:14:07 -05:00
|
|
|
isBelt ? newPoint(-wtb.w, wtb.d * 2, 0) : printPoint.sub(offset),
|
2021-01-09 19:10:39 -05:00
|
|
|
offset,
|
2021-01-09 23:06:03 -05:00
|
|
|
tmpout, //layerout,
|
2020-11-05 18:29:04 -05:00
|
|
|
{
|
2021-01-09 19:10:39 -05:00
|
|
|
first: slice.index === 0,
|
|
|
|
|
support: slice.widget.support
|
2020-11-05 18:29:04 -05:00
|
|
|
}
|
|
|
|
|
);
|
2021-01-09 23:06:03 -05:00
|
|
|
// alter settings for base extrusions (touching the bed)
|
|
|
|
|
if (isBelt) {
|
|
|
|
|
let widget = slice.widget;
|
2021-01-10 22:22:36 -05:00
|
|
|
let lastout, first = false;
|
|
|
|
|
let minz = Infinity, maxy = -Infinity, minx = Infinity, maxx = -Infinity;
|
|
|
|
|
let thresh = firstLayerHeight * 1.1;
|
2021-01-09 23:06:03 -05:00
|
|
|
for (let out of tmpout) {
|
|
|
|
|
let point = out.point;
|
|
|
|
|
let belty = out.belty = -point.y + point.z * bfactor;
|
2021-01-10 22:22:36 -05:00
|
|
|
if (out.emit && belty < thresh && lastout && lastout.belty < thresh) {
|
2021-01-09 23:06:03 -05:00
|
|
|
out.speed = firstLayerRate;
|
|
|
|
|
out.emit *= firstLayerMult;
|
|
|
|
|
minx = Math.min(minx, point.x, lastout.point.x);
|
|
|
|
|
maxx = Math.max(maxx, point.x, lastout.point.x);
|
2021-01-10 22:22:36 -05:00
|
|
|
maxy = Math.max(maxy, point.y);
|
|
|
|
|
minz = Math.min(minz, point.z);
|
2021-01-09 23:06:03 -05:00
|
|
|
first = out;
|
|
|
|
|
}
|
|
|
|
|
lastout = out;
|
|
|
|
|
}
|
|
|
|
|
// add brim, if specified
|
|
|
|
|
if (first && firstLayerBrim) {
|
2021-01-10 22:22:36 -05:00
|
|
|
let {emit, tool } = first;
|
|
|
|
|
let y = maxy;
|
|
|
|
|
let z = minz;
|
2021-01-09 23:06:03 -05:00
|
|
|
let b = Math.max(firstLayerBrim, 1);
|
2021-01-11 18:01:55 -05:00
|
|
|
print.addOutput(layerout, newPoint(maxx + b, y, z), 0, firstLayerSeek, tool);
|
|
|
|
|
print.addOutput(layerout, newPoint(maxx + 0, y, z), emit, firstLayerRate, tool);
|
2021-01-11 19:14:07 -05:00
|
|
|
print.addOutput(layerout, newPoint(minx - b, y, z), 0, firstLayerSeek, tool);
|
|
|
|
|
print.addOutput(layerout, newPoint(minx - 0, y, z), emit, firstLayerRate, tool);
|
|
|
|
|
// print.addOutput(layerout, newPoint(minx - 0, y, z + 5), 0, firstLayerRate, tool);
|
2021-01-09 23:06:03 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
layerout.appendAll(tmpout);
|
2021-01-09 19:10:39 -05:00
|
|
|
lastOut = slice;
|
2020-11-05 18:29:04 -05:00
|
|
|
lastExt = lastOut.ext
|
|
|
|
|
lastIndex = minidx;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// if a declared extruder isn't used in a layer, use selected
|
|
|
|
|
// extruder to fill the relevant purge blocks for later support
|
|
|
|
|
track.forEach(ext => {
|
|
|
|
|
if (ext) {
|
|
|
|
|
printPoint = purge(ext.extruder, track, layerout, printPoint, lastOut.z, lastExt);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// if layer produced output, append to output array
|
2021-01-09 19:10:39 -05:00
|
|
|
if (layerout.length) {
|
|
|
|
|
output.append(layerout);
|
|
|
|
|
}
|
2020-11-05 18:29:04 -05:00
|
|
|
|
|
|
|
|
// retract after last layer
|
|
|
|
|
if (layer === maxLayers && layerout.length) {
|
|
|
|
|
layerout.last().retract = true;
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-26 09:10:20 -05:00
|
|
|
// notify progress
|
|
|
|
|
layerout.layer = layer++;
|
|
|
|
|
update((layer / maxLayers) * 0.5, "prepare");
|
|
|
|
|
|
2020-11-05 18:29:04 -05:00
|
|
|
slices = [];
|
|
|
|
|
layerout = [];
|
|
|
|
|
lastOut = undefined;
|
|
|
|
|
}
|
2020-11-08 08:19:05 -05:00
|
|
|
|
2020-11-08 16:44:32 -05:00
|
|
|
print.output = output;
|
2021-01-01 11:55:49 -05:00
|
|
|
|
2021-01-09 11:51:46 -05:00
|
|
|
// render if not explicitly disabled
|
|
|
|
|
if (render) {
|
|
|
|
|
print.render = FDM.prepareRender(output, (progress, layer) => {
|
|
|
|
|
update(0.5 + progress * 0.5, "render", layer);
|
|
|
|
|
}, { tools: device.extruders, thin: isThin, flat: isFlat, fdm: true });
|
2021-01-08 14:42:01 -05:00
|
|
|
}
|
|
|
|
|
|
2021-01-08 23:44:26 -05:00
|
|
|
return print.render;
|
2020-11-05 18:29:04 -05:00
|
|
|
};
|
|
|
|
|
|
2020-11-09 16:11:02 -05:00
|
|
|
class Counter {
|
|
|
|
|
constructor() {
|
|
|
|
|
this.map = {};
|
|
|
|
|
this.total = 0;
|
|
|
|
|
}
|
|
|
|
|
put(key) {
|
|
|
|
|
const map = this.map;
|
|
|
|
|
const kp = key || 'bad';
|
|
|
|
|
map[kp] = (map[kp] || 0) + 1;
|
|
|
|
|
this.total++;
|
|
|
|
|
}
|
|
|
|
|
get() {
|
|
|
|
|
return { map: this.map, total: this.total };
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-11 08:07:02 -05:00
|
|
|
FDM.rateToColor = function(rate, max) {
|
2020-11-11 10:07:43 -05:00
|
|
|
return currentColorFunction(rate/max, 1, 0.85);
|
2020-11-10 21:48:04 -05:00
|
|
|
};
|
|
|
|
|
|
2020-11-08 13:04:54 -05:00
|
|
|
FDM.prepareRender = function(levels, update, options) {
|
2020-11-22 17:01:36 -05:00
|
|
|
levels = levels.filter(level => level.length);
|
2020-11-10 21:54:18 -05:00
|
|
|
if (levels.length === 0) {
|
|
|
|
|
self.worker.print.maxSpeed = 0;
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-08 13:04:54 -05:00
|
|
|
const opts = options || {};
|
2020-11-08 16:44:32 -05:00
|
|
|
const tools = opts.tools || {};
|
2020-11-12 09:55:19 -05:00
|
|
|
const flat = opts.flat;
|
|
|
|
|
const thin = opts.thin && !flat;
|
2020-11-09 00:21:35 -05:00
|
|
|
const ckspeed = opts.speed !== false;
|
2020-11-08 16:44:32 -05:00
|
|
|
const moveColor = opts.move >= 0 ? opts.move : 0xaaaaaa;
|
|
|
|
|
const printColor = opts.print >= 0 ? opts.print : 0x777700;
|
2020-11-08 10:55:36 -05:00
|
|
|
const layers = [];
|
2020-11-09 18:21:59 -05:00
|
|
|
|
2020-11-12 09:55:19 -05:00
|
|
|
const moveOpt = {
|
|
|
|
|
face: moveColor,
|
|
|
|
|
line: flat ? 1 : moveColor,
|
|
|
|
|
opacity: flat ? 0.5 : 1
|
|
|
|
|
};
|
|
|
|
|
const printOpt = {
|
|
|
|
|
face: printColor,
|
|
|
|
|
line: flat ? 1 : printColor,
|
|
|
|
|
opacity: flat ? 0.5 : 1
|
|
|
|
|
};
|
|
|
|
|
|
2020-11-09 00:00:57 -05:00
|
|
|
const maxspd = levels.map(level => {
|
2020-11-09 18:21:59 -05:00
|
|
|
return level.map(o => o.speed || 0).reduce((a, v) => Math.max(a,v));
|
2020-11-09 16:11:02 -05:00
|
|
|
}).reduce((a, v) => Math.max(a, v)) + 1;
|
2020-11-08 16:44:32 -05:00
|
|
|
|
2020-11-10 21:48:04 -05:00
|
|
|
// for reporting
|
|
|
|
|
self.worker.print.maxSpeed = maxspd - 1;
|
2020-11-10 23:22:58 -05:00
|
|
|
self.worker.print.thinColor = thin;
|
2020-11-12 09:55:19 -05:00
|
|
|
self.worker.print.flatColor = flat;
|
2020-11-10 21:48:04 -05:00
|
|
|
|
2020-11-15 01:01:10 -05:00
|
|
|
let lastEnd = null;
|
2020-11-08 16:44:32 -05:00
|
|
|
let lastOut = null;
|
|
|
|
|
let current = null;
|
2020-11-21 15:58:25 -05:00
|
|
|
let retracted = false;
|
2020-11-08 16:44:32 -05:00
|
|
|
|
2020-11-10 23:22:58 -05:00
|
|
|
function color(point) {
|
2020-11-11 08:07:02 -05:00
|
|
|
return FDM.rateToColor(point.speed, maxspd);
|
2020-11-09 19:22:28 -05:00
|
|
|
}
|
|
|
|
|
|
2020-11-08 13:04:54 -05:00
|
|
|
levels.forEach((level, index) => {
|
2020-11-08 10:55:36 -05:00
|
|
|
const prints = {};
|
|
|
|
|
const moves = [];
|
2020-11-16 11:00:52 -05:00
|
|
|
const heads = [];
|
|
|
|
|
const retracts = [];
|
2020-11-21 15:58:25 -05:00
|
|
|
const engages = [];
|
2020-11-30 22:36:06 -05:00
|
|
|
const output = new KIRI.Layers();
|
2020-11-09 00:00:57 -05:00
|
|
|
layers.push(output);
|
2020-11-08 16:44:32 -05:00
|
|
|
|
2020-11-08 13:04:54 -05:00
|
|
|
const pushPrint = (toolid, poly) => {
|
|
|
|
|
toolid = toolid || 0;
|
|
|
|
|
const array = prints[toolid] = prints[toolid] || [];
|
2020-11-08 16:44:32 -05:00
|
|
|
const tool = tools[toolid] || {};
|
2020-11-08 13:04:54 -05:00
|
|
|
array.width = (tool.extNozzle || 1) / 2;
|
2020-11-08 10:55:36 -05:00
|
|
|
array.push(poly);
|
2020-11-11 17:33:20 -05:00
|
|
|
emits++;
|
2020-11-08 10:55:36 -05:00
|
|
|
};
|
2020-11-08 16:44:32 -05:00
|
|
|
|
2020-11-08 10:55:36 -05:00
|
|
|
let height = level.height / 2;
|
|
|
|
|
let width = 1;
|
2020-11-11 17:33:20 -05:00
|
|
|
let emits = 0;
|
2020-11-08 16:44:32 -05:00
|
|
|
|
2020-11-15 01:01:10 -05:00
|
|
|
level.forEach((out,oi) => {
|
2020-11-21 15:58:25 -05:00
|
|
|
if (retracted && out.emit) {
|
|
|
|
|
retracted = false;
|
|
|
|
|
engages.push(lastOut.point);
|
|
|
|
|
}
|
2020-11-16 11:00:52 -05:00
|
|
|
if (out.retract) {
|
|
|
|
|
retracts.push(out.point);
|
2020-11-21 15:58:25 -05:00
|
|
|
retracted = true;
|
2020-11-16 11:00:52 -05:00
|
|
|
}
|
2020-11-08 16:44:32 -05:00
|
|
|
if (!out.point) {
|
|
|
|
|
// in cam mode, these are drilling or dwell ops
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-11-14 21:33:46 -05:00
|
|
|
|
2020-11-08 10:55:36 -05:00
|
|
|
if (lastOut) {
|
2020-11-16 11:00:52 -05:00
|
|
|
if (lastOut.emit !== out.emit) {
|
|
|
|
|
heads.push({p1: lastOut.point, p2: out.point});
|
|
|
|
|
}
|
2020-12-19 23:24:03 -05:00
|
|
|
const op = out.point, lp = lastOut.point;
|
|
|
|
|
// const moved = Math.max(
|
|
|
|
|
// Math.abs(op.x - lp.x),
|
|
|
|
|
// Math.abs(op.y - lp.y),
|
|
|
|
|
// Math.abs(op.z - lp.z));
|
|
|
|
|
// if (moved < 0.0001) return;
|
2020-11-08 10:55:36 -05:00
|
|
|
if (out.emit) {
|
2020-11-15 01:01:10 -05:00
|
|
|
if (!lastOut.emit || (ckspeed && out.speed !== lastOut.speed) || lastEnd) {
|
2020-11-08 10:55:36 -05:00
|
|
|
current = newPolygon().setOpen();
|
|
|
|
|
current.push(lastOut.point);
|
2020-11-09 19:22:28 -05:00
|
|
|
current.color = color(out);
|
2020-11-08 10:55:36 -05:00
|
|
|
pushPrint(out.tool, current);
|
|
|
|
|
}
|
|
|
|
|
current.push(out.point);
|
|
|
|
|
} else {
|
2020-11-15 01:01:10 -05:00
|
|
|
if (lastOut.emit || lastEnd) {
|
2020-11-08 10:55:36 -05:00
|
|
|
current = newPolygon().setOpen();
|
|
|
|
|
current.push(lastOut.point);
|
|
|
|
|
moves.push(current);
|
|
|
|
|
}
|
|
|
|
|
current.push(out.point);
|
|
|
|
|
}
|
2020-11-15 01:01:10 -05:00
|
|
|
lastEnd = null;
|
2020-11-08 10:55:36 -05:00
|
|
|
} else {
|
|
|
|
|
current = newPolygon().setOpen();
|
|
|
|
|
current.push(out.point);
|
|
|
|
|
if (out.emit) {
|
2020-11-09 19:22:28 -05:00
|
|
|
current.color = color(out);
|
2020-11-08 10:55:36 -05:00
|
|
|
pushPrint(out.tool, current);
|
|
|
|
|
} else {
|
|
|
|
|
moves.push(current);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
lastOut = out;
|
|
|
|
|
});
|
2020-11-11 17:33:20 -05:00
|
|
|
// all moves with an emit at the very end (common in contouring)
|
|
|
|
|
if (lastOut.emit && !emits) {
|
|
|
|
|
pushPrint(lastOut.tool, current)
|
|
|
|
|
}
|
2020-11-15 01:01:10 -05:00
|
|
|
lastEnd = lastOut;
|
2020-11-16 11:00:52 -05:00
|
|
|
if (retracts.length) {
|
|
|
|
|
output
|
|
|
|
|
.setLayer('retract', { line: 0x550000, face: 0xff0000, opacity: 0.5 }, true)
|
|
|
|
|
.addAreas(retracts.map(point => {
|
|
|
|
|
return newPolygon().centerCircle(point, 0.2, 16).setZ(point.z + 0.01);
|
|
|
|
|
}), { outline: true });
|
|
|
|
|
}
|
2020-11-21 15:58:25 -05:00
|
|
|
if (engages.length) {
|
|
|
|
|
output
|
|
|
|
|
.setLayer('engage', { line: 0x005500, face: 0x00ff00, opacity: 0.5 }, true)
|
|
|
|
|
.addAreas(engages.map(point => {
|
|
|
|
|
return newPolygon().centerCircle(point, 0.2, 16).setZ(point.z + 0.01);
|
|
|
|
|
}), { outline: true });
|
|
|
|
|
}
|
2020-11-16 11:00:52 -05:00
|
|
|
if (heads.length) {
|
|
|
|
|
output
|
|
|
|
|
.setLayer('arrows', { face: moveColor, line: 0x555555, opacity: 0.5 }, true)
|
|
|
|
|
.addAreas(heads.map(points => {
|
|
|
|
|
const {p1, p2} = points;
|
|
|
|
|
const slope = p2.slopeTo(p1);
|
|
|
|
|
const s1 = BASE.newSlopeFromAngle(slope.angle + 20);
|
|
|
|
|
const s2 = BASE.newSlopeFromAngle(slope.angle - 20);
|
|
|
|
|
const p3 = points.p2.projectOnSlope(s1, 0.4);
|
|
|
|
|
const p4 = points.p2.projectOnSlope(s2, 0.4);
|
|
|
|
|
return newPolygon().addPoints([p2,p3,p4]).setZ(p2.z + 0.01);
|
|
|
|
|
}), { thin: true, outline: true });
|
|
|
|
|
}
|
2020-11-08 20:19:50 -05:00
|
|
|
output
|
2020-11-12 09:55:19 -05:00
|
|
|
.setLayer(opts.other || 'move', moveOpt, opts.moves !== true)
|
2020-11-08 20:19:50 -05:00
|
|
|
.addPolys(moves, { thin: true, z: opts.z });
|
2020-11-08 10:55:36 -05:00
|
|
|
Object.values(prints).forEach(array => {
|
2020-11-12 09:55:19 -05:00
|
|
|
array.forEach(poly => {
|
2020-11-12 12:15:39 -05:00
|
|
|
if (flat && poly.appearsClosed()) {
|
2020-11-12 09:55:19 -05:00
|
|
|
poly.setClosed();
|
|
|
|
|
poly.points.pop();
|
|
|
|
|
poly.length--;
|
|
|
|
|
}
|
|
|
|
|
output
|
|
|
|
|
.setLayer(opts.action || 'print', printOpt)
|
|
|
|
|
.addPolys([ poly ],
|
|
|
|
|
thin ? { thin, z: opts.z, color: poly.color } :
|
|
|
|
|
flat ? {
|
|
|
|
|
flat, z: opts.z, color: poly.color,
|
|
|
|
|
outline: true, offset: array.width, open: poly.open } :
|
|
|
|
|
{
|
|
|
|
|
offset: array.width, height, z: opts.z,
|
|
|
|
|
color: { face: poly.color, line: poly.color }
|
|
|
|
|
})
|
2020-11-09 16:11:02 -05:00
|
|
|
});
|
2020-11-08 16:44:32 -05:00
|
|
|
});
|
|
|
|
|
|
2020-11-26 09:10:20 -05:00
|
|
|
update(index / levels.length, output);
|
2020-11-08 10:55:36 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return layers;
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-11 10:07:43 -05:00
|
|
|
const colorFunctions = {
|
|
|
|
|
default: hsv2rgb.bind({ seg: 5, fn: color5 }),
|
|
|
|
|
dark: hsv2rgb.bind({ seg: 3, fn: color3 })
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let currentColorFunction = colorFunctions.default;
|
|
|
|
|
|
2020-11-09 13:57:18 -05:00
|
|
|
// hsv values all = 0 to 1
|
2020-11-11 10:07:43 -05:00
|
|
|
function hsv2rgb(h, s, v) {
|
|
|
|
|
const div = this.seg;
|
2020-11-11 08:27:16 -05:00
|
|
|
const ss = 1 / div;
|
2020-11-10 23:07:18 -05:00
|
|
|
const seg = Math.floor(h / ss);
|
|
|
|
|
const rem = h - (seg * ss);
|
|
|
|
|
const inc = (rem / ss);
|
|
|
|
|
const dec = (1 - inc);
|
|
|
|
|
const rgb = {r: 0, g: 0, b: 0};
|
2020-11-11 10:07:43 -05:00
|
|
|
this.fn(rgb, inc, seg);
|
2020-11-11 08:27:16 -05:00
|
|
|
rgb.r = ((rgb.r * 255 * v) & 0xff) << 16;
|
|
|
|
|
rgb.g = ((rgb.g * 255 * v) & 0xff) << 8;
|
|
|
|
|
rgb.b = ((rgb.b * 255 * v) & 0xff);
|
|
|
|
|
return rgb.r | rgb.g | rgb.b;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function color5(rgb, inc, seg) {
|
|
|
|
|
const dec = 1 - inc;
|
2020-11-09 13:57:18 -05:00
|
|
|
switch (seg) {
|
|
|
|
|
case 0:
|
2020-11-10 23:07:18 -05:00
|
|
|
rgb.r = 1;
|
|
|
|
|
rgb.g = inc;
|
|
|
|
|
rgb.b = 0;
|
2020-11-09 13:57:18 -05:00
|
|
|
break;
|
|
|
|
|
case 1:
|
2020-11-10 23:07:18 -05:00
|
|
|
rgb.r = dec;
|
|
|
|
|
rgb.g = 1;
|
2020-11-11 08:07:02 -05:00
|
|
|
rgb.b = 0;
|
2020-11-09 13:57:18 -05:00
|
|
|
break;
|
|
|
|
|
case 2:
|
2020-11-11 08:07:02 -05:00
|
|
|
rgb.r = 0;
|
2020-11-10 23:07:18 -05:00
|
|
|
rgb.g = dec;
|
2020-11-11 08:07:02 -05:00
|
|
|
rgb.b = inc;
|
2020-11-09 13:57:18 -05:00
|
|
|
break;
|
|
|
|
|
case 3:
|
2020-11-11 08:07:02 -05:00
|
|
|
rgb.r = inc;
|
|
|
|
|
rgb.g = 0;
|
|
|
|
|
rgb.b = 1;
|
|
|
|
|
break;
|
|
|
|
|
case 4:
|
2020-11-10 23:07:18 -05:00
|
|
|
rgb.r = dec;
|
|
|
|
|
rgb.g = 0;
|
|
|
|
|
rgb.b = dec;
|
2020-11-09 13:57:18 -05:00
|
|
|
break;
|
|
|
|
|
}
|
2020-11-11 08:27:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function color3(rgb, inc, seg) {
|
|
|
|
|
const dec = 1 - inc;
|
|
|
|
|
switch (seg) {
|
|
|
|
|
case 0:
|
|
|
|
|
rgb.r = dec;
|
|
|
|
|
rgb.g = inc;
|
|
|
|
|
rgb.b = 0;
|
|
|
|
|
break;
|
|
|
|
|
case 1:
|
|
|
|
|
rgb.r = 0;
|
|
|
|
|
rgb.g = dec;
|
|
|
|
|
rgb.b = inc;
|
|
|
|
|
break;
|
|
|
|
|
case 2:
|
|
|
|
|
rgb.r = inc/2;
|
|
|
|
|
rgb.g = 0;
|
|
|
|
|
rgb.b = dec/2 + 0.5;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2020-11-09 13:57:18 -05:00
|
|
|
}
|
|
|
|
|
|
2020-11-05 18:29:04 -05:00
|
|
|
})();
|