UNPKG

awv3

Version:
82 lines (75 loc) 3.14 kB
import { Dropdown, Input, Group } from '../../session/elements'; import { buildFeaturePath } from '../../session/helpers'; import Plugin from '../../session/plugin'; import Graphics from './graphics'; const resources = ['plane'].reduce((prev, item) => ({ ...prev, [item]: require('!!url-loader!awv3-icons/32x32/' + item + '.png') }), {}); export default class Workplane extends Plugin { static persistent = true; constructor(session, args) { super(session, { type: 'Workplane', icon: 'plane', resources, ...args }); this.etype = new Dropdown(this, { name: 'Type', value: '', children: [ '0 WP_XYPLANE', '1 WP_XZPLANE', '2 WP_YZPLANE', '3 WP_USERDEFINED', '4 WP_FIXED', '10 WP_PLANE', '11 WP_EDGEPOINT', '12 WP_3POINTS', '13 WP_POINTNORMAL', '14 WP_POINTFACE', '15 WP_2CURVES', '16 WP_LINEPLANEANGLE' ] }); this.origin = new Input(this, { name: 'Origin', value: '0,0,0', format: Input.Format.Vector }); this.normal = new Input(this, { name: 'Normal', value: '1,0,0', format: Input.Format.Vector }); this.offset = new Input(this, { name: 'Offset', value: '0' }); this.addElement( new Group(this, { format: Group.Format.Table, children: [this.etype, this.origin, this.normal, this.offset] }) ); this.workplane = new Graphics(); this.pool.measurable = true; this.pool.add(this.workplane); this.unsubscribe = this.connection.observe(state => state.tree[this.feature], state => this.onChange(state), { fireOnStart: true, unsubscribeOnUndefined: true, }); } onDestroyed() { this.unsubscribe(); this.workplane.destroy(); } onEnabled() { this.apply.observe( state => state.lastEvent, async event => { const path = buildFeaturePath(this.tree, this.feature); // SetPosition must be called last because it recalculates curPosition this.connection.execute( `${path}.SetType(${Number.parseInt(this.etype.value)}); ${path}.SetNormal({${this.normal.value}}); ${path}.SetOffset(${this.offset.value}); ${path}.SetPosition({${this.origin.value}});` ); } ); } onChange(state) { const type = String(state.members.Type.value); this.etype.value = this.etype.children.find(x => x.startsWith(type)) || type; this.origin.value = state.members.Position.value.join(','); this.normal.value = state.members.Normal.value.join(','); this.offset.value = state.members.Offset.value; this.workplane.updateFromState(state); this.pool.view.invalidate(); this.workplane.visible = Boolean(state.visible); this.connection.updateView(); } }