awv3
Version:
⚡ AWV3 embedded CAD
77 lines (70 loc) • 3.21 kB
JavaScript
import Plugin from '../../session/plugin';
import Dimension from '../dimension/';
import { Checkbox, Input, Group, Label, Link, Selection, Slider } from '../../session/elements';
const resources = ['extrusion'].reduce(
(prev, item) => ({ ...prev, [item]: require('!!url-loader!awv3-icons/32x32/' + item + '.png') }),
{},
);
export default class Feature extends Plugin {
constructor(session, args) {
super(session, { type: 'Extrusion', icon: 'extrusion', resources, ...args });
this.dimension = new Dimension(this.session, {
name: 'Dimensions',
collapsed: true,
closeable: false,
parent: this.id,
});
this.dependencies.push(this.dimension);
}
onEnabled() {
this.addElement(
new Group(this, {
format: Group.Format.Table,
children: [
this.makeElement(Input, 'extrudeDir', { format: Input.Format.Vector }),
this.makeElement(Slider, 'extrudeType', { max: 2, positions: { ...['up', 'down', 'symmetric'] } }),
this.makeElement(Checkbox, 'invertExtrusion'),
this.makeElement(Input, 'limit1'),
this.makeElement(Input, 'limit2'),
this.makeElement(Slider, 'taperAngle', {
max: 360,
step: 10,
positions: { 0: '0°', 90: '90°', 180: '180°', 270: '270°', 360: '360°' },
}),
new Selection(this, { name: 'sketch', types: ['SketcherMesh'] }),
],
}),
);
this.addElement(new Link(this, { value: this.dimension.id, collapsable: true }));
this.dimension.enabled = true;
}
onDisabled() {
this.dimension.enabled = false;
this.destroyElements();
}
getDescriptor(memberName) {
const scope = this, id = this.feature;
return {
get value() {
return scope.tree[id].members[memberName].value;
},
set value(value) {
const m = JSON.stringify(memberName), v = JSON.stringify(value), f = 1;
scope.connection.execute(`_C.CADApplication.SetExpressions(${id}, [${m}], [${v}], ${f});`);
},
};
}
makeElement(elementClass, memberName, options = {}) {
const element = new elementClass(this, { name: memberName, ...options });
const descriptor = this.getDescriptor(memberName);
// Convert between the type of a value in the tree (t) and the type of element.value (e)
let e2t = e => e, t2e = t => t;
if (elementClass === Input && element.type !== Input.Format.Vector) (e2t = Number), (t2e = String);
else if (elementClass === Input && element.type === Input.Format.Vector)
(e2t = e => e.split(',').map(Number)), (t2e = t => t.map(String).join(','));
else if (elementClass === Checkbox) (e2t = Number), (t2e = Boolean);
element.value = t2e(descriptor.value);
element.observe(state => state.value, e => (descriptor.value = e2t(e)));
return element;
}
}