grid-apps-cmms/src/mode/fdm/client.js

459 lines
16 KiB
JavaScript
Raw Normal View History

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-22 12:12:23 -05:00
FDM = KIRI.driver.FDM,
2021-02-11 14:33:00 -05:00
SPACE, API, VIEWS, LANG, PROC, UI, UC,
2020-11-22 14:53:17 -05:00
p1, p2, iw,
2020-11-22 20:06:11 -05:00
lastMode, lastView, lastPillar,
2020-11-25 15:26:24 -05:00
isFdmMode = false,
addingSupports = false,
2020-11-22 14:53:17 -05:00
alert = [],
2020-11-25 15:26:24 -05:00
boxes = {},
2021-02-11 14:33:00 -05:00
func = {};
2020-11-05 18:29:04 -05:00
FDM.init = function(kiri, api) {
2021-02-10 11:45:42 -05:00
UI = api.ui;
UC = api.uc;
2020-11-22 17:01:36 -05:00
API = api;
2020-11-22 12:12:23 -05:00
SPACE = api.const.SPACE;
2021-02-10 11:45:42 -05:00
VIEWS = api.const.VIEWS;
2021-02-11 14:33:00 -05:00
LANG = KIRI.lang.current;
2021-02-10 11:45:42 -05:00
PROC = Object.keys(kiri.conf.defaults.fdm.p);
2021-02-11 14:33:00 -05:00
let rangeVars = {
"sliceShells": LANG.sl_shel_s,
"sliceFillType": LANG.fi_type,
"sliceFillSparse": LANG.fi_pcnt_s,
"sliceShellOrder": LANG.sl_ordr_s,
"outputTemp": LANG.ou_nozl_s,
"outputBedTemp": LANG.ou_bedd_s,
"outputFeedrate": LANG.ou_feed_s,
"outputFinishrate": LANG.ou_fini_s,
"outputShellMult": LANG.ou_shml_s,
"outputFillMult": LANG.ou_flml_s,
"outputSparseMult": LANG.ou_spml_s,
"outputRetractDist": LANG.ad_rdst_s,
"outputRetractSpeed": LANG.outputRetractSpeed,
"outputRetractWipe": LANG.ad_wpln_s,
"outputRetractDwell": LANG.ad_rdwl_s,
"outputShortPoly": LANG.ad_spol_s,
"outputMinSpeed": LANG.ad_mins_s,
"outputCoastDist": LANG.ad_scst_s,
"sliceSupportDensity": LANG.sp_dens_s,
"sliceSupportOffset": LANG.sp_offs_s,
"sliceSolidMinArea": LANG.ad_msol_s,
"sliceLayerStart": LANG.sl_strt_s,
"zHopDistance": LANG.ad_zhop_s
};
2021-02-10 11:45:42 -05:00
for (let key of Object.keys(rangeVars)) {
UI[key].range = true;
}
2021-01-24 00:13:09 -05:00
function filterSynth() {
api.widgets.filter((widget) => {
if (widget.track.synth) {
api.const.SPACE.platform.remove(widget.mesh);
kiri.Widget.Groups.remove(widget);
}
return !widget.track.synth
});
}
2021-02-10 11:45:42 -05:00
function updateRanges(ranges = []) {
UI.rangeGroup.style.display = isFdmMode && ranges && ranges.length ? 'flex' : 'none';
let html = [];
2021-02-10 14:56:57 -05:00
let bind = [];
let now = Date.now();
let sorted = ranges.sort((a,b) => b.lo - a.lo);
for (let range of sorted) {
let id = (now++).toString(36);
let rows = Object.entries(range.fields).map(a => `<div><label class="pad">${rangeVars[a[0]]}</label><span></span><label class="val">${a[1]}</label></div>`).join('');
let hover = `<div id="hov_${id}" class="range-detail">${rows}</div>`;
let info = `<button id="sel_${id}" class="j-center grow">${range.lo} - ${range.hi}</button><button id="del_${id}"><i class="far fa-trash-alt"></i></button>`;
html.appendAll([
`<div id="rng_${id}" class="range-info">${hover}${info}</div>`
]);
bind.push({id, range});
2021-02-10 11:45:42 -05:00
}
2021-02-10 14:56:57 -05:00
UI.rangeGroup.firstElementChild.innerHTML = html.join('');
for (let rec of bind) {
$(`sel_${rec.id}`).onclick = () => {
api.show.layer(rec.range.hi, rec.range.lo);
};
$(`del_${rec.id}`).onclick = () => {
let io = ranges.indexOf(rec.range);
ranges.splice(io,1);
updateRanges(ranges);
};
}
2021-02-10 11:45:42 -05:00
}
2020-11-22 17:01:36 -05:00
api.event.on("mode.set", mode => {
2020-11-25 15:26:24 -05:00
isFdmMode = mode === 'FDM';
2020-11-22 19:10:49 -05:00
lastMode = mode;
updateVisiblity();
});
api.event.on("view.set", view => {
lastView = view;
updateVisiblity();
2021-01-24 00:13:09 -05:00
filterSynth();
2021-02-10 11:45:42 -05:00
// let ranges = API.conf.get().process.ranges;
if (isFdmMode) {
if (lastView === VIEWS.SLICE) {
for (let key of PROC) {
if (UI[key] && !UI[key].range) {
UI[key].disabled = true;
}
}
} else {
for (let key of PROC) {
if (UI[key]) UI[key].disabled = false;
}
}
}
// UI.rangeGroup.style.display = ranges && ranges.length ? 'flex' : 'none';
2020-11-22 17:01:36 -05:00
});
2021-02-10 11:45:42 -05:00
api.event.on("range.updates", updateRanges);
2020-11-05 18:29:04 -05:00
api.event.on("settings.load", (settings) => {
if (settings.mode !== 'FDM') return;
settings.process.outputOriginCenter = (settings.device.originCenter || false);
2020-11-22 17:01:36 -05:00
restoreSupports(api.widgets.all());
2021-02-10 11:45:42 -05:00
updateRanges(settings.process.ranges);
2020-11-05 18:29:04 -05:00
});
api.event.on("settings.saved", (settings) => {
2021-02-10 11:45:42 -05:00
updateRanges(settings.process.ranges);
// let ranges = settings.process.ranges;
// UI.rangeGroup.style.display = isFdmMode && ranges && ranges.length ? 'flex' : 'none';
2020-11-05 18:29:04 -05:00
});
api.event.on("button.click", target => {
switch (target) {
case api.ui.ssaGen: return func.sgen();
case api.ui.ssmAdd: return func.sadd();
case api.ui.ssmDun: return func.sdone();
2020-11-22 13:15:27 -05:00
case api.ui.ssmClr:
return api.uc.confirm("clear supports?").then(ok => {
2020-11-22 13:15:27 -05:00
if (ok) func.sclear();
});
}
});
2020-12-14 14:58:55 -05:00
api.event.on("fdm.supports.detect", func.sgen = () => {
alert = api.show.alert("analyzing part(s)...", 1000);
2020-12-14 14:28:14 -05:00
FDM.support_generate(array => {
func.sclear();
api.hide.alert(alert);
2020-12-14 14:28:14 -05:00
for (let rec of array) {
let { widget, supports } = rec;
let wa = API.widgets.annotate(widget.id);
let ws = wa.support || [];
for (let support of supports) {
let { from, to, mid } = support;
let dw = api.conf.get().process.sliceSupportSize / 2;
let dh = from.z - to.z;
let rec = {
x: mid.x,
y: mid.y,
z: mid.z,
dw,
dh,
id: Math.random() * 0xffffffffff
};
addWidgetSupport(widget, rec);
ws.push(Object.clone(rec));
}
wa.support = ws;
}
});
});
api.event.on("fdm.supports.add", func.sadd = () => {
2020-12-01 18:19:28 -05:00
alert = api.show.alert("[esc] key cancels support editing");
api.feature.hover = addingSupports = true;
});
api.event.on("fdm.supports.done", func.sdone = () => {
delbox('intZ');
delbox('intW');
2020-11-22 19:10:49 -05:00
delbox('supp');
2020-11-22 14:53:17 -05:00
api.hide.alert(alert);
api.feature.hover = addingSupports = false;
});
api.event.on("fdm.supports.clear", func.sclear = () => {
2020-11-22 13:15:27 -05:00
func.sdone();
2020-11-22 17:01:36 -05:00
clearAllWidgetSupports();
API.conf.save();
});
2020-11-22 19:10:49 -05:00
api.event.on("slice.begin", () => {
2020-11-25 15:26:24 -05:00
if (!isFdmMode) {
return;
}
2020-11-22 19:10:49 -05:00
func.sdone();
updateVisiblity();
2021-01-24 00:13:09 -05:00
// synth support widget for each widget group
let synth = [];
for (let group of kiri.Widget.Groups.list()) {
let count = 0;
let merged = new THREE.Geometry();
for (let widget of group) {
if (widget.sups) {
for (let sup of Object.values(widget.sups)) {
merged.merge(sup.box.geometry, sup.box.matrix);
count++;
}
}
}
if (!count) {
continue;
}
let bbg = new THREE.BufferGeometry().fromGeometry(merged);
let sw = kiri.newWidget(null, group);
let fwp = group[0].track.pos;
sw.loadGeometry(bbg);
sw._move(fwp.x, fwp.y, fwp.z);
api.widgets.add(sw);
sw.track.synth = true;
api.const.SPACE.platform.add(sw.mesh);
}
});
api.event.on("slice.end", () => {
if (!isFdmMode) {
return;
}
2020-11-22 19:10:49 -05:00
});
2020-11-22 14:53:17 -05:00
api.event.on("key.esc", () => {
2020-11-25 15:26:24 -05:00
if (!isFdmMode) {
return;
}
2020-11-22 14:53:17 -05:00
func.sdone()
});
api.event.on("selection.scale", () => {
if (isFdmMode) {
func.sclear();
}
});
api.event.on("widget.rotate", rot => {
2020-11-25 15:26:24 -05:00
if (!isFdmMode) {
return;
}
2021-01-03 17:13:02 -05:00
let {widget, x, y, z} = rot;
2020-11-22 19:10:49 -05:00
if (x || y) {
clearWidgetSupports(widget);
2020-11-22 19:10:49 -05:00
} else {
let ann = API.widgets.annotate(widget.id);
let sups = ann.support || [];
sups.forEach(sup => {
let wsup = widget.sups[sup.id];
let vc = new THREE.Vector3(sup.x, sup.y, sup.z);
let m4 = new THREE.Matrix4();
m4 = m4.makeRotationFromEuler(new THREE.Euler(x || 0, y || 0, z || 0));
vc.applyMatrix4(m4);
wsup.box.position.x = wsup.x = sup.x = vc.x;
wsup.box.position.y = wsup.y = sup.y = vc.y;
wsup.box.position.z = wsup.z = sup.z = vc.z;
2020-11-22 19:10:49 -05:00
});
}
});
2020-12-27 19:23:40 -05:00
api.event.on("mouse.hover.up", on => {
let { object, event } = on;
2020-11-25 15:26:24 -05:00
if (!isFdmMode) {
return;
}
if (!addingSupports) {
return;
}
2020-11-22 14:53:17 -05:00
delbox('supp');
2020-11-22 20:06:11 -05:00
if (lastPillar) {
const {widget, box, id} = lastPillar;
widget.adds.remove(box);
widget.mesh.remove(box);
delete widget.sups[id];
let sa = API.widgets.annotate(widget.id).support;
let ix = 0;
sa.forEach((rec,i) => {
if (rec.id === id) {
ix = i;
}
});
sa.splice(ix,1);
API.conf.save();
2020-11-22 20:06:11 -05:00
return;
}
2020-11-22 17:01:36 -05:00
if (!iw) return;
p1.y = Math.max(0, p1.y);
p2.y = Math.max(0, p2.y);
2020-11-22 14:53:17 -05:00
let hy = (p1.y + p2.y) / 2;
2020-11-22 17:01:36 -05:00
let dh = Math.abs(p1.y - p2.y);
2020-11-22 14:53:17 -05:00
let dw = api.conf.get().process.sliceSupportSize / 2;
2020-11-22 17:01:36 -05:00
let ip = iw.track.pos;
let wa = api.widgets.annotate(iw.id);
let ws = (wa.support = wa.support || []);
2020-11-22 20:06:11 -05:00
let x = p1.x - ip.x, y = -p1.z - ip.y, z = hy, id = Date.now();
let rec = {x, y, z, dw, dh, id};
ws.push(Object.clone(rec));
addWidgetSupport(iw, rec);
2020-11-22 17:01:36 -05:00
API.conf.save();
2020-11-22 13:15:27 -05:00
});
api.event.on("mouse.hover", data => {
2020-11-25 15:26:24 -05:00
if (!isFdmMode) {
return;
}
if (!addingSupports) {
return;
}
2020-11-22 20:06:11 -05:00
// delbox('intZ');
// delbox('intW');
// addbox(point, 0xff0000, 'intZ');
delbox('supp');
2020-11-22 12:12:23 -05:00
const { int, type, point } = data;
2020-11-22 20:06:11 -05:00
const pillar = int ? int.object.pillar : undefined;
if (lastPillar) {
lastPillar.box.material.color.r = 0;
lastPillar = null;
}
if (pillar) {
pillar.box.material.color.r = 0.5;
lastPillar = pillar;
return;
}
2020-11-22 17:01:36 -05:00
if (int && type === 'widget') {
2020-11-22 14:53:17 -05:00
iw = int.object.widget || iw;
2020-11-22 17:01:36 -05:00
} else {
iw = null;
2020-11-22 12:12:23 -05:00
}
2020-11-22 14:53:17 -05:00
p1 = point;
2020-11-22 12:12:23 -05:00
let dir = new THREE.Vector3(0,1,0)
let ray = new THREE.Raycaster(point, dir);
// when on object, project down on downward faces
if (int && int.face && int.face.normal.z < -0.1) {
dir.y = -1;
}
2020-11-22 19:10:49 -05:00
let targets = api.widgets.meshes()
.append(SPACE.internals().platform)
.appendAll(activeSupports())
;
2020-11-22 13:15:27 -05:00
let i2 = ray.intersectObjects(targets, false);
2020-11-22 12:12:23 -05:00
if (i2 && i2.length > 0) {
// prevent false matches close to origin of ray
i2 = i2.filter(i => i.distance > 0.01);
// prevent single point base to top matches
if (i2.length > 1) {
2020-11-22 14:53:17 -05:00
p2 = i2[0].point;
iw = i2[0].object.widget || iw;
let hy = (p1.y + p2.y) / 2;
let dy = Math.abs(p1.y - p2.y);
let dw = api.conf.get().process.sliceSupportSize / 2;
// addbox(p2, 0x00ff00, 'intW');
addbox({x:p1.x, y:hy, z:p1.z}, 0x0000dd, 'supp', {
x:dw, y:dw, z:dy
});
2020-11-22 12:12:23 -05:00
}
}
});
}
2020-11-22 19:10:49 -05:00
function activeSupports() {
const active = [];
API.widgets.all().forEach(widget => {
Object.values(widget.sups || {}).forEach(support => {
active.push(support.box);
support.box.support = true;
// console.log({support});
});
});
return active;
}
2020-11-22 17:01:36 -05:00
function restoreSupports(widgets) {
widgets.forEach(widget => {
const supports = API.widgets.annotate(widget.id).support || [];
supports.forEach(pos => {
addWidgetSupport(widget, pos);
});
});
}
function addWidgetSupport(widget, pos) {
const { x, y, z, dw, dh, id } = pos;
const sups = widget.sups = (widget.sups || {});
// prevent duplicate restore from repeated settings load calls
if (!sups[id]) {
2020-11-25 15:26:24 -05:00
pos.box = addbox(
{ x, y, z }, 0x0000dd, id,
2020-11-25 15:26:24 -05:00
{ x:dw, y:dw, z:dh }, { group: widget.mesh }
);
2020-11-22 20:06:11 -05:00
pos.box.pillar = Object.assign({widget}, pos);
2020-11-22 17:01:36 -05:00
sups[id] = pos;
2020-11-22 20:06:11 -05:00
widget.adds.push(pos.box);
2020-11-22 17:01:36 -05:00
}
}
2020-11-22 19:10:49 -05:00
function updateVisiblity() {
API.widgets.all().forEach(w => {
2021-02-10 11:45:42 -05:00
setSupportVisiblity(w, lastMode === 'FDM' && lastView === VIEWS.ARRANGE);
2020-11-22 19:10:49 -05:00
});
}
function setSupportVisiblity(widget, bool) {
Object.values(widget.sups || {}).forEach(support => {
support.box.visible = bool;
});
}
2020-11-22 17:01:36 -05:00
function clearAllWidgetSupports() {
API.widgets.all().forEach(widget => {
clearWidgetSupports(widget);
});
}
function clearWidgetSupports(widget) {
Object.values(widget.sups || {}).forEach(support => {
2020-11-22 20:06:11 -05:00
widget.adds.remove(support.box);
2020-11-22 17:01:36 -05:00
widget.mesh.remove(support.box);
});
2020-11-22 20:06:11 -05:00
widget.sups = {};
2020-11-22 17:01:36 -05:00
delete API.widgets.annotate(widget.id).support;
}
2020-11-25 15:26:24 -05:00
function delbox(name) {
const old = boxes[name];
2020-11-22 12:12:23 -05:00
if (old) {
2020-11-25 15:26:24 -05:00
old.groupTo.remove(old);
2020-11-22 12:12:23 -05:00
}
}
2020-11-25 15:26:24 -05:00
function addbox(point, color, name, dim = {x:1,y:1,z:1}, opt = {}) {
2020-11-22 12:12:23 -05:00
delbox(name);
2020-11-25 15:26:24 -05:00
const box = boxes[name] = new THREE.Mesh(
2020-11-22 14:53:17 -05:00
new THREE.BoxGeometry(dim.x, dim.y, dim.z),
new THREE.MeshPhongMaterial({
transparent: true,
opacity: 0.5,
color
})
2020-11-22 12:12:23 -05:00
);
box.position.x = point.x;
box.position.y = point.y;
box.position.z = point.z;
2020-11-25 15:26:24 -05:00
const group = opt.group || SPACE.scene
group.add(box);
box.groupTo = group;
if (opt.rotate) {
opt.matrix = new THREE.Matrix4().makeRotationFromQuaternion(opt.rotate);
}
if (opt.matrix) {
box.geometry.applyMatrix4(opt.matrix);
}
2020-11-22 17:01:36 -05:00
return box;
2020-11-05 18:29:04 -05:00
}
2020-11-25 15:26:24 -05:00
FDM.delbox = delbox;
FDM.addbox = addbox;
2020-11-05 18:29:04 -05:00
})();