-
Notifications
You must be signed in to change notification settings - Fork 1
/
gcode2dviewer.min.js
5 lines (4 loc) · 24.8 KB
/
gcode2dviewer.min.js
1
2
3
4
5
/**
* Written by Alex Canales for ShopBotTools, Inc.
*/
var GParser=function(){function a(a,b){function c(){this.constructor=a}c.prototype=b.prototype,a.prototype=new c}function b(a,b,c,d,e,f){this.message=a,this.expected=b,this.found=c,this.offset=d,this.line=e,this.column=f,this.name="SyntaxError"}function c(a){function c(){return a.substring(Jb,Ib)}function d(b){function c(b,c,d){var e,f;for(e=c;d>e;e++)f=a.charAt(e),"\n"===f?(b.seenCR||b.line++,b.column=1,b.seenCR=!1):"\r"===f||"\u2028"===f||"\u2029"===f?(b.line++,b.column=1,b.seenCR=!0):(b.column++,b.seenCR=!1)}return Kb!==b&&(Kb>b&&(Kb=0,Lb={line:1,column:1,seenCR:!1}),c(Lb,Kb,b),Kb=b),Lb}function e(a){Mb>Ib||(Ib>Mb&&(Mb=Ib,Nb=[]),Nb.push(a))}function f(c,e,f){function g(a){var b=1;for(a.sort(function(a,b){return a.description<b.description?-1:a.description>b.description?1:0});b<a.length;)a[b-1]===a[b]?a.splice(b,1):b++}function h(a,b){function c(a){function b(a){return a.charCodeAt(0).toString(16).toUpperCase()}return a.replace(/\\/g,"\\\\").replace(/"/g,'\\"').replace(/\x08/g,"\\b").replace(/\t/g,"\\t").replace(/\n/g,"\\n").replace(/\f/g,"\\f").replace(/\r/g,"\\r").replace(/[\x00-\x07\x0B\x0E\x0F]/g,function(a){return"\\x0"+b(a)}).replace(/[\x10-\x1F\x80-\xFF]/g,function(a){return"\\x"+b(a)}).replace(/[\u0180-\u0FFF]/g,function(a){return"\\u0"+b(a)}).replace(/[\u1080-\uFFFF]/g,function(a){return"\\u"+b(a)})}var d,e,f,g=new Array(a.length);for(f=0;f<a.length;f++)g[f]=a[f].description;return d=a.length>1?g.slice(0,-1).join(", ")+" or "+g[a.length-1]:g[0],e=b?'"'+c(b)+'"':"end of input","Expected "+d+" but "+e+" found."}var i=d(f),j=f<a.length?a.charAt(f):null;return null!==e&&g(e),new b(null!==c?c:h(e,j),e,j,f,i.line,i.column)}function g(){var a;return a=h()}function h(){var a,b,c,d;if(a=Ib,b=j(),b===B&&(b=F),b!==B){for(c=[],d=i();d!==B;)c.push(d),d=i();c!==B?(Jb=a,b=G(b,c),a=b):(Ib=a,a=E)}else Ib=a,a=E;return a}function i(){var a,b,c;return a=Ib,b=y(),b!==B?(c=q(),c!==B?(Jb=a,b=H(b,c),a=b):(Ib=a,a=E)):(Ib=a,a=E),a}function j(){var b,c,d;return b=Ib,78===a.charCodeAt(Ib)?(c=I,Ib++):(c=B,0===Ob&&e(J)),c!==B?(d=k(),d!==B?(c=[c,d],b=c):(Ib=b,b=E)):(Ib=b,b=E),b}function k(){var b,c,d;if(b=Ib,c=[],K.test(a.charAt(Ib))?(d=a.charAt(Ib),Ib++):(d=B,0===Ob&&e(L)),d!==B)for(;d!==B;)c.push(d),K.test(a.charAt(Ib))?(d=a.charAt(Ib),Ib++):(d=B,0===Ob&&e(L));else c=E;return c!==B&&(Jb=b,c=M()),b=c}function l(){var b,c,d,f,g,h,i;if(b=Ib,N.test(a.charAt(Ib))?(c=a.charAt(Ib),Ib++):(c=B,0===Ob&&e(O)),c===B&&(c=F),c!==B){if(d=[],K.test(a.charAt(Ib))?(f=a.charAt(Ib),Ib++):(f=B,0===Ob&&e(L)),f!==B)for(;f!==B;)d.push(f),K.test(a.charAt(Ib))?(f=a.charAt(Ib),Ib++):(f=B,0===Ob&&e(L));else d=E;if(d!==B){if(f=Ib,P.test(a.charAt(Ib))?(g=a.charAt(Ib),Ib++):(g=B,0===Ob&&e(Q)),g!==B){if(h=[],K.test(a.charAt(Ib))?(i=a.charAt(Ib),Ib++):(i=B,0===Ob&&e(L)),i!==B)for(;i!==B;)h.push(i),K.test(a.charAt(Ib))?(i=a.charAt(Ib),Ib++):(i=B,0===Ob&&e(L));else h=E;h!==B?(g=[g,h],f=g):(Ib=f,f=E)}else Ib=f,f=E;f===B&&(f=F),f!==B?(Jb=b,c=R(),b=c):(Ib=b,b=E)}else Ib=b,b=E}else Ib=b,b=E;return b}function m(){var b,c,d,f;return b=Ib,91===a.charCodeAt(Ib)?(c=S,Ib++):(c=B,0===Ob&&e(T)),c!==B?(d=t(),d!==B?(93===a.charCodeAt(Ib)?(f=U,Ib++):(f=B,0===Ob&&e(V)),f!==B?(Jb=b,c=W(d),b=c):(Ib=b,b=E)):(Ib=b,b=E)):(Ib=b,b=E),b}function n(){var b,c,d,f,g;return b=Ib,a.substr(Ib,4)===X?(c=X,Ib+=4):(c=B,0===Ob&&e(Y)),c!==B?(d=m(),d!==B?(47===a.charCodeAt(Ib)?(f=Z,Ib++):(f=B,0===Ob&&e($)),f!==B?(g=m(),g!==B?(Jb=b,c=_(d,g),b=c):(Ib=b,b=E)):(Ib=b,b=E)):(Ib=b,b=E)):(Ib=b,b=E),b}function o(){var a,b,c;return a=Ib,b=x(),b!==B?(c=m(),c!==B?(Jb=a,b=aa(b,c),a=b):(Ib=a,a=E)):(Ib=a,a=E),a}function p(){var b,c,d;return b=Ib,35===a.charCodeAt(Ib)?(c=ba,Ib++):(c=B,0===Ob&&e(ca)),c!==B?(d=m(),d===B&&(d=l(),d===B&&(d=p())),d!==B?(Jb=b,c=da(d),b=c):(Ib=b,b=E)):(Ib=b,b=E),b}function q(){var a;return a=m(),a===B&&(a=l(),a===B&&(a=n(),a===B&&(a=o(),a===B&&(a=p())))),a}function r(){var a,b,c,d,e,f;if(a=Ib,b=q(),b!==B){for(c=[],d=Ib,e=u(),e!==B?(f=q(),f!==B?(e=[e,f],d=e):(Ib=d,d=E)):(Ib=d,d=E);d!==B;)c.push(d),d=Ib,e=u(),e!==B?(f=q(),f!==B?(e=[e,f],d=e):(Ib=d,d=E)):(Ib=d,d=E);c!==B?(Jb=a,b=ea(b,c),a=b):(Ib=a,a=E)}else Ib=a,a=E;return a}function s(){var a,b,c,d,e,f;if(a=Ib,b=r(),b!==B){for(c=[],d=Ib,e=v(),e!==B?(f=r(),f!==B?(e=[e,f],d=e):(Ib=d,d=E)):(Ib=d,d=E);d!==B;)c.push(d),d=Ib,e=v(),e!==B?(f=r(),f!==B?(e=[e,f],d=e):(Ib=d,d=E)):(Ib=d,d=E);c!==B?(Jb=a,b=fa(b,c),a=b):(Ib=a,a=E)}else Ib=a,a=E;return a}function t(){var a,b,c,d,e,f;if(a=Ib,b=s(),b!==B){for(c=[],d=Ib,e=w(),e!==B?(f=s(),f!==B?(e=[e,f],d=e):(Ib=d,d=E)):(Ib=d,d=E);d!==B;)c.push(d),d=Ib,e=w(),e!==B?(f=s(),f!==B?(e=[e,f],d=e):(Ib=d,d=E)):(Ib=d,d=E);c!==B?(Jb=a,b=fa(b,c),a=b):(Ib=a,a=E)}else Ib=a,a=E;return a}function u(){var b;return a.substr(Ib,2)===ga?(b=ga,Ib+=2):(b=B,0===Ob&&e(ha)),b}function v(){var b;return 42===a.charCodeAt(Ib)?(b=ia,Ib++):(b=B,0===Ob&&e(ja)),b===B&&(47===a.charCodeAt(Ib)?(b=Z,Ib++):(b=B,0===Ob&&e($)),b===B&&(a.substr(Ib,3)===ka?(b=ka,Ib+=3):(b=B,0===Ob&&e(la)))),b}function w(){var b;return 43===a.charCodeAt(Ib)?(b=ma,Ib++):(b=B,0===Ob&&e(na)),b===B&&(45===a.charCodeAt(Ib)?(b=oa,Ib++):(b=B,0===Ob&&e(pa)),b===B&&(a.substr(Ib,2)===qa?(b=qa,Ib+=2):(b=B,0===Ob&&e(ra)),b===B&&(a.substr(Ib,3)===sa?(b=sa,Ib+=3):(b=B,0===Ob&&e(ta)),b===B&&(a.substr(Ib,3)===ua?(b=ua,Ib+=3):(b=B,0===Ob&&e(va)))))),b}function x(){var b;return a.substr(Ib,3)===wa?(b=wa,Ib+=3):(b=B,0===Ob&&e(xa)),b===B&&(a.substr(Ib,4)===ya?(b=ya,Ib+=4):(b=B,0===Ob&&e(za)),b===B&&(a.substr(Ib,4)===Aa?(b=Aa,Ib+=4):(b=B,0===Ob&&e(Ba)),b===B&&(a.substr(Ib,3)===Ca?(b=Ca,Ib+=3):(b=B,0===Ob&&e(Da)),b===B&&(a.substr(Ib,3)===Ea?(b=Ea,Ib+=3):(b=B,0===Ob&&e(Fa)),b===B&&(a.substr(Ib,3)===Ga?(b=Ga,Ib+=3):(b=B,0===Ob&&e(Ha)),b===B&&(a.substr(Ib,3)===Ia?(b=Ia,Ib+=3):(b=B,0===Ob&&e(Ja)),b===B&&(a.substr(Ib,5)===Ka?(b=Ka,Ib+=5):(b=B,0===Ob&&e(La)),b===B&&(a.substr(Ib,2)===Ma?(b=Ma,Ib+=2):(b=B,0===Ob&&e(Na)),b===B&&(a.substr(Ib,3)===Oa?(b=Oa,Ib+=3):(b=B,0===Ob&&e(Pa)),b===B&&(a.substr(Ib,4)===Qa?(b=Qa,Ib+=4):(b=B,0===Ob&&e(Ra)),b===B&&(a.substr(Ib,3)===Sa?(b=Sa,Ib+=3):(b=B,0===Ob&&e(Ta)),b===B&&(a.substr(Ib,6)===Ua?(b=Ua,Ib+=6):(b=B,0===Ob&&e(Va)))))))))))))),b}function y(){var b;return 65===a.charCodeAt(Ib)?(b=Wa,Ib++):(b=B,0===Ob&&e(Xa)),b===B&&(66===a.charCodeAt(Ib)?(b=Ya,Ib++):(b=B,0===Ob&&e(Za)),b===B&&(67===a.charCodeAt(Ib)?(b=$a,Ib++):(b=B,0===Ob&&e(_a)),b===B&&(68===a.charCodeAt(Ib)?(b=ab,Ib++):(b=B,0===Ob&&e(bb)),b===B&&(70===a.charCodeAt(Ib)?(b=cb,Ib++):(b=B,0===Ob&&e(db)),b===B&&(71===a.charCodeAt(Ib)?(b=eb,Ib++):(b=B,0===Ob&&e(fb)),b===B&&(72===a.charCodeAt(Ib)?(b=gb,Ib++):(b=B,0===Ob&&e(hb)),b===B&&(73===a.charCodeAt(Ib)?(b=ib,Ib++):(b=B,0===Ob&&e(jb)),b===B&&(74===a.charCodeAt(Ib)?(b=kb,Ib++):(b=B,0===Ob&&e(lb)),b===B&&(75===a.charCodeAt(Ib)?(b=mb,Ib++):(b=B,0===Ob&&e(nb)),b===B&&(76===a.charCodeAt(Ib)?(b=ob,Ib++):(b=B,0===Ob&&e(pb)),b===B&&(77===a.charCodeAt(Ib)?(b=qb,Ib++):(b=B,0===Ob&&e(rb)),b===B&&(80===a.charCodeAt(Ib)?(b=sb,Ib++):(b=B,0===Ob&&e(tb)),b===B&&(81===a.charCodeAt(Ib)?(b=ub,Ib++):(b=B,0===Ob&&e(vb)),b===B&&(82===a.charCodeAt(Ib)?(b=wb,Ib++):(b=B,0===Ob&&e(xb)),b===B&&(83===a.charCodeAt(Ib)?(b=yb,Ib++):(b=B,0===Ob&&e(zb)),b===B&&(84===a.charCodeAt(Ib)?(b=Ab,Ib++):(b=B,0===Ob&&e(Bb)),b===B&&(88===a.charCodeAt(Ib)?(b=Cb,Ib++):(b=B,0===Ob&&e(Db)),b===B&&(89===a.charCodeAt(Ib)?(b=Eb,Ib++):(b=B,0===Ob&&e(Fb)),b===B&&(90===a.charCodeAt(Ib)?(b=Gb,Ib++):(b=B,0===Ob&&e(Hb))))))))))))))))))))),b}var z,A=arguments.length>1?arguments[1]:{},B={},C={start:g},D=g,E=B,F=null,G=function(a,b){return{N:a,words:b}},H=function(a,b){return[a,b]},I="N",J={type:"literal",value:"N",description:'"N"'},K=/^[0-9]/,L={type:"class",value:"[0-9]",description:"[0-9]"},M=function(){return parseInt(c())},N=/^[+\-]/,O={type:"class",value:"[+\\-]",description:"[+\\-]"},P=/^[.]/,Q={type:"class",value:"[.]",description:"[.]"},R=function(){return parseFloat(c())},S="[",T={type:"literal",value:"[",description:'"["'},U="]",V={type:"literal",value:"]",description:'"]"'},W=function(a){return a},X="ATAN",Y={type:"literal",value:"ATAN",description:'"ATAN"'},Z="/",$={type:"literal",value:"/",description:'"/"'},_=function(a,b){return{op:"ATAN",left:a,right:b}},aa=function(a,b){return{op:a,right:b}},ba="#",ca={type:"literal",value:"#",description:'"#"'},da=function(a){return{op:"#",right:a}},ea=function(a,b){return buildTree(a,b)},fa=function(a,b){return buildTree(a,b)},ga="**",ha={type:"literal",value:"**",description:'"**"'},ia="*",ja={type:"literal",value:"*",description:'"*"'},ka="MOD",la={type:"literal",value:"MOD",description:'"MOD"'},ma="+",na={type:"literal",value:"+",description:'"+"'},oa="-",pa={type:"literal",value:"-",description:'"-"'},qa="OR",ra={type:"literal",value:"OR",description:'"OR"'},sa="XOR",ta={type:"literal",value:"XOR",description:'"XOR"'},ua="AND",va={type:"literal",value:"AND",description:'"AND"'},wa="ABS",xa={type:"literal",value:"ABS",description:'"ABS"'},ya="ACOS",za={type:"literal",value:"ACOS",description:'"ACOS"'},Aa="ASIN",Ba={type:"literal",value:"ASIN",description:'"ASIN"'},Ca="COS",Da={type:"literal",value:"COS",description:'"COS"'},Ea="EXP",Fa={type:"literal",value:"EXP",description:'"EXP"'},Ga="FIX",Ha={type:"literal",value:"FIX",description:'"FIX"'},Ia="FUP",Ja={type:"literal",value:"FUP",description:'"FUP"'},Ka="ROUND",La={type:"literal",value:"ROUND",description:'"ROUND"'},Ma="LN",Na={type:"literal",value:"LN",description:'"LN"'},Oa="SIN",Pa={type:"literal",value:"SIN",description:'"SIN"'},Qa="SQRT",Ra={type:"literal",value:"SQRT",description:'"SQRT"'},Sa="TAN",Ta={type:"literal",value:"TAN",description:'"TAN"'},Ua="EXISTS",Va={type:"literal",value:"EXISTS",description:'"EXISTS"'},Wa="A",Xa={type:"literal",value:"A",description:'"A"'},Ya="B",Za={type:"literal",value:"B",description:'"B"'},$a="C",_a={type:"literal",value:"C",description:'"C"'},ab="D",bb={type:"literal",value:"D",description:'"D"'},cb="F",db={type:"literal",value:"F",description:'"F"'},eb="G",fb={type:"literal",value:"G",description:'"G"'},gb="H",hb={type:"literal",value:"H",description:'"H"'},ib="I",jb={type:"literal",value:"I",description:'"I"'},kb="J",lb={type:"literal",value:"J",description:'"J"'},mb="K",nb={type:"literal",value:"K",description:'"K"'},ob="L",pb={type:"literal",value:"L",description:'"L"'},qb="M",rb={type:"literal",value:"M",description:'"M"'},sb="P",tb={type:"literal",value:"P",description:'"P"'},ub="Q",vb={type:"literal",value:"Q",description:'"Q"'},wb="R",xb={type:"literal",value:"R",description:'"R"'},yb="S",zb={type:"literal",value:"S",description:'"S"'},Ab="T",Bb={type:"literal",value:"T",description:'"T"'},Cb="X",Db={type:"literal",value:"X",description:'"X"'},Eb="Y",Fb={type:"literal",value:"Y",description:'"Y"'},Gb="Z",Hb={type:"literal",value:"Z",description:'"Z"'},Ib=0,Jb=0,Kb=0,Lb={line:1,column:1,seenCR:!1},Mb=0,Nb=[],Ob=0;if("startRule"in A){if(!(A.startRule in C))throw new Error("Can't start parsing from rule \""+A.startRule+'".');D=C[A.startRule]}if(buildTree=function(a,b){if(0==b.length)return a;var c=b.shift(),d=c[0],e=c[1];return{left:a,right:buildTree(e,b),op:d}},z=D(),z!==B&&Ib===a.length)return z;throw z!==B&&Ib<a.length&&e({type:"end",description:"end of input"}),f(null,Nb,Mb)}return a(b,Error),{SyntaxError:b,parse:c}}(),GCodeToGeometry={};GCodeToGeometry.INCH_TO_MILLIMETER=25.4,GCodeToGeometry.MILLIMETER_TO_INCH=.03937008,GCodeToGeometry.FLOAT_PRECISION=.001,GCodeToGeometry.calculateFeedrate=function(a,b){return b===!1?a:a*GCodeToGeometry.MILLIMETER_TO_INCH},GCodeToGeometry.nearlyEqual=function(a,b,c){var d=void 0===c?GCodeToGeometry.FLOAT_PRECISION:c;return Math.abs(b-a)<=d},GCodeToGeometry.swapObjects=function(a,b){function c(a,b,c){var d;d=a[c],a[c]=b[c],b[c]=d}var d=Object.keys(a),e=0;for(e=0;e<d.length;e++)"object"==typeof a[d[e]]?GCodeToGeometry.swapObjects(a[d[e]],b[d[e]]):c(a,b,d[e])},GCodeToGeometry.copyObject=function(a){var b=Object.keys(a),c=0,d={};for(c=0;c<b.length;c++)"object"==typeof a[b[c]]?d[b[c]]=GCodeToGeometry.copyObject(a[b[c]]):d[b[c]]=a[b[c]];return d},GCodeToGeometry.movePoint=function(a,b){var c=Object.keys(b),d=0;for(d=0;d<c.length;d++)void 0!==a[c[d]]&&(a[c[d]]+=b[c[d]])},GCodeToGeometry.dotProduct2=function(a,b){return a.x*b.x+a.y*b.y},GCodeToGeometry.crossProduct2=function(a,b){return a.x*b.y-b.x*a.y},GCodeToGeometry.lengthVector3=function(a){return Math.sqrt(a.x*a.x+a.y*a.y+a.z*a.z)},GCodeToGeometry.findAxes=function(a){return"x"===a.toLowerCase()?{re:"y",im:"z",cr:"x"}:"y"===a.toLowerCase()?{re:"z",im:"x",cr:"y"}:{re:"x",im:"y",cr:"z"}},GCodeToGeometry.scaleAndRotation=function(a,b,c,d,e,f,g){var h=a,i=b,j=c,k=e,l=Math.cos(d),m=Math.sin(d),n=i[f],o=i[g],p=h[f],q=h[g];j[f]=k*((n-p)*l-(o-q)*m)+p,j[g]=k*((o-q)*l+(n-p)*m)+q},GCodeToGeometry.findAngleVectors2=function(a,b){var c=GCodeToGeometry.crossProduct2(a,b)<0?-1:1,d=GCodeToGeometry.dotProduct2(a,b),e=Math.sqrt(a.x*a.x+a.y*a.y),f=Math.sqrt(b.x*b.x+b.y*b.y);return 0===e||0===f?0:c*Math.acos(d/(e*f))},GCodeToGeometry.findAngleOrientedVectors2=function(a,b,c){var d=GCodeToGeometry.findAngleVectors2(a,b);return c===!1&&d>0?d-2*Math.PI:c===!0&&0>d?2*Math.PI+d:d},GCodeToGeometry.isInclude=function(a,b,c){return b>c?a>=c&&b>=a:a>=b&&c>=a},GCodeToGeometry.StraightLine=function(a,b,c,d,e){"use strict";function f(a,b,d,e){g.index=a,g.word=d.type,g.start={x:b.x,y:b.y,z:b.z},g.end=c,"G0"===d.type?g.feedrate=0:void 0===d.f?g.feedrate=e.feedrate:g.feedrate=GCodeToGeometry.calculateFeedrate(d.f,e.inMm)}var g=this;g.returnLine=function(){return{lineNumber:g.index,type:g.word,start:g.start,end:g.end,feedrate:g.feedrate}},g.getSize=function(){return{min:{x:Math.min(g.start.x,g.end.x),y:Math.min(g.start.y,g.end.y),z:Math.min(g.start.z,g.end.z)},max:{x:Math.max(g.start.x,g.end.x),y:Math.max(g.start.y,g.end.y),z:Math.max(g.start.z,g.end.z)}}},f(a,b,d,e)},GCodeToGeometry.CurvedLine=function(a,b,c,d,e){"use strict";function f(){var a=GCodeToGeometry.findAxes(r.crossAxe),b={x:r.start[a.re]-r.center[a.re],y:r.start[a.im]-r.center[a.im],z:0},c={x:r.end[a.re]-r.center[a.re],y:r.end[a.im]-r.center[a.im],z:0};return GCodeToGeometry.findAngleOrientedVectors2(b,c,r.clockwise===!1)}function g(){var a=GCodeToGeometry.findAxes(r.crossAxe),b={x:r.start[a.re]-r.center[a.re],y:r.start[a.im]-r.center[a.im],z:0};return GCodeToGeometry.lengthVector3(b)}function h(a,b){var c={},d={},e={},f={};return a=Math.abs(a),a===Math.PI/2?(c={x:.707106781186548,y:.707106781186548,z:0},d={x:1.097631072937817,y:.316582489435277,z:0}):(c={x:Math.cos(a/2),y:Math.sin(a/2),z:0},d={x:(4-c.x)/3,y:(1-c.x)*(3-c.x)/(3*c.y),z:0}),c.x*=b,c.y*=b,d.x*=b,d.y*=b,e={x:d.x,y:-d.y,z:0},f={x:c.x,y:-c.y,z:0},{p0:c,p1:d,p2:e,p3:f}}function i(a,b,c,d){var e=0;return b===!1&&(GCodeToGeometry.swapObjects(a.p0,a.p3),GCodeToGeometry.swapObjects(a.p1,a.p2)),e=c/3,"z"===d.toLowerCase()?(a.p0.z=0,a.p1.z=e,a.p2.z=2*e,a.p3.z=3*e):"x"===d.toLowerCase()?(a.p0.z=a.p0.y,a.p0.y=a.p0.x,a.p0.x=0,a.p1.z=a.p1.y,a.p1.y=a.p1.x,a.p1.x=e,a.p2.z=a.p2.y,a.p2.y=a.p2.x,a.p2.x=2*e,a.p3.z=a.p3.y,a.p3.y=a.p3.x,a.p3.x=3*e):"y"===d.toLowerCase()&&(a.p0.z=a.p0.x,a.p0.x=a.p0.y,a.p0.y=0,a.p1.z=a.p1.x,a.p1.x=a.p1.y,a.p1.y=e,a.p2.z=a.p2.x,a.p2.x=a.p2.y,a.p2.y=2*e,a.p3.z=a.p3.x,a.p3.x=a.p3.y,a.p3.y=3*e),a}function j(a,b,c,d,e){var f={x:0,y:0,z:0};GCodeToGeometry.scaleAndRotation(f,a.p0,a.p0,c,1,d,e),GCodeToGeometry.scaleAndRotation(f,a.p1,a.p1,c,1,d,e),GCodeToGeometry.scaleAndRotation(f,a.p2,a.p2,c,1,d,e),GCodeToGeometry.scaleAndRotation(f,a.p3,a.p3,c,1,d,e),GCodeToGeometry.movePoint(a.p0,b),GCodeToGeometry.movePoint(a.p1,b),GCodeToGeometry.movePoint(a.p2,b),GCodeToGeometry.movePoint(a.p3,b)}function k(a,b,c,d,e){var f=[],g=GCodeToGeometry.copyObject(r.center),h=GCodeToGeometry.findAxes(r.crossAxe),i={x:r.start[h.re]-g[h.re],y:r.start[h.im]-g[h.im]},k=0,l=0,m=r.clockwise===!0?-1:1;if(0===a&&0===c)return f;if(a>0)for(l=GCodeToGeometry.findAngleOrientedVectors2({x:b.p0[h.re],y:b.p0[h.im]},i,r.clockwise===!1),k=0;a>k;k++)f.push(GCodeToGeometry.copyObject(b)),j(f[k],g,l,h.re,h.im),l+=1.570796326794897*m,g[r.crossAxe]+=e;return c>0&&(l=GCodeToGeometry.findAngleOrientedVectors2({x:d.p0[h.re],y:d.p0[h.im]},i,r.clockwise===!1),0!==a&&(l+=1.570796326794897*a*m),f.push(GCodeToGeometry.copyObject(d)),j(f[k],g,l,h.re,h.im)),f[0].p0.x=r.start.x,f[0].p0.y=r.start.y,f[0].p0.z=r.start.z,f[f.length-1].p3.x=r.end.x,f[f.length-1].p3.y=r.end.y,f[f.length-1].p3.z=r.end.z,f}function l(){var a=0,b=1,c={},d={},e=0,j=0,l=0,m=f(),n=g(),o=Math.abs(m),p=1.570796326794897;return 0===m||0===n?[]:(o>p&&(a=parseInt(o/p,10),b=o%p!==0?1:0),l=(r.end[r.crossAxe]-r.start[r.crossAxe])/o,e=p*l,j=(o-a*p)*l,a>0&&(c=h(p,n),i(c,0>m,e,r.crossAxe)),b>0&&(m=o-a*p,r.clockwise===!0&&(m=-m),d=h(m,n),i(d,0>m,j,r.crossAxe)),k(a,c,b,d,e))}function m(){var a={},b=[],c=0,d=1.570796326794897,e=r.clockwise===!0?-1:1,f=e*Math.PI*2,l=g(),m=0,n=GCodeToGeometry.copyObject(r.center),o=GCodeToGeometry.findAxes(r.crossAxe);if(0===l)return[];for(c=(r.end[r.crossAxe]-r.start[r.crossAxe])/4,a=h(d,l),i(a,r.clockwise,c,r.crossAxe),m=0;4>m;m++)b.push(GCodeToGeometry.copyObject(a)),j(b[m],n,f,o.re,o.im),f+=d*e,n[r.crossAxe]+=c;return k(4,a,0,a,c)}function n(a,b,c,d,e){var f={x:b.x-a.x,y:b.y-a.y,z:b.z-a.z},g=0,h=1,i=0,j=Math.abs(c),k=0,l={x:0,y:0,z:0},m=GCodeToGeometry.findAxes(e);if(i=Math.sqrt(f[m.re]*f[m.re]+f[m.im]*f[m.im]),i>Math.abs(2*c)||0===i)return!1;if(g=Math.acos(i/(2*j)),h=j/i,GCodeToGeometry.scaleAndRotation(a,b,l,g,h,m.re,m.im),k=GCodeToGeometry.findAngleVectors2({x:a[m.re]-l[m.re],y:a[m.im]-l[m.im]},{x:b[m.re]-l[m.re],y:b[m.im]-l[m.im]}),d===!0){if(c>0&&-Math.PI<=k&&0>=k)return l;if(0>c&&k>=0&&k<=Math.PI)return l}else{if(c>0&&k>=0&&k<=Math.PI)return l;if(0>c&&-Math.PI<=k&&0>=k)return l}return GCodeToGeometry.scaleAndRotation(a,b,l,-g,h,m.re,m.im),l}function o(a,b,c,d,e,f){var g,h,i=f===!1?1:GCodeToGeometry.MILLIMETER_TO_INCH,j={x:a.x,y:a.y,z:a.z},k=GCodeToGeometry.findAxes(e);if(void 0===c.r){if(void 0!==c.i&&(j.x+=c.i*i),void 0!==c.j&&(j.y+=c.j*i),void 0!==c.k&&(j.z+=c.k*i),g=Math.pow(j[k.re]-a[k.re],2),g+=Math.pow(j[k.im]-a[k.im],2),h=Math.pow(j[k.re]-b[k.re],2),h+=Math.pow(j[k.im]-b[k.im],2),GCodeToGeometry.nearlyEqual(g,0)===!0||GCodeToGeometry.nearlyEqual(h,0)===!0)return!1;if(GCodeToGeometry.nearlyEqual(g,h)===!1)return!1}else if(j=n(a,b,c.r*i,d,e),j===!1)return!1;return j[e]=a[e],j}function p(a,b,c,d){var e=GCodeToGeometry.findAngleOrientedVectors2(d,{x:a,y:b},r.clockwise===!1);return GCodeToGeometry.isInclude(e,0,c)===!0}function q(a,b,d,e){r.index=a,r.word=d.type,r.start={x:b.x,y:b.y,z:b.z},r.end=c,r.clockwise="G2"===d.type,r.center=o(b,r.end,d,r.clockwise,e.crossAxe,e.inMm),r.crossAxe=e.crossAxe,void 0===d.f?r.feedrate=e.feedrate:r.feedrate=GCodeToGeometry.calculateFeedrate(d.f,e.inMm)}var r=this;r.returnLine=function(){var a=[],b=GCodeToGeometry.findAxes(r.crossAxe);return a=r.start[b.re]===r.end[b.re]&&r.start[b.im]===r.end[b.im]?m():l(),0===a.length?!1:{lineNumber:r.index,type:r.word,beziers:a,feedrate:r.feedrate}},r.getSize=function(){var a=GCodeToGeometry.findAxes(r.crossAxe),b={x:r.start[a.re]-r.center[a.re],y:r.start[a.im]-r.center[a.im]},c=g(),d=f(),e={x:0,y:0,z:0},h={x:0,y:0,z:0};return r.start[a.re]===r.end[a.re]&&r.start[a.im]===r.end[a.im]?(e[a.re]=r.center[a.re]-c,e[a.im]=r.center[a.im]-c,e[a.cr]=Math.min(r.start[a.cr],r.end[a.cr]),h[a.re]=r.center[a.re]+c,h[a.im]=r.center[a.im]+c,h[a.cr]=Math.max(r.start[a.cr],r.end[a.cr]),{min:e,max:h}):(e.x=Math.min(r.start.x,r.end.x),e.y=Math.min(r.start.y,r.end.y),e.z=Math.min(r.start.z,r.end.z),h.x=Math.max(r.start.x,r.end.x),h.y=Math.max(r.start.y,r.end.y),h.z=Math.max(r.start.z,r.end.z),p(0,1,d,b)===!0&&(h[a.im]=r.center[a.im]+c),p(0,-1,d,b)===!0&&(e[a.im]=r.center[a.im]-c),p(1,0,d,b)===!0&&(h[a.re]=r.center[a.re]+c),p(-1,0,d,b)===!0&&(e[a.re]=r.center[a.re]-c),{min:e,max:h})},q(a,b,d,e)},GCodeToGeometry.parse=function(a){"use strict";function b(a){var b=a.split("(")[0].split(";")[0];return b.split(/\s/).join("").trim()}function c(a){var b={},c=0,d="",e="",f=[],g=!0;for(c=0;c<a.words.length;c++)d=a.words[c][0],e=a.words[c][1],"G"===d||"M"===d?(g===!1&&(f.push(b),b={}),b.type=d+e,g=!1):b[d.toLowerCase()]=parseFloat(e,10);return f.push(b),f}function d(a,b){var c=0,d=0,e=!0;for(d=b.length-1;d>=0;d--){for(c=a.length-1;c>=0;c--)if(e=!1,b[d].toUpperCase()===a[c].toUpperCase()){e=!0,a.splice(c,1);break}if(e===!1)return!0}return!1}function e(a,b){var c=["x","y","z"],d=0;for(d=c.length-1;d>=0;d--)a.min[c[d]]>b.min[c[d]]&&(a.min[c[d]]=b.min[c[d]]),a.max[c[d]]<b.max[c[d]]&&(a.max[c[d]]=b.max[c[d]])}function f(a,b,c){return{line:a,message:b,isSkipped:c}}function g(a,b,c,d){var e=a,g=void 0===e.f?d.feedrate:e.f;return void 0!==e.type&&"G1"!==e.type&&"G2"!==e.type&&"G3"!==e.type?!1:g>0?!1:0>g?(b.push(f(c,"(warning) Cannot use a negative feed rate (the absolute value is used).",!1)),e.f=Math.abs(g),!1):(b.push(f(c,"(error) Cannot use a null feed rate (skipped).",!0)),d.feedrate=0,!0)}function h(a,b){void 0===a.type&&""!==b&&(a.type=b)}function i(a,b,c,d){var e={x:a.x,y:a.y,z:a.z},f=d===!1?1:GCodeToGeometry.MILLIMETER_TO_INCH;return c===!0?(void 0!==b.x&&(e.x+=b.x*f),void 0!==b.y&&(e.y+=b.y*f),void 0!==b.z&&(e.z+=b.z*f)):(void 0!==b.x&&(e.x=b.x*f),void 0!==b.y&&(e.y=b.y*f),void 0!==b.z&&(e.z=b.z*f)),e}function j(a,b,c){var e=["X","Y","Z"],g=Object.keys(a);return g.splice(g.indexOf("type"),1),d(e,g)===!0&&b.push(f(c,"(warning) Some parameters are wrong.",!1)),!0}function k(a,b,c,e){var h=["X","Y","Z","F"],i=Object.keys(a);return i.splice(i.indexOf("type"),1),d(h,i)===!0&&b.push(f(c,"(warning) Some parameters are wrong.",!1)),!g(a,b,c,e)}function l(a,b,c,e){var h=["X","Y","Z","F","I","J","K","R"],i=Object.keys(a);return i.splice(i.indexOf("type"),1),d(h,i)===!0&&b.push(f(c,"(warning) Some parameters are wrong.",!1)),void 0===a.r&&void 0===a.i&&void 0===a.j&&void 0===a.k?(b.push(f(c,"(error) No parameter R, I, J or K.",!0)),!1):void 0===a.r||void 0===a.i&&void 0===a.j&&void 0===a.k?!g(a,b,c,e):(b.push(f(c,"(error) Cannot use R and I, J or K at the same time.",!0)),!1)}function m(a,b,c,d,f){var g=i(b.position,a,b.relative,b.inMm),h=new GCodeToGeometry.StraightLine(c,b.position,g,a,b);b.previousMoveCommand=a.type,e(f,h.getSize()),d.push(h.returnLine()),b.position=GCodeToGeometry.copyObject(h.end),void 0!==a.f&&(b.feedrate=a.f)}function n(a,b,c,d,g,h){var j=i(b.position,a,b.relative,b.inMm),k=new GCodeToGeometry.CurvedLine(c,b.position,j,a,b);if(k.center!==!1){var l=k.returnLine();if(l===!1)return void h.push(f(c,"(error) Impossible to create arc.",!0));b.feedrate=k.feedrate,b.previousMoveCommand=a.type,e(g,k.getSize()),d.push(l),b.position=GCodeToGeometry.copyObject(k.end)}else h.push(f(c,"(error) Physically impossible to do with those values.",!0))}function o(a,b,c,d,e,f){if(void 0===a.type&&0===Object.keys(a).length)return!0;if(h(a,b.previousMoveCommand),void 0===a.type)void 0!==a.f&&(g(a,f,c,b.feedrate),b.feedrate=a.f);else if("G0"===a.type&&j(a,f,c)===!0)m(a,b,c,d,e);else if("G1"===a.type&&k(a,f,c,b)===!0)m(a,b,c,d,e);else if("G2"!==a.type&&"G3"!==a.type||l(a,f,c,b)!==!0){if("G17"===a.type)b.crossAxe="z";else if("G18"===a.type)b.crossAxe="y";else if("G19"===a.type)b.crossAxe="x";else if("G20"===a.type)b.inMm=!1,p===!1&&(q=!0,p=!0);else if("G21"===a.type)b.inMm=!0,p===!1&&(q=!1,p=!0);else if("G90"===a.type)b.relative=!1;else if("G91"===a.type)b.relative=!0;else if("M2"===a.type)return!1}else n(a,b,c,d,e,f);return!0}var p=!1,q=!0,r={min:{x:0,y:0,z:0},max:{x:0,y:0,z:0}},s=0,t=0,u=[],v=!0,w=[],x=[],y={feedrate:0,previousMoveCommand:"",crossAxe:"z",inMm:!1,relative:!1,position:{x:0,y:0,z:0}};if("string"!=typeof a||""===a)return{gcode:[],lines:[],size:r,displayInInch:q,errorList:[{line:0,message:"(error) No command."}]};var z=a.split("\n");for(s=0;s<z.length&&v===!0;){for(u=c(GParser.parse(b(z[s]).toUpperCase())),t=0;t<u.length&&v===!0;)v=o(u[t],y,s+1,w,r,x),t++;s++}return s<z.length&&x.push(f(s+1,"(warning) The next code is not executed.",!1)),{gcode:z,lines:w,size:r,displayInInch:q,errorList:x}};var GCode2DViewer={};GCode2DViewer.preview=function(a,b,c){"use strict";function d(a,b){var c=Math.abs(a.size.max.x-a.size.min.x),d=Math.abs(a.size.max.y-a.size.min.y),e=parseInt(b.width,10),f=parseInt(b.height,10);return Math.min(e/c,f/d)}function e(a,b,c,d,e,f){var g=Math.round(b*(d.start.x-c.x)),h=Math.round(e-b*(d.start.y-c.y)),i=Math.round(b*(d.end.x-c.x)),j=Math.round(e-b*(d.end.y-c.y));a.beginPath(),a.moveTo(g,h),a.lineTo(i,j),a.strokeStyle=f,a.lineWidth=2,a.stroke(),a.closePath()}function f(a,b,c,d,e,f){var g=0,h=d.beziers,i={};for(a.beginPath(),g=0;g<h.length;g++)i=h[g],a.moveTo(b*(i.p0.x-c.x),e-b*(i.p0.y-c.y)),a.bezierCurveTo(b*(i.p1.x-c.x),e-b*(i.p1.y-c.y),b*(i.p2.x-c.x),e-b*(i.p2.y-c.y),b*(i.p3.x-c.x),e-b*(i.p3.y-c.y));a.strokeStyle=f,a.lineWidth=2,a.stroke(),a.closePath()}if(void 0!==b){var g=GCodeToGeometry.parse(a);if(g.size.max.x!==g.size.min.x||g.size.max.y!==g.size.min.y){var h=c.getContext("2d");h.clearRect(0,0,c.width,c.height);var i={x:g.size.min.x,y:g.size.min.y},j=0,k=d(g,c),l=parseInt(c.height,10);for(j=0;j<g.lines.length;j++)"G0"===g.lines[j].type&&void 0!==b.G0?e(h,k,i,g.lines[j],l,b.G0):"G1"===g.lines[j].type&&void 0!==b.G1?e(h,k,i,g.lines[j],l,b.G1):"G2"!==g.lines[j].type&&"G3"!==g.lines[j].type||void 0===b.G2G3||f(h,k,i,g.lines[j],l,b.G2G3)}}},GCode2DViewer.getImage=function(a,b,c,d){var e=document.createElement("canvas");return e.width=c,e.height=d,GCode2DViewer.preview(a,b,e),e.toDataURL("img/png")};