awv3
Version:
⚡ AWV3 embedded CAD
407 lines (361 loc) • 15 kB
JavaScript
import _extends from "@babel/runtime/helpers/extends";
import _inheritsLoose from "@babel/runtime/helpers/inheritsLoose";
import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized";
var _dec, _class, _class2, _temp2, _dec2, _class3, _class4, _temp4;
import * as THREE from 'three';
import React from 'react';
import { connect } from 'react-redux';
import PropTypes from 'prop-types';
import Plugin from '../../session/plugin';
import { actions as globalActions } from '../../session/store/globals';
import { emptyObject, emptyArray } from '../../session/helpers';
import Hud from '../../core/hud';
import Graphics from './graphics';
import { PositionHandle, ValueHandle } from './handle';
var Dimension = (_dec = connect(function (state, _ref) {
var _connection, _connection2;
var connection = _ref.connection,
dimensionId = _ref.dimensionId,
instance = _ref.instance;
connection = state.connections[connection];
return {
day: state.globals.day,
dimension: (_connection = connection) === null || _connection === void 0 ? void 0 : _connection.tree[dimensionId],
csys: (_connection2 = connection) === null || _connection2 === void 0 ? void 0 : _connection2.tree[instance].coordinateSystem
};
}), _dec(_class = (_temp2 = _class2 =
/*#__PURE__*/
function (_React$Component) {
_inheritsLoose(Dimension, _React$Component);
function Dimension() {
var _temp, _this;
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
return (_temp = _this = _React$Component.call.apply(_React$Component, [this].concat(args)) || this, Object.defineProperty(_assertThisInitialized(_this), "setInputName", {
configurable: true,
enumerable: true,
writable: true,
value: function value(inputName) {
return _this.setState({
inputName: inputName
});
}
}), Object.defineProperty(_assertThisInitialized(_this), "setInputValue", {
configurable: true,
enumerable: true,
writable: true,
value: function value(inputValue) {
return _this.setState({
inputValue: inputValue
});
}
}), Object.defineProperty(_assertThisInitialized(_this), "setInfoExpression", {
configurable: true,
enumerable: true,
writable: true,
value: function value(e) {
return e.key === 'Enter' && (_this.info.expression = _this.state.inputValue);
}
}), Object.defineProperty(_assertThisInitialized(_this), "setInfoName", {
configurable: true,
enumerable: true,
writable: true,
value: function value(e) {
return e.key === 'Enter' && (_this.info.name = _this.state.inputName);
}
}), Object.defineProperty(_assertThisInitialized(_this), "setDimension", {
configurable: true,
enumerable: true,
writable: true,
value: function value() {
return _this.context.plugin.connection.execute("VAR o;o = CADH_RealToId(" + _this.info.dimension.id + ");o.SetIsDriven(" + (!!_this.info.driven ? 0 : 1) + ", 1);");
}
}), _temp) || _assertThisInitialized(_this);
}
var _proto = Dimension.prototype;
_proto.componentWillMount = function componentWillMount() {
this.info = this.getDimensionInfo(this.props);
this.setState({
inputValue: this.info.value,
inputName: this.info.name
});
this.graphics = Graphics(this.info.dimension.class, this.context.plugin);
this.context.plugin.hud.scene.add(this.graphics);
this.graphics.updateFromState(this.info);
};
_proto.componentWillUnmount = function componentWillUnmount() {
this.graphics && this.graphics.destroy();
};
_proto.componentWillReceiveProps = function componentWillReceiveProps(props) {
if (props.dimension) {
this.info = this.getDimensionInfo(props);
if (!this.graphics) this.graphics = Graphics(this.info.dimension.class, this.context.plugin);
this.graphics.updateFromState(this.info);
} else {
this.graphics.destroy();
this.graphics = undefined;
} // This is kind of a hack. Which value should "win"? The one that the user typed, or the one
// that comes through the wire from ClassCAD? The current solution overrides the input value.
this.setState(_extends({}, this.state, {
inputValue: this.info.value
}));
};
_proto.setExpression = function setExpression(target, name, expression, overrideFlag) {
var plugin = this.context.plugin;
var expressionSet = target.class === 'CC_ExpressionSet';
var flag = overrideFlag !== undefined ? overrideFlag : plugin.sketcher !== undefined ? 0 : plugin.recalc && expressionSet ? 1 : 2;
plugin.connection.execute("_C.CADApplication.SetExpressions(" + target.id + ",[\"" + name + "\"],[" + JSON.stringify(expression) + "]," + flag + ");");
plugin.afterSetCallback(target, name, expression, overrideFlag);
};
_proto.setMember = function setMember(target, name, value) {
this.context.plugin.connection.execute("VAR o; o = CADH_RealToId(" + target.id + "); o." + name + " = " + JSON.stringify(value) + ";");
};
_proto.getDimensionInfo = function getDimensionInfo(props) {
var _dimension$members$is, _dimension$members$is2, _plugin$tree$dimensio, _plugin$tree$dimensio2;
var scope = this;
var plugin = this.context.plugin;
var dimension = props.dimension,
expressions = props.expressions,
feature = props.feature,
search = props.search,
instanceCsys = props.instanceCsys;
dimension.previewValue = undefined;
var master = dimension.members.master ? plugin.tree[dimension.members.master.value] : dimension;
var driven = (_dimension$members$is = (_dimension$members$is2 = dimension.members.isDriven) === null || _dimension$members$is2 === void 0 ? void 0 : _dimension$members$is2.value) !== null && _dimension$members$is !== void 0 ? _dimension$members$is : {};
var linkName = master.class === 'CC_Extrusion' ? dimension.name : master.members.userValue ? 'userValue' : 'value';
var expression = master.members[linkName].expression;
var angle = expression.match(/^ *a_r\((.*)\) *$/);
if (angle) expression = angle[1];
var matches = expression.match(/^ *ExpressionSet\.(\w+) *$/);
var target = matches ? expressions : master;
var memberName = matches ? matches[1] : linkName;
var owner = (_plugin$tree$dimensio = (_plugin$tree$dimensio2 = plugin.tree[dimension.parent].members.owner) === null || _plugin$tree$dimensio2 === void 0 ? void 0 : _plugin$tree$dimensio2.value) !== null && _plugin$tree$dimensio !== void 0 ? _plugin$tree$dimensio : {};
var visible = (!search || dimension.members.paramName.value.toLowerCase().indexOf(search) != -1) && (!feature || owner === feature);
var setMember = this.setMember.bind(this);
var setExpression = this.setExpression.bind(this);
return {
instanceCsys: plugin.tree[props.instance].coordinateSystem,
dimension: dimension,
angle: angle,
driven: driven,
visible: visible,
get name() {
return dimension.members.paramName.value;
},
set name(value) {
setMember(dimension, 'paramName', value);
},
get value() {
return target.members[memberName].value;
},
set value(value) {
setMember(target, memberName, value);
},
get expression() {
return target.members[memberName].expression || target.members[memberName].value;
},
set expression(value) {
setExpression(target, memberName, value);
},
set dimPt(value) {
setExpression(dimension, 'dimPt', value
/*, 0*/
);
}
};
};
_proto.render = function render() {
if (!this.info) return null;
var _info = this.info,
dimension = _info.dimension,
visible = _info.visible,
name = _info.name,
value = _info.value,
driven = _info.driven;
return React.createElement("group", {
visible: visible,
format: "Rows"
}, React.createElement("input", {
value: this.state.inputName,
onValue: this.setInputName,
onLastEvent: this.setInfoName,
readonly: !!driven,
flex: 1
}), React.createElement("input", {
format: "Number",
value: this.state.inputValue,
onValue: this.setInputValue,
onLastEvent: this.setInfoExpression,
readonly: !!driven,
flex: 2
}), React.createElement("button", {
name: driven ? '🔒' : '🔓',
onLastEvent: this.setDimension,
flex: 0
}));
};
return Dimension;
}(React.Component), Object.defineProperty(_class2, "contextTypes", {
configurable: true,
enumerable: true,
writable: true,
value: {
plugin: PropTypes.object
}
}), _temp2)) || _class);
var Root = (_dec2 = connect(function (state, props) {
var _connection$tree, _connection$activeRoo, _state$plugins$props$, _state$plugins$parent, _tree$tree$root$dimen, _tree$root$dimensions;
var activeConnection = state.globals.activeConnection;
var connection = state.connections[activeConnection];
var tree = (_connection$tree = connection === null || connection === void 0 ? void 0 : connection.tree) !== null && _connection$tree !== void 0 ? _connection$tree : emptyObject;
var root = (_connection$activeRoo = connection === null || connection === void 0 ? void 0 : connection.activeRoot) !== null && _connection$activeRoo !== void 0 ? _connection$activeRoo : tree.root;
var parent = (_state$plugins$props$ = state.plugins[props.id]) === null || _state$plugins$props$ === void 0 ? void 0 : _state$plugins$props$.parent;
return {
connection: activeConnection,
root: root,
feature: (_state$plugins$parent = state.plugins[parent]) === null || _state$plugins$parent === void 0 ? void 0 : _state$plugins$parent.feature,
rootDimensions: (_tree$tree$root$dimen = tree[tree.root].dimensions) !== null && _tree$tree$root$dimen !== void 0 ? _tree$tree$root$dimen : emptyArray,
dimensions: (_tree$root$dimensions = tree[root].dimensions) !== null && _tree$root$dimensions !== void 0 ? _tree$root$dimensions : emptyArray,
expressions: tree[tree[root].expressionSet]
};
}), _dec2(_class3 = (_temp4 = _class4 =
/*#__PURE__*/
function (_React$Component2) {
_inheritsLoose(Root, _React$Component2);
function Root() {
var _temp3, _this2;
for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
args[_key2] = arguments[_key2];
}
return (_temp3 = _this2 = _React$Component2.call.apply(_React$Component2, [this].concat(args)) || this, Object.defineProperty(_assertThisInitialized(_this2), "state", {
configurable: true,
enumerable: true,
writable: true,
value: {
search: ''
}
}), Object.defineProperty(_assertThisInitialized(_this2), "search", {
configurable: true,
enumerable: true,
writable: true,
value: function value(search) {
return _this2.setState({
search: search
});
}
}), Object.defineProperty(_assertThisInitialized(_this2), "clearSearch", {
configurable: true,
enumerable: true,
writable: true,
value: function value() {
return _this2.setState({
search: ''
});
}
}), _temp3) || _assertThisInitialized(_this2);
}
var _proto2 = Root.prototype;
_proto2.render = function render() {
var _tree$tree$root$insta, _tree$tree$root$insta2;
var plugin = this.context.plugin;
var search = this.state.search;
var _props = this.props,
connection = _props.connection,
root = _props.root,
feature = _props.feature,
expressions = _props.expressions,
dimensions = _props.dimensions,
rootDimensions = _props.rootDimensions;
var commons = {
feature: feature,
search: search,
expressions: expressions,
connection: connection
};
var hud = plugin.hud;
var tree = plugin.tree;
var itemRef = (_tree$tree$root$insta = (_tree$tree$root$insta2 = tree[tree.root].instances) === null || _tree$tree$root$insta2 === void 0 ? void 0 : _tree$tree$root$insta2.find(function (instance) {
return tree[instance].link === root;
})) !== null && _tree$tree$root$insta !== void 0 ? _tree$tree$root$insta : root; // This will filter a set of dimensions
// Assembly offsets will link to their target via drivenComponent
// We use this to compose a set that contains both instance dimensions as well as suitable offsets
var dims = (tree[root].class !== 'CC_Assembly' ? dimensions.map(function (id) {
return {
id: id,
instance: itemRef
};
}) : []).concat(dimensions !== rootDimensions ? rootDimensions.filter(function (dim) {
return tree[tree[dim].members.drivenComponent.value].link === root;
}).map(function (id) {
return {
id: id,
instance: tree.root
};
}) : []);
return React.createElement("group", null, React.createElement("group", {
format: "Rows"
}, React.createElement("input", {
value: search,
placeholder: "Search...",
onValue: this.search
}), React.createElement("button", {
name: "\u2718",
flex: 0,
disabled: !search,
onLastEvent: this.clearSearch
})), React.createElement("spacer", null), dims.sort(function (dim1, dim2) {
return plugin.tree[dim1.id].members.paramName.value.localeCompare(plugin.tree[dim2.id].members.paramName.value);
}).map(function (dim) {
return React.createElement(Dimension, _extends({
key: dim.id,
dimensionId: dim.id,
instance: dim.instance
}, commons));
}));
};
return Root;
}(React.Component), Object.defineProperty(_class4, "contextTypes", {
configurable: true,
enumerable: true,
writable: true,
value: {
plugin: PropTypes.object
}
}), _temp4)) || _class3);
var _default =
/*#__PURE__*/
function (_Plugin) {
_inheritsLoose(_default, _Plugin);
function _default(session, args) {
var _this3;
_this3 = _Plugin.call(this, session, _extends({
type: 'Dimensions',
icon: 'dimension',
recalc: true,
handlePrototypes: [PositionHandle, ValueHandle],
resources: {
dimension: require('!!url-loader!awv3-icons/32x32/dimension.png')
}
}, args)) || this;
_this3.selector = undefined;
_this3.afterSetCallback = function () {};
return _this3;
}
var _proto3 = _default.prototype;
_proto3.render = function render() {
return React.createElement(Root, {
id: this.id
});
};
_proto3.onEnabled = function onEnabled() {
this.hud = new Hud(this.view);
this.view.addHud(this.hud);
};
_proto3.onDisabled = function onDisabled() {
this.view.removeHud(this.hud);
this.hud.destroy();
};
return _default;
}(Plugin);
export { _default as default };