Doodle3D-Slicer/build/d3d.min.js
2015-05-01 14:09:45 +02:00

1 line
11 KiB
JavaScript

function sendAPI(e,t,i){"use strict";$.ajax({url:e,type:"POST",data:t,dataType:"json",timeout:1e4,success:function(e){"success"===e.status?void 0!==i&&i(e.data):console.warn(e.msg)}}).fail(function(){console.warn("failed connecting to "+e),sendAPI(e,t,i)})}function getAPI(e,t){"use strict";$.ajax({url:e,dataType:"json",timeout:5e3,success:function(e){"success"===e.status?void 0!==t&&t(e.data):console.warn(e.msg)}}).fail(function(){console.warn("failed connecting to "+e),getAPI(e,t)})}function downloadFile(e,t){"use strict";$(document.createElement("a")).attr({download:e,href:"data:text/plain,"+t})[0].click()}function applyMouseControls(e,t,i){"use strict";function r(){t.position.x=Math.cos(s)*Math.sin(o)*n,t.position.y=Math.sin(s)*n,t.position.z=Math.cos(s)*Math.cos(o)*n,t.lookAt(new THREE.Vector3(0,0,0))}var n=20,o=0,s=0,a=!1;$(e.domElement).on("mousedown",function(e){a=!0}).on("wheel",function(e){var t=e.originalEvent;t.preventDefault(),n=THREE.Math.clamp(n-t.wheelDelta,1,i),r()}),$(window).on("mouseup",function(e){a=!1}).on("mousemove",function(e){var t=e.originalEvent;a===!0&&(o=(o-t.webkitMovementX/100)%(2*Math.PI),s=THREE.Math.clamp(s+t.webkitMovementY/100,-Math.PI/2,Math.PI/2),r())}),r()}var D3D={version:"0.1",website:"http://www.doodle3d.com/",contact:"develop@doodle3d.com"};THREE.Vector2.prototype.normal=function(){"use strict";var e=this.y,t=-this.x;return this.set(e,t)},Array.prototype.clone=function(){"use strict";for(var e=[],t=0;t<this.length;t++)e[t]=this[t];return e};var requestAnimFrame=function(){"use strict";return requestAnimationFrame||webkitRequestAnimationFrame||mozRequestAnimationFrame||function(e){setTimeout(e,1e3/60)}}();D3D.Box=function(e){"use strict";var t=this;this.batchSize=512,this.maxBufferedLines=4096,this.localIp=e,this.api="http://"+e+"/d3dapi/",this.config={},this.printBatches=[],this.currentBatch=0,this.loaded=!1,this.onload,getAPI(t.api+"config/all",function(e){for(var i in e)0===i.indexOf("doodle3d")&&(t.config[i]=e[i]);t.printer=new D3D.Printer(e),t.update(),t.loaded=!0,void 0!==t.onload&&t.onload()})},D3D.Box.prototype.update=function(){"use strict";this.printBatches.length>0?this.printBatch():this.updateState()},D3D.Box.prototype.updateState=function(){"use strict";var e=this;getAPI(this.api+"info/status",function(t){e.printer.status=t,e.update()})},D3D.Box.prototype.print=function(e){"use strict";for(this.currentBatch=0,e=e.clone();e.length>0;){var t=e.splice(0,Math.min(this.batchSize,e.length));this.printBatches.push(t)}},D3D.Box.prototype.printBatch=function(){"use strict";var e=this,t=this.printBatches.shift();sendAPI(this.api+"printer/print",{start:0===this.currentBatch?"true":"false",first:0===this.currentBatch?"true":"false",gcode:t.join("\n")},function(t){console.log("batch sent: "+e.currentBatch,t),e.printBatches.length>0&&e.currentBatch++,e.updateState()})},D3D.Box.prototype.stop=function(){"use strict";this.printBatches=[],this.currentBatch=0;var e=["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)"];sendAPI(this.api+"printer/stop",{gcode:e.join("\n")},function(e){console.log("Printer stop command sent")})},D3D.Printer=function(e){"use strict";this.status={},this.config={};for(var t in e)0===t.indexOf("printer")&&(this.config[t]=e[t])},D3D.Printer.prototype.getStartCode=function(){"use strict";var e=this.config["printer.startcode"];return e=this.subsituteVariables(e),e.split("\n")},D3D.Printer.prototype.getEndCode=function(){"use strict";var e=this.config["printer.endcode"];return e=this.subsituteVariables(e),e.split("\n")},D3D.Printer.prototype.subsituteVariables=function(e){"use strict";var t=this.config["printer.temperature"],i=this.config["printer.bed.temperature"],r=this.config["printer.heatup.temperature"],n=this.config["printer.heatup.bed.temperature"],o=this.config["printer.type"],s=this.config["printer.heatedbed"];switch(o){case"makerbot_replicator2":o="r2";break;case"makerbot_replicator2x":o="r2x";break;case"makerbot_thingomatic":o="t6";break;case"makerbot_generic":o="r2";break;case"_3Dison_plus":o="r2"}var a=s?"":";";return e=e.replace(/{printingTemp}/gi,t),e=e.replace(/{printingBedTemp}/gi,i),e=e.replace(/{preheatTemp}/gi,r),e=e.replace(/{preheatBedTemp}/gi,n),e=e.replace(/{printerType}/gi,o),e=e.replace(/{if heatedBed}/gi,a)},D3D.Slicer=function(){"use strict";this.geometry,this.lines=[],this.lineLookup={}},D3D.Slicer.prototype.setGeometry=function(e){"use strict";return this.geometry=e,this.geometry.mergeVertices(),this.createLines(),this},D3D.Slicer.prototype.addLine=function(e,t){"use strict";var i=this.lineLookup[e+"_"+t]||this.lineLookup[t+"_"+e];return void 0===i&&(i=this.lines.length,this.lineLookup[e+"_"+t]=i,this.lines.push({line:new THREE.Line3(this.geometry.vertices[e],this.geometry.vertices[t]),connects:[],normals:[]})),i},D3D.Slicer.prototype.createLines=function(){"use strict";this.lines=[],this.lineLookup={};for(var e=0;e<this.geometry.faces.length;e++){var t=this.geometry.faces[e],i=(new THREE.Vector2).set(t.normal.x,t.normal.z).normalize(),r=this.addLine(t.a,t.b),n=this.addLine(t.b,t.c),o=this.addLine(t.c,t.a);this.lines[r].connects.push(n,o),this.lines[n].connects.push(o,r),this.lines[o].connects.push(r,n),this.lines[r].normals.push(i),this.lines[n].normals.push(i),this.lines[o].normals.push(i)}},D3D.Slicer.prototype.slice=function(e,t){"use strict";for(var i=[],r=new THREE.Plane,n=0;e>n;n+=t){r.set(new THREE.Vector3(0,-1,0),n);for(var o=[],s=[],a=0;a<this.lines.length;a++){var p=this.lines[a].line,c=r.intersectLine(p);if(void 0!==c){var l=new THREE.Vector2(c.x+100,c.z+100);s.push(l)}else s.push(!1)}for(var h=[],a=0;a<s.length;a++)if(s[a]&&-1===h.indexOf(a)){for(var u=a,f=[];-1!==u;){var c=s[u];f.push({X:c.x,Y:c.y}),h.push(u);for(var d=this.lines[u].connects,g=this.lines[u].normals,m=0;m<d.length;m++)if(u=d[m],s[u]&&-1===h.indexOf(u)){var v=(new THREE.Vector2).set(c.x,c.y),b=s[u],y=v.sub(b).normal().normalize(),C=g[Math.floor(m/2)];if(y.dot(C)>0)break;u=-1}else u=-1}f.length>0&&o.push(f)}if(!(o.length>0))break;i.push(o)}return i},D3D.Slicer.prototype.getInset=function(e,t){"use strict";var i=new ClipperLib.Paths,r=new ClipperLib.ClipperOffset(1,1);return r.AddPaths(e,ClipperLib.JoinType.jtRound,ClipperLib.EndType.etClosedPolygon),r.Execute(i,-t),i},D3D.Slicer.prototype.getFillTemplate=function(e,t,i,r){"use strict";var n=new ClipperLib.Paths;if(i)for(var o=0;e>=o;o+=t)n.push([{X:o,Y:0},{X:o,Y:e}]);if(r)for(var o=0;e>=o;o+=t)n.push([{X:0,Y:o},{X:e,Y:o}]);return n},D3D.Slicer.prototype.slicesToData=function(e,t){"use strict";for(var i=100,r=t.config["printer.layerHeight"]*i,n=t.config["printer.dimensions.z"]*i,o=t.config["printer.wallThickness"]*i,s=t.config["printer.shellThickness"]*i,a=t.config["printer.fillSize"]*i,p=(t.config["printer.brimOffset"]*i,[]),c=this.getFillTemplate(n,a,!0,!0),l=0;l<e.length;l++){var h=e[l],u=h.clone();ClipperLib.JS.ScaleUpPaths(u,i);for(var f=[],d=o;s>d;d+=o){var g=this.getInset(u,d);f=f.concat(g)}for(var m=this.getInset(g||u,o),v=void 0,d=1;s/r+4>d;d++){var b=ClipperLib.JS.Clone(e[l+d]);if(ClipperLib.JS.ScaleUpPaths(b,i),0===b.length||v&&0===v.length){v=[];break}if(void 0===v)v=b;else{var y=new ClipperLib.Clipper,C=new ClipperLib.Paths;y.AddPaths(m,ClipperLib.PolyType.ptSubject,!0),y.AddPaths(v,ClipperLib.PolyType.ptClip,!0),y.Execute(ClipperLib.ClipType.ctIntersection,C),v=C}}var D=new ClipperLib.Clipper,L=new ClipperLib.Paths;D.AddPaths(m,ClipperLib.PolyType.ptSubject,!0),D.AddPaths(v,ClipperLib.PolyType.ptClip,!0),D.Execute(ClipperLib.ClipType.ctDifference,L);var D=new ClipperLib.Clipper,P=new ClipperLib.Paths;D.AddPaths(m,ClipperLib.PolyType.ptSubject,!0),D.AddPaths(L,ClipperLib.PolyType.ptClip,!0),D.Execute(ClipperLib.ClipType.ctDifference,P);var w=[],D=new ClipperLib.Clipper,T=new ClipperLib.Paths;D.AddPaths(c,ClipperLib.PolyType.ptSubject,!1),D.AddPaths(P,ClipperLib.PolyType.ptClip,!0),D.Execute(ClipperLib.ClipType.ctIntersection,T),w=w.concat(T);var x=this.getFillTemplate(n,o,l%2===0,l%2===1),D=new ClipperLib.Clipper,E=new ClipperLib.Paths;D.AddPaths(x,ClipperLib.PolyType.ptSubject,!1),D.AddPaths(L,ClipperLib.PolyType.ptClip,!0),D.Execute(ClipperLib.ClipType.ctIntersection,E),w=w.concat(E),ClipperLib.JS.ScaleDownPaths(u,i),ClipperLib.JS.ScaleDownPaths(f,i),ClipperLib.JS.ScaleDownPaths(w,i),p.push({outerLayer:u,innerLayer:f,fill:w})}return p},D3D.Slicer.prototype.dataToGcode=function(e,t){"use strict";function i(e){for(var t=[],i=0;i<e.length;i++)for(var n,o=e[i],s=0;s<=o.length;s++){var a=o[s%o.length];if(0===s)m>f&&h&&t.push(["G0","E"+(m-d).toFixed(3),"F"+(60*u).toFixed(3)].join(" ")),t.push(["G0","X"+a.X.toFixed(3)+" Y"+a.Y.toFixed(3)+" Z"+L,"F"+60*p].join(" ")),m>f&&h&&t.push(["G0","E"+m.toFixed(3),"F"+(60*u).toFixed(3)].join(" "));else{var c=(new THREE.Vector2).set(a.X,a.Y),g=(new THREE.Vector2).set(n.X,n.Y),C=c.distanceTo(g);m+=C*l*r/b*y,t.push(["G1","X"+a.X.toFixed(3)+" Y"+a.Y.toFixed(3)+" Z"+L,"F"+v,"E"+m.toFixed(3)].join(" "))}n=a}return t}for(var r=t.config["printer.layerHeight"],n=t.config["printer.speed"],o=t.config["printer.bottomLayerSpeed"],s=t.config["printer.firstLayerSlow"],a=t.config["printer.bottomFlowRate"],p=t.config["printer.travelSpeed"],c=t.config["printer.filamentThickness"],l=t.config["printer.wallThickness"],h=(t.config["printer.enableTraveling"],t.config["printer.retraction.enabled"]),u=t.config["printer.retraction.speed"],f=t.config["printer.retraction.minDistance"],d=t.config["printer.retraction.amount"],g=t.getStartCode(),m=0,v=s?(60*o).toFixed(3):(60*n).toFixed(3),b=Math.pow(c/2,2)*Math.PI,y=a,C=0;C<e.length;C++){var D=e[C];2===C&&(g.push("M106"),v=(60*n).toFixed(3),y=1);var L=((C+1)*r).toFixed(3);g=g.concat(i(D.outerLayer)),g=g.concat(i(D.innerLayer)),g=g.concat(i(D.fill))}return g=g.concat(t.getEndCode())},D3D.Slicer.prototype.drawPaths=function(e,t,i){"use strict";function r(e,t){c.fillStyle=t,c.strokeStyle=t,c.beginPath();for(var i=0;i<e.length;i++){var r=e[i];c.moveTo(6*(r[0].X-100)+200,6*(r[0].Y-100)+200);for(var n=0;n<r.length;n++){var o=r[n];c.lineTo(6*(o.X-100)+200,6*(o.Y-100)+200)}c.closePath()}c.stroke()}var n=e.config["printer.layerHeight"],o=e.config["printer.dimensions.z"],s=this.slice(o,n);s.shift();var a=this.slicesToData(s,e),p=document.createElement("canvas");p.width=400,p.height=400;for(var c=p.getContext("2d"),l=t;i>l;l++){var l=0;c.clearRect(0,0,400,400);var h=a[l%a.length];r(h.outerLayer,"red"),r(h.innerLayer,"green"),r(h.fill,"blue")}return p},D3D.Slicer.prototype.getGcode=function(e){"use strict";var t=e.config["printer.layerHeight"],i=e.config["printer.dimensions.z"],r=this.slice(i,t);r.shift();var n=this.slicesToData(r,e),o=this.dataToGcode(n,e);return o};