Doodle3D-Slicer/build/d3d.min.js
2015-05-12 11:29:01 +02:00

1 line
15 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.Path=function(t,e){"use strict";this.path=t||[],this.closed=void 0!==e?e:!0},D3D.Path.prototype.setPath=function(t){"use strict";return this.path=t,this},D3D.Path.prototype.union=function(t){"use strict";var e=new ClipperLib.Paths,i=new ClipperLib.Clipper;return i.AddPaths(this.path,ClipperLib.PolyType.ptSubject,this.closed),i.AddPaths(t.path,ClipperLib.PolyType.ptClip,t.closed),i.Execute(ClipperLib.ClipType.ctUnion,e),new D3D.Path(e,this.closed)},D3D.Path.prototype.difference=function(t){"use strict";var e=new ClipperLib.Paths,i=new ClipperLib.Clipper;return i.AddPaths(this.path,ClipperLib.PolyType.ptSubject,this.closed),i.AddPaths(t.path,ClipperLib.PolyType.ptClip,t.closed),i.Execute(ClipperLib.ClipType.ctDifference,e),new D3D.Path(e,this.closed)},D3D.Path.prototype.intersect=function(t){"use strict";var e=new ClipperLib.Paths,i=new ClipperLib.Clipper;return i.AddPaths(this.path,ClipperLib.PolyType.ptSubject,this.closed),i.AddPaths(t.path,ClipperLib.PolyType.ptClip,t.closed),i.Execute(ClipperLib.ClipType.ctIntersection,e),new D3D.Path(e,this.closed)},D3D.Path.prototype.xor=function(){"use strict";var t=new ClipperLib.Paths,e=new ClipperLib.Clipper;return e.AddPaths(this.path,ClipperLib.PolyType.ptSubject,this.closed),e.AddPaths(path.path,ClipperLib.PolyType.ptClip,path.closed),e.Execute(ClipperLib.ClipType.ctXor,t),new D3D.Path(t,this.closed)},D3D.Path.prototype.offset=function(t){"use strict";var e=new ClipperLib.Paths,i=new ClipperLib.ClipperOffset(1,1);return i.AddPaths(this.path,ClipperLib.JoinType.jtRound,ClipperLib.EndType.etClosedPolygon),i.Execute(e,t),new D3D.Path(e,this.closed)},D3D.Path.prototype.scaleUp=function(t){"use strict";ClipperLib.JS.ScaleUpPaths(this.path,t);return this},D3D.Path.prototype.scaleDown=function(t){"use strict";ClipperLib.JS.ScaleDownPaths(this.path,t);return this},D3D.Path.prototype.tresholdArea=function(t){"use strict";for(var e=0;e<this.path.length;e++){var i=this.path[e],r=ClipperLib.Clipper.Area(i);t>r&&(this.path.splice(e,1),e--)}return areas},D3D.Path.prototype.area=function(){"use strict";for(var t=[],e=0;e<this.path.length;e++){var i=this.path[e];t.push(ClipperLib.Clipper.Area(i))}return t},D3D.Path.prototype.join=function(t){"use strict";return this.path=this.path.concat(t.path),this},D3D.Path.prototype.clone=function(){"use strict";var t=ClipperLib.JS.Clone(this.path);return new D3D.Path(t,this.closed)},D3D.Path.prototype.draw=function(t,e){"use strict";t.strokeStyle=e;for(var i=0;i<this.path.length;i++){var r=this.path[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";t.updateMatrix();var e=t.geometry.clone();return e.mergeVertices(),e.applyMatrix(t.matrix),e instanceof THREE.BufferGeometry&&(e=(new THREE.Geometry).fromBufferGeometry(e)),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:[]})),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),p=t(n.c,n.a);this.lines[o].connects.push(a,p),this.lines[a].connects.push(p,o),this.lines[p].connects.push(o,a),this.lines[o].normals.push(s),this.lines[a].normals.push(s),this.lines[p].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 p=[],c=1;c<i.length;c++){for(var h=i[c],u=c*e,l=[],r=0;r<h.length;r++){var f=h[r],n=this.lines[f].line,d=(u-n.start.y)/(n.end.y-n.start.y),D=n.end.x*d+n.start.x*(1-d),g=n.end.z*d+n.start.z*(1-d);l[f]=new THREE.Vector2(D,g)}for(var y=[],P=[],r=0;r<h.length;r++){var f=h[r];if(-1===y.indexOf(f)){for(var v=[];-1!==f;){var w=l[f];v.push({X:w.x,Y:w.y}),y.push(f);for(var m=this.lines[f].connects,x=this.lines[f].normals,b=0;b<m.length;b++)if(f=m[b],l[f]&&-1===y.indexOf(f)){var C=(new THREE.Vector2).set(w.x,w.y),B=l[f],A=C.sub(B).normal().normalize(),T=x[Math.floor(b/2)];if(A.dot(T)>0)break;f=-1}else f=-1}v.length>0&&P.push(v)}}if(!(P.length>0))break;p.push(new D3D.Path(P,!0))}return p},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,p=(e.config["printer.brimOffset"]*i,Math.ceil(o/r)),c=[],h=this.getFillTemplate(n,a,!0,!0),u=0;u<t.length;u++){var l=t[u],f=l.clone();f.scaleUp(i);for(var d=new D3D.Path,D=s;o>=D;D+=s){var g=f.offset(-D);d.join(g)}var y=(g||f).offset(-s/2),P=u-p>=0?t[u-p]:new D3D.Path,v=u+p<t.length?t[u+p]:new D3D.Path,w=y.difference(P.intersect(v).scaleUp(i)),m=y.difference(w),x=new D3D.Path([],!1);if(x.join(h.intersect(m)),w.path.length>0){var b=this.getFillTemplate(n,s,u%2===0,u%2===1);x.join(b.intersect(w))}c.push({outerLayer:f.scaleDown(i),insets:d.scaleDown(i),fill:x.scaleDown(i)})}return c},D3D.Slicer.prototype.getFillTemplate=function(t,e,i,r){"use strict";var n=[];if(i)for(var s=0;t>=s;s+=e)n.push([{X:s,Y:0},{X:s,Y:t}]);if(r)for(var s=0;t>=s;s+=e)n.push([{X:0,Y:s},{X:t,Y:s}]);return new D3D.Path(n,!1)},D3D.Slicer.prototype.dataToGcode=function(t,e){"use strict";function i(t){for(var e=[],i=0;i<t.path.length;i++)for(var n,s=t.path[i],o=0;o<s.length;o++){var a=s[o];if(0===o)g>f&&u&&e.push(["G0","E"+(g-d).toFixed(3),"F"+(60*l).toFixed(3)].join(" ")),e.push(["G0","X"+a.X.toFixed(3)+" Y"+a.Y.toFixed(3)+" Z"+x,"F"+60*p].join(" ")),g>f&&u&&e.push(["G0","E"+g.toFixed(3),"F"+(60*l).toFixed(3)].join(" "));else{var c=(new THREE.Vector2).set(a.X,a.Y),D=(new THREE.Vector2).set(n.X,n.Y),w=c.distanceTo(D);g+=w*h*r/P*v,e.push(["G1","X"+a.X.toFixed(3)+" Y"+a.Y.toFixed(3)+" Z"+x,"F"+y,"E"+g.toFixed(3)].join(" "))}n=a}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"],p=e.config["printer.travelSpeed"],c=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"],d=e.config["printer.retraction.amount"],D=e.getStartCode(),g=0,y=o?(60*s).toFixed(3):(60*n).toFixed(3),P=Math.pow(c/2,2)*Math.PI,v=a,w=0;w<t.length;w++){var m=t[w];2===w&&(D.push("M106"),y=(60*n).toFixed(3),v=1);var x=((w+1)*r).toFixed(3);D=D.concat(i(m.outerLayer)),D=D.concat(i(m.insets)),D=D.concat(i(m.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 p=a.getContext("2d"),c=e;i>c;c++){var h=o[c%o.length];h.insets.draw(p,"blue"),h.outerLayer.draw(p,"green"),h.fill.draw(p,"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};