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;t0?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;en;n+=t){r.set(new THREE.Vector3(0,-1,0),n);for(var o=[],s=[],a=0;a0)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;ld;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;if&&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;Cl;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};