awv3
Version:
⚡ AWV3 embedded CAD
110 lines (99 loc) • 4.1 kB
JavaScript
import * as THREE from 'three';
import Object3 from '../../../three/object3';
import HoverHandler from './hover';
import FilletProcessor from '../fillet';
import { updateGeomObjectContainer } from '../preview';
export default class FilletHandler extends HoverHandler {
constructor(sketcher, name) {
super(sketcher, name);
this.processor = new FilletProcessor(sketcher.activeSketch);
this.preview = [new THREE.Object3D(), new THREE.Object3D()];
this.sketcher.pool.add(this.preview[0]);
this.sketcher.pool.add(this.preview[1]);
}
destroy(...args) {
this.preview[0].destroy();
this.preview[1].destroy();
super.destroy(...args);
}
filterObjectsWithInteraction(object) {
if (object.graphics === undefined)
return false;
if (object.isPoint() && !!this.processor.recognizeFilletableAngle(object.pos))
return true;
if (object.isArc() && !!this.processor.recognizeFilletByArcOrEdge(object))
return true;
return false;
}
calculateNewFilletData(object) {
if (object.isPoint()) {
let info = this.processor.recognizeFilletableAngle(object.pos);
if (!info) return null;
let offset = 1/4 * Math.min(
info.lines[0].startPoint.pos.distanceTo(info.lines[0].endPoint.pos),
info.lines[1].startPoint.pos.distanceTo(info.lines[1].endPoint.pos)
);
let params = this.processor.calculateFilletParams(info, {offset: offset});
if (!params) return null;
return {info, params};
}
}
[Object3.Events.Interaction.Hovered](object, hitObject) {
if (!hitObject.first) return;
this.sketcher.selector.hover(hitObject);
if (object.isPoint()) {
let data = this.calculateNewFilletData(object);
if (!data) return;
updateGeomObjectContainer(this.preview[0], {
coordinateSystem: this.sketch.graphics.matrix,
scale: this.sketcher.graphicScale,
...data.params,
});
}
else {
let info = this.processor.recognizeFilletByArcOrEdge(object);
for (let i = 0; i < 2; i++) {
updateGeomObjectContainer(this.preview[i], {
coordinateSystem: this.sketch.graphics.matrix,
scale: this.sketcher.graphicScale,
start: info.lineStarts[i].pos,
end: info.vertex.pos,
});
}
}
}
[Object3.Events.Interaction.Unhovered](object, hitObject) {
this.sketcher.selector.unhover(hitObject);
updateGeomObjectContainer(this.preview[0], {});
updateGeomObjectContainer(this.preview[1], {});
}
[Object3.Events.Interaction.Clicked](object, hitObject) {
if (!hitObject.first) return;
let commands;
if (object.isPoint()) {
let data = this.calculateNewFilletData(object);
if (!data) return;
commands = this.processor.createNewFilletStatement(data.info, data.params);
}
else { //object = arc
let info = this.processor.recognizeFilletByArcOrEdge(object);
commands = this.processor.deleteFilletStatement(info);
}
return (async () => {
await Promise.resolve();
//note: execution is continued in the next tick
//because we cannot remove interaction from one of its callbacks
await this.sketcher.run(commands);
await this.sketcher.incrementalSolveConstraints();
})();
}
//TODO: perhaps remove?
[Object3.Events.Interaction.Picked](object, hitObject) {
if (!hitObject.first) return;
this.sketcher.view.controls.enabled = false; //do not update camera when dragging
}
[Object3.Events.Interaction.Dropped](object, hitObject) {
if (!hitObject.first) return;
this.sketcher.view.controls.enabled = true;
}
}