2022-02-18 14:56:59 -05:00
|
|
|
/** Copyright Stewart Allen <sa@grid.space> -- All Rights Reserved */
|
|
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
2022-02-20 22:47:04 -05:00
|
|
|
// dep: kiri.api
|
2022-02-23 09:29:33 -05:00
|
|
|
// dep: kiri.client
|
2022-02-22 00:05:29 -05:00
|
|
|
// dep: kiri.export
|
2022-02-20 22:47:04 -05:00
|
|
|
// dep: moto.space
|
2022-02-22 11:32:35 -05:00
|
|
|
// use: kiri-mode.cam.client
|
|
|
|
|
// dep: kiri-mode.fdm.client
|
|
|
|
|
// dep: kiri-mode.sla.client
|
2022-02-22 00:05:29 -05:00
|
|
|
// dep: kiri-mode.laser.driver
|
2022-02-20 22:47:04 -05:00
|
|
|
gapp.register("kiri.function", [], (root, exports) => {
|
2022-02-18 14:56:59 -05:00
|
|
|
|
2022-02-20 22:47:04 -05:00
|
|
|
const { kiri, noop } = root;
|
2022-02-18 14:56:59 -05:00
|
|
|
const { api, client, consts, utils } = kiri;
|
|
|
|
|
const { space } = moto;
|
|
|
|
|
const { COLOR, PMODES } = consts;
|
|
|
|
|
|
|
|
|
|
let complete = {};
|
|
|
|
|
|
|
|
|
|
function prepareSlices(callback, scale = 1, offset = 0) {
|
|
|
|
|
const { conf, event, feature, hide, mode, view, platform, show } = api;
|
|
|
|
|
const { stacks } = kiri;
|
|
|
|
|
|
|
|
|
|
if (view.is_arrange()) {
|
|
|
|
|
let snap = space.screenshot();
|
|
|
|
|
view.snapshot = snap.substring(snap.indexOf(",") + 1);
|
|
|
|
|
client.snap(space.screenshot2({width: 640}));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (mode.is_sla() && !callback) {
|
|
|
|
|
callback = preparePreview;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const widgets = api.widgets.all();
|
|
|
|
|
const settings = conf.get();
|
|
|
|
|
const { device, process, controller } = settings;
|
|
|
|
|
const isBelt = device.bedBelt || false;
|
|
|
|
|
const mark = Date.now();
|
|
|
|
|
|
|
|
|
|
// force layout in belt mode when widget exceeds bed length
|
|
|
|
|
if (widgets.length && isBelt) {
|
|
|
|
|
let doLayout = false;
|
|
|
|
|
for (let w of widgets) {
|
|
|
|
|
let bb = w.getBoundingBox();
|
|
|
|
|
let yspan = bb.max.y - bb.min.y;
|
|
|
|
|
if (yspan > device.bedDepth) {
|
|
|
|
|
doLayout = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (doLayout) {
|
|
|
|
|
platform.layout();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-18 18:42:08 -05:00
|
|
|
functions.clear_progress();
|
2022-02-18 14:56:59 -05:00
|
|
|
platform.deselect();
|
|
|
|
|
hide.slider();
|
|
|
|
|
view.set_slice();
|
|
|
|
|
conf.save();
|
|
|
|
|
|
2022-02-19 00:27:51 -05:00
|
|
|
// allow client.js to generate support widgets
|
2022-02-18 14:56:59 -05:00
|
|
|
event.emit('slice.begin', settings.mode);
|
|
|
|
|
|
2022-02-19 20:01:10 -05:00
|
|
|
// refresh widget list because 'slice.begin' may generate support widgets
|
2022-02-19 00:27:51 -05:00
|
|
|
const slicing = api.widgets.all()
|
2022-02-18 14:56:59 -05:00
|
|
|
.slice()
|
|
|
|
|
.filter(w => !w.track.ignore && !w.meta.disabled);
|
2022-02-20 22:47:04 -05:00
|
|
|
|
|
|
|
|
if (slicing.length === 0) {
|
|
|
|
|
return api.show.alert('nothing to slice');
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-18 14:56:59 -05:00
|
|
|
const totalv = slicing.map(w => w.getVertices().count).reduce((a,v) => a + v);
|
|
|
|
|
const defvert = totalv / slicing.length;
|
|
|
|
|
const track = {};
|
|
|
|
|
|
|
|
|
|
let totalProgress;
|
|
|
|
|
|
|
|
|
|
api.widgets.opacity(COLOR.slicing_opacity);
|
|
|
|
|
|
|
|
|
|
let segtimes = {},
|
|
|
|
|
segNumber = 0,
|
|
|
|
|
errored = false,
|
|
|
|
|
startTime = Date.now(),
|
|
|
|
|
toSlice = slicing.slice(),
|
|
|
|
|
camOrLaser = mode.is_cam() || mode.is_laser(),
|
|
|
|
|
extruders = {},
|
|
|
|
|
lastMsg;
|
|
|
|
|
|
|
|
|
|
for (let widget of toSlice) {
|
2022-11-01 23:45:11 -04:00
|
|
|
widget.belt = null;
|
2022-02-18 14:56:59 -05:00
|
|
|
widget.stats.progress = 0;
|
|
|
|
|
widget.setColor(COLOR.slicing);
|
2022-04-03 23:32:34 -04:00
|
|
|
let { extruder } = widget.anno;
|
|
|
|
|
if (extruder >= 0) {
|
|
|
|
|
extruders[extruder] = extruder;
|
|
|
|
|
}
|
2022-02-18 14:56:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// in multi-material belt mode, the anchor needs to be extended
|
|
|
|
|
// to allow room for the purge tower to be built. calculate here
|
|
|
|
|
extruders = Object.values(extruders);
|
|
|
|
|
if (isBelt && extruders.length > 1 && process.outputPurgeTower) {
|
|
|
|
|
process.beltAnchor = Math.max(
|
|
|
|
|
process.firstLayerBeltLead,
|
|
|
|
|
Math.sqrt(process.outputPurgeTower) * extruders.length * (1/Math.sqrt(2)));
|
2022-10-19 00:11:54 -04:00
|
|
|
} else if (isBelt) {
|
2022-02-18 14:56:59 -05:00
|
|
|
process.beltAnchor = process.firstLayerBeltLead;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
stacks.clear();
|
|
|
|
|
if (isBelt) {
|
|
|
|
|
// force re-sync in belt mode
|
|
|
|
|
client.clear();
|
|
|
|
|
}
|
2023-01-24 08:27:42 -05:00
|
|
|
client.sync(undefined, isBelt);
|
2022-02-18 14:56:59 -05:00
|
|
|
client.rotate(settings);
|
|
|
|
|
|
|
|
|
|
function sliceNext() {
|
|
|
|
|
if (toSlice.length) {
|
|
|
|
|
// while more widgets to slice
|
|
|
|
|
sliceWidget(toSlice.shift())
|
|
|
|
|
} else {
|
|
|
|
|
// once all slicing done, run sliceAll() once
|
|
|
|
|
client.sliceAll(settings, sliceDone);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function sliceWidget(widget) {
|
|
|
|
|
widget.stack = stacks.create(widget.id, widget.mesh);
|
|
|
|
|
let factor = (widget.getVertices().count / defvert);
|
|
|
|
|
|
|
|
|
|
// compensate for zcut (widget moved through floor)
|
|
|
|
|
widget.stack.obj.view.position.z = widget.track.zcut || 0;
|
|
|
|
|
|
|
|
|
|
widget.slice(settings, (sliced, error) => {
|
|
|
|
|
let mark = Date.now();
|
|
|
|
|
// update UI info
|
|
|
|
|
if (sliced) {
|
|
|
|
|
// update segment time
|
|
|
|
|
if (lastMsg) {
|
|
|
|
|
segtimes[`${widget.id}_${segNumber++}_${lastMsg}`] = mark - startTime;
|
|
|
|
|
}
|
|
|
|
|
event.emit('slice', settings.mode);
|
|
|
|
|
}
|
|
|
|
|
// handle slicing errors
|
|
|
|
|
if (error && !errored) {
|
|
|
|
|
errored = true;
|
|
|
|
|
view.set_arrange();
|
|
|
|
|
show.alert(error, 5);
|
|
|
|
|
show.progress(0);
|
|
|
|
|
client.restart();
|
|
|
|
|
event.emit('slice.error', error);
|
2023-03-07 09:09:49 -05:00
|
|
|
setTimeout(view.set_arrange, 1);
|
2022-02-18 14:56:59 -05:00
|
|
|
}
|
|
|
|
|
if (errored) {
|
|
|
|
|
// terminate slicing
|
|
|
|
|
sliceDone();
|
|
|
|
|
} else {
|
|
|
|
|
// start next widget slice
|
|
|
|
|
sliceNext();
|
|
|
|
|
}
|
2022-09-08 13:26:47 -04:00
|
|
|
}, (update, msg, alert) => {
|
|
|
|
|
if (alert) {
|
|
|
|
|
api.show.alert(alert);
|
|
|
|
|
}
|
2022-02-18 14:56:59 -05:00
|
|
|
if (msg && msg !== lastMsg) {
|
|
|
|
|
let mark = Date.now();
|
|
|
|
|
if (lastMsg) {
|
|
|
|
|
segtimes[`${widget.id}_${segNumber++}_${lastMsg}`] = mark - startTime;
|
|
|
|
|
}
|
|
|
|
|
lastMsg = msg;
|
|
|
|
|
startTime = mark;
|
|
|
|
|
}
|
|
|
|
|
// on update
|
2022-09-08 13:26:47 -04:00
|
|
|
if (update >= 0) {
|
|
|
|
|
track[widget.id] = (update || 0) * factor;
|
|
|
|
|
totalProgress = 0;
|
|
|
|
|
for (let w of slicing) {
|
|
|
|
|
totalProgress += (track[w.id] || 0);
|
|
|
|
|
}
|
2022-10-13 20:31:44 -04:00
|
|
|
show.progress(offset + (totalProgress / slicing.length) * scale, msg);
|
2022-02-18 14:56:59 -05:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function sliceDone() {
|
|
|
|
|
let alert = null;
|
|
|
|
|
if (scale === 1 && feature.work_alerts && slicing.length) {
|
|
|
|
|
alert = show.alert("Rendering");
|
|
|
|
|
};
|
2022-11-01 23:45:11 -04:00
|
|
|
for (let widget of slicing) {
|
|
|
|
|
// on done
|
|
|
|
|
segtimes[`${widget.id}_${segNumber++}_draw`] = widget.render(widget.stack);
|
|
|
|
|
// rotate stack for belt beds
|
|
|
|
|
if (widget.belt) {
|
|
|
|
|
widget.stack.obj.rotate(widget.belt);
|
2022-02-18 14:56:59 -05:00
|
|
|
}
|
|
|
|
|
if (scale === 1) {
|
2022-11-01 23:45:11 -04:00
|
|
|
// clear wireframe
|
|
|
|
|
widget.setWireframe(false, COLOR.wireframe, COLOR.wireframe_opacity);
|
|
|
|
|
widget.setOpacity(camOrLaser ? COLOR.cam_sliced_opacity : COLOR.sliced_opacity);
|
|
|
|
|
widget.setColor(COLOR.deselected);
|
|
|
|
|
api.hide.alert(alert);
|
2022-02-18 14:56:59 -05:00
|
|
|
}
|
2022-11-01 23:45:11 -04:00
|
|
|
}
|
|
|
|
|
view.update_slider_max(true);
|
|
|
|
|
show.layer(-1, 0);
|
|
|
|
|
if (scale === 1) {
|
|
|
|
|
view.update_stack_labels();
|
|
|
|
|
}
|
|
|
|
|
if (!isBelt && controller.lineType === 'line' && !process.xray && !controller.devel) {
|
|
|
|
|
$('render-ghost').onclick();
|
|
|
|
|
}
|
2022-02-18 14:56:59 -05:00
|
|
|
if (scale === 1) {
|
|
|
|
|
show.progress(0);
|
|
|
|
|
}
|
|
|
|
|
// cause visuals to update
|
|
|
|
|
space.scene.active();
|
|
|
|
|
// mark slicing complete for prep/preview
|
|
|
|
|
complete.slice = true;
|
|
|
|
|
event.emit('slice.end', settings.mode);
|
|
|
|
|
// print stats
|
|
|
|
|
segtimes.total = Date.now() - mark;
|
|
|
|
|
console.log(segtimes);
|
|
|
|
|
if (callback && typeof callback === 'function') {
|
|
|
|
|
callback();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// kick of slicing chain
|
|
|
|
|
sliceNext();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function preparePreview(callback, scale = 1, offset = 0) {
|
|
|
|
|
const { conf, event, feature, hide, mode, view, platform, show } = api;
|
|
|
|
|
const { stacks } = kiri;
|
|
|
|
|
|
|
|
|
|
const widgets = api.widgets.all();
|
|
|
|
|
const settings = conf.get();
|
|
|
|
|
const { device, process, controller } = settings;
|
|
|
|
|
|
|
|
|
|
if (complete.preview === feature.pmode) {
|
2022-05-16 22:42:07 -04:00
|
|
|
if (device.extruders && device.extruders.length > 1) {
|
2022-02-18 14:56:59 -05:00
|
|
|
if (++feature.pmode > 2) {
|
|
|
|
|
feature.pmode = 1;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (callback) {
|
|
|
|
|
callback();
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!complete.slice) {
|
|
|
|
|
settings.render = false;
|
|
|
|
|
prepareSlices(() => { preparePreview(callback, 0.25, 0.75) }, 0.75);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hide.slider(true);
|
|
|
|
|
|
|
|
|
|
const isCam = mode.is_cam(), pMode = settings.mode;
|
2022-09-26 13:28:31 -04:00
|
|
|
const isDark = api.space.is_dark();
|
2022-02-18 14:56:59 -05:00
|
|
|
const isBelt = device.bedBelt || false;
|
|
|
|
|
|
|
|
|
|
view.set_preview();
|
|
|
|
|
conf.save();
|
|
|
|
|
event.emit('preview.begin', pMode);
|
|
|
|
|
|
|
|
|
|
if (isCam) {
|
2022-09-26 13:28:31 -04:00
|
|
|
api.widgets.opacity(isDark ? COLOR.cam_preview_opacity_dark : COLOR.cam_preview_opacity);
|
2022-02-18 14:56:59 -05:00
|
|
|
api.widgets.each(widget => {
|
|
|
|
|
widget.setColor(COLOR.cam_preview);
|
|
|
|
|
});
|
|
|
|
|
} else if (offset === 0) {
|
|
|
|
|
api.widgets.opacity(COLOR.preview_opacity);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let mark = Date.now(),
|
|
|
|
|
segNumber = 0,
|
|
|
|
|
segtimes = {},
|
|
|
|
|
startTime,
|
|
|
|
|
lastMsg,
|
|
|
|
|
output = [];
|
|
|
|
|
|
|
|
|
|
// pass preview mode to worker
|
|
|
|
|
settings.pmode = feature.pmode;
|
|
|
|
|
settings.render = true;
|
|
|
|
|
|
|
|
|
|
client.prepare(settings, (progress, message, layer) => {
|
|
|
|
|
if (layer) {
|
|
|
|
|
output.push(kiri.codec.decode(layer));
|
|
|
|
|
}
|
|
|
|
|
if (message && message !== lastMsg) {
|
|
|
|
|
const mark = Date.now();
|
|
|
|
|
if (lastMsg) {
|
|
|
|
|
segtimes[`${segNumber++}_${lastMsg}`] = mark - startTime;
|
|
|
|
|
}
|
|
|
|
|
lastMsg = message;
|
|
|
|
|
startTime = mark;
|
|
|
|
|
}
|
|
|
|
|
show.progress(offset + progress * scale, message);
|
|
|
|
|
}, (reply, maxSpeed, minSpeed) => {
|
|
|
|
|
// handle worker errors
|
|
|
|
|
if (reply && reply.error) {
|
|
|
|
|
show.alert(reply.error, 5);
|
|
|
|
|
view.set_arrange();
|
|
|
|
|
show.progress(0);
|
|
|
|
|
space.update();
|
|
|
|
|
event.emit('preview.error', reply.error);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (lastMsg) {
|
|
|
|
|
segtimes[`${segNumber++}_${lastMsg}`] = Date.now() - startTime;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
show.progress(0);
|
|
|
|
|
if (!isCam) {
|
|
|
|
|
api.widgets.opacity(0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (output.length) {
|
|
|
|
|
let alert = feature.work_alerts ? show.alert("Rendering") : null;
|
|
|
|
|
startTime = Date.now();
|
|
|
|
|
stacks.clear();
|
|
|
|
|
const stack = stacks.create('print', space.world)
|
|
|
|
|
output.forEach(layer => {
|
|
|
|
|
stack.add(layer);
|
|
|
|
|
});
|
|
|
|
|
// rotate stack for belt beds
|
2022-11-01 23:45:11 -04:00
|
|
|
if (isBelt && widgets[0].belt) {
|
|
|
|
|
let ri = widgets[0].belt;
|
2022-02-18 14:56:59 -05:00
|
|
|
ri.dz = 0;
|
|
|
|
|
ri.dy = settings.device.bedDepth / 2;
|
2022-11-01 23:45:11 -04:00
|
|
|
stack.obj.rotate(widgets[0].belt);
|
2022-02-18 14:56:59 -05:00
|
|
|
}
|
|
|
|
|
api.hide.alert(alert);
|
|
|
|
|
segtimes[`${segNumber}_draw`] = Date.now() - startTime;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// print stats
|
|
|
|
|
segtimes.total = Date.now() - mark;
|
|
|
|
|
console.log(segtimes);
|
|
|
|
|
|
|
|
|
|
event.emit('print', pMode);
|
|
|
|
|
event.emit('preview.end', pMode);
|
|
|
|
|
|
|
|
|
|
space.update();
|
|
|
|
|
view.update_slider_max(true);
|
|
|
|
|
show.layer(-1, 0);
|
|
|
|
|
if (feature.pmode === PMODES.SPEED) {
|
|
|
|
|
view.update_speeds(maxSpeed, minSpeed);
|
|
|
|
|
} else {
|
|
|
|
|
view.update_speeds();
|
|
|
|
|
}
|
2022-02-19 14:13:35 -05:00
|
|
|
view.update_stack_labels();
|
2022-02-18 14:56:59 -05:00
|
|
|
|
|
|
|
|
let { controller, process } = settings;
|
2022-02-23 11:41:57 -05:00
|
|
|
if (!isBelt && controller.lineType === 'line' && !process.xray && !controller.devel) {
|
2022-02-18 14:56:59 -05:00
|
|
|
$('render-ghost').onclick();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// mark preview complete for export
|
|
|
|
|
complete.preview = feature.pmode;
|
|
|
|
|
|
|
|
|
|
if (typeof(callback) === 'function') {
|
|
|
|
|
callback();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function prepareAnimation() {
|
2022-10-13 15:08:47 -04:00
|
|
|
if (!window.SharedArrayBuffer) {
|
|
|
|
|
api.alerts.show("The security context of this");
|
|
|
|
|
api.alerts.show("window prevents animations");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-02-19 14:13:35 -05:00
|
|
|
const settings = api.conf.get();
|
2022-02-18 14:56:59 -05:00
|
|
|
api.event.emit("function.animate", {mode: settings.mode});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function prepareExport() {
|
2022-02-19 14:13:35 -05:00
|
|
|
const settings = api.conf.get();
|
2022-02-18 14:56:59 -05:00
|
|
|
const argsave = arguments;
|
|
|
|
|
if (!complete.preview) {
|
|
|
|
|
preparePreview(() => { prepareExport(...argsave) });
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
api.event.emit("function.export", {mode: settings.mode});
|
|
|
|
|
complete.export = true;
|
|
|
|
|
kiri.export(...argsave);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function cancelWorker() {
|
|
|
|
|
if (client.isBusy()) {
|
|
|
|
|
client.restart();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-19 14:13:35 -05:00
|
|
|
function parseCode(code, type) {
|
|
|
|
|
const { conf, event, show, widgets, view } = api;
|
2022-02-18 14:56:59 -05:00
|
|
|
const { stacks } = kiri;
|
2022-02-19 14:13:35 -05:00
|
|
|
const settings = conf.get();
|
|
|
|
|
|
2022-02-18 14:56:59 -05:00
|
|
|
event.emit("code.load", {code, type});
|
|
|
|
|
view.set_preview();
|
|
|
|
|
widgets.opacity(0);
|
2022-02-19 14:13:35 -05:00
|
|
|
|
2022-02-18 14:56:59 -05:00
|
|
|
client.parse({code, type, settings}, progress => {
|
|
|
|
|
show.progress(progress, "parsing");
|
|
|
|
|
}, (layers, maxSpeed, minSpeed) => {
|
|
|
|
|
show.progress(0);
|
|
|
|
|
stacks.clear();
|
|
|
|
|
const stack = stacks.create('parse', space.world);
|
|
|
|
|
layers.forEach(layer => stack.add(layer));
|
|
|
|
|
view.update_slider_max(true);
|
|
|
|
|
view.update_speeds(maxSpeed, minSpeed);
|
|
|
|
|
show.slices();
|
2022-02-19 14:13:35 -05:00
|
|
|
view.update_stack_labels();
|
2022-02-18 14:56:59 -05:00
|
|
|
space.update();
|
|
|
|
|
event.emit("code.loaded", {code, type});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-18 18:42:08 -05:00
|
|
|
const functions = api.function = {
|
2022-02-18 14:56:59 -05:00
|
|
|
slice: prepareSlices,
|
|
|
|
|
print: preparePreview,
|
|
|
|
|
prepare: preparePreview,
|
|
|
|
|
animate: prepareAnimation,
|
|
|
|
|
export: prepareExport,
|
|
|
|
|
cancel: cancelWorker,
|
2022-02-19 14:13:35 -05:00
|
|
|
parse: parseCode,
|
2022-02-18 14:56:59 -05:00
|
|
|
clear: client.clear,
|
|
|
|
|
clear_progress() { complete = {} }
|
|
|
|
|
};
|
|
|
|
|
|
2022-02-20 22:47:04 -05:00
|
|
|
});
|