UNPKG

awv3

Version:
511 lines (478 loc) 19.5 kB
/** * @module Properties plugin for awv3 */ import * as THREE from 'three'; import Plugin from '../../session/plugin'; import { Group, Button, Input, Selection, Checkbox } from '../../session/elements'; /** * Class representing the CC_Bending class with equal member-names. * bendAngle = params[0]; * preAngle = params[1]; * bendRadiusDB = params[2]; * bendRadius = params[3]; * color = params[4]; * lineType = params[5]; * bendDeduction = params[6]; * upperTool = params[7]; * lowerTool = params[8]; * bendType = params[9]; * bendMethod = params[10]; * bendTechnique = params[11]; */ class BendingParameters { constructor(bendingObject = undefined) { if (bendingObject) { this.bendAngle = bendingObject.members.bendAngle.value; this.preAngle = bendingObject.members.preAngle.value; this.bendRadiusDB = bendingObject.members.bendRadiusDB.value; this.bendRadius = bendingObject.members.bendRadius.value; this.color = bendingObject.members.color.value; this.lineType = bendingObject.members.lineType.value; this.bendDeduction = bendingObject.members.bendDeduction.value; this.upperTool = bendingObject.members.upperTool.value; this.lowerTool = bendingObject.members.lowerTool.value; this.bendType = bendingObject.members.bendType.value; this.bendMethod = bendingObject.members.bendMethod.value; this.bendTechnique = bendingObject.members.bendTechnique.value; } else { this.bendAngle = 0; this.preAngle = 0; this.bendRadiusDB = 0; this.bendRadius = 0; this.color = 3; this.lineType = 0; this.bendDeduction = 0; this.upperTool = ''; this.lowerTool = ''; this.bendType = 0; this.bendMethod = 0; this.bendTechnique = 0; } } getParameterString() { const str = `${this.angle}, ` + `${this.preAngle}, ` + `${this.bendRadiusDB}, ` + `${this.radius}, ` + `${this.color}, ` + `${this.lineType}, ` + `${this.bendDeduction}, ` + `"${this.upperTool}", ` + `"${this.lowerTool}", ` + `${this.bendType}, ` + `${this.bendMethod}, ` + `${this.bendTechnique}`; return str; } } /** * Class Properties is based on awv3's plugin architecture. It can be used to * define bend- and signature-lines in the 2D view. */ export default class Properties extends Plugin { constructor(session, { name = 'Properties', icon = 'align center' } = {}) { super(session, { type: 'Properties', name, icon }); this.bendLineColor = new THREE.Color(0x00ff00); this.signatureLineColor = new THREE.Color(0xff0000); this.contourLineColor = new THREE.Color(0x000000); // constant settings by Blexon this.foldUpAngle = 180; this.foldUpPreAngle = 30; this.foldDownAngle = -180; this.foldDownPreAngle = -30; this.foldRadius = 0.05; // Thomas: (Spaltbreite des Falz = 0.1 (fix) => RadiusStart = Spaltbreite / 2) this.foldType = 1; this.foldMethod = 2; this.foldTechnique = 1; // selection this.selection = new Selection(this, { name: 'Lines', limit: 1, types: ['LineSegments'], active: true }); // bending this.isBendLine = new Checkbox(this, { name: 'Bendline', value: false, Tooltip: 'define as bend line', visible: true }); this.foldUp = new Checkbox(this, { name: 'Fold up', value: false, Tooltip: 'fold downwards', visible: false }); this.foldDown = new Checkbox(this, { name: 'Fold down', value: false, Tooltip: 'fold upwards', visible: false }); this.bend = new Checkbox(this, { name: 'Bend', value: true, Tooltip: 'bend', visible: false }); this.kFactor = new Input(this, { name: 'k-Factor', Tooltip: 'The k-Factor is...', visible: true }); this.radius = new Input(this, { name: 'Inner radius', Tooltip: 'The inner radius is...', visible: true }); this.angle = new Input(this, { name: 'Angle', visible: false, optional: false }); this.bendDeduction = new Input(this, { name: 'Bend deduction', visible: false, optional: false }); this.selectionGroup = new Group(this, { name: 'Selection', children: [this.selection] }); this.upperTool = new Input(this, { name: 'Upper Tool', readonly: true }); this.lowerTool = new Input(this, { name: 'Lower Tool', readonly: true }); this.bending = new Group(this, { name: 'Bending', format: Group.Format.Table, children: [ this.upperTool, this.lowerTool, this.kFactor, this.radius, this.isBendLine, this.foldUp, this.foldDown, this.bend, this.angle, this.bendDeduction ] }); this.bendProperties = [this.foldUp, this.foldDown, this.bend, this.angle, this.bendDeduction]; this.foldLock = false; // laser signature this.isLaserSignature = new Checkbox(this, { name: 'Lasersignature', value: false, Tooltip: 'Define as signature line', visible: true }); this.laserGroup = new Group(this, { name: 'Laser', format: Group.Format.Table, children: [this.isLaserSignature] }); // buttons this.acceptButton = new Button(this, { name: 'Accept', color: 'blue' }); this.deleteButton = new Button(this, { name: 'Delete', color: 'red' }); // order of elements this.addElement(this.selectionGroup); this.addElement(this.deleteButton); this.addElement(this.bending); this.addElement(this.acceptButton); this.addElement(this.laserGroup); } /** * Called when the plugin is enabled. */ onEnabled() { this.resetElements(); // selection changed this.selection.observe(state => state.children, value => { const selectedElements = this.session.selector.getSelectedElements(); if (selectedElements.length > 0) { const lineId = this.getOwnerId(selectedElements[0]); if (lineId) { // get information from line-object const parentId = this.session.tree[lineId].parent; if (this.session.tree[parentId].class === 'CC_Bending') { const bending = this.session.tree[parentId]; if (bending) { this.isBendLine.value = true; this.upperTool.value = bending.members.upperTool.value; this.lowerTool.value = bending.members.lowerTool.value; this.radius.value = bending.members.bendRadius.value; if ( bending.members.bendType.value === 1 && bending.members.bendMethod.value === 2 && bending.members.bendTechnique.value === 1 ) { if (bending.members.preAngle.value > 0) { // 30 degrees this.foldUp.value = true; this.radius.value = bending.members.bendRadius.value; } else { this.foldDown.value = true; // -30 degrees this.radius.value = bending.members.bendRadius.value; } } else { this.bend.value = true; } this.angle.value = bending.members.bendAngle.value; this.bendDeduction.value = bending.members.bendDeduction.value; } else { this.resetProperties(); } // console.log('bending', bending) } else if (this.session.tree[parentId].class === 'CC_SignatureContainer') { this.isLaserSignature.value = true; } else { this.resetProperties(); } } } else { this.resetProperties(); } }); // bending this.isBendLine.observe(state => state.value, value => { this.bendProperties.forEach(element => element.visible = value); if (value) { const bending = new BendingParameters(); this.kFactor.value = 0.0; this.radius.value = bending.bendRadius; this.angle.value = bending.bendAngle; this.bendDeduction.value = bending.bendDeduction; } }); this.foldUp.observe(state => state.value, value => { if (!this.foldLock && value) { this.foldLock = true; this.radius.value = this.foldRadius; this.foldDown.value = false; this.bend.value = false; this.foldLock = false; } }); this.foldDown.observe(state => state.value, value => { if (!this.foldLock && value) { this.foldLock = true; this.radius.value = this.foldRadius; this.foldUp.value = false; this.bend.value = false; this.foldLock = false; } }); this.bend.observe(state => state.value, value => { if (!this.foldLock && value) { this.foldLock = true; this.bendDeduction.visible = true; this.angle.visible = true; this.foldDown.value = false; this.foldUp.value = false; this.foldLock = false; } if (!value) { this.bendDeduction.visible = false; this.angle.visible = false; } }); this.kFactor.observe(state => state.value, value => { console.log('kFactor changed', value); }); this.radius.observe(state => state.value, value => { console.log('radius changed', value); }); this.angle.observe(state => state.value, value => { console.log('angle changed', value); }); this.bendDeduction.observe(state => state.value, value => { console.log('bendDeduction changed', value); }); // laser this.isLaserSignature.observe(state => state.value, value => { if (value) { this.isBendLine.value = false; } }); // buttons this.acceptButton.observe(state => state.lastEvent, event => { if (event.type === 'click') { let params = ''; console.log('accept values'); // get bending const selectedElements = this.session.selector.getSelectedElements(); if (selectedElements.length === 0) { return; } const lineId = this.getOwnerId(selectedElements[0]); const lineIds = this.getOwnerIds(selectedElements); if (lineId === undefined) { return; } // make all lines un-dashed selectedElements.forEach(element => { element.gapSize = 0; element.dashSize = 1; }); // TODO validate const sketchId = this.getSketchId(lineId); if (this.isBendLine.value) { // get information from line-object let bending; const parentId = this.session.tree[lineId].parent; if (this.session.tree[parentId].class === 'CC_Bending') { bending = new BendingParameters(this.session.tree[parentId]); } else { bending = new BendingParameters(); } if (this.bend.value) { bending.angle = this.angle.value; bending.radius = this.radius.value; bending.bendDeduction = this.bendDeduction.value; } else if (this.foldUp.value) { bending.angle = this.foldUpAngle; bending.preAngle = this.foldUpPreAngle; bending.radius = this.foldRadius; bending.bendDeduction = this.bendDeduction.value; bending.bendType = this.foldType; bending.bendMethod = this.foldMethod; bending.bendTechnique = this.foldTechnique; } else if (this.foldDown.value) { bending.angle = this.foldDownAngle; bending.preAngle = this.foldDownPreAngle; bending.radius = this.foldRadius; bending.bendDeduction = this.bendDeduction.value; bending.bendType = this.foldType; bending.bendMethod = this.foldMethod; bending.bendTechnique = this.foldTechnique; } params = bending.getParameterString(); this.session.selector.unselectElements(selectedElements); console.log(`_C.SheetDesigner.ChangeLinesToBendLines(${sketchId}, [${lineIds}], [${params}]);`); this.connection.execute( `_C.SheetDesigner.ChangeLinesToBendLines(${sketchId}, [${lineIds}], [${params}]);` ); selectedElements.forEach(element => element.animate({ color: this.bendLineColor.clone() }).start(500)); // make bend lines with negative bending angle dashed if (bending.angle < 0) selectedElements.forEach(element => { element.gapSize = 2; element.dashSize = 2; }); } else if (this.isLaserSignature.value) { this.session.selector.unselectElements(selectedElements); console.log(`_C.SheetDesigner.ChangeLinesToSignatureLines(${sketchId}, [${lineIds}]);`); this.connection.execute(`_C.SheetDesigner.ChangeLinesToSignatureLines(${sketchId}, [${lineIds}]);`); selectedElements.forEach(element => element.animate({ color: this.signatureLineColor.clone() }).start(500)); } else { this.session.selector.unselectElements(selectedElements); console.log(`_C.SheetDesigner.ChangeLinesToContourLines(${sketchId}, [${lineIds}]);`); this.connection.execute(`_C.SheetDesigner.ChangeLinesToContourLines(${sketchId}, [${lineIds}]);`); selectedElements.forEach(element => element.animate({ color: this.contourLineColor.clone() }).start(500)); } } }); this.deleteButton.observe(state => state.lastEvent, event => { if (event.type === 'click') { const ownerIds = this.getOwnerIds(this.session.selector.getSelectedElements()); this.connection.execute(`_C.SheetDesigner.DeleteLines([${ownerIds}]);`); this.session.selector.removeAll(); console.log(`_C.SheetDesigner.DeleteLines([${ownerIds}]);`); } }); // Plugins can observe the whole session including the tree // The second arg to observe fires on init // this.session.observe(state => state.tree.features, features => { // this.dropdown.children = features.map(item => this.session.tree[item].name) // }, true); } /** * Get the ids of the owner of the selected elements. * @param {array} selectedElements - array of materials with meta-info * @return {array} - owners of the selected elements */ getOwnerIds(selectedElements) { const ownerIds = []; selectedElements.forEach(element => { element.meta && element.meta.owner && ownerIds.push(element.meta.owner); }); return ownerIds; } getOwnerId(selectedElement) { let id; if (selectedElement.meta && selectedElement.meta.owner) { id = selectedElement.meta.owner; } return id; } getOwners(selectedElements) { const owners = []; selectedElements.forEach(element => { const owner = this.getOwner(element); owner && owner.push(owner); }); return owners; } getOwner(element) { let owner; if (element.meta && element.meta.owner) { owner = this.session.tree[element.meta.owner]; } return owner; } /** * Climbs up the object tree to find the sketch this line belongs to. * @return undefined, if no sketch was found. */ getSketchId(lineId) { let sketchId; let parentId = this.session.tree[lineId] ? this.session.tree[lineId].parent : undefined; while (parentId !== undefined) { if (this.session.tree[parentId]) { if (this.session.tree[parentId].class === 'CC_Sketch') { sketchId = parentId; break; } else { parentId = this.session.tree[parentId].parent; } } else { parentId = undefined; } } return sketchId; } resetProperties() { this.lowerTool.reset(); this.upperTool.reset(); this.kFactor.reset(); this.radius.reset(); this.isBendLine.reset(); this.foldUp.reset(); this.foldDown.reset(); this.bend.reset(); this.angle.reset(); this.bendDeduction.reset(); this.isLaserSignature.reset(); } /** * Called when the plugin is being disabled. Clean up * plugin-specific things in here. */ onDisabled() {} }