Doodle3D-Slicer/build/d3d.min.js
2015-05-15 17:35:18 +02:00

1 line
17 KiB
JavaScript

function sendAPI(t,e,i){"use strict";$.ajax({url:t,type:"POST",data:e,dataType:"json",timeout:1e4,success:function(t){"success"===t.status?void 0!==i&&i(t.data):console.warn(t.msg)}}).fail(function(){console.warn("Failed connecting to "+t),sendAPI(t,e,i)})}function getAPI(t,e){"use strict";$.ajax({url:t,dataType:"json",timeout:5e3,success:function(t){"success"===t.status?void 0!==e&&e(t.data):console.warn(t.msg)}}).fail(function(){console.warn("Failed connecting to "+t),getAPI(t,e)})}function downloadFile(t,e){"use strict";$(document.createElement("a")).attr({download:t,href:"data:text/plain,"+e})[0].click()}var D3D={version:"0.1",website:"http://www.doodle3d.com/",contact:"develop@doodle3d.com"};THREE.Vector2.prototype.normal=function(){"use strict";var t=this.y,e=-this.x;return this.set(t,e)},Array.prototype.clone=function(){"use strict";for(var t=[],e=0;e<this.length;e++)t[e]=this[e];return t},D3D.Box=function(t){"use strict";var e=this;this.batchSize=512,this.maxBufferedLines=4096,this.localIp=t,this.api="http://"+t+"/d3dapi/",this.config={},this.printBatches=[],this.currentBatch=0,this.loaded=!1,this.getConfigAll(function(t){e.updateConfig(t),e.printer=new D3D.Printer(t),e.update(),e.loaded=!0,void 0!==e.onload&&e.onload()})},D3D.Box.prototype.updateConfig=function(t){"use strict";for(var e in t)0===e.indexOf("doodle3d")&&(this.config[e]=t[e]);return this},D3D.Box.prototype.update=function(){"use strict";this.printBatches.length>0&&this.printer.status.buffered_lines+this.batchSize<=this.maxBufferedLines?this.printBatch():this.updateState()},D3D.Box.prototype.updateState=function(){"use strict";var t=this;this.getInfoStatus(function(e){t.printer.status=e,void 0!==t.onupdate&&t.onupdate(e),t.update()})},D3D.Box.prototype.print=function(t){"use strict";for(this.currentBatch=0,t=t.clone();t.length>0;){var e=t.splice(0,Math.min(this.batchSize,t.length));this.printBatches.push(e)}return this},D3D.Box.prototype.printBatch=function(){"use strict";var t=this,e=this.printBatches.shift();this.setPrinterPrint({start:0===this.currentBatch?!0:!1,first:0===this.currentBatch?!0:!1,gcode:e.join("\n")},function(e){console.log("batch sent: "+t.currentBatch,e),t.printBatches.length>0&&t.currentBatch++,t.updateState()})},D3D.Box.prototype.stopPrint=function(){"use strict";this.printBatches=[],this.currentBatch=0;var t=["M107 ;fan off","G91 ;relative positioning","G1 E-1 F300 ;retract the filament a bit before lifting the nozzle, to release some of the pressure","G1 Z+0.5 E-5 X-20 Y-20 F9000 ;move Z up a bit and retract filament even more","G28 X0 Y0 ;move X/Y to min endstops, so the head is out of the way","M84 ;disable axes / steppers","G90 ;absolute positioning","M104 S180",";M140 S70","M117 Done ;display message (20 characters to clear whole screen)"];return this.setPrinterStop({gcode:t.join("\n")},function(t){console.log("Printer stop command sent")}),this},D3D.Box.prototype.getConfig=function(t,e){"use strict";return getAPI(this.api+"config/?"+t.join("=&")+"=",e),this},D3D.Box.prototype.getConfigAll=function(t){"use strict";return getAPI(this.api+"config/all",t),this},D3D.Box.prototype.setConfig=function(t,e){"use strict";var i=this;return sendAPI(this.api+"config",t,function(r){for(var n in r.validation)"ok"!==r.validation[n]&&delete t[n];i.updateConfig(t),i.printer.updateConfig(t),void 0!==e&&e(r)}),this},D3D.Box.prototype.getInfo=function(t){"use strict";return getAPI(this.api+"info",t),this},D3D.Box.prototype.getInfoStatus=function(t){"use strict";return getAPI(this.api+"info/status",t),this},D3D.Box.prototype.downloadInfoLogFiles=function(){"use strict";window.location=this.api+"info/logfiles"},D3D.Box.prototype.getInfoAcces=function(t){"use strict";return getAPI(this.api+"info/access",t),this},D3D.Box.prototype.getNetworkScan=function(t){"use strict";return getAPI(this.api+"network/scan",t),this},D3D.Box.prototype.getNetworkKnown=function(t){"use strict";return getAPI(this.api+"network/known",t),this},D3D.Box.prototype.getNetworkStatus=function(t){"use strict";return getAPI(this.api+"network/status",t),this},D3D.Box.prototype.setNetworkAssosiate=function(t,e){"use strict";return sendAPI(this.api+"network/associate",t,e),this},D3D.Box.prototype.setNetworkDisassosiate=function(t){"use strict";return sendAPI(this.api+"network/disassociate",{},t),this},D3D.Box.prototype.setNetworkOpenAP=function(t){"use strict";return sendAPI(this.api+"network/openap",{},t),this},D3D.Box.prototype.setNetworkRemove=function(t,e){"use strict";return sendAPI(this.api+"network/remove",{ssid:t},e),this},D3D.Box.prototype.getNetworkSignin=function(t){"use strict";return getAPI(this.api+"network/signin",t),this},D3D.Box.prototype.getNetworkAlive=function(t){"use strict";return getAPI(this.api+"network/alive",t),this},D3D.Box.prototype.getPrinterTemperature=function(t){"use strict";return getAPI(this.api+"printer/temperature",t),this},D3D.Box.prototype.getPrinterProgress=function(t){"use strict";return getAPI(this.api+"printer/progress",t),this},D3D.Box.prototype.getPrinterState=function(t){"use strict";return getAPI(this.api+"printer/state",t),this},D3D.Box.prototype.getPrinterListAll=function(t){"use strict";return getAPI(this.api+"printer/listall",t),this},D3D.Box.prototype.setPrinterHeatup=function(t){"use strict";return sendAPI(this.api+"printer/heatup",{},t),this},D3D.Box.prototype.setPrinterPrint=function(t,e){"use strict";return sendAPI(this.api+"printer/print",t,e),this},D3D.Box.prototype.setPrinterStop=function(t,e){"use strict";return sendAPI(this.api+"printer/stop",t,e),this},D3D.Box.prototype.getSketch=function(t,e){"use strict";return getAPI(this.api+"sketch/?id="+t,e),this},D3D.Box.prototype.setSketch=function(t,e){"use strict";return sendAPI(this.api+"sketch",{data:t},e),this},D3D.Box.prototype.getSketchStatus=function(t){"use strict";return getAPI(this.api+"sketch/status",t),this},D3D.Box.prototype.setSketchClear=function(t){"use strict";return sendAPI(this.api+"sketch/clear",t),this},D3D.Box.prototype.getSystemVersions=function(t){"use strict";return getAPI(this.api+"system/fwversions",t),this},D3D.Box.prototype.getUpdateStatus=function(t){"use strict";return getAPI(this.api+"update/status",t),this},D3D.Box.prototype.setUpdateDownload=function(t){"use strict";return sendAPI(this.api+"update/download",{},t),this},D3D.Box.prototype.setUpdateInstall=function(t){"use strict";return sendAPI(this.api+"update/install",{},t),this},D3D.Box.prototype.setUpdateClear=function(t){"use strict";return sendAPI(this.api+"update/clear",{},t),this},D3D.Printer=function(t){"use strict";this.status={},this.config={},this.updateConfig(t)},D3D.Printer.prototype.updateConfig=function(t){"use strict";for(var e in t)0===e.indexOf("printer")&&(this.config[e]=t[e]);return this},D3D.Printer.prototype.getStartCode=function(){"use strict";var t=this.config["printer.startcode"];return t=this.subsituteVariables(t),t.split("\n")},D3D.Printer.prototype.getEndCode=function(){"use strict";var t=this.config["printer.endcode"];return t=this.subsituteVariables(t),t.split("\n")},D3D.Printer.prototype.subsituteVariables=function(t){"use strict";var e=this.config["printer.temperature"],i=this.config["printer.bed.temperature"],r=this.config["printer.heatup.temperature"],n=this.config["printer.heatup.bed.temperature"],s=this.config["printer.type"],o=this.config["printer.heatedbed"];switch(s){case"makerbot_replicator2":s="r2";break;case"makerbot_replicator2x":s="r2x";break;case"makerbot_thingomatic":s="t6";break;case"makerbot_generic":s="r2";break;case"_3Dison_plus":s="r2"}var a=o?"":";";return t=t.replace(/{printingTemp}/gi,e),t=t.replace(/{printingBedTemp}/gi,i),t=t.replace(/{preheatTemp}/gi,r),t=t.replace(/{preheatBedTemp}/gi,n),t=t.replace(/{printerType}/gi,s),t=t.replace(/{if heatedBed}/gi,a)},D3D.Paths=function(t,e){"use strict";Array.call(this),this.setPaths(t||[]),this.closed=void 0!==e?e:!0},D3D.Paths.prototype=Object.create(Array.prototype),D3D.Paths.prototype.setPaths=function(t){"use strict";for(var e=0;e<t.length;e++){var i=t[e];i.length>0&&this.push(i)}return this},D3D.Paths.prototype.clip=function(t,e){"use strict";var i=new ClipperLib.Paths,r=new ClipperLib.Clipper;return r.AddPaths(this,ClipperLib.PolyType.ptSubject,this.closed),r.AddPaths(t,ClipperLib.PolyType.ptClip,t.closed),r.Execute(e,i),new D3D.Paths(i,this.closed)},D3D.Paths.prototype.union=function(t){"use strict";return this.clip(t,ClipperLib.ClipType.ctUnion)},D3D.Paths.prototype.difference=function(t){"use strict";return this.clip(t,ClipperLib.ClipType.ctDifference)},D3D.Paths.prototype.intersect=function(t){"use strict";return this.clip(t,ClipperLib.ClipType.ctIntersection)},D3D.Paths.prototype.xor=function(){"use strict";return this.clip(path,ClipperLib.ClipType.ctXor)},D3D.Paths.prototype.offset=function(t){"use strict";var e=new ClipperLib.Paths,i=new ClipperLib.ClipperOffset(1,1);return i.AddPaths(this,ClipperLib.JoinType.jtRound,ClipperLib.EndType.etClosedPolygon),i.Execute(e,t),new D3D.Paths(e)},D3D.Paths.prototype.scaleUp=function(t){"use strict";ClipperLib.JS.ScaleUpPaths(this,t);return this},D3D.Paths.prototype.scaleDown=function(t){"use strict";ClipperLib.JS.ScaleDownPaths(this,t);return this},D3D.Paths.prototype.lastPoint=function(){"use strict";var t=this[this.length-1],e=this.closed?t[0]:t[t.length-1];return new THREE.Vector2(e.X,e.Y)},D3D.Paths.prototype.optimizePath=function(t){"use strict";for(var e=new D3D.Paths([],this.closed),i=[];e.length!==this.length;){for(var r,n,s,o,a=!1,c=0;c<this.length;c++){var p=this[c];if(-1===i.indexOf(c))if(this.closed)for(var h=0;h<p.length;h++){var u=new THREE.Vector2(p[h].X,p[h].Y),l=u.sub(t).length();(a===!1||a>l)&&(n=p,a=l,s=h,o=c)}else{var f=new THREE.Vector2(p[0].X,p[0].Y),l=f.sub(t).length();(a===!1||a>l)&&(n=p,a=l,r=!1,o=c);var g=new THREE.Vector2(p[p.length-1].X,p[p.length-1].Y),l=g.sub(t).length();a>l&&(n=p,a=l,r=!0,o=c)}}if(this.closed){n=n.concat(n.splice(0,s));var u=n[0]}else{r&&n.reverse();var u=n[n.length-1]}i.push(o),t=new THREE.Vector2(u.X,u.Y),e.push(n)}return e},D3D.Paths.prototype.tresholdArea=function(t){"use strict";for(var e=0;e<this.length;e++){var i=this[e],r=ClipperLib.Clipper.Area(i);t>r&&(this.splice(e,1),e--)}return areas},D3D.Paths.prototype.join=function(t){"use strict";for(var e=0;e<t.length;e++)this.push(t[e]);return this},D3D.Paths.prototype.clone=function(){"use strict";return new D3D.Paths(ClipperLib.JS.Clone(this),this.closed)},D3D.Paths.prototype.bounds=function(){"use strict";return ClipperLib.Clipper.GetBounds(this)},D3D.Paths.prototype.draw=function(t,e){"use strict";t.strokeStyle=e;for(var i=0;i<this.length;i++){var r=this[i];t.beginPath();for(var n=this.closed?r.length+1:r.length,s=0;n>s;s++){var o=r[s%r.length];t.lineTo(2*o.X,2*o.Y)}t.stroke()}},D3D.Slicer=function(){"use strict";this.lines=[]},D3D.Slicer.prototype.setMesh=function(t){"use strict";var e=t.geometry.clone();e instanceof THREE.BufferGeometry&&(e=(new THREE.Geometry).fromBufferGeometry(e));for(var i=0;i<e.vertices.length;i++)for(var r=e.vertices[i],n=i+1;n<e.vertices.length;n++){var s=e.vertices[n];r.equals(s)&&(e.vertices[n]=r)}return e.mergeVertices(),t.updateMatrix(),e.applyMatrix(t.matrix),e.computeFaceNormals(),this.geometry=e,this.createLines(),this},D3D.Slicer.prototype.createLines=function(){"use strict";function t(t,r){var n=e[t+"_"+r]||e[r+"_"+t];return void 0===n&&(n=i.lines.length,e[t+"_"+r]=n,i.lines.push({line:new THREE.Line3(i.geometry.vertices[t],i.geometry.vertices[r]),connects:[],normals:[],ignore:0})),n}this.lines=[];for(var e={},i=this,r=0;r<this.geometry.faces.length;r++){var n=this.geometry.faces[r],s=(new THREE.Vector2).set(n.normal.x,n.normal.z).normalize(),o=t(n.a,n.b),a=t(n.b,n.c),c=t(n.c,n.a);this.lines[o].connects.push(a,c),this.lines[a].connects.push(c,o),this.lines[c].connects.push(o,a),this.lines[o].normals.push(s),this.lines[a].normals.push(s),this.lines[c].normals.push(s)}},D3D.Slicer.prototype.slice=function(t,e){"use strict";for(var i=[],r=0;r<this.lines.length;r++)for(var n=this.lines[r],s=Math.ceil(Math.min(n.line.start.y,n.line.end.y)/e),o=Math.floor(Math.max(n.line.start.y,n.line.end.y)/e),a=s;o>=a;a++)a>=0&&(void 0===i[a]&&(i[a]=[]),i[a].push(r));for(var c=[],p=1;p<i.length-1;p++){for(var h=i[p],u=p*e,l=[],r=0;r<h.length;r++){var f=h[r],n=this.lines[f].line,g=(u-n.start.y)/(n.end.y-n.start.y),d=n.end.x*g+n.start.x*(1-g),D=n.end.z*g+n.start.z*(1-g);l[f]=new THREE.Vector2(d,D)}for(var y=[],v=new D3D.Paths([],!0),r=0;r<h.length;r++){var f=h[r];if(-1===y.indexOf(f)){for(var P=[];-1!==f;){var m=l[f];P.push({X:m.x,Y:m.y}),y.push(f);for(var w=this.lines[f].connects,x=this.lines[f].normals,b=0;b<w.length;b++)if(f=w[b],l[f]&&-1===y.indexOf(f)){var B=(new THREE.Vector2).set(m.x,m.y),T=l[f],A=B.sub(T).normal().normalize(),S=x[Math.floor(b/2)];if(A.dot(S)>0)break;f=-1}else f=-1}P.length>0&&v.push(new D3D.Paths([P]))}}for(var k=[],r=0;r<v.length;r++){for(var C=v[r],I=!1,b=0;b<k.length;b++){var E=k[b];if(E.intersect(C).length>0){E.join(C),I=!0;break}}I||k.push(C)}if(!(k.length>0))break;c.push(k)}return c},D3D.Slicer.prototype.slicesToData=function(t,e){"use strict";for(var i=100,r=e.config["printer.layerHeight"]*i,n=e.config["printer.dimensions.z"]*i,s=e.config["printer.wallThickness"]*i,o=e.config["printer.shellThickness"]*i,a=e.config["printer.fillSize"]*i,c=(e.config["printer.brimOffset"]*i,Math.ceil(o/r)),p=new THREE.Vector2(0,0),h=[],u=this.getFillTemplate({left:0,top:0,right:n,bottom:n},a,!0,!0),l=0;l<t.length;l++){var f=t[l],g=[];h.push(g);var d=new D3D.Paths([],!0);if(l-c>=0)for(var D=t[l-c],y=0;y<D.length;y++)d.join(D[y]);var v=new D3D.Paths([],!0);if(l+c<t.length)for(var D=t[l+c],y=0;y<D.length;y++)v.join(D[y]);for(var P=v.intersect(d).clone().scaleUp(i),y=0;y<f.length;y++){var m=f[y],w=m.clone();w.scaleUp(i);for(var x=new D3D.Paths([],!0),b=s;o>=b;b+=s){var B=w.offset(-b);x.join(B)}var T=(B||w).offset(-s/2),A=T.difference(P),S=T.difference(A),k=new D3D.Paths([],!1);if(u.length>0&&k.join(u.intersect(S)),A.length>0){var C=A.bounds(),I=l%2===0,E=this.getFillTemplate(C,s,I,!I);k.join(E.intersect(A))}w=w.optimizePath(p),x.length>0?(x=x.optimizePath(w.lastPoint()),k=k.optimizePath(x.lastPoint())):k=k.optimizePath(w.lastPoint()),p=k.length>0?k.lastPoint():x.length>0?x.lastPoint():w.lastPoint(),g.push({outerLayer:w.scaleDown(i),fill:k.scaleDown(i),insets:x.scaleDown(i)})}}return h},D3D.Slicer.prototype.getFillTemplate=function(t,e,i,r){"use strict";var n=new D3D.Paths([],!1);if(i)for(var s=Math.floor(t.left);s<=Math.ceil(t.right);s+=e)n.push([{X:s,Y:t.top},{X:s,Y:t.bottom}]);if(r)for(var s=Math.floor(t.top);s<=Math.floor(t.bottom);s+=e)n.push([{X:t.left,Y:s},{X:t.right,Y:s}]);return n},D3D.Slicer.prototype.dataToGcode=function(t,e){"use strict";function i(t){for(var e=[],i=0;i<t.length;i++)for(var n,s=t[i],o=t.closed?s.length+1:s.length,a=0;o>a;a++){var p=s[a%s.length];if(0===a)D>f&&u&&e.push(["G0","E"+(D-g).toFixed(3),"F"+(60*l).toFixed(3)].join(" ")),e.push(["G0","X"+p.X.toFixed(3)+" Y"+p.Y.toFixed(3)+" Z"+x,"F"+60*c].join(" ")),D>f&&u&&e.push(["G0","E"+D.toFixed(3),"F"+(60*l).toFixed(3)].join(" "));else{var d=(new THREE.Vector2).set(p.X,p.Y),m=(new THREE.Vector2).set(n.X,n.Y),w=d.distanceTo(m);D+=w*h*r/v*P,e.push(["G1","X"+p.X.toFixed(3)+" Y"+p.Y.toFixed(3)+" Z"+x,"F"+y,"E"+D.toFixed(3)].join(" "))}n=p}return e}for(var r=e.config["printer.layerHeight"],n=e.config["printer.speed"],s=e.config["printer.bottomLayerSpeed"],o=e.config["printer.firstLayerSlow"],a=e.config["printer.bottomFlowRate"],c=e.config["printer.travelSpeed"],p=e.config["printer.filamentThickness"],h=e.config["printer.wallThickness"],u=(e.config["printer.enableTraveling"],e.config["printer.retraction.enabled"]),l=e.config["printer.retraction.speed"],f=e.config["printer.retraction.minDistance"],g=e.config["printer.retraction.amount"],d=e.getStartCode(),D=0,y=o?(60*s).toFixed(3):(60*n).toFixed(3),v=Math.pow(p/2,2)*Math.PI,P=a,m=0;m<t.length;m++){var w=t[m];2===m&&(d.push("M106"),y=(60*n).toFixed(3),P=1);for(var x=((m+1)*r).toFixed(3),b=0;b<w.length;b++){var B=w[b];d=d.concat(i(B.outerLayer)),d=d.concat(i(B.insets)),d=d.concat(i(B.fill))}}return d=d.concat(e.getEndCode())},D3D.Slicer.prototype.drawPaths=function(t,e,i){"use strict";var r=t.config["printer.layerHeight"],n=t.config["printer.dimensions.z"],s=this.slice(n,r),o=this.slicesToData(s,t),a=document.createElement("canvas");a.width=400,a.height=400;for(var c=a.getContext("2d"),p=e;i>p;p++)for(var h=o[p%o.length],u=0;u<h.length;u++){var l=h[u];l.insets.draw(c,"blue"),l.outerLayer.draw(c,"green"),l.fill.draw(c,"red")}return a},D3D.Slicer.prototype.getGcode=function(t){"use strict";var e=t.config["printer.layerHeight"],i=t.config["printer.dimensions.z"],r=(new Date).getTime(),n=this.slice(i,e),s=(new Date).getTime();console.log("Slicing: "+(s-r)+"ms");var r=(new Date).getTime(),o=this.slicesToData(n,t),s=(new Date).getTime();console.log("Data: "+(s-r)+"ms");var r=(new Date).getTime(),a=this.dataToGcode(o,t),s=(new Date).getTime();return console.log("Gcode: "+(s-r)+"ms"),a};