awv3
Version:
⚡ AWV3 embedded CAD
195 lines (168 loc) • 8.17 kB
JavaScript
import { bindActionCreators } from 'redux';
import { actions as connectionActions } from '../../session/store/connections';
import { actions as elementActions } from '../../session/store/elements';
import Plugin from '../../session/plugin';
import Dimension from '../dimension/';
import { buildFeaturePath } from '../../session/helpers';
import {
Spacer,
Group,
Button,
Input,
Label,
Selection,
Checkbox,
Dropdown,
Console,
Divider,
} from '../../session/elements';
const resources = ['isometric'].reduce(
(prev, item) => ({ ...prev, [item]: require('!!url-loader!awv3-icons/32x32/' + item + '.png') }),
{},
);
export default class Fillet extends Plugin {
constructor(session, args) {
super(session, { type: 'Fillet', icon: 'isometric', selectionVisible: true, resources, ...args });
this.actions = bindActionCreators({ execute: connectionActions.execute }, this.session.store.dispatch);
this.elementSelection = new Selection(this, { name: 'Target', types: ['Mesh', 'LineSegments'], limit: 1, visible: this.selectionVisible });
this.elementType = new Dropdown(this, { name: 'Type', children: ['Fillet', 'Chamfer'], value: 'Fillet' });
this.elementRadius = new Input(this, { name: 'Radius', visible: false, hint: 'expression' });
this.elementVertical = new Input(this, { name: 'Vertical', visible: false, hint: 'expression' });
this.elementHorizontal = new Input(this, { name: 'Horizontal', visible: false, hint: 'expression' });
this.elementCreate = new Button(this, { name: 'Create', visible: false });
this.fillets = new Group(this, {
name: "Fillets",
format: Group.Format.Collapse,
});
this.addElement(
new Group(this, {
name: "Create Fillet",
format: Group.Format.Collapse,
children: [
new Group(this, {
format: Group.Format.Table,
children: [
this.elementSelection,
this.elementType,
this.elementRadius,
this.elementVertical,
this.elementHorizontal
],
}),
this.elementCreate
]
}),
new Spacer(this),
this.fillets,
);
}
collectFillets() {
let part = this.tree[this.tree.root];
if (part && part.features) {
let oldChildren = this.fillets.children;
this.fillets.children = part.features
.filter(feature => this.tree[feature].class.indexOf('Fillet') != -1)
.map(feature => {
let label = new Label(this, { value: this.tree[feature].name });
label.observe(state => state.lastEvent, event => console.log(event))
let group = new Group(this, {
format: Group.Format.Rows,
children: [
label,
new Input(this, { value: this.tree[feature].members.radius.value, flex: 2 }),
new Button(this, { name: "Update" }),
new Button(this, { name: "Delete", color: 'black' })
]
});
return group.id;
});
oldChildren.forEach(element => this.session.dispatch(elementActions.unregister(element)));
}
}
onEnabled() {
this.collectFillets();
this.connection.observe(
state => state.tree.root,
root => this.collectFillets(),
{ unsubscribeOnUndefined: true, fireOnStart: true }
)
this.elementType.observe(
state => state.value,
value => {
this.elementRadius.visible = value === 'Fillet';
this.elementVertical.visible = value === 'Chamfer';
this.elementHorizontal.visible = value === 'Chamfer';
},
{ fireOnStart: true },
);
this.elementSelection.observe(
state => state.children,
() => {
const hit = this.connection.pool.findMaterial(material =>
material.meta && material.meta.id === this.elementSelection.children[0]);
if (hit) {
let operation = this.tree[hit.material.meta.operationId];
if (operation &&
(operation.class.indexOf('Fillet') != -1 || operation.class.indexOf('Chamfer') != -1)) {
// We're in a previously created fillet feature
this.elementType.value = operation.class.indexOf('Fillet') ? 'Fillet' : 'Chamfer';
this.elementRadius.value = operation.members.radius.value;
this.elementCreate.visible = false;
return;
}
// We have a positive hit, but no fillet yet
this.elementCreate.visible = true;
this.elementRadius.value = 1;
return;
}
this.elementCreate.visible = false;
this.elementRadius.value = undefined;
},
);
this.elementRadius.observe(
state => state.lastEvent,
async event => {
if (event.key === 'Enter') {
const hit = this.connection.pool.findMaterial(material =>
material.meta && material.meta.id === this.elementSelection.children[0]);
if (hit) {
let operation = this.tree[hit.material.meta.operationId];
if (operation && this.elementRadius.value > 0 &&
(operation.class.indexOf('Fillet') != -1 || operation.class.indexOf('Chamfer') != -1)) {
await this.actions.execute(this.connection.id,
`_C.CADApplication.SetFeatureParams(${operation.id}, [${this.elementRadius.value}], 1);`);
const { material } = this.connection.pool.findMaterial(material =>
material.meta && material.meta.operationId === operation.id);
this.elementSelection.children = [material.meta.id];
}
}
}
},
);
this.elementCreate.observe(
state => state.lastEvent,
async () => {
const hit = this.connection.pool.findMaterial(material =>
material.meta && material.meta.id === this.elementSelection.children[0]);
if (hit && this.elementRadius.value > 0) {
let id = hit.material.meta.originalId;
await this.actions.execute(this.connection.id,
`_C.CADApplication.NewFeature(${this.tree.root}, "CC_ConstantRadiusFillet", "Fillet");`);
const operation = this.tree[this.root.features[this.root.features.length - 1]].id;
await this.actions.execute(this.connection.id,
`_C.CADApplication.FeatureBuildReferences(${operation}, [${id}]);`);
if (!this.elementRadius.value) this.elementRadius.value = 0.1;
await this.actions.execute(this.connection.id,
`_C.CADApplication.SetFeatureParams(${operation}, [${this.elementRadius.value}], 1);`);
const { material } = this.connection.pool.findMaterial(material =>
material.meta && material.meta.operationId === operation);
this.elementSelection.children = [material.meta.id];
this.collectFillets();
}
},
);
}
onDisabled() {
this.resetElements();
}
}