UNPKG

awv3

Version:
266 lines (225 loc) 7.5 kB
import _extends from "@babel/runtime/helpers/extends"; import _createClass from "@babel/runtime/helpers/createClass"; import _inheritsLoose from "@babel/runtime/helpers/inheritsLoose"; import * as THREE from 'three'; import Ccbaseref from './ccbaseref'; import * as ConstraintType from './constraint/type'; var Ccref = /*#__PURE__*/ function (_Ccbaseref) { _inheritsLoose(Ccref, _Ccbaseref); function Ccref(sketcher, id) { var _this; _this = _Ccbaseref.call(this) || this; _this.sketcher = sketcher; _this.id = id; return _this; } var _proto = Ccref.prototype; _proto.isTrimmedPart = function isTrimmedPart() { var it = this; while (it.parent) { if (it.parent.isContainer()) return true; it = it.parent; } return false; }; _proto.updateGraphics = function updateGraphics() { if (this.graphics) { this.graphics.updateFromGeomParams(this.geomParams); this.sketcher.instantiated && this.graphics.updateFromSketcherAndId(this.sketcher, this.id); this.sketcher.refresh(); } }; _proto.updateGraphicsRecursive = function updateGraphicsRecursive() { for (var _iterator = this.descendants, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) { var _ref; if (_isArray) { if (_i >= _iterator.length) break; _ref = _iterator[_i++]; } else { _i = _iterator.next(); if (_i.done) break; _ref = _i.value; } var _obj = _ref; _obj.updateGraphics(); } }; _proto.getMember = function getMember(name, wrap) { if (wrap === void 0) { wrap = undefined; } var raw = this.state.members[name]; switch (wrap) { case 'Ccref': return new Ccref(this.sketcher, raw.value || raw.id); case 'Vector3': return new THREE.Vector3().fromArray(raw.value); default: return raw; } }; _proto.getNamedChild = function getNamedChild(childName) { return this.children.find(function (child) { return child.state.name === childName; }); }; _createClass(Ccref, [{ key: "graphics", get: function get() { return this.sketcher.graphics.get(this.id); } }, { key: "state", get: function get() { return this.sketcher.tree[this.id]; } }, { key: "pos", get: function get() { return this.getMember('pos', 'Vector3'); } }, { key: "class", get: function get() { return this.state.class; } }, { key: "entities", get: function get() { var _this2 = this; return this.state.members.entities.members.map(function (e) { return new Ccref(_this2.sketcher, e.value); }); } // entities, but with pairs of endpoints replaced by lines }, { key: "mergedEntities", get: function get() { var result = []; var lineToPoint = new Map(); for (var _iterator2 = this.entities, _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : _iterator2[Symbol.iterator]();;) { var _ref2; if (_isArray2) { if (_i2 >= _iterator2.length) break; _ref2 = _iterator2[_i2++]; } else { _i2 = _iterator2.next(); if (_i2.done) break; _ref2 = _i2.value; } var _entity = _ref2; var parent = _entity.parent; if (!parent.isLine()) result.push(_entity);else if (lineToPoint.delete(parent.id)) result.push(new Ccref(this.sketcher, parent.id));else lineToPoint.set(parent.id, _entity); } result.push.apply(result, lineToPoint.values()); return result; } }, { key: "parent", get: function get() { var parentId = this.state.parent; return parentId ? new Ccref(this.sketcher, parentId) : undefined; } }, { key: "children", get: function get() { var _this3 = this; return (this.state.children || []).map(function (child) { return new Ccref(_this3.sketcher, child); }); } // local-to-world transformation as a THREE matrix }, { key: "matrixWorld", get: function get() { var _state$coordinateSyst = this.state.coordinateSystem.map(function (row) { return new THREE.Vector3().fromArray(row); }), orig = _state$coordinateSyst[0], vx = _state$coordinateSyst[1], vy = _state$coordinateSyst[2], vz = _state$coordinateSyst[3]; return new THREE.Matrix4().makeBasis(vx, vy, vz).setPosition(orig); } }, { key: "geomParams", get: function get() { var _this4 = this; var geomParams = { coordinateSystem: this.matrixWorld, scale: this.sketcher.graphicScale }; if (this.sketcher.transientGeomParams.has(this.id)) return _extends({}, this.sketcher.transientGeomParams.get(this.id), geomParams); if (this.isConstraint()) { var textureName = Object.entries(ConstraintType).find(function (_ref3) { var name = _ref3[0], typeObject = _ref3[1]; return typeObject.type === _this4.class; })[0]; return _extends({}, geomParams, { coordinateSystem: this.parent.matrixWorld, // constraints have incorrect server-side csys class: this.class, entities: this.entities, texture: this.sketcher.textures[textureName], value: { type: (this.state.members.type || {}).value, userValue: (this.state.members.userValue || {}).value, value: (this.state.members.value || {}).value } }); } switch (this.class) { case 'CC_Point': return _extends({}, geomParams, { start: this.pos }); case 'CC_Line': return _extends({}, geomParams, { start: this.startPoint.pos, end: this.endPoint.pos }); case 'CC_Arc': return _extends({}, geomParams, { start: this.startPoint.pos, end: this.endPoint.pos, center: this.center.pos, clockwise: this.state.members.bulge.value < 0, radius: this.state.members.radius.value // ignored by most methods }); case 'CC_Circle': return _extends({}, geomParams, { center: this.center.pos, radius: this.state.members.radius.value }); default: return geomParams; } }, set: function set(geomParams) { if (geomParams !== undefined) this.sketcher.transientGeomParams.set(this.id, geomParams);else this.sketcher.transientGeomParams.delete(this.id); for (var _iterator3 = this.children, _isArray3 = Array.isArray(_iterator3), _i3 = 0, _iterator3 = _isArray3 ? _iterator3 : _iterator3[Symbol.iterator]();;) { var _ref4; if (_isArray3) { if (_i3 >= _iterator3.length) break; _ref4 = _iterator3[_i3++]; } else { _i3 = _iterator3.next(); if (_i3.done) break; _ref4 = _i3.value; } var _child = _ref4; _child.geomParams = geomParams !== undefined ? { start: geomParams[_child.pointType] } : undefined; } } }, { key: "pointType", get: function get() { return this.state.name.replace('Point', ''); } }]); return Ccref; }(Ccbaseref); export { Ccref as default };