UNPKG

awv3

Version:
1 lines 18.1 kB
"use strict";function _interopRequireDefault(e){return e&&e.__esModule?e:{default:e}}exports.__esModule=!0;var _regenerator=require("babel-runtime/regenerator"),_regenerator2=_interopRequireDefault(_regenerator),_asyncToGenerator2=require("babel-runtime/helpers/asyncToGenerator"),_asyncToGenerator3=_interopRequireDefault(_asyncToGenerator2),_getIterator2=require("babel-runtime/core-js/get-iterator"),_getIterator3=_interopRequireDefault(_getIterator2),_map=require("babel-runtime/core-js/map"),_map2=_interopRequireDefault(_map),_log=require("babel-runtime/core-js/math/log10"),_log2=_interopRequireDefault(_log),_promise=require("babel-runtime/core-js/promise"),_promise2=_interopRequireDefault(_promise),_weakSet=require("babel-runtime/core-js/weak-set"),_weakSet2=_interopRequireDefault(_weakSet),_classCallCheck2=require("babel-runtime/helpers/classCallCheck"),_classCallCheck3=_interopRequireDefault(_classCallCheck2),_possibleConstructorReturn2=require("babel-runtime/helpers/possibleConstructorReturn"),_possibleConstructorReturn3=_interopRequireDefault(_possibleConstructorReturn2),_inherits2=require("babel-runtime/helpers/inherits"),_inherits3=_interopRequireDefault(_inherits2),_ccobjects=require("sketcher/ccobjects"),_sketcher=require("sketcher/sketcher"),_sketcher2=_interopRequireDefault(_sketcher),_constraint=require("sketcher/constraint"),_constraint2=_interopRequireDefault(_constraint),_constraintgen=require("sketcher/constraintgen"),_cchelpers=require("sketcher/cchelpers"),_geomobjects=require("sketcher/geomobjects"),_object=require("three/object3"),_object2=_interopRequireDefault(_object),_view=require("core/view"),_view2=_interopRequireDefault(_view),_three=require("three"),_three2=_interopRequireDefault(_three),Sketch=function(e){function t(){var r;(0,_classCallCheck3.default)(this,t);var n=(0,_possibleConstructorReturn3.default)(this,e.call(this));n.sketchObjects=new _weakSet2.default,n.infiniteBoxMaterial=new _three2.default.MeshBasicMaterial({color:4144959,wireframe:!0,visible:!1}),n.frameBoxMaterial=new _three2.default.MeshPhongMaterial({color:16777215,emissive:16777215,specular:16777215,shininess:1,transparent:!0,opacity:.3,depthWrite:!1}),n.__box=new _three2.default.Mesh(new _three2.default.BoxGeometry(1,1,1),n.infiniteBoxMaterial),n.sketcher=window.globalSketcher;var i=n,o=n.sketcher,a=o.__parentView;return n.__box.createInteraction({priority:1}).on((r={},r[_object2.default.Events.Interaction.Clicked]=function(e){switch(a.input.recent.mouseup.button){case 0:(0,_cchelpers.trackPromise)(_promise2.default.resolve(o.sketchAreaClicked(i)));break;case 2:o.sketchAreaRightClicked(i)}},r[_object2.default.Events.Lifecycle.Rendered]=function(){if(o.currentSketch===i&&a.input.enabled&&(i.updateGrid(a.camera.size),a.input.recent.mousemove)){var e=new _three2.default.Vector2(a.input.recent.mousemove.offsetX,a.input.recent.mousemove.offsetY),t=a.getViewLine3(e),r=(new _three2.default.Matrix4).getInverse(i.matrixWorld);t.applyMatrix4(r);var n=(0-t.start.z)/(t.end.z-t.start.z),s=t.at(n);s.z=0,s.equals(o.modelData.interactionPoint)||(o.modelData.interactionPoint.copy(s),o.setActionCursor()),o.refresh()}},r)),n.__grid=new _three2.default.LineSegments(new _three2.default.Geometry,new _three2.default.LineBasicMaterial({color:5263440})),n.__axis=new _three2.default.LineSegments(new _three2.default.Geometry,new _three2.default.LineBasicMaterial({color:16711680})),n.__grid.frustumCulled=!1,n.__axis.frustumCulled=!1,n.__dimensions=new _three2.default.Object3D,n.gridScale=4,n.gridSize=1e3,n.snappingSizeInGrid=.1,n.pointSizeInGrid=.1/n.gridScale,n.chosenItemOpacity=.5,n.updateGrid(),n}return(0,_inherits3.default)(t,e),t.FindSketchOf=function(e){for(;e;){if(e instanceof t)return e;e=e.parent}},t.FindSketchSetOf=function(e){var t;return e.traverseAncestors(function(e){t=t||e.members.SketchSet}),t},t.prototype.getPart=function(){return this.parent.parent},t.prototype.addToSketch=function(e){var r=this,n=arguments.length<=1||void 0===arguments[1]?{}:arguments[1];void 0===n.onServer&&(n.onServer=!0),void 0===n.onClient&&(n.onClient=!0),void 0===n.updateSketch&&(n.updateSketch=!1),void 0===n.autoconstraint&&(n.autoconstraint=!1),e=[].concat(e);var i=this.sketcher,o=i.__parentView;if(0===e.length)return _promise2.default.resolve();n.onClient&&this.add.apply(this,e);var a=function(){if(e.forEach(function(e){r.sketchObjects.has(e)||(r.sketchObjects.add(e),_sketcher2.default.registerVisualization(o,e),r.emit("SketchAdded",e))}),n.autoconstraint&&i.modelData.autoconstraintIncremental){var a=new _constraintgen.ConstraintGenerator(r),s=a.generateImpliedConstraints(t.ForObjectsWithChildren(e));if(s.length>0)return r.addToSketch(s,{updateSketch:!0})}};if(n.onServer){var s=e.map(function(e){return e.prepareAddOnServer(r)});return(n.updateSketch?this.updateFromServer(i.server,s):(0,_cchelpers.executePrepared)(i.server,s)).then(a)}return _promise2.default.resolve(a())},t.prototype.removeFromSketch=function(e){var r=this,n=arguments.length<=1||void 0===arguments[1]?{}:arguments[1];void 0===n.onServer&&(n.onServer=!0),void 0===n.onClient&&(n.onClient=!0),void 0===n.updateSketch&&(n.updateSketch=!1),e=[].concat(e);var i=this.sketcher,o=(i.__parentView,t.ForObjectsWithChildren(e)),a=this.getConstraintsOnSketch(o);e=a.concat(e.filter(function(e){return a.indexOf(e)===-1})),o=a.concat(o.filter(function(e){return a.indexOf(e)===-1})),o.forEach(function(e){e.interact.removeInteraction(),e.removeInteraction()});var s=function(){e.forEach(function(e){n.onClient&&r.remove(e),r.sketchObjects.delete(e),r.emit("SketchRemoved",e)})};if(n.onServer){var c=e.map(function(e){return e.prepareRemoveOnServer(r)});return(n.updateSketch?this.updateFromServer(i.server,c):(0,_cchelpers.executePrepared)(i.server,c)).then(s)}return _promise2.default.resolve(s())},t.prototype.getObjectsOnSketch=function(){var e=this;return this.children.filter(function(t){return e.sketchObjects.has(t)})},t.prototype.getConstraintsOnSketch=function(e){e&&(e=[].concat(e));var t=function(t){return!e||e.indexOf(t)!==-1},r=function(e){return e.args.some(t)},n=this.getObjectsOnSketch().filter(function(e){return e instanceof _constraint2.default}).filter(r);return n},t.prototype.getAncestorOnSketch=function(e){var t=this;return e.traverseAncestors(function(r){r.parent===t&&(e=r)}),e},t.prototype.isOnSketch=function(e){return this.sketchObjects.has(e)},t.prototype.addAuxObject=function(e){var t=this;e=[].concat(e);var r=this.sketcher.__parentView;if(e.some(function(e){return this.isOnSketch(e)},this))throw"Sketch.addAuxObject: can't add object on sketch. Remove it first";e.forEach(function(e){t.add(e);try{_sketcher2.default.registerVisualization(r,e)}catch(e){}})},t.prototype.removeAuxObject=function(e){var t=this;e=[].concat(e);this.sketcher.__parentView;e.forEach(function(e){t.remove(e)})},t.prototype.addMember=function(e){_ccobjects.ClassCadObject.prototype.addMember.apply(this,arguments),"objectMem"===e.userData.memberInfo.type&&this.addToSketch(e,{onServer:!1,onClient:!1})},t.prototype.removeSuperfluousMembers=function(){var e=_ccobjects.ClassCadObject.prototype.removeSuperfluousMembers.apply(this,arguments);return this.removeFromSketch(e,{onServer:!1,onClient:!1}),e},t.prototype.showBox=function(){this.addAuxObject(this.__box)},t.prototype.hideBox=function(){this.removeAuxObject(this.__box)},t.prototype.updateBox=function(){var e=this;this.updateMatrixWorld();var t=new _three2.default.Box3,r=new _three2.default.Vector3;this.getObjectsOnSketch().forEach(function(n){return n.traverseVisible(function(n){n.geometry instanceof _three2.default.Geometry&&n.geometry.vertices.forEach(function(i){r.copy(i),n.localToWorld(r),e.worldToLocal(r),t.expandByPoint(r)})})});var n=this.__box;n.material=this.frameBoxMaterial,t.isEmpty()?(n.scale.set(.001,.001,.001),n.position.set(0,0,0)):(t.min.z=-.001,t.max.z=.001,t.size(n.scale),t.center(n.position))},t.prototype.infiniteBox=function(){var e=new _three2.default.Box3;e.max.set(1e5,1e5,.001),e.min.copy(e.max),e.min.negate();var t=this.__box;t.material=this.infiniteBoxMaterial,e.size(t.scale),e.center(t.position)},t.prototype.showGrid=function(){this.addAuxObject([this.__grid,this.__axis]),this.zoom=-250},t.prototype.hideGrid=function(){this.removeAuxObject([this.__grid,this.__axis])},t.prototype.updateGrid=function(e){var t=this;e=e||100;var r=this.calcGridStep(e,this.gridStep),n=this.calcAxisSize(e,r),i=r*this.gridSize*2;if(this.gridStep!==r||this.axisSize!==n){this.gridStep=r,this.axisSize=n;for(var o=0,a=0,s=function(e,r,n){t.__grid.geometry.vertices[o++]=new _three2.default.Vector3(e,r,n)},c=function(e,r,n){t.__axis.geometry.vertices[a++]=new _three2.default.Vector3(e,r,n)},h=1;h<=this.gridSize;++h)s(-i/2,r*h,0),s(i/2,r*h,0),s(-i/2,-r*h,0),s(i/2,-r*h,0),s(r*h,i/2,0),s(r*h,-i/2,0),s(-r*h,i/2,0),s(-r*h,-i/2,0),1===h&&(s(-i/2,0,0),s(-n,0,0),s(i/2,0,0),s(n,0,0),s(0,i/2,0),s(0,n,0),s(0,-i/2,0),s(0,-n,0));c(-n,0,0),c(n,0,0),c(0,-n,0),c(0,n,0);var u=this.gridStep/4;c(n-this.gridStep,-u,0),c(n,0,0),c(n-this.gridStep,+u,0),c(n,0,0),c(-u,n-this.gridStep,0),c(0,n,0),c(u,n-this.gridStep,0),c(0,n,0);var l=3*r/10,p=r-2*l,f=.6*p;c(n+l,l,0),c(n+l+f,l+p,0),c(n+l,l+p,0),c(n+l+f,l,0),c(l,n+l,0),c(l+f,n+l+p,0),c(l,n+l+p,0),c(l+f/2,n+l+p/2,0),o==this.__grid.geometry.vertices.length&&a==this.__axis.geometry.vertices.length||console.error("Grid and axis vertex count has changed!"),this.__grid.geometry.verticesNeedUpdate=!0,this.__axis.geometry.verticesNeedUpdate=!0}},t.prototype.calcGridStep=function(e,t){var r=Math.floor((0,_log2.default)(Math.abs(e*this.gridScale)))-1;return Math.pow(10,r)},t.prototype.calcAxisSize=function(e,t){if(e<=10)return this.axisSize;var r=5*Math.floor(e/(20*t));return r<5&&(r=5),r*t},t.prototype.getClosestGeomPoint=function(e,r,n){n||(n=function(e){return!1});var i=r,o=e.clone(),a=!1;return this.getObjectsOnSketch().forEach(function(r){t.ForObjectsWithChildren(r,function(t){if(!n(t)&&t instanceof _geomobjects.Point){var r=t,s=r.position.distanceTo(e);s<i&&(i=s,o=r.position.clone(),a=!0)}})}),{snappedPosition:o,snappingShift:o.clone().sub(e),isSnapped:a}},t.prototype.getClosestGridPoint=function(e,t,r){var n=e.clone(),i=!1;if(this.gridStep>0){var o=Math.min(t,this.gridStep/3),a=Math.round(e.x/this.gridStep)*this.gridStep,s=Math.round(e.y/this.gridStep)*this.gridStep;(a-e.x)*(a-e.x)+(s-e.y)*(s-e.y)<o*o&&(n.x=a,n.y=s,i=!0)}return{snappedPosition:n,snappingShift:n.clone().sub(e),isSnapped:i}},t.prototype.getClosestSnapPoint=function(e,t,r){var n=e.clone(),i=this.getClosestGridPoint(n,t,r),o=this.getClosestGeomPoint(n,t,r);return o.isSnapped&&i.isSnapped?o.snappingShift.length()<i.snappingShift.length()?o:i:o.isSnapped?o:i},t.prototype.getSnappedPointWrap=function(e,r,n){function i(){var t=e.clone(),n=r.parent,i=n.points[0]==r?0:1,o=n.points[1-i].position,a=new _three2.default.Vector3;a.subVectors(t,o),a.z=0;var c=a.length();if(c>.001){var h=new _three2.default.Vector3(1,0,0),u=a.angleTo(h);u=a.y<-1e-6?2*Math.PI-u:u;var l=h,p=l.angleTo(h);p=l.y<-1e-6?2*Math.PI-p:p;var f=u>p?u-p:2*Math.PI+u-p,d=Math.round(f*s/(2*Math.PI)),_=2*Math.PI*d/s;_+=p;var v=new _three2.default.Vector3(Math.cos(_),Math.sin(_),0);v.multiplyScalar(v.dot(a)),t.addVectors(o,v)}return t}if(!r)return e;var o=this.sketcher,a=1.5*r.points[0].mesh.scale.x,s=o.__parentView.interaction.shiftKey?o.__sketchControlsHandler.shiftDirections:0;if(s&&r.parent instanceof _geomobjects.Line)return i();var c=e.clone(),h=[];r instanceof _geomobjects.Point?h=[r]:t.ForObjectsWithChildren(r,function(e){e instanceof _geomobjects.Point&&h.push(e)});var u=new _three2.default.Vector3(0,0,0),l=a,p=this;return h.forEach(function(i){void 0!==n&&(u=i.position.clone().sub(n));var o=e.clone();"center"!==r.name&&o.add(u);var s=p.getClosestSnapPoint(o,a,function(e){return t.ForObjectsWithChildren(e).indexOf(i)!==-1});s.isSnapped&&s.snappingShift.length()<l&&(l=s.snappingShift.length(),c=s.snappedPosition,r instanceof _geomobjects.Point||c.sub(u))}),c},t.prototype.showDimensions=function(){this.addAuxObject(this.__dimensions)},t.prototype.hideDimensions=function(){this.removeAuxObject(this.__dimensions)},t.prototype.updateDimensionsFactory=function(e,t){var r=e.data,n=(new _three2.default.Matrix4).getInverse(this.matrix);switch(e.type){case"Cone":var i=(new _three2.default.Vector3).fromArray(r.position).applyMatrix4(n),o=(new _three2.default.Vector3).fromArray(r.axis).transformDirection(n),a=o.length(),s=Math.max(r.diameters[0],r.diameters[1]),c=new _three2.default.Geometry;c.vertices.push(new _three2.default.Vector3(a,-s/2,0),new _three2.default.Vector3(a,s/2,0),new _three2.default.Vector3(0,0,0)),c.faces.push(new _three2.default.Face3(0,1,2),new _three2.default.Face3(0,2,1)),c.computeFaceNormals();var h=new _three2.default.Mesh(c,new _three2.default.MeshBasicMaterial({color:8388479}));h.position.copy(i),h.quaternion.setFromUnitVectors(new _three2.default.Vector3(1,0,0),o),t.push(h);break;case"Text":if("@"==r.text)break;var u={size:1,height:0,font:this.sketcher.font},c=new _three2.default.TextGeometry(r.text,u);c.computeBoundingBox();var h=new _three2.default.Mesh(c,new _three2.default.MeshBasicMaterial({color:4194239})),l=new _three2.default.Object3D;l.mesh=h,l.add(h),l.position.fromArray(r.position).applyMatrix4(n),l.setGeomSizes=function(e){var t=e[0];this.mesh.scale.set(t,t,t)},_sketcher2.default.mixinPixelSizedGeometry.call(l,r.fontSize||12);var p=this.sketcher.__parentView;_sketcher2.default.registerVisualization(p,l),t.push(l);break;case"Line":r.geometry.applyMatrix(n),r.material=new _three2.default.MeshBasicMaterial({color:8388479}),t.push(r)}},t.prototype.showConstraints=function(e){var t=this.getConstraintsOnSketch(e);0!==t.length&&(e.constraintsSubstrate.visible=!0,this.updateConstraints(e),t.forEach(function(e){return e.show()}))},t.prototype.hideConstraints=function(){var e=this.getConstraintsOnSketch();e.forEach(function(e){e.args.forEach(function(e){e.hovered=!1}),e.hide()}),this.getObjectsOnSketch().forEach(function(e){t.ForObjectsWithChildren(e,function(e){e.constraintsSubstrate&&(e.constraintsSubstrate.visible=!1)})})},t.prototype.updateConstraints=function(e){var t=e.constraintsSubstrate,r=this.getConstraintsOnSketch(e);if(0===r.length)return e.constraintsSubstrate.visible=!1,e.interact.unhover&&e.interact.unhover(),0;var n=r.length,i=r[0].mesh.scale.x,o=1.2*i;t.scale.set(o*r.length,o,Math.min(o,1));var a=e.getSubstratePosition(t.scale.x,o);t.position.set(a.x,a.y,-1);for(var s=t.position.x+e.position.x-t.scale.x/2,c=t.position.y+e.position.y,h=0;h<n;++h)r[h].position.set(s+(h+.5)*o,c,0)},t.prototype.getState=function(){for(var e=new _map2.default,t=this.getObjectsOnSketch(),r=Array.isArray(t),n=0,t=r?t:(0,_getIterator3.default)(t);;){var i;if(r){if(n>=t.length)break;i=t[n++]}else{if(n=t.next(),n.done)break;i=n.value}var o=i;e.set(o,o.getState())}return e},t.prototype.getStateLinearContinuation=function(e){function t(e){return Array.isArray(e)?new _map2.default([["x",e[1].get("x")-e[0].get("x")],["y",e[1].get("y")-e[0].get("y")]]):new _map2.default([["x",0],["y",0]])}function r(e,r){var n=new _map2.default;r[0].has("startPoint")&&n.set("startPoint",t([r[0].get("startPoint"),r[1].get("startPoint")])),r[0].has("endPoint")&&n.set("endPoint",t([r[0].get("endPoint"),r[1].get("endPoint")])),i.set(e,n)}function n(e,r){var n=new _map2.default;r[0].has("startPoint")&&n.set("startPoint",t([r[0].get("startPoint"),r[1].get("startPoint")])),r[0].has("endPoint")&&n.set("endPoint",t([r[0].get("endPoint"),r[1].get("endPoint")])),r[0].has("center")&&n.set("center",t([r[0].get("center"),r[1].get("center")])),r[0].has("clockwise")&&n.set("clockwise",r[0].get("clockwise")),i.set(e,n)}if(null!==e[0]&&null!==e[1]){var i=new _map2.default;return e[0].forEach(function(i,o){if(0!==i.size){var a=e[1].get(o);o instanceof _geomobjects.Point?i.set(o,t(o,[i,a])):o instanceof _geomobjects.Line?r(o,[i,a]):o instanceof _geomobjects.Arc&&n(o,[i,a])}},this),i}},t.prototype.applyStateDiff=function(e){e.forEach(function(e,t){return void 0===e?(this.isOnSketch(t)||console.error("sketch invariant fail: don't have",t.id),void this.removeFromSketch(t)):(this.isOnSketch(t)||this.addToSketch(t),void t.applyStateDiff(e))},this);for(var t=this.getObjectsOnSketch(),r=Array.isArray(t),n=0,t=r?t:(0,_getIterator3.default)(t);;){var i;if(r){if(n>=t.length)break;i=t[n++]}else{if(n=t.next(),n.done)break;i=n.value}var o=i;o.updateGeometry()}},t.prototype.addStateDiff=function(e,t){e.forEach(function(e,r){null!==e&&this.isOnSketch(r)&&r.addStateDiff(e,t)},this);for(var r=this.getObjectsOnSketch(),n=Array.isArray(r),i=0,r=n?r:(0,_getIterator3.default)(r);;){var o;if(n){if(i>=r.length)break;o=r[i++]}else{if(i=r.next(),i.done)break;o=i.value}var a=o;a.updateGeometry()}},t.prototype.finishedUpdating=function(){this.sketcher.currentSketch===this?this.infiniteBox():this.updateBox(),this.showBox()},t.prototype.updateFromServer=function(){function e(e,r){return t.apply(this,arguments)}var t=(0,_asyncToGenerator3.default)(_regenerator2.default.mark(function e(t,r){var n,i,o,a,s=this;return _regenerator2.default.wrap(function(e){for(;;)switch(e.prev=e.next){case 0:return n=this.getPart(),i=(r||[]).concat([_cchelpers.Interop.CreateSketchDimensions(this,!0),(0,_cchelpers.getObjectStatement)(n).parseToObject(n)]),o=[],e.next=5,(0,_cchelpers.executePrepared)(t,i,function(e){return s.updateDimensionsFactory(e,o)});case 5:return a=e.sent,this.__dimensions.remove(this.__dimensions.children.slice()),this.__dimensions.add(o),e.abrupt("return",a);case 9:case"end":return e.stop()}},e,this)}));return e}(),t.prototype.solveConstraints=function(){return this.updateFromServer(this.sketcher.server,[_cchelpers.Interop.SolveConstraints(this)]).then(function(e){return e[0]})},t.ForObjectsWithChildren=function(e,t){var r=void 0;return t||(r=[],t=function(e){return r.push(e)}),e=[].concat(e),e.forEach(function(e){e&&(t(e),e instanceof _geomobjects.Point||(e.points&&e.points.forEach(t),e.centerPoint&&t(e.centerPoint)))}),r},t}(_ccobjects.ClassCadObject);exports.default=Sketch,_ccobjects.ClassCadObject.classMapping.CC_Sketch=Sketch;