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

202 lines
7 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,
2020-11-05 18:29:04 -05:00
CAM = KIRI.driver.CAM,
PRO = CAM.process,
newPolygon = BASE.newPolygon,
2020-11-14 11:12:12 -05:00
newPoint = BASE.newPoint,
2020-11-24 11:01:29 -05:00
isAnimate,
2020-11-14 11:12:12 -05:00
isArrange,
isCamMode,
camStock,
API, SPACE, STACKS, MODES, VIEWS, UI, UC;
2020-11-05 18:29:04 -05:00
CAM.init = function(kiri, api) {
2020-11-14 11:12:12 -05:00
// console.log({kiri,api})
STACKS = api.const.STACKS;
2020-11-14 11:12:12 -05:00
SPACE = api.const.SPACE;
MODES = api.const.MODES;
VIEWS = api.const.VIEWS;
UI = api.ui;
UC = api.uc;
API = api;
2020-11-05 18:29:04 -05:00
api.event.on("mode.set", (mode) => {
2020-11-14 11:12:12 -05:00
isCamMode = mode === 'CAM';
$('set-tools').style.display = isCamMode ? '' : 'none';
kiri.space.platform.setColor(isCamMode ? 0xeeeeee : 0xcccccc);
updateStock(undefined, 'internal');
});
api.event.on("view.set", (mode) => {
isArrange = (mode === VIEWS.ARRANGE);
2020-11-24 11:01:29 -05:00
isAnimate = false;
CAM.animate_clear(api);
2020-11-05 18:29:04 -05:00
});
2020-11-14 11:12:12 -05:00
2020-11-05 18:29:04 -05:00
api.event.on("settings.saved", (settings) => {
const proc = settings.process;
2020-11-14 11:12:12 -05:00
// show/hide dots in enabled process pop buttons
2020-11-05 18:29:04 -05:00
api.ui.camTabs.marker.style.display = proc.camTabsOn ? 'flex' : 'none';
api.ui.camRough.marker.style.display = proc.camRoughOn ? 'flex' : 'none';
api.ui.camDrill.marker.style.display =
proc.camDrillingOn || proc.camDrillReg !== 'none' ? 'flex' : 'none';
api.ui.camOutline.marker.style.display = proc.camOutlineOn ? 'flex' : 'none';
api.ui.camContour.marker.style.display =
proc.camContourXOn || proc.camContourYOn ? 'flex' : 'none';
2020-11-14 11:12:12 -05:00
updateStock(settings, 'settings.saved.internal');
2020-11-05 18:29:04 -05:00
});
2020-11-14 11:12:12 -05:00
api.event.on([
"init-done",
// update stock when modes change?
"slice.end",
"preview.end",
// update stock when preferences change
// "boolean.click",
"boolean.update",
// update stock when objects move
"platform.layout",
"selection.drag",
"selection.move",
// force bounds update, too
"selection.scale",
"selection.rotate"
], updateStock);
api.event.on("preview.end", () => {
if (camStock) STACKS.getStack("bounds").button("animate", animate);
});
2020-11-14 11:12:12 -05:00
};
function animate() {
2020-11-24 11:01:29 -05:00
isAnimate = true;
API.widgets.opacity(0.75);
2020-11-24 00:48:45 -05:00
API.hide.slider();
STACKS.clear();
2020-11-24 00:11:49 -05:00
CAM.animate(API);
}
function updateStock(args, event) {
2020-11-24 11:01:29 -05:00
if (isAnimate) {
return;
}
STACKS.remove('bounds');
if (isCamMode && isArrange) {
STACKS.clear();
}
2020-11-14 11:12:12 -05:00
if (!isCamMode) {
if (camStock) {
SPACE.platform.remove(camStock);
camStock = null;
}
2020-11-14 11:12:12 -05:00
return;
}
2020-11-14 11:12:12 -05:00
const refresh = (event === "selection.scale" || event === 'selection.rotate');
let settings = API.conf.get();
let widgets = API.widgets.all();
let proc = settings.process;
let enabled = UI.camStockOn.checked;
let offset = UI.camStockOffset.checked;
let stockSet = offset || (proc.camStockX && proc.camStockY && proc.camStockZ > 0);
let topZ = API.platform.top_z();
let delta = 0;
let csox = 0;
let csoy = 0;
let stock = settings.stock = { };
// create/inject cam stock if stock size other than default
if (enabled && stockSet && widgets.length) {
let csx = proc.camStockX;
let csy = proc.camStockY;
let csz = proc.camStockZ;
2020-11-14 11:12:12 -05:00
if (offset) {
let min = { x: Infinity, y: Infinity, z: 0 };
let max = { x: -Infinity, y: -Infinity, z: -Infinity };
widgets.forEach(function(widget) {
let wbnd = widget.getBoundingBox(refresh);
let wpos = widget.track.pos;
min = {
x: Math.min(min.x, wpos.x + wbnd.min.x),
y: Math.min(min.y, wpos.y + wbnd.min.y),
z: 0
};
max = {
x: Math.max(max.x, wpos.x + wbnd.max.x),
y: Math.max(max.y, wpos.y + wbnd.max.y),
z: Math.max(max.z, wbnd.max.z)
};
});
csx += max.x - min.x;
csy += max.y - min.y;
csz += max.z - min.z;
csox = min.x + ((max.x - min.x) / 2);
csoy = min.y + ((max.y - min.y) / 2);
}
if (!camStock) {
let geo = new THREE.BoxGeometry(1, 1, 1);
let mat = new THREE.MeshBasicMaterial({
color: 0x777777,
opacity: 0.2,
transparent: true,
side:THREE.DoubleSide
});
camStock = new THREE.Mesh(geo, mat);
camStock.renderOrder = 2;
SPACE.platform.add(camStock);
}
stock = settings.stock = {
x: csx,
y: csy,
z: csz,
center: {
x: csox,
y: csoy,
z: csz / 2
}
};
camStock.scale.x = csx + 0.005;
camStock.scale.y = csy + 0.005;
camStock.scale.z = csz + 0.005;
camStock.position.x = csox;
camStock.position.y = csoy;
camStock.position.z = csz / 2;
delta = csz - topZ;
} else if (camStock) {
SPACE.platform.remove(camStock);
camStock = null;
delta = 0;
}
const {x, y, z} = stock;
2020-11-14 12:07:05 -05:00
if (x && y && z && !STACKS.getStack('bounds')) {
2020-11-14 11:12:12 -05:00
const render = new KIRI.Render().setLayer('bounds', { face: 0xaaaaaa, line: 0xaaaaaa });
const stack = STACKS.create('bounds', SPACE.platform.world);
stack.add(render.addPolys([
newPolygon().centerRectangle({x:csox, y:csoy, z:0}, x, y),
newPolygon().centerRectangle({x:csox, y:csoy, z}, x, y)
], { thin: true } ));
const hx = x/2, hy = y/2;
stack.add(render.addLines([
newPoint(csox + hx, csoy - hy, 0), newPoint(csox + hx, csoy - hy, stock.z),
newPoint(csox + hx, csoy + hy, 0), newPoint(csox + hx, csoy + hy, stock.z),
newPoint(csox - hx, csoy - hy, 0), newPoint(csox - hx, csoy - hy, stock.z),
newPoint(csox - hx, csoy + hy, 0), newPoint(csox - hx, csoy + hy, stock.z),
], { thin: true }));
}
API.platform.update_top_z(delta);
API.platform.update_origin();
SPACE.update();
}
2020-11-05 18:29:04 -05:00
})();