awv3
Version:
AWV client CAD framework
1 lines • 21 kB
JavaScript
"use strict";function _interopRequireWildcard(e){if(e&&e.__esModule)return e;var t={};if(null!=e)for(var r in e)Object.prototype.hasOwnProperty.call(e,r)&&(t[r]=e[r]);return t.default=e,t}function _interopRequireDefault(e){return e&&e.__esModule?e:{default:e}}function transformWorldToScreen(e,t,r,n){r instanceof _three2.default.Object3D&&(r.updateMatrixWorld(),r=r.matrixWorld),r instanceof _three2.default.Matrix4||(r=new _three2.default.Matrix4),e.camera.updateMatrixWorld();var a=new _three2.default.Matrix4;a.getInverse(e.camera.matrixWorld),a.multiplyMatrices(a,r),a.multiplyMatrices(e.camera.projectionMatrix,a);var o=(new _three2.default.Vector4).copy(t).applyMatrix4(a),i=1/o.w,s=(new _three2.default.Vector3).copy(o).multiplyScalar(i),c=new _three2.default.Matrix4;if(n){var u=new _three2.default.Matrix4;u.set(i,0,0,-o.x*i*i,0,i,0,-o.y*i*i,0,0,i,-o.z*i*i,0,0,0,0),c.multiplyMatrices(u,a)}var l=e.dom.clientWidth/2,h=e.dom.clientHeight/2,p=l,d=-h,f=l,_=h,m={};if(m.position=new _three2.default.Vector2,m.position.x=s.x*p+f,m.position.y=s.y*d+_,n){var k=m.derivatives=[],v=c.elements;k.push(new _three2.default.Vector2(p*v[0],d*v[1])),k.push(new _three2.default.Vector2(p*v[4],d*v[5])),k.push(new _three2.default.Vector2(p*v[8],d*v[9]))}return m}function calculateWorldToScreenRadiusMultiplier(e){var t=e.derivatives,r=(new _three2.default.Vector3).set(t[0].x,t[1].x,t[2].x),n=(new _three2.default.Vector3).set(t[0].y,t[1].y,t[2].y),a=r.lengthSq(),o=n.lengthSq(),i=r.dot(n),s=(a+o)/2,c=(a-o)/2,u=c*c+i*i,l=s+Math.sqrt(Math.max(u,0));return Math.sqrt(Math.max(l,0))}function updateSizesAndVisualization(){_sketch2.default.ForObjectsWithChildren(this,function(e){e&&(e.updateSizes&&e.view&&e.updateSizes(e.view),e.updateVisualization&&e.updateVisualization())})}exports.__esModule=!0;var _map=require("babel-runtime/core-js/map"),_map2=_interopRequireDefault(_map),_getPrototypeOf=require("babel-runtime/core-js/object/get-prototype-of"),_getPrototypeOf2=_interopRequireDefault(_getPrototypeOf),_getIterator2=require("babel-runtime/core-js/get-iterator"),_getIterator3=_interopRequireDefault(_getIterator2),_regenerator=require("babel-runtime/regenerator"),_regenerator2=_interopRequireDefault(_regenerator),_asyncToGenerator2=require("babel-runtime/helpers/asyncToGenerator"),_asyncToGenerator3=_interopRequireDefault(_asyncToGenerator2),_extends2=require("babel-runtime/helpers/extends"),_extends3=_interopRequireDefault(_extends2),_classCallCheck2=require("babel-runtime/helpers/classCallCheck"),_classCallCheck3=_interopRequireDefault(_classCallCheck2),_createClass2=require("babel-runtime/helpers/createClass"),_createClass3=_interopRequireDefault(_createClass2),_possibleConstructorReturn2=require("babel-runtime/helpers/possibleConstructorReturn"),_possibleConstructorReturn3=_interopRequireDefault(_possibleConstructorReturn2),_inherits2=require("babel-runtime/helpers/inherits"),_inherits3=_interopRequireDefault(_inherits2),_cchelpers=require("../sketcher/cchelpers"),_constraintgen=require("../sketcher/constraintgen"),_sketch=require("../sketcher/sketch"),_sketch2=_interopRequireDefault(_sketch),_constraint=require("../sketcher/constraint"),_constraint2=_interopRequireDefault(_constraint),_controls=require("../sketcher/controls"),Controls=_interopRequireWildcard(_controls),_geomobjects=require("../sketcher/geomobjects"),_orbit=require("../controls/orbit"),_orbit2=_interopRequireDefault(_orbit),_elements=require("../plugin/elements"),Elements=_interopRequireWildcard(_elements),_plugin=require("../plugin/plugin"),_plugin2=_interopRequireDefault(_plugin),_object=require("../three/object3"),_object2=_interopRequireDefault(_object),_three=require("three"),_three2=_interopRequireDefault(_three),_droid_sans_regularTypeface=require("json!../../node_modules/three/examples/fonts/droid/droid_sans_regular.typeface.json"),_droid_sans_regularTypeface2=_interopRequireDefault(_droid_sans_regularTypeface),_helpers=require("../core/helpers"),_unittests=require("../sketcher/unittests"),_unittests2=_interopRequireDefault(_unittests),CursorPosition=function(e){function t(r){(0,_classCallCheck3.default)(this,t);var n=(0,_possibleConstructorReturn3.default)(this,e.call(this));return n._x=n._y=0,n.notifier=r,n}return(0,_inherits3.default)(t,e),t.prototype.set=function(e,t){this._x=e,this._y=t,this.notifier()},t.prototype.copy=function(e){this.set(e.x,e.y)},t.prototype.clone=function(){return new _three2.default.Vector3(this.x,this.y,this.z)},(0,_createClass3.default)(t,[{key:"x",get:function(){return this._x}},{key:"y",get:function(){return this._y}},{key:"z",get:function(){return 0}}]),t}(_three2.default.Vector3),Sketcher=function(e){function t(r){(0,_classCallCheck3.default)(this,t);var n=(0,_possibleConstructorReturn3.default)(this,e.call(this,(0,_extends3.default)({name:"Sketcher",icon:"write",autoconstraintIncremental:!0,expressionSet:[],promptText:""},r)));n.data.interactionPoint=new CursorPosition(n.interactionPointMoved.bind(n)),Object.defineProperty(n.data,"incrementalLevel",{enumerable:!0,configurable:!0,get:function(){return o._incrementalLevel},set:function(e){o.setIncrementalLevel(e)}}),t.globalSketcher=n,n.view=n.data.view,n.socket=n.data.socket,n.currentSketch=null,n.__sketchControlsHandler=null;var a=new _three2.default.FontLoader;n.font=a.parse(_droid_sans_regularTypeface2.default),_constraint2.default.TextParameters.font=n.font;var o=n;return n._incrementalLevel=!1,n.modelMethods={addExpression:function(){this.expressionSet.push({name:"",value:0,expression:"",fixed:!0,visible:!0})},removeExpression:function(e){var t=this.expressionSet.indexOf(e);t!==-1&&this.expressionSet.splice(t,1)},resetExpressionSet:function(){(0,_cchelpers.trackPromise)(o.currentSketch.updateFromServer.execute(o.socket))},submitExpressionSet:function(){var e=this,t=this.expressionSet.map(function(e){return[e.name,e.value,e.expression,e.fixed]}),r=_cchelpers.Statement.Return(_cchelpers.Expression.UpdateExpressionSet(o.currentSketch,t)).execute(o.socket);this.incrementalLevel&&(r=r.then(function(){return e.solveConstraints()})),(0,_cchelpers.trackPromise)(r)}},n}return(0,_inherits3.default)(t,e),t.prototype.onEnabled=function(e){var t=this,r=["AllObjects","CC_Part","CC_GeometrySet","CC_SketchSet"];this.data.title="Select a sketch",this.data.elements=[new Elements.Selection({name:"Sketch",types:function(e){if(r.indexOf(e.type)===-1)return["CC_WorkPlane","CC_Sketch","Mesh"].indexOf(e.type)!==-1},limit:1,active:!0,onChange:function(){var e=(0,_asyncToGenerator3.default)(_regenerator2.default.mark(function e(r){var n;return _regenerator2.default.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:if(1===r.objects.length){e.next=2;break}return e.abrupt("return",t.deselectSketch());case 2:if(n=r.objects[0].object.parent,"CC_WorkPlane"!==n.type){e.next=7;break}return e.next=6,t.newSketchFromPlane(n);case 6:n=e.sent;case 7:if("CC_Sketch"!==n.type){e.next=10;break}return e.next=10,t.selectSketch(n);case 10:case"end":return e.stop()}},e,t)}));return function(t){return e.apply(this,arguments)}}()})]},t.prototype.onDisabled=function(e){return this.data.elements=[],this.deselectSketch()},t.prototype.addSketchControlElements=function(){var e=this;this.data.title="Sketch mode";var t=(0,_getIterator3.default)(["add to cart","add user","adjust","archive","ban","bookmark","call","call square","clone","cloud download","cloud upload","talk","talk outline","compress","configure","download","edit","erase","exchange","expand","external share","filter","hide","in cart","lock","mail forward","object group","object ungroup","pin","print","random","recycle","refresh","remove bookmark","remove user","repeat","reply all","reply","retweet","send","send outline","share alternate","share alternate square","share","share square","sign in","sign out","theme","translate","undo","unhide","unlock alternate","unlock","upload","wait","wizard","write","write square"]);this.data.elements.push(new Elements.CheckBox({name:"solve constraints immediately",value:this.data.incrementalLevel,onChange:function(t){var r=t.value;return e.data.incrementalLevel=r}}),new Elements.CheckBox({name:"automatically add implied constraints",value:this.data.autoconstraintIncremental,onChange:function(t){var r=t.value;return e.data.autoconstraintIncremental=r}})),this.data.toolbar=[new Elements.ButtonGroup({name:"Constraints",items:["incidence","tangency","verticality","horizontality","parallelity","perpendicularity","fixation","colinear","concentric","midpoint","symmetric","equal_distance","equal_radius","distance","horizontal_distance","vertical_distance","radius","angle","angleox"].map(function(r,n){return new Elements.Button({tooltip:r.replace("_"," "),icon:t.next().value,onClick:function(){return e.chooseTool(r)}})})}),new Elements.ButtonGroup({name:"Tools",items:["point","line","arcCenter","arcControl","arcTangent","arcMiddle","polyline","fillet"].map(function(r){return new Elements.Button({tooltip:r,icon:t.next().value,onClick:function(){return e.chooseTool(r)}})})}),new Elements.ButtonGroup({name:"Actions",items:[new Elements.Button({tooltip:"solve",icon:t.next().value,onClick:function(){return e.solveConstraints()}}),new Elements.Button({tooltip:"add autoconstraints",icon:t.next().value,onClick:function(){return e.autoconstraintAll()}}),new Elements.Button({tooltip:"delete",icon:t.next().value,onClick:function(){}}),new Elements.Button({tooltip:"run all tests",icon:t.next().value,onClick:function(){return(0,_unittests2.default)(e)}})]})],this.data.console=new Elements.Console({onChange:function(e){e.element,e.value;return 0},onEnter:function(t){var r=t.element,n=t.value;e.__sketchControlsHandler&&e.__sketchControlsHandler.consoleExecute(n),r.value=""}})},t.prototype.removeSketchControlElements=function(){this.data.title="Select a sketch",this.data.elements.splice(1),this.data.toolbar=[],this.data.console=void 0},t.prototype.refresh=function(){this.view.invalidate()},t.prototype.sketchAreaClicked=function(){function e(e){return t.apply(this,arguments)}var t=(0,_asyncToGenerator3.default)(_regenerator2.default.mark(function e(t){return _regenerator2.default.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:if(this.currentSketch===t){e.next=4;break}return e.next=3,this.selectSketch(t);case 3:return e.abrupt("return");case 4:return e.next=6,this.click();case 6:case"end":return e.stop()}},e,this)}));return e}(),t.prototype.sketchAreaRightClicked=function(){function e(e){return t.apply(this,arguments)}var t=(0,_asyncToGenerator3.default)(_regenerator2.default.mark(function e(t){var r,n;return _regenerator2.default.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:if(t===this.currentSketch){e.next=2;break}return e.abrupt("return");case 2:return r=(0,_getPrototypeOf2.default)(this.__sketchControlsHandler),e.next=5,this.chooseTool("empty");case 5:if(n=(0,_getPrototypeOf2.default)(this.__sketchControlsHandler),r!==n){e.next=9;break}return e.next=9,this.deselectSketch();case 9:case"end":return e.stop()}},e,this)}));return e}(),t.prototype.setActionCursor=function(e){var t=new _map2.default([["empty","default"],["delete","no-drop"],["point","crosshair"],["line","crosshair"],["arc","crosshair"],["polyline","crosshair"],["fixation","default"],["incidence","default"],["distance","default"],["angle","default"],["angleOX","default"],["tangency","default"],["horizontality","default"],["verticality","default"],["perpendicularity","default"],["parallelity","default"],["concentric","default"],["midpoint","default"],["symmetric","default"],["colinear","default"],["equal_distance","default"],["radius","default"],["equal_radius","default"],["horizontal_distance","default"],["vertical_distance","default"],["autoconstraint","default"]]),r=this.view;void 0===e&&this.currentCursor?r.setCursor(this.currentCursor):t.has(e)?(this.currentCursor=t.get(e),r.setCursor(this.currentCursor)):this.currentCursor=null},t.prototype.chooseTool=function(){function e(e){return t.apply(this,arguments)}var t=(0,_asyncToGenerator3.default)(_regenerator2.default.mark(function e(t){var r;return _regenerator2.default.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:if(this.setActionCursor(t),!t){e.next=5;break}if(r=Controls[t+"Handler"],void 0!==r){e.next=5;break}return e.abrupt("return");case 5:if(!(this.__sketchControlsHandler instanceof Controls.polylineHandler)){e.next=10;break}if(!t||!t.startsWith("line")&&!t.startsWith("arc")){e.next=10;break}return e.next=9,this.__sketchControlsHandler.changeEdgeType(t);case 9:return e.abrupt("return",e.sent);case 10:if(!this.__sketchControlsHandler){e.next=14;break}return e.next=13,this.__sketchControlsHandler.finish();case 13:this.__sketchControlsHandler=null;case 14:if("empty"!==t||!this.data.incrementalLevel){e.next=17;break}return e.next=17,this.currentSketch.solveConstraints();case 17:t&&(this.__sketchControlsHandler=new r(this));case 18:case"end":return e.stop()}},e,this)}));return e}(),t.prototype.checkSolveResult=function(e){return 0!==e||(alert("Failed to solve sketch"),this.data.incrementalLevel&&this.setIncrementalLevel(!1),!1)},t.prototype.solveConstraints=function(){function e(){return t.apply(this,arguments)}var t=(0,_asyncToGenerator3.default)(_regenerator2.default.mark(function e(){var t;return _regenerator2.default.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:if(this.currentSketch){e.next=2;break}return e.abrupt("return");case 2:return e.next=4,this.currentSketch.solveConstraints();case 4:return t=e.sent,e.abrupt("return",t);case 6:case"end":return e.stop()}},e,this)}));return e}(),t.prototype.setIncrementalLevel=function(){function e(e){return t.apply(this,arguments)}var t=(0,_asyncToGenerator3.default)(_regenerator2.default.mark(function e(t){return _regenerator2.default.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:if(this._incrementalLevel=t,!t){e.next=4;break}return e.next=4,this.solveConstraints();case 4:case"end":return e.stop()}},e,this)}));return e}(),t.prototype.autoconstraintAll=function(){function e(){return t.apply(this,arguments)}var t=(0,_asyncToGenerator3.default)(_regenerator2.default.mark(function e(){var t,r;return _regenerator2.default.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return t=new _constraintgen.ConstraintGenerator(this.currentSketch),r=t.generateImpliedConstraints(),e.next=4,this.currentSketch.addToSketch(r,{updateSketch:!0});case 4:case"end":return e.stop()}},e,this)}));return e}(),t.prototype.moveUnderConstraints=function(e,t){function r(e){this.sketchStates[1]=this.sketchStates[0],this.sketchStates[0]=this.currentSketch.getState(),this.stateDiff=this.currentSketch.getStateLinearContinuation(this.sketchStates)}function n(){this.mouseStates[1]=this.mouseStates[0],this.mouseStates[0]=t}function a(e){var t=_sketch2.default.ForObjectsWithChildren(e);return t=t.filter(function(e){return e instanceof _geomobjects.Point})}var o=this;if(this.currentSketch){var i=a(e);if(!this.currentSketch.solveInProgress){this.currentSketch.solveInProgress=!0,n.call(this);var s=this.mouseStates[0].clone().sub(this.mouseStates[1]);if(s.length()>0){_cchelpers.logLevel>=5&&console.log(_helpers.Timestamp.get(),"MUC solve");var c=_cchelpers.Statement.Return(_cchelpers.Expression.MoveObjects(this.currentSketch,this.movedObject,s)).and(this.currentSketch.updateFromServer()),u=c.execute(this.socket).then(function(e){o.checkSolveResult(e[0]),r.call(o,i),o.currentSketch.solveInProgress=!1}).catch(function(){o.currentSketch.solveInProgress=!1});(0,_cchelpers.trackPromise)(u)}else this.currentSketch.solveInProgress=!1}}},t.prototype.newSketch=function(e,t,r){var n=[new _three2.default.Vector3(1,0,0),new _three2.default.Vector3(0,1,0),new _three2.default.Vector3(0,0,1)];n.forEach(function(e){return e.applyQuaternion(t)}),n.unshift(e);var a=_sketch2.default.FindSketchSetOf(r);return _cchelpers.Statement.JSONExport(_cchelpers.Expression.AddSketch(r,n)).then((0,_cchelpers.parseToChildOf)(a)).execute(this.socket)},t.prototype.newSketchFromPlane=function(e){var t=_sketch2.default.FindSketchSetOf(e);return _cchelpers.Statement.JSONExport(_cchelpers.Expression.AddSketchFromPlane(e)).then((0,_cchelpers.parseToChildOf)(t)).execute(this.socket)},t.prototype.deleteSketch=function(){var e=this;if(confirm("Do you really want to irreversibly delete current sketch and all its contents?")){var t=this.currentSketch;if(t)return this.deselectSketch({skipServerCalls:!0}),t.__box.removeInteraction(),(0,_cchelpers.trackPromise)(_cchelpers.Statement.Return(_cchelpers.Expression.RemoveSketch(t)).then(function(){t.parent&&t.parent.remove(t),e.refresh()}).execute(this.socket))}},t.prototype.switchToOrthographicCamera=function(e,t){e.perspectiveControls=e.controls,e.camera=new _three2.default.OrthographicCamera(0,1,1,0),t.localToWorld(e.camera.position.set(0,0,1e3)),e.camera.up.transformDirection(t.matrixWorld),e.camera.size=1,e.controls=e.controls.clone(),e.controls.noRotate=!0,e.controls.zoomMode=_orbit2.default.ZoomMode.Mouse,e.controls.focus(t).zoom(200).now()},t.prototype.switchToPerspectiveCamera=function(e){e.controls=e.perspectiveControls,e.camera=e.controls.camera,e.perspectiveControls=void 0},t.prototype.selectSketch=function(){function e(e){return t.apply(this,arguments)}var t=(0,_asyncToGenerator3.default)(_regenerator2.default.mark(function e(t){var r;return _regenerator2.default.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:if(this.currentSketch!==t){e.next=2;break}return e.abrupt("return",!0);case 2:return e.next=4,this.deselectSketch();case 4:if(e.sent){e.next=6;break}return e.abrupt("return",!1);case 6:return this.currentSketch=t,this.data.expressionSet=t.getPart().members.ExpressionSet.data,this.addSketchControlElements(),t.showBox(),t.infiniteBox(),this.switchToOrthographicCamera(this.view,t),e.next=14,t.updateFromServer().execute(this.socket);case 14:return e.next=16,this.chooseTool("empty");case 16:return this.refresh(),this.currentSketch.showGrid(),this.currentSketch.showDimensions(),r=t.getPart().item,r&&r.materials&&r.materials.all&&r.materials.all.forEach(function(e){e.transparent=!0,e.opacity=t.chosenItemOpacity}),e.abrupt("return",!0);case 22:case"end":return e.stop()}},e,this)}));return e}(),t.prototype.deselectSketch=function(){function e(e){return t.apply(this,arguments)}var t=(0,_asyncToGenerator3.default)(_regenerator2.default.mark(function e(t){var r,n;return _regenerator2.default.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:if(null!==this.currentSketch){e.next=2;break}return e.abrupt("return",!0);case 2:return r=this.currentSketch.getPart().item,r&&r.materials&&r.materials.all&&r.materials.all.forEach(function(e){e.opacity=1,e.transparent=!1}),this.switchToPerspectiveCamera(this.view),e.next=7,this.chooseTool(null);case 7:if(this.currentSketch.hideGrid(),this.currentSketch.hideDimensions(),this.currentSketch.hideConstraints(),n=this.currentSketch,t=t||{},t.skipServerCalls){e.next=15;break}return e.next=15,_cchelpers.Statement.Return(_cchelpers.Expression.Recalc(n)).and(n.updateFromServer()).execute(this.socket);case 15:return this.currentSketch.updateBox(),this.removeSketchControlElements(),this.currentSketch=null,this.data.expressionSet=[],this.refresh(),e.abrupt("return",!0);case 21:case"end":return e.stop()}},e,this)}));return e}(),t.prototype.click=function(){return this.__sketchControlsHandler.click(this.data.interactionPoint.clone())},t.prototype.interactionPointMoved=function(){this.__sketchControlsHandler&&this.__sketchControlsHandler.mouseMove(this.data.interactionPoint.clone())},t.prototype.setPrompt=function(e){this.data.promptText=e},t.prototype.askConstraintParams=function(){function e(e){return t.apply(this,arguments)}var t=(0,_asyncToGenerator3.default)(_regenerator2.default.mark(function e(t){var r;return _regenerator2.default.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this.requestModal({title:"Constraint parameters",elements:[{name:"expression",type:"text",value:t.expression},{name:"value",type:"number",value:t.value},{name:"fixed",type:"checkbox",value:t.fixed}]});case 2:return r=e.sent,e.abrupt("return",{expression:r[0].value,value:Number(r[1].value),fixed:r[2].value});case 4:case"end":return e.stop()}},e,this)}));return e}(),t.registerVisualization=function(e,t){var r;t.createInteraction({priority:-1e4}).on((r={},r[_object2.default.Events.Lifecycle.Rendered]=updateSizesAndVisualization,r))},t.mixinPixelSizedGeometry=function(e){return this instanceof _three2.default.Object3D?this.setGeomSizes?(e||(e=1),this.pixelSizes=[].concat(e),this.getSizingPoint||(this.getSizingPoint=function(){return new _three2.default.Vector3}),void(this.updateSizes=function(e){for(var t=this.getSizingPoint(),r=transformWorldToScreen(e,t,this,!0),n=1/calculateWorldToScreenRadiusMultiplier(r),a=this.pixelSizes.slice(0),o=0,i=a.length;o<i;o++)a[o]=a[o]*n;this.setGeomSizes(a)})):void console.warn("PixelSizedGeometry: missing required methods"):void console.warn("PixelSizedGeometry: not a THREE object")},t}(_plugin2.default);exports.default=Sketcher;