awv3
Version:
⚡ AWV3 embedded CAD
251 lines (214 loc) • 8.12 kB
JavaScript
import _inheritsLoose from "@babel/runtime/helpers/inheritsLoose";
import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized";
import _extends from "@babel/runtime/helpers/extends";
var _dec, _class, _class2, _temp, _class3, _temp2, _initialiseProps;
import * as THREE from 'three';
import React from 'react';
import { connect } from 'react-redux';
import { actions as connectionActions } from 'awv3/session/store/connections';
import PropTypes from 'prop-types';
import { buildFeaturePath } from '../../session/helpers';
import Plugin from '../../session/plugin';
import Graphics from './graphics';
import Hud from '../../core/hud';
var resources = ['plane'].reduce(function (prev, item) {
var _extends2;
return _extends({}, prev, (_extends2 = {}, _extends2[item] = require('!!url-loader!awv3-icons/32x32/' + item + '.png'), _extends2));
}, {});
var Root = (_dec = connect(function (state, props) {
return {
feature: props.plugin.connection.tree[props.plugin.feature]
};
}), _dec(_class = (_temp = _class2 =
/*#__PURE__*/
function (_React$Component) {
_inheritsLoose(Root, _React$Component);
function Root(props, context) {
var _this;
_this = _React$Component.call(this) || this;
Object.defineProperty(_assertThisInitialized(_this), "setPosition", {
configurable: true,
enumerable: true,
writable: true,
value: function value(position) {
var positionArr = [];
if (typeof position === "string") {
positionArr = position.split(',');
position = {
x: positionArr[0],
y: positionArr[1],
z: positionArr[2]
};
} else {
positionArr = [position.x, position.y, position.z];
}
_this.setState({
position: positionArr
});
_this.context.plugin.translate({
position: position
});
}
});
Object.defineProperty(_assertThisInitialized(_this), "update", {
configurable: true,
enumerable: true,
writable: true,
value: function value() {
var plugin = _this.context.plugin;
var path = buildFeaturePath(plugin.tree, plugin.feature);
plugin.connection.execute(path + ".SetPosition({" + _this.state.position + "}, FALSE);").then(function () {
_this.context.plugin.sourceMatrix = _this.context.plugin.graphics.matrix;
});
}
});
_this.state = context.plugin.getFeatureData();
return _this;
}
var _proto = Root.prototype;
_proto.componentWillReceiveProps = function componentWillReceiveProps() {
this.setState(this.context.plugin.getFeatureData());
};
_proto.render = function render() {
var _this2 = this;
var _state = this.state,
activeSelector = _state.activeSelector,
position = _state.position;
return React.createElement("group", {
format: "Table"
}, React.createElement("input", {
name: "position",
value: position.join(','),
format: "Vector",
onValue: this.setPosition
}), React.createElement("selection", {
name: "Vertex",
active: activeSelector === 0,
types: ['Region'],
limit: 1,
onItems: function onItems(items) {
if (items.length) {
var _position = _this2.context.session.selector.selectedMaterials[0].meta.position;
_this2.setPosition(_position);
}
}
}), React.createElement("button", {
name: this.props.feature ? 'Update' : 'Create',
flex: 0,
color: "blue",
onLastEvent: this.update
}));
};
return Root;
}(React.Component), Object.defineProperty(_class2, "contextTypes", {
configurable: true,
enumerable: true,
writable: true,
value: {
session: PropTypes.object,
plugin: PropTypes.object
}
}), _temp)) || _class);
var Workpoint = (_temp2 = _class3 =
/*#__PURE__*/
function (_Plugin) {
_inheritsLoose(Workpoint, _Plugin);
function Workpoint(session, args) {
var _this3;
_this3 = _Plugin.call(this, session, _extends({
type: 'Point',
icon: 'plane',
resources: resources
}, args)) || this;
_initialiseProps.call(_assertThisInitialized(_this3));
_this3.graphics = new Graphics(_assertThisInitialized(_this3));
_this3.hud = new Hud(_this3.view);
_this3.view.addHud(_this3.hud);
_this3.hud.scene.add(_this3.graphics);
_this3.translate({
position: {
x: _this3.getFeatureData().position[0],
y: _this3.getFeatureData().position[1],
z: _this3.getFeatureData().position[2]
}
});
_this3.visibleSubscription = _this3.connection.observe(function (state) {
var _state$tree$_this3$fe, _state$tree$_this3$fe2;
return (_state$tree$_this3$fe = (_state$tree$_this3$fe2 = state.tree[_this3.feature]) === null || _state$tree$_this3$fe2 === void 0 ? void 0 : _state$tree$_this3$fe2.visible) !== null && _state$tree$_this3$fe !== void 0 ? _state$tree$_this3$fe : false;
}, function (visible) {
_this3.graphics.visible = Boolean(visible);
_this3.connection.updateView();
}, {
fireOnStart: true
});
return _this3;
}
var _proto2 = Workpoint.prototype;
_proto2.render = function render() {
return React.createElement(Root, _extends({
plugin: this
}, this.props));
};
_proto2.onEnabled = function onEnabled() {
this.graphics.setActive(true);
this.sourceMatrix = this.graphics.matrix;
this.view.invalidate();
};
_proto2.onDisabled = function onDisabled() {
this.graphics.setActive(false);
this.graphics.matrix = this.sourceMatrix;
this.graphics.matrixAutoUpdate = false;
this.view.invalidate();
};
_proto2.willBeDestroyed = function willBeDestroyed() {
this.hud.destroy();
};
_proto2.onDestroyed = function onDestroyed() {
this.graphics.destroy();
};
_proto2.getFeatureData = function getFeatureData() {
var _featureObj$members$T, _featureObj$members$D, _featureObj$members$P, _featureObj$members$R;
var featureObj = this.tree[this.feature];
return {
type: (_featureObj$members$T = featureObj.members.Type) === null || _featureObj$members$T === void 0 ? void 0 : _featureObj$members$T.value,
defined: (_featureObj$members$D = featureObj.members.Defined) === null || _featureObj$members$D === void 0 ? void 0 : _featureObj$members$D.value,
position: (_featureObj$members$P = featureObj.members.Position) === null || _featureObj$members$P === void 0 ? void 0 : _featureObj$members$P.value,
references: (_featureObj$members$R = featureObj.members.References) === null || _featureObj$members$R === void 0 ? void 0 : _featureObj$members$R.value
};
};
//TODO: copypaste from csys/index.js
_proto2.switchToFeature = function switchToFeature(featureId) {
featureId === undefined && (featureId = this.feature);
var tree = this.connection.tree;
var root = this.connection.activeRoot || tree.root;
var features = tree[root].features;
if (features.indexOf(featureId) != -1) this.store.dispatch(connectionActions.setActiveFeature(this.connection.id, featureId)); //TODO: else throw error.
};
return Workpoint;
}(Plugin), Object.defineProperty(_class3, "persistent", {
configurable: true,
enumerable: true,
writable: true,
value: true
}), _initialiseProps = function _initialiseProps() {
var _this4 = this;
Object.defineProperty(this, "translate", {
configurable: true,
enumerable: true,
writable: true,
value: function value(args) {
// Update local values
if (args) Object.entries(args).forEach(function (_ref) {
var key = _ref[0],
value = _ref[1];
return value && _this4[key] !== value && (_this4[key] = value);
});
var newMatrix = new THREE.Matrix4().setPosition(_this4.position);
_this4.graphics.matrix = newMatrix;
_this4.graphics.matrixAutoUpdate = false;
_this4.graphics.applyMatrix(_this4.pool.matrixWorld);
_this4.view.invalidate();
}
});
}, _temp2);
export { Workpoint as default };