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

178 lines
6 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,
2020-11-22 17:01:36 -05:00
SPACE, API,
2020-11-22 14:53:17 -05:00
p1, p2, iw,
alert = [],
func = {};
2020-11-05 18:29:04 -05:00
FDM.init = function(kiri, api) {
2020-11-22 17:01:36 -05:00
API = api;
2020-11-22 12:12:23 -05:00
SPACE = api.const.SPACE;
2020-11-22 17:01:36 -05:00
api.event.on("mode.set", mode => {
if (mode !== 'FDM') {
clearAllWidgetSupports();
}
});
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());
2020-11-05 18:29:04 -05:00
});
api.event.on("settings.saved", (settings) => {
let proc = settings.process;
api.ui.fdmSupport.marker.style.display = proc.sliceSupportEnable ? 'flex' : 'none';
});
api.event.on("button.click", target => {
switch (target) {
2020-11-22 13:15:27 -05:00
case api.ui.ssmAdd:
return func.sadd();
2020-11-22 13:35:01 -05:00
case api.ui.ssmDun:
return func.sdone();
2020-11-22 13:15:27 -05:00
case api.ui.ssmClr:
api.uc.confirm("clear supports?").then(ok => {
if (ok) func.sclear();
});
break;
}
});
api.event.on("fdm.supports.add", func.sadd = () => {
2020-11-22 14:53:17 -05:00
alert = api.show.alert("&lt;esc&gt; key when done editing supports");
api.feature.hover = true;
});
api.event.on("fdm.supports.done", func.sdone = () => {
delbox('intZ');
delbox('intW');
2020-11-22 14:53:17 -05:00
api.hide.alert(alert);
api.feature.hover = 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 14:53:17 -05:00
api.event.on("key.esc", () => {
func.sdone()
});
2020-11-22 17:01:36 -05:00
// api.event.on("selection.rotate", (x,y,z) => {
// console.log('rotate', x,y,z);
2020-11-22 14:53:17 -05:00
// });
2020-11-22 13:15:27 -05:00
api.event.on("mouse.hover.up", int => {
2020-11-22 14:53:17 -05:00
delbox('supp');
2020-11-22 17:01:36 -05:00
if (!iw) return;
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 || []);
let x = p1.x - ip.x, y = -p1.z - ip.y, z = hy;
let id = Date.now();
ws.push({x, y, z, dw, dh, id});
addWidgetSupport(iw, {x, y, z, dw, dh, id});
API.conf.save();
2020-11-22 13:15:27 -05:00
});
api.event.on("mouse.hover", data => {
2020-11-22 12:12:23 -05:00
const { int, type, point } = data;
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
// delbox('intZ');
// delbox('intW');
// addbox(point, 0xff0000, 'intZ');
2020-11-22 17:01:36 -05:00
delbox('supp');
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 13:15:27 -05:00
let targets = api.widgets.meshes().append(SPACE.internals().platform);
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 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]) {
pos.box = addbox({x, y, z}, 0x0000dd, 'supp', { x:dw, y:dw, z:dh }, widget.mesh);
sups[id] = pos;
}
}
function clearAllWidgetSupports() {
API.widgets.all().forEach(widget => {
clearWidgetSupports(widget);
});
}
function clearWidgetSupports(widget) {
Object.values(widget.sups || {}).forEach(support => {
widget.mesh.remove(support.box);
});
delete API.widgets.annotate(widget.id).support;
}
2020-11-22 14:53:17 -05:00
function delbox(name, group) {
2020-11-22 12:12:23 -05:00
const old = SPACE[`__${name}`];
if (old) {
2020-11-22 14:53:17 -05:00
(group || SPACE.scene).remove(old);
2020-11-22 12:12:23 -05:00
}
}
2020-11-22 14:53:17 -05:00
function addbox(point, color, name, dim = {x:1,y:1,z:1}, group) {
2020-11-22 12:12:23 -05:00
delbox(name);
name = `__${name}`;
const box = SPACE[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-22 14:53:17 -05:00
(group || SPACE.scene).add(box);
2020-11-22 17:01:36 -05:00
return box;
2020-11-05 18:29:04 -05:00
}
})();