@sienci/gviewer 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +480 -0
- package/dist/GCodeSVGRenderer-DaLc9_L-.cjs +1 -0
- package/dist/GCodeSVGRenderer-DnFLLJmH.js +1487 -0
- package/dist/gviewer.cjs +1 -0
- package/dist/gviewer.js +654 -0
- package/dist/react.cjs +1 -0
- package/dist/react.js +141 -0
- package/dist/types/geometry.d.ts +92 -0
- package/dist/types/index.d.ts +4 -0
- package/dist/types/parser.d.ts +6 -0
- package/dist/types/react/GCodeSVGVisualizer.d.ts +20 -0
- package/dist/types/react/GCodeVisualizer.d.ts +10 -0
- package/dist/types/react/index.d.ts +4 -0
- package/dist/types/types.d.ts +71 -0
- package/dist/types/viewer/GCodeViewer.d.ts +89 -0
- package/dist/types/viewer/ViewCube.d.ts +16 -0
- package/dist/types/viewer/bbox/boundingBox.d.ts +4 -0
- package/dist/types/viewer/bit/bit.d.ts +13 -0
- package/dist/types/viewer/bit/drill-stl-data.d.ts +1 -0
- package/dist/types/viewer/camera/camera.d.ts +21 -0
- package/dist/types/viewer/grid/grid.d.ts +21 -0
- package/dist/types/viewer/index.d.ts +7 -0
- package/dist/types/viewer/render/textSprite.d.ts +12 -0
- package/dist/types/viewer/simulation/heightmap.d.ts +36 -0
- package/dist/types/viewer/simulation/materialSlab.d.ts +12 -0
- package/dist/types/viewer/svg/GCodeSVGRenderer.d.ts +53 -0
- package/dist/types/viewer/svg/types.d.ts +10 -0
- package/dist/types/viewer/themes.d.ts +3 -0
- package/dist/types/viewer/toolpath/streams.d.ts +51 -0
- package/dist/types/viewer/types.d.ts +171 -0
- package/dist/types/virtualizer.d.ts +26 -0
- package/dist/viewcube.css +54 -0
- package/dist/viewer.cjs +1 -0
- package/dist/viewer.js +109 -0
- package/package.json +81 -0
package/dist/gviewer.cjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const $=/([A-Za-z])\s*([+-]?(?:\d+(?:\.\d*)?|\.\d+))/g;class C{parseLine(e){const t=[],n=this.stripComments(e,t),s=this.parseWords(n),r=s.filter(a=>{const c=a.letter.toUpperCase();return c==="G"||c==="M"}),i=s.filter(a=>{const c=a.letter.toUpperCase();return c!=="G"&&c!=="M"});return{raw:e,words:s,gcodes:r,params:i,comments:t}}stripComments(e,t){let n="",s=!1,r=-1,i="";for(let a=0;a<e.length;a+=1){const c=e[a];if(!s&&c===";"){const u=e.slice(a+1);t.push({type:"semicolon",text:u,start:a,end:e.length});break}if(c==="("){s?i+=c:(s=!0,r=a,i="");continue}if(c===")"&&s){t.push({type:"paren",text:i,start:r,end:a+1}),s=!1,r=-1,i="";continue}if(s){i+=c;continue}n+=c}return s&&t.push({type:"paren",text:i,start:r,end:e.length}),n}parseWords(e){const t=[];for(const n of e.matchAll($)){const s=n[0],r=n[1].toUpperCase(),i=Number(n[2]),a=n.index??0,c=a+s.length;t.push({letter:r,value:i,raw:s,start:a,end:c})}return t}}const X=["X","Y","Z","A","B","C"],b={motion:"G0",distance:"G90",plane:"G17",units:"G21",feedMode:"G94",feedRate:null,spindleSpeed:null,tool:null,coolant:null,spindle:null,coordinateSystem:"G54"};class I{constructor(e={}){this.parser=new C,this.modals={...b},this.position={X:0,Y:0,Z:0,A:0,B:0,C:0},this.callbacks=e,this.feedRates=new Set,this.spindleSpeeds=new Set,this.tools=new Set}setCallbacks(e){this.callbacks=e}getModals(){return{...this.modals}}getPosition(){return{...this.position}}getUniqueFeedRates(){return Array.from(this.feedRates)}getUniqueSpindleSpeeds(){return Array.from(this.spindleSpeeds)}getUniqueTools(){return Array.from(this.tools)}reset(){this.modals={...b},this.position={X:0,Y:0,Z:0,A:0,B:0,C:0},this.feedRates.clear(),this.spindleSpeeds.clear(),this.tools.clear()}processLine(e){const t=this.parser.parseLine(e),n={...this.position};this.updateModals(t);const s=this.modals.motion,r=this.modals.plane;let i=this.applyAxes(t,n),a,c,u="none";return s==="G0"||s==="G1"?T(n,i)||(u="linear",this.position={...i},this.emitLinear({start:n,end:i})):(s==="G2"||s==="G3")&&(T(n,i)||(u="arc",c=this.arcCenter(t,n,i,r),a=this.computeArcMax(n,i,s,r,c),this.position={...i},this.emitArc({start:n,end:i,max:a,center:c,plane:r,motion:s}))),{parsed:t,modals:{...this.modals},start:n,end:i,movement:u,arcMax:a}}unitsScale(){return this.modals.units==="G20"?25.4:1}updateModals(e){for(const t of e.gcodes){if(t.letter.toUpperCase()==="G"){const n=`G${Math.trunc(t.value)}`;(n==="G0"||n==="G1"||n==="G2"||n==="G3")&&(this.modals.motion=n),(n==="G90"||n==="G91")&&(this.modals.distance=n),(n==="G17"||n==="G18"||n==="G19")&&(this.modals.plane=n),(n==="G20"||n==="G21")&&(this.modals.units=n),(n==="G93"||n==="G94")&&(this.modals.feedMode=n),(n==="G54"||n==="G55"||n==="G56"||n==="G57"||n==="G58"||n==="G59")&&(this.modals.coordinateSystem=n)}if(t.letter.toUpperCase()==="M"){const n=`M${Math.trunc(t.value)}`;(n==="M7"||n==="M8"||n==="M9")&&(this.modals.coolant=n),(n==="M3"||n==="M4"||n==="M5")&&(this.modals.spindle=n)}}for(const t of e.params){const n=t.letter.toUpperCase();n==="F"&&(this.modals.feedRate=t.value,this.feedRates.add(t.value)),n==="S"&&(this.modals.spindleSpeed=t.value,this.spindleSpeeds.add(t.value)),n==="T"&&(this.modals.tool=t.value,this.tools.add(t.value))}}applyAxes(e,t){const n={...t},s=this.unitsScale();for(const r of X){const i=e.params.find(c=>c.letter.toUpperCase()===r);if(!i)continue;const a=r==="X"||r==="Y"||r==="Z"?i.value*s:i.value;this.modals.distance==="G90"?n[r]=a:n[r]=n[r]+a}return n}computeArcMax(e,t,n,s,r){const{primary:i,secondary:a,tertiary:c}=N(s),u=W(e[i],e[a],r[i],r[a]),f=Math.atan2(e[a]-r[a],e[i]-r[i]),d=Math.atan2(t[a]-r[a],t[i]-r[i]),x=D(f,d,n);let l=Number.NEGATIVE_INFINITY,p=Number.NEGATIVE_INFINITY;for(const A of x){const w=r[i]+u*Math.cos(A),M=r[a]+u*Math.sin(A);w>l&&(l=w),M>p&&(p=M)}const v={...e};v[i]=l,v[a]=p,v[c]=Math.max(e[c],t[c]);for(const A of X)A!==i&&A!==a&&A!==c&&(v[A]=Math.max(e[A],t[A]));return v}arcCenter(e,t,n,s){const{primary:r,secondary:i}=N(s),a={...t},c=this.unitsScale(),u=F(e,"R"),f=q(e,s),d=f.primary===null?null:f.primary*c,x=f.secondary===null?null:f.secondary*c;if(d!==null||x!==null)return a[r]=t[r]+(d??0),a[i]=t[i]+(x??0),a;if(u===null)return a;const l=u*c,p=t[r],v=t[i],A=n[r],w=n[i],M=A-p,L=w-v,S=Math.hypot(M,L);if(S===0)return a;const P=Math.abs(l),m=Math.sqrt(Math.max(0,P*P-S/2*(S/2))),h=(p+A)/2,y=(v+w)/2,G=-L/S,Y=M/S,E=l>=0?1:-1;return a[r]=h+E*G*m,a[i]=y+E*Y*m,a}emitLinear(e){const t=this.callbacks.onLinearMove;if(!t)return;const n=e.end.A-e.start.A,s=Math.max(1,Math.ceil(Math.abs(n)/K));let r={...e.start};for(let i=1;i<=s;i+=1){const a=i/s,c=j(e.start,e.end,a),u=V(r),f=V(c);t({modals:{...this.modals},start:{...r},end:{...c},transformedStart:u,transformedEnd:f}),r=c}}emitArc(e){const t=this.callbacks.onArcMove;if(!t)return;const n=V(e.start),s=V(e.end),r=V(e.max),i=V(e.center);t({modals:{...this.modals},start:{...e.start},end:{...e.end},max:{...e.max},center:{...e.center},plane:e.plane,motion:e.motion,transformedStart:n,transformedEnd:s,transformedMax:r,transformedCenter:i})}}function F(o,e){const t=o.params.find(n=>n.letter.toUpperCase()===e);return t?t.value:null}function N(o){return o==="G18"?{primary:"Z",secondary:"X",tertiary:"Y"}:o==="G19"?{primary:"Y",secondary:"Z",tertiary:"X"}:{primary:"X",secondary:"Y",tertiary:"Z"}}function q(o,e){const t=F(o,"I"),n=F(o,"J"),s=F(o,"K");return e==="G18"?{primary:s,secondary:t}:e==="G19"?{primary:n,secondary:s}:{primary:t,secondary:n}}function D(o,e,t){const n=Math.PI*2;let s=g(o),r=g(e);const i=[0,Math.PI/2,Math.PI,3*Math.PI/2,n];if(t==="G2"){s<r&&(s+=n);const c=[s,r];for(const u of i){let f=u;f<r&&(f+=n),f<=s&&f>=r&&c.push(f)}return c}r<s&&(r+=n);const a=[s,r];for(const c of i){let u=c;u<s&&(u+=n),u>=s&&u<=r&&a.push(u)}return a}function g(o){const e=Math.PI*2;let t=o%e;return t<0&&(t+=e),t}function W(o,e,t,n){return Math.hypot(o-t,e-n)}function T(o,e){return X.every(t=>o[t]===e[t])}const K=5;function j(o,e,t){return{X:o.X+(e.X-o.X)*t,Y:o.Y+(e.Y-o.Y)*t,Z:o.Z+(e.Z-o.Z)*t,A:o.A+(e.A-o.A)*t,B:o.B+(e.B-o.B)*t,C:o.C+(e.C-o.C)*t}}function V(o){const e=o.A*Math.PI/180,t=Math.cos(e),n=Math.sin(e),s=o.Y*t-o.Z*n,r=o.Y*n+o.Z*t;return{...o,Y:s,Z:r}}function J(o,e={}){const t=[],n=e.arcSegments??30,s=e.collector??{},r=new I({onLinearMove:i=>{(s.onLinearMove??_)(i,t)},onArcMove:i=>{(s.onArcMove??((c,u)=>{Z(c,u,n)}))(i,t)}});for(const i of o)i&&r.processLine(i);return Float32Array.from(t)}function H(o,e={}){const t=[],n=[],s=e.arcSegments??30,r=new I({onLinearMove:i=>{const a=i.transformedStart??i.start,c=i.transformedEnd??i.end;(i.modals.motion==="G0"?t:n).push(a.X,a.Y,a.Z,c.X,c.Y,c.Z)},onArcMove:i=>{const a=i.modals.motion==="G0"?t:n;Z(i,a,s)}});for(const i of o)i&&r.processLine(i);return{rapid:Float32Array.from(t),cutting:Float32Array.from(n)}}async function Q(o,e={}){var x;const t=[],n=[],s=e.arcSegments??30,r=e.batch,i=Math.max(1,Math.floor((r==null?void 0:r.everyLines)??5e3)),a=Math.max(0,Math.floor((r==null?void 0:r.yieldEveryLines)??5e4));let c=i,u=a>0?a:Number.POSITIVE_INFINITY;const f=new I({onLinearMove:l=>{const p=l.transformedStart??l.start,v=l.transformedEnd??l.end;(l.modals.motion==="G0"?t:n).push(p.X,p.Y,p.Z,v.X,v.Y,v.Z)},onArcMove:l=>{const p=l.modals.motion==="G0"?t:n;Z(l,p,s)}}),d=o.length;for(let l=0;l<d;l+=1){if((x=r==null?void 0:r.shouldAbort)!=null&&x.call(r))throw new Error("Aborted.");const p=o[l];if(!p){r!=null&&r.onProgress&&(l+1===d||l+1===c)&&(r.onProgress(l+1,d),c+=i),a>0&&(l+1===d||l+1===u)&&(await new Promise(v=>{setTimeout(v,0)}),u+=a);continue}f.processLine(p),r!=null&&r.onProgress&&(l+1===d||l+1===c)&&(r.onProgress(l+1,d),c+=i),a>0&&(l+1===d||l+1===u)&&(await new Promise(v=>{setTimeout(v,0)}),u+=a)}return{rapid:Float32Array.from(t),cutting:Float32Array.from(n)}}async function ee(o,e={}){var w;const t=[],n=e.arcSegments??30,s=e.batch,r=Math.max(1,Math.floor((s==null?void 0:s.everyLines)??5e3)),i=Math.max(0,Math.floor((s==null?void 0:s.yieldEveryLines)??5e4));let a=r,c=i>0?i:Number.POSITIVE_INFINITY;const u=new Int32Array(o.length);u.fill(-1);const f=new Int32Array(o.length);f.fill(-1);const d=new Uint8Array(o.length),x=new Int32Array(o.length);let l=-1,p=0;const v=new I({onLinearMove:M=>{const L=M.modals.motion==="G0"?1:2;l>=0&&(p===0?p=L:p!==L&&(p=3)),_(M,t)},onArcMove:M=>{const L=M.modals.motion==="G0"?1:2;l>=0&&(p===0?p=L:p!==L&&(p=3)),Z(M,t,n)}}),A=o.length;for(let M=0;M<A;M+=1){if((w=s==null?void 0:s.shouldAbort)!=null&&w.call(s))throw new Error("Aborted.");const L=t.length/3;l=M,p=0;const S=o[M];S&&v.processLine(S),l=-1;const P=t.length/3;P>L&&(u[M]=L,f[M]=P,d[M]=p),x[M]=P,s!=null&&s.onProgress&&(M+1===A||M+1===a)&&(s.onProgress(M+1,A),a+=r),i>0&&(M+1===A||M+1===c)&&(await new Promise(m=>{setTimeout(m,0)}),c+=i)}return{positions:Float32Array.from(t),lineStartVertex:u,lineEndVertex:f,lineKind:d,prefixEndVertex:x}}function te(o,e={}){const t=e.arcSegments??30,n=Math.max(1,Math.floor(e.bucketCount??16)),s=e.baseOpacity??.9,{minPower:r,maxPower:i}=ae(o),a=[],c=Array.from({length:n},()=>[]),u=new I({onLinearMove:d=>{const x=d.transformedStart??d.start,l=d.transformedEnd??d.end;if(d.modals.motion==="G0"){a.push(x.X,x.Y,x.Z,l.X,l.Y,l.Z);return}const p=z(d.modals.spindleSpeed,r,i,n);c[p].push(x.X,x.Y,x.Z,l.X,l.Y,l.Z)},onArcMove:d=>{if(d.modals.motion==="G0")return;const x=z(d.modals.spindleSpeed,r,i,n),l=c[x];Z(d,l,t)}});for(const d of o)d&&u.processLine(d);const f=c.map((d,x)=>({opacity:U(x,n,s),vertices:Float32Array.from(d)}));return{rapid:Float32Array.from(a),buckets:f,minPower:r,maxPower:i}}async function ne(o,e={}){const t=await R(o,e);return{rapid:t.rapidPositions,buckets:t.buckets.map(n=>({opacity:n.opacity,vertices:n.positions})),minPower:t.minPower,maxPower:t.maxPower}}async function O(o,e={}){var S,P;const t=e.arcSegments??30,n=Math.max(1,Math.floor(e.bucketCount??16)),s=!!e.laserMode,r=e.batch,i=o.length*(s?2:1),a=Math.max(1,Math.floor((r==null?void 0:r.everyLines)??5e3)),c=Math.max(0,Math.floor((r==null?void 0:r.yieldEveryLines)??5e4));let u=a,f=c>0?c:Number.POSITIVE_INFINITY,d=Number.NEGATIVE_INFINITY,x=!1;if(s){const m=new C;for(const h of o){if(!h)continue;if(m.parseLine(h).gcodes.some(Y=>{if(Y.letter!=="M")return!1;const E=Math.trunc(Y.value);return E===3||E===4||E===5})){x=!1;break}x=!0}}const l=m=>m.spindle==="M5"||!(m.spindle==="M3"||m.spindle==="M4"||x&&m.spindle===null)?!1:m.spindleSpeed===null?!0:m.spindleSpeed>0;if(s){const m=new I({onLinearMove:h=>{if(!l(h.modals))return;const y=h.modals.spindleSpeed;y===null||y<=0||(d=Math.max(d,y))},onArcMove:h=>{if(!l(h.modals))return;const y=h.modals.spindleSpeed;y===null||y<=0||(d=Math.max(d,y))}});for(let h=0;h<o.length;h+=1){if((S=r==null?void 0:r.shouldAbort)!=null&&S.call(r))throw new Error("Aborted.");const y=o[h];y&&m.processLine(y);const G=h+1;r!=null&&r.onProgress&&(G===i||G===u)&&(r.onProgress(G,i),u+=a),c>0&&(G===i||G===f)&&(await new Promise(Y=>{setTimeout(Y,0)}),f+=c)}Number.isFinite(d)||(d=0)}else d=0;const p=m=>{if(n<=1)return 0;if(m===null)return n-1;const h=m;if(h<=0)return 0;if(d<=0)return n-1;const y=Math.min(1,Math.max(0,h/d)),G=1+Math.floor(y*(n-2));return Math.min(n-1,Math.max(1,G))},v=[],A=s?Array.from({length:n},()=>[]):[new Array],w=new Int32Array(o.length),M=Array.from({length:s?n:1},()=>new Int32Array(o.length)),L=new I({onLinearMove:m=>{const h=m.transformedStart??m.start,y=m.transformedEnd??m.end;if(m.modals.motion==="G0"){v.push(h.X,h.Y,h.Z,y.X,y.Y,y.Z);return}if(s&&!l(m.modals))return;const G=s?p(m.modals.spindleSpeed):0;A[G].push(h.X,h.Y,h.Z,y.X,y.Y,y.Z)},onArcMove:m=>{if(m.modals.motion==="G0"||s&&!l(m.modals))return;const h=s?p(m.modals.spindleSpeed):0,y=A[h];Z(m,y,t)}});for(let m=0;m<o.length;m+=1){if((P=r==null?void 0:r.shouldAbort)!=null&&P.call(r))throw new Error("Aborted.");const h=o[m];h&&L.processLine(h),w[m]=v.length/3;for(let G=0;G<A.length;G+=1)M[G][m]=A[G].length/3;const y=s?o.length+m+1:m+1;r!=null&&r.onProgress&&(y===i||y===u)&&(r.onProgress(y,i),u+=a),c>0&&(y===i||y===f)&&(await new Promise(G=>{setTimeout(G,0)}),f+=c)}return{rapid:{positions:Float32Array.from(v),prefixEndVertex:w},cuts:A.map((m,h)=>({positions:Float32Array.from(m),prefixEndVertex:M[h]})),cutBucketCount:A.length,minPower:0,maxPower:d}}async function R(o,e={}){const t=Math.max(1,Math.floor(e.bucketCount??16)),n=e.baseOpacity??.9,s=await O(o,{arcSegments:e.arcSegments,bucketCount:t,laserMode:!0,batch:e.batch}),r=s.cuts.map((i,a)=>({opacity:U(a,t,n),positions:i.positions,prefixEndVertex:i.prefixEndVertex}));return{rapidPositions:s.rapid.positions,rapidPrefixEndVertex:s.rapid.prefixEndVertex,buckets:r,minPower:0,maxPower:s.maxPower}}function oe(o,e){o.push(e.X,e.Y,e.Z)}function _(o,e){const t=o.transformedStart??o.start,n=o.transformedEnd??o.end;e.push(t.X,t.Y,t.Z,n.X,n.Y,n.Z)}function Z(o,e,t){const n=Math.max(1,Math.floor(t)),{primary:s,secondary:r}=re(o.plane),i=se(o.start[s],o.start[r],o.center[s],o.center[r]),a=Math.atan2(o.start[r]-o.center[r],o.start[s]-o.center[s]),c=Math.atan2(o.end[r]-o.center[r],o.end[s]-o.center[s]),u=Math.PI*2;let f=B(a),d=B(c);o.motion==="G2"?f<=d&&(f+=u):d<=f&&(d+=u);const x=o.motion==="G2"?f-d:d-f;let l={...o.start};for(let p=1;p<=n;p+=1){const v=p/n,A=o.motion==="G2"?f-x*v:f+x*v,w=ie(o.start,o.end,v);w[s]=o.center[s]+i*Math.cos(A),w[r]=o.center[r]+i*Math.sin(A);const M=k(l),L=k(w);e.push(M.X,M.Y,M.Z,L.X,L.Y,L.Z),l=w}}function k(o){const e=o.A*Math.PI/180,t=Math.cos(e),n=Math.sin(e),s=o.Y*t-o.Z*n,r=o.Y*n+o.Z*t;return{...o,Y:s,Z:r}}function re(o){return o==="G18"?{primary:"Z",secondary:"X"}:o==="G19"?{primary:"Y",secondary:"Z"}:{primary:"X",secondary:"Y"}}function B(o){const e=Math.PI*2;let t=o%e;return t<0&&(t+=e),t}function se(o,e,t,n){return Math.hypot(o-t,e-n)}function ie(o,e,t){return{X:o.X+(e.X-o.X)*t,Y:o.Y+(e.Y-o.Y)*t,Z:o.Z+(e.Z-o.Z)*t,A:o.A+(e.A-o.A)*t,B:o.B+(e.B-o.B)*t,C:o.C+(e.C-o.C)*t}}function ae(o){let e=Number.POSITIVE_INFINITY,t=Number.NEGATIVE_INFINITY;const n=new I({onLinearMove:s=>{const r=s.modals.spindleSpeed;r!==null&&(e=Math.min(e,r),t=Math.max(t,r))},onArcMove:s=>{const r=s.modals.spindleSpeed;r!==null&&(e=Math.min(e,r),t=Math.max(t,r))}});for(const s of o)s&&n.processLine(s);return!Number.isFinite(e)||!Number.isFinite(t)?{minPower:0,maxPower:0}:{minPower:e,maxPower:t}}function z(o,e,t,n){if(n<=1)return 0;const s=o??e;if(t<=e)return n-1;const r=(s-e)/(t-e),i=Math.floor(r*(n-1));return Math.min(n-1,Math.max(0,i))}function U(o,e,t){if(e<=1)return t;const n=o/(e-1);return t*n}exports.GCodeParser=C;exports.GCodeVirtualizer=I;exports.buildLaserGeometryFromLinesBatched=R;exports.buildLaserVerticesFromLines=te;exports.buildLaserVerticesFromLinesBatched=ne;exports.buildMovementGeometryFromLinesBatched=ee;exports.buildMovementVerticesFromLines=H;exports.buildMovementVerticesFromLinesBatched=Q;exports.buildToolpathGeometryFromLinesBatched=O;exports.buildVerticesFromLines=J;exports.pushXYZ=oe;
|
package/dist/gviewer.js
ADDED
|
@@ -0,0 +1,654 @@
|
|
|
1
|
+
const _ = /([A-Za-z])\s*([+-]?(?:\d+(?:\.\d*)?|\.\d+))/g;
|
|
2
|
+
class z {
|
|
3
|
+
parseLine(e) {
|
|
4
|
+
const t = [], n = this.stripComments(e, t), s = this.parseWords(n), r = s.filter((a) => {
|
|
5
|
+
const c = a.letter.toUpperCase();
|
|
6
|
+
return c === "G" || c === "M";
|
|
7
|
+
}), i = s.filter((a) => {
|
|
8
|
+
const c = a.letter.toUpperCase();
|
|
9
|
+
return c !== "G" && c !== "M";
|
|
10
|
+
});
|
|
11
|
+
return {
|
|
12
|
+
raw: e,
|
|
13
|
+
words: s,
|
|
14
|
+
gcodes: r,
|
|
15
|
+
params: i,
|
|
16
|
+
comments: t
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
stripComments(e, t) {
|
|
20
|
+
let n = "", s = !1, r = -1, i = "";
|
|
21
|
+
for (let a = 0; a < e.length; a += 1) {
|
|
22
|
+
const c = e[a];
|
|
23
|
+
if (!s && c === ";") {
|
|
24
|
+
const u = e.slice(a + 1);
|
|
25
|
+
t.push({ type: "semicolon", text: u, start: a, end: e.length });
|
|
26
|
+
break;
|
|
27
|
+
}
|
|
28
|
+
if (c === "(") {
|
|
29
|
+
s ? i += c : (s = !0, r = a, i = "");
|
|
30
|
+
continue;
|
|
31
|
+
}
|
|
32
|
+
if (c === ")" && s) {
|
|
33
|
+
t.push({
|
|
34
|
+
type: "paren",
|
|
35
|
+
text: i,
|
|
36
|
+
start: r,
|
|
37
|
+
end: a + 1
|
|
38
|
+
}), s = !1, r = -1, i = "";
|
|
39
|
+
continue;
|
|
40
|
+
}
|
|
41
|
+
if (s) {
|
|
42
|
+
i += c;
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
n += c;
|
|
46
|
+
}
|
|
47
|
+
return s && t.push({
|
|
48
|
+
type: "paren",
|
|
49
|
+
text: i,
|
|
50
|
+
start: r,
|
|
51
|
+
end: e.length
|
|
52
|
+
}), n;
|
|
53
|
+
}
|
|
54
|
+
parseWords(e) {
|
|
55
|
+
const t = [];
|
|
56
|
+
for (const n of e.matchAll(_)) {
|
|
57
|
+
const s = n[0], r = n[1].toUpperCase(), i = Number(n[2]), a = n.index ?? 0, c = a + s.length;
|
|
58
|
+
t.push({ letter: r, value: i, raw: s, start: a, end: c });
|
|
59
|
+
}
|
|
60
|
+
return t;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
const C = ["X", "Y", "Z", "A", "B", "C"], N = {
|
|
64
|
+
motion: "G0",
|
|
65
|
+
distance: "G90",
|
|
66
|
+
plane: "G17",
|
|
67
|
+
units: "G21",
|
|
68
|
+
feedMode: "G94",
|
|
69
|
+
feedRate: null,
|
|
70
|
+
spindleSpeed: null,
|
|
71
|
+
tool: null,
|
|
72
|
+
coolant: null,
|
|
73
|
+
spindle: null,
|
|
74
|
+
coordinateSystem: "G54"
|
|
75
|
+
};
|
|
76
|
+
class L {
|
|
77
|
+
constructor(e = {}) {
|
|
78
|
+
this.parser = new z(), this.modals = { ...N }, this.position = { X: 0, Y: 0, Z: 0, A: 0, B: 0, C: 0 }, this.callbacks = e, this.feedRates = /* @__PURE__ */ new Set(), this.spindleSpeeds = /* @__PURE__ */ new Set(), this.tools = /* @__PURE__ */ new Set();
|
|
79
|
+
}
|
|
80
|
+
setCallbacks(e) {
|
|
81
|
+
this.callbacks = e;
|
|
82
|
+
}
|
|
83
|
+
getModals() {
|
|
84
|
+
return { ...this.modals };
|
|
85
|
+
}
|
|
86
|
+
getPosition() {
|
|
87
|
+
return { ...this.position };
|
|
88
|
+
}
|
|
89
|
+
getUniqueFeedRates() {
|
|
90
|
+
return Array.from(this.feedRates);
|
|
91
|
+
}
|
|
92
|
+
getUniqueSpindleSpeeds() {
|
|
93
|
+
return Array.from(this.spindleSpeeds);
|
|
94
|
+
}
|
|
95
|
+
getUniqueTools() {
|
|
96
|
+
return Array.from(this.tools);
|
|
97
|
+
}
|
|
98
|
+
reset() {
|
|
99
|
+
this.modals = { ...N }, this.position = { X: 0, Y: 0, Z: 0, A: 0, B: 0, C: 0 }, this.feedRates.clear(), this.spindleSpeeds.clear(), this.tools.clear();
|
|
100
|
+
}
|
|
101
|
+
processLine(e) {
|
|
102
|
+
const t = this.parser.parseLine(e), n = { ...this.position };
|
|
103
|
+
this.updateModals(t);
|
|
104
|
+
const s = this.modals.motion, r = this.modals.plane;
|
|
105
|
+
let i = this.applyAxes(t, n), a, c, u = "none";
|
|
106
|
+
return s === "G0" || s === "G1" ? T(n, i) || (u = "linear", this.position = { ...i }, this.emitLinear({ start: n, end: i })) : (s === "G2" || s === "G3") && (T(n, i) || (u = "arc", c = this.arcCenter(t, n, i, r), a = this.computeArcMax(n, i, s, r, c), this.position = { ...i }, this.emitArc({ start: n, end: i, max: a, center: c, plane: r, motion: s }))), {
|
|
107
|
+
parsed: t,
|
|
108
|
+
modals: { ...this.modals },
|
|
109
|
+
start: n,
|
|
110
|
+
end: i,
|
|
111
|
+
movement: u,
|
|
112
|
+
arcMax: a
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
unitsScale() {
|
|
116
|
+
return this.modals.units === "G20" ? 25.4 : 1;
|
|
117
|
+
}
|
|
118
|
+
updateModals(e) {
|
|
119
|
+
for (const t of e.gcodes) {
|
|
120
|
+
if (t.letter.toUpperCase() === "G") {
|
|
121
|
+
const n = `G${Math.trunc(t.value)}`;
|
|
122
|
+
(n === "G0" || n === "G1" || n === "G2" || n === "G3") && (this.modals.motion = n), (n === "G90" || n === "G91") && (this.modals.distance = n), (n === "G17" || n === "G18" || n === "G19") && (this.modals.plane = n), (n === "G20" || n === "G21") && (this.modals.units = n), (n === "G93" || n === "G94") && (this.modals.feedMode = n), (n === "G54" || n === "G55" || n === "G56" || n === "G57" || n === "G58" || n === "G59") && (this.modals.coordinateSystem = n);
|
|
123
|
+
}
|
|
124
|
+
if (t.letter.toUpperCase() === "M") {
|
|
125
|
+
const n = `M${Math.trunc(t.value)}`;
|
|
126
|
+
(n === "M7" || n === "M8" || n === "M9") && (this.modals.coolant = n), (n === "M3" || n === "M4" || n === "M5") && (this.modals.spindle = n);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
for (const t of e.params) {
|
|
130
|
+
const n = t.letter.toUpperCase();
|
|
131
|
+
n === "F" && (this.modals.feedRate = t.value, this.feedRates.add(t.value)), n === "S" && (this.modals.spindleSpeed = t.value, this.spindleSpeeds.add(t.value)), n === "T" && (this.modals.tool = t.value, this.tools.add(t.value));
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
applyAxes(e, t) {
|
|
135
|
+
const n = { ...t }, s = this.unitsScale();
|
|
136
|
+
for (const r of C) {
|
|
137
|
+
const i = e.params.find((c) => c.letter.toUpperCase() === r);
|
|
138
|
+
if (!i)
|
|
139
|
+
continue;
|
|
140
|
+
const a = r === "X" || r === "Y" || r === "Z" ? i.value * s : i.value;
|
|
141
|
+
this.modals.distance === "G90" ? n[r] = a : n[r] = n[r] + a;
|
|
142
|
+
}
|
|
143
|
+
return n;
|
|
144
|
+
}
|
|
145
|
+
computeArcMax(e, t, n, s, r) {
|
|
146
|
+
const { primary: i, secondary: a, tertiary: c } = F(s), u = q(
|
|
147
|
+
e[i],
|
|
148
|
+
e[a],
|
|
149
|
+
r[i],
|
|
150
|
+
r[a]
|
|
151
|
+
), m = Math.atan2(
|
|
152
|
+
e[a] - r[a],
|
|
153
|
+
e[i] - r[i]
|
|
154
|
+
), d = Math.atan2(
|
|
155
|
+
t[a] - r[a],
|
|
156
|
+
t[i] - r[i]
|
|
157
|
+
), x = $(m, d, n);
|
|
158
|
+
let l = Number.NEGATIVE_INFINITY, p = Number.NEGATIVE_INFINITY;
|
|
159
|
+
for (const A of x) {
|
|
160
|
+
const S = r[i] + u * Math.cos(A), M = r[a] + u * Math.sin(A);
|
|
161
|
+
S > l && (l = S), M > p && (p = M);
|
|
162
|
+
}
|
|
163
|
+
const v = { ...e };
|
|
164
|
+
v[i] = l, v[a] = p, v[c] = Math.max(e[c], t[c]);
|
|
165
|
+
for (const A of C)
|
|
166
|
+
A !== i && A !== a && A !== c && (v[A] = Math.max(e[A], t[A]));
|
|
167
|
+
return v;
|
|
168
|
+
}
|
|
169
|
+
arcCenter(e, t, n, s) {
|
|
170
|
+
const { primary: r, secondary: i } = F(s), a = { ...t }, c = this.unitsScale(), u = V(e, "R"), m = U(e, s), d = m.primary === null ? null : m.primary * c, x = m.secondary === null ? null : m.secondary * c;
|
|
171
|
+
if (d !== null || x !== null)
|
|
172
|
+
return a[r] = t[r] + (d ?? 0), a[i] = t[i] + (x ?? 0), a;
|
|
173
|
+
if (u === null)
|
|
174
|
+
return a;
|
|
175
|
+
const l = u * c, p = t[r], v = t[i], A = n[r], S = n[i], M = A - p, w = S - v, P = Math.hypot(M, w);
|
|
176
|
+
if (P === 0)
|
|
177
|
+
return a;
|
|
178
|
+
const I = Math.abs(l), f = Math.sqrt(Math.max(0, I * I - P / 2 * (P / 2))), h = (p + A) / 2, y = (v + S) / 2, G = -w / P, Y = M / P, E = l >= 0 ? 1 : -1;
|
|
179
|
+
return a[r] = h + E * G * f, a[i] = y + E * Y * f, a;
|
|
180
|
+
}
|
|
181
|
+
emitLinear(e) {
|
|
182
|
+
const t = this.callbacks.onLinearMove;
|
|
183
|
+
if (!t)
|
|
184
|
+
return;
|
|
185
|
+
const n = e.end.A - e.start.A, s = Math.max(1, Math.ceil(Math.abs(n) / D));
|
|
186
|
+
let r = { ...e.start };
|
|
187
|
+
for (let i = 1; i <= s; i += 1) {
|
|
188
|
+
const a = i / s, c = W(e.start, e.end, a), u = Z(r), m = Z(c);
|
|
189
|
+
t({
|
|
190
|
+
modals: { ...this.modals },
|
|
191
|
+
start: { ...r },
|
|
192
|
+
end: { ...c },
|
|
193
|
+
transformedStart: u,
|
|
194
|
+
transformedEnd: m
|
|
195
|
+
}), r = c;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
emitArc(e) {
|
|
199
|
+
const t = this.callbacks.onArcMove;
|
|
200
|
+
if (!t)
|
|
201
|
+
return;
|
|
202
|
+
const n = Z(e.start), s = Z(e.end), r = Z(e.max), i = Z(e.center);
|
|
203
|
+
t({
|
|
204
|
+
modals: { ...this.modals },
|
|
205
|
+
start: { ...e.start },
|
|
206
|
+
end: { ...e.end },
|
|
207
|
+
max: { ...e.max },
|
|
208
|
+
center: { ...e.center },
|
|
209
|
+
plane: e.plane,
|
|
210
|
+
motion: e.motion,
|
|
211
|
+
transformedStart: n,
|
|
212
|
+
transformedEnd: s,
|
|
213
|
+
transformedMax: r,
|
|
214
|
+
transformedCenter: i
|
|
215
|
+
});
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
function V(o, e) {
|
|
219
|
+
const t = o.params.find((n) => n.letter.toUpperCase() === e);
|
|
220
|
+
return t ? t.value : null;
|
|
221
|
+
}
|
|
222
|
+
function F(o) {
|
|
223
|
+
return o === "G18" ? { primary: "Z", secondary: "X", tertiary: "Y" } : o === "G19" ? { primary: "Y", secondary: "Z", tertiary: "X" } : { primary: "X", secondary: "Y", tertiary: "Z" };
|
|
224
|
+
}
|
|
225
|
+
function U(o, e) {
|
|
226
|
+
const t = V(o, "I"), n = V(o, "J"), s = V(o, "K");
|
|
227
|
+
return e === "G18" ? { primary: s, secondary: t } : e === "G19" ? { primary: n, secondary: s } : { primary: t, secondary: n };
|
|
228
|
+
}
|
|
229
|
+
function $(o, e, t) {
|
|
230
|
+
const n = Math.PI * 2;
|
|
231
|
+
let s = g(o), r = g(e);
|
|
232
|
+
const i = [0, Math.PI / 2, Math.PI, 3 * Math.PI / 2, n];
|
|
233
|
+
if (t === "G2") {
|
|
234
|
+
s < r && (s += n);
|
|
235
|
+
const c = [s, r];
|
|
236
|
+
for (const u of i) {
|
|
237
|
+
let m = u;
|
|
238
|
+
m < r && (m += n), m <= s && m >= r && c.push(m);
|
|
239
|
+
}
|
|
240
|
+
return c;
|
|
241
|
+
}
|
|
242
|
+
r < s && (r += n);
|
|
243
|
+
const a = [s, r];
|
|
244
|
+
for (const c of i) {
|
|
245
|
+
let u = c;
|
|
246
|
+
u < s && (u += n), u >= s && u <= r && a.push(u);
|
|
247
|
+
}
|
|
248
|
+
return a;
|
|
249
|
+
}
|
|
250
|
+
function g(o) {
|
|
251
|
+
const e = Math.PI * 2;
|
|
252
|
+
let t = o % e;
|
|
253
|
+
return t < 0 && (t += e), t;
|
|
254
|
+
}
|
|
255
|
+
function q(o, e, t, n) {
|
|
256
|
+
return Math.hypot(o - t, e - n);
|
|
257
|
+
}
|
|
258
|
+
function T(o, e) {
|
|
259
|
+
return C.every((t) => o[t] === e[t]);
|
|
260
|
+
}
|
|
261
|
+
const D = 5;
|
|
262
|
+
function W(o, e, t) {
|
|
263
|
+
return {
|
|
264
|
+
X: o.X + (e.X - o.X) * t,
|
|
265
|
+
Y: o.Y + (e.Y - o.Y) * t,
|
|
266
|
+
Z: o.Z + (e.Z - o.Z) * t,
|
|
267
|
+
A: o.A + (e.A - o.A) * t,
|
|
268
|
+
B: o.B + (e.B - o.B) * t,
|
|
269
|
+
C: o.C + (e.C - o.C) * t
|
|
270
|
+
};
|
|
271
|
+
}
|
|
272
|
+
function Z(o) {
|
|
273
|
+
const e = o.A * Math.PI / 180, t = Math.cos(e), n = Math.sin(e), s = o.Y * t - o.Z * n, r = o.Y * n + o.Z * t;
|
|
274
|
+
return { ...o, Y: s, Z: r };
|
|
275
|
+
}
|
|
276
|
+
function te(o, e = {}) {
|
|
277
|
+
const t = [], n = e.arcSegments ?? 30, s = e.collector ?? {}, r = new L({
|
|
278
|
+
onLinearMove: (i) => {
|
|
279
|
+
(s.onLinearMove ?? O)(i, t);
|
|
280
|
+
},
|
|
281
|
+
onArcMove: (i) => {
|
|
282
|
+
(s.onArcMove ?? ((c, u) => {
|
|
283
|
+
X(c, u, n);
|
|
284
|
+
}))(i, t);
|
|
285
|
+
}
|
|
286
|
+
});
|
|
287
|
+
for (const i of o)
|
|
288
|
+
i && r.processLine(i);
|
|
289
|
+
return Float32Array.from(t);
|
|
290
|
+
}
|
|
291
|
+
function ne(o, e = {}) {
|
|
292
|
+
const t = [], n = [], s = e.arcSegments ?? 30, r = new L({
|
|
293
|
+
onLinearMove: (i) => {
|
|
294
|
+
const a = i.transformedStart ?? i.start, c = i.transformedEnd ?? i.end;
|
|
295
|
+
(i.modals.motion === "G0" ? t : n).push(a.X, a.Y, a.Z, c.X, c.Y, c.Z);
|
|
296
|
+
},
|
|
297
|
+
onArcMove: (i) => {
|
|
298
|
+
const a = i.modals.motion === "G0" ? t : n;
|
|
299
|
+
X(i, a, s);
|
|
300
|
+
}
|
|
301
|
+
});
|
|
302
|
+
for (const i of o)
|
|
303
|
+
i && r.processLine(i);
|
|
304
|
+
return {
|
|
305
|
+
rapid: Float32Array.from(t),
|
|
306
|
+
cutting: Float32Array.from(n)
|
|
307
|
+
};
|
|
308
|
+
}
|
|
309
|
+
async function oe(o, e = {}) {
|
|
310
|
+
var x;
|
|
311
|
+
const t = [], n = [], s = e.arcSegments ?? 30, r = e.batch, i = Math.max(1, Math.floor((r == null ? void 0 : r.everyLines) ?? 5e3)), a = Math.max(0, Math.floor((r == null ? void 0 : r.yieldEveryLines) ?? 5e4));
|
|
312
|
+
let c = i, u = a > 0 ? a : Number.POSITIVE_INFINITY;
|
|
313
|
+
const m = new L({
|
|
314
|
+
onLinearMove: (l) => {
|
|
315
|
+
const p = l.transformedStart ?? l.start, v = l.transformedEnd ?? l.end;
|
|
316
|
+
(l.modals.motion === "G0" ? t : n).push(p.X, p.Y, p.Z, v.X, v.Y, v.Z);
|
|
317
|
+
},
|
|
318
|
+
onArcMove: (l) => {
|
|
319
|
+
const p = l.modals.motion === "G0" ? t : n;
|
|
320
|
+
X(l, p, s);
|
|
321
|
+
}
|
|
322
|
+
}), d = o.length;
|
|
323
|
+
for (let l = 0; l < d; l += 1) {
|
|
324
|
+
if ((x = r == null ? void 0 : r.shouldAbort) != null && x.call(r))
|
|
325
|
+
throw new Error("Aborted.");
|
|
326
|
+
const p = o[l];
|
|
327
|
+
if (!p) {
|
|
328
|
+
r != null && r.onProgress && (l + 1 === d || l + 1 === c) && (r.onProgress(l + 1, d), c += i), a > 0 && (l + 1 === d || l + 1 === u) && (await new Promise((v) => {
|
|
329
|
+
setTimeout(v, 0);
|
|
330
|
+
}), u += a);
|
|
331
|
+
continue;
|
|
332
|
+
}
|
|
333
|
+
m.processLine(p), r != null && r.onProgress && (l + 1 === d || l + 1 === c) && (r.onProgress(l + 1, d), c += i), a > 0 && (l + 1 === d || l + 1 === u) && (await new Promise((v) => {
|
|
334
|
+
setTimeout(v, 0);
|
|
335
|
+
}), u += a);
|
|
336
|
+
}
|
|
337
|
+
return {
|
|
338
|
+
rapid: Float32Array.from(t),
|
|
339
|
+
cutting: Float32Array.from(n)
|
|
340
|
+
};
|
|
341
|
+
}
|
|
342
|
+
async function re(o, e = {}) {
|
|
343
|
+
var S;
|
|
344
|
+
const t = [], n = e.arcSegments ?? 30, s = e.batch, r = Math.max(1, Math.floor((s == null ? void 0 : s.everyLines) ?? 5e3)), i = Math.max(0, Math.floor((s == null ? void 0 : s.yieldEveryLines) ?? 5e4));
|
|
345
|
+
let a = r, c = i > 0 ? i : Number.POSITIVE_INFINITY;
|
|
346
|
+
const u = new Int32Array(o.length);
|
|
347
|
+
u.fill(-1);
|
|
348
|
+
const m = new Int32Array(o.length);
|
|
349
|
+
m.fill(-1);
|
|
350
|
+
const d = new Uint8Array(o.length), x = new Int32Array(o.length);
|
|
351
|
+
let l = -1, p = 0;
|
|
352
|
+
const v = new L({
|
|
353
|
+
onLinearMove: (M) => {
|
|
354
|
+
const w = M.modals.motion === "G0" ? 1 : 2;
|
|
355
|
+
l >= 0 && (p === 0 ? p = w : p !== w && (p = 3)), O(M, t);
|
|
356
|
+
},
|
|
357
|
+
onArcMove: (M) => {
|
|
358
|
+
const w = M.modals.motion === "G0" ? 1 : 2;
|
|
359
|
+
l >= 0 && (p === 0 ? p = w : p !== w && (p = 3)), X(M, t, n);
|
|
360
|
+
}
|
|
361
|
+
}), A = o.length;
|
|
362
|
+
for (let M = 0; M < A; M += 1) {
|
|
363
|
+
if ((S = s == null ? void 0 : s.shouldAbort) != null && S.call(s))
|
|
364
|
+
throw new Error("Aborted.");
|
|
365
|
+
const w = t.length / 3;
|
|
366
|
+
l = M, p = 0;
|
|
367
|
+
const P = o[M];
|
|
368
|
+
P && v.processLine(P), l = -1;
|
|
369
|
+
const I = t.length / 3;
|
|
370
|
+
I > w && (u[M] = w, m[M] = I, d[M] = p), x[M] = I, s != null && s.onProgress && (M + 1 === A || M + 1 === a) && (s.onProgress(M + 1, A), a += r), i > 0 && (M + 1 === A || M + 1 === c) && (await new Promise((f) => {
|
|
371
|
+
setTimeout(f, 0);
|
|
372
|
+
}), c += i);
|
|
373
|
+
}
|
|
374
|
+
return {
|
|
375
|
+
positions: Float32Array.from(t),
|
|
376
|
+
lineStartVertex: u,
|
|
377
|
+
lineEndVertex: m,
|
|
378
|
+
lineKind: d,
|
|
379
|
+
prefixEndVertex: x
|
|
380
|
+
};
|
|
381
|
+
}
|
|
382
|
+
function se(o, e = {}) {
|
|
383
|
+
const t = e.arcSegments ?? 30, n = Math.max(1, Math.floor(e.bucketCount ?? 16)), s = e.baseOpacity ?? 0.9, { minPower: r, maxPower: i } = ee(o), a = [], c = Array.from({ length: n }, () => []), u = new L({
|
|
384
|
+
onLinearMove: (d) => {
|
|
385
|
+
const x = d.transformedStart ?? d.start, l = d.transformedEnd ?? d.end;
|
|
386
|
+
if (d.modals.motion === "G0") {
|
|
387
|
+
a.push(x.X, x.Y, x.Z, l.X, l.Y, l.Z);
|
|
388
|
+
return;
|
|
389
|
+
}
|
|
390
|
+
const p = B(d.modals.spindleSpeed, r, i, n);
|
|
391
|
+
c[p].push(x.X, x.Y, x.Z, l.X, l.Y, l.Z);
|
|
392
|
+
},
|
|
393
|
+
onArcMove: (d) => {
|
|
394
|
+
if (d.modals.motion === "G0")
|
|
395
|
+
return;
|
|
396
|
+
const x = B(d.modals.spindleSpeed, r, i, n), l = c[x];
|
|
397
|
+
X(d, l, t);
|
|
398
|
+
}
|
|
399
|
+
});
|
|
400
|
+
for (const d of o)
|
|
401
|
+
d && u.processLine(d);
|
|
402
|
+
const m = c.map((d, x) => ({
|
|
403
|
+
opacity: R(x, n, s),
|
|
404
|
+
vertices: Float32Array.from(d)
|
|
405
|
+
}));
|
|
406
|
+
return {
|
|
407
|
+
rapid: Float32Array.from(a),
|
|
408
|
+
buckets: m,
|
|
409
|
+
minPower: r,
|
|
410
|
+
maxPower: i
|
|
411
|
+
};
|
|
412
|
+
}
|
|
413
|
+
async function ie(o, e = {}) {
|
|
414
|
+
const t = await j(o, e);
|
|
415
|
+
return {
|
|
416
|
+
rapid: t.rapidPositions,
|
|
417
|
+
buckets: t.buckets.map((n) => ({ opacity: n.opacity, vertices: n.positions })),
|
|
418
|
+
minPower: t.minPower,
|
|
419
|
+
maxPower: t.maxPower
|
|
420
|
+
};
|
|
421
|
+
}
|
|
422
|
+
async function K(o, e = {}) {
|
|
423
|
+
var P, I;
|
|
424
|
+
const t = e.arcSegments ?? 30, n = Math.max(1, Math.floor(e.bucketCount ?? 16)), s = !!e.laserMode, r = e.batch, i = o.length * (s ? 2 : 1), a = Math.max(1, Math.floor((r == null ? void 0 : r.everyLines) ?? 5e3)), c = Math.max(0, Math.floor((r == null ? void 0 : r.yieldEveryLines) ?? 5e4));
|
|
425
|
+
let u = a, m = c > 0 ? c : Number.POSITIVE_INFINITY, d = Number.NEGATIVE_INFINITY, x = !1;
|
|
426
|
+
if (s) {
|
|
427
|
+
const f = new z();
|
|
428
|
+
for (const h of o) {
|
|
429
|
+
if (!h)
|
|
430
|
+
continue;
|
|
431
|
+
if (f.parseLine(h).gcodes.some((Y) => {
|
|
432
|
+
if (Y.letter !== "M")
|
|
433
|
+
return !1;
|
|
434
|
+
const E = Math.trunc(Y.value);
|
|
435
|
+
return E === 3 || E === 4 || E === 5;
|
|
436
|
+
})) {
|
|
437
|
+
x = !1;
|
|
438
|
+
break;
|
|
439
|
+
}
|
|
440
|
+
x = !0;
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
const l = (f) => f.spindle === "M5" || !(f.spindle === "M3" || f.spindle === "M4" || x && f.spindle === null) ? !1 : f.spindleSpeed === null ? !0 : f.spindleSpeed > 0;
|
|
444
|
+
if (s) {
|
|
445
|
+
const f = new L({
|
|
446
|
+
onLinearMove: (h) => {
|
|
447
|
+
if (!l(h.modals))
|
|
448
|
+
return;
|
|
449
|
+
const y = h.modals.spindleSpeed;
|
|
450
|
+
y === null || y <= 0 || (d = Math.max(d, y));
|
|
451
|
+
},
|
|
452
|
+
onArcMove: (h) => {
|
|
453
|
+
if (!l(h.modals))
|
|
454
|
+
return;
|
|
455
|
+
const y = h.modals.spindleSpeed;
|
|
456
|
+
y === null || y <= 0 || (d = Math.max(d, y));
|
|
457
|
+
}
|
|
458
|
+
});
|
|
459
|
+
for (let h = 0; h < o.length; h += 1) {
|
|
460
|
+
if ((P = r == null ? void 0 : r.shouldAbort) != null && P.call(r))
|
|
461
|
+
throw new Error("Aborted.");
|
|
462
|
+
const y = o[h];
|
|
463
|
+
y && f.processLine(y);
|
|
464
|
+
const G = h + 1;
|
|
465
|
+
r != null && r.onProgress && (G === i || G === u) && (r.onProgress(G, i), u += a), c > 0 && (G === i || G === m) && (await new Promise((Y) => {
|
|
466
|
+
setTimeout(Y, 0);
|
|
467
|
+
}), m += c);
|
|
468
|
+
}
|
|
469
|
+
Number.isFinite(d) || (d = 0);
|
|
470
|
+
} else
|
|
471
|
+
d = 0;
|
|
472
|
+
const p = (f) => {
|
|
473
|
+
if (n <= 1)
|
|
474
|
+
return 0;
|
|
475
|
+
if (f === null)
|
|
476
|
+
return n - 1;
|
|
477
|
+
const h = f;
|
|
478
|
+
if (h <= 0)
|
|
479
|
+
return 0;
|
|
480
|
+
if (d <= 0)
|
|
481
|
+
return n - 1;
|
|
482
|
+
const y = Math.min(1, Math.max(0, h / d)), G = 1 + Math.floor(y * (n - 2));
|
|
483
|
+
return Math.min(n - 1, Math.max(1, G));
|
|
484
|
+
}, v = [], A = s ? Array.from({ length: n }, () => []) : [new Array()], S = new Int32Array(o.length), M = Array.from(
|
|
485
|
+
{ length: s ? n : 1 },
|
|
486
|
+
() => new Int32Array(o.length)
|
|
487
|
+
), w = new L({
|
|
488
|
+
onLinearMove: (f) => {
|
|
489
|
+
const h = f.transformedStart ?? f.start, y = f.transformedEnd ?? f.end;
|
|
490
|
+
if (f.modals.motion === "G0") {
|
|
491
|
+
v.push(h.X, h.Y, h.Z, y.X, y.Y, y.Z);
|
|
492
|
+
return;
|
|
493
|
+
}
|
|
494
|
+
if (s && !l(f.modals))
|
|
495
|
+
return;
|
|
496
|
+
const G = s ? p(f.modals.spindleSpeed) : 0;
|
|
497
|
+
A[G].push(h.X, h.Y, h.Z, y.X, y.Y, y.Z);
|
|
498
|
+
},
|
|
499
|
+
onArcMove: (f) => {
|
|
500
|
+
if (f.modals.motion === "G0" || s && !l(f.modals))
|
|
501
|
+
return;
|
|
502
|
+
const h = s ? p(f.modals.spindleSpeed) : 0, y = A[h];
|
|
503
|
+
X(f, y, t);
|
|
504
|
+
}
|
|
505
|
+
});
|
|
506
|
+
for (let f = 0; f < o.length; f += 1) {
|
|
507
|
+
if ((I = r == null ? void 0 : r.shouldAbort) != null && I.call(r))
|
|
508
|
+
throw new Error("Aborted.");
|
|
509
|
+
const h = o[f];
|
|
510
|
+
h && w.processLine(h), S[f] = v.length / 3;
|
|
511
|
+
for (let G = 0; G < A.length; G += 1)
|
|
512
|
+
M[G][f] = A[G].length / 3;
|
|
513
|
+
const y = s ? o.length + f + 1 : f + 1;
|
|
514
|
+
r != null && r.onProgress && (y === i || y === u) && (r.onProgress(y, i), u += a), c > 0 && (y === i || y === m) && (await new Promise((G) => {
|
|
515
|
+
setTimeout(G, 0);
|
|
516
|
+
}), m += c);
|
|
517
|
+
}
|
|
518
|
+
return {
|
|
519
|
+
rapid: { positions: Float32Array.from(v), prefixEndVertex: S },
|
|
520
|
+
cuts: A.map((f, h) => ({
|
|
521
|
+
positions: Float32Array.from(f),
|
|
522
|
+
prefixEndVertex: M[h]
|
|
523
|
+
})),
|
|
524
|
+
cutBucketCount: A.length,
|
|
525
|
+
minPower: 0,
|
|
526
|
+
maxPower: d
|
|
527
|
+
};
|
|
528
|
+
}
|
|
529
|
+
async function j(o, e = {}) {
|
|
530
|
+
const t = Math.max(1, Math.floor(e.bucketCount ?? 16)), n = e.baseOpacity ?? 0.9, s = await K(o, {
|
|
531
|
+
arcSegments: e.arcSegments,
|
|
532
|
+
bucketCount: t,
|
|
533
|
+
laserMode: !0,
|
|
534
|
+
batch: e.batch
|
|
535
|
+
}), r = s.cuts.map((i, a) => ({
|
|
536
|
+
opacity: R(a, t, n),
|
|
537
|
+
positions: i.positions,
|
|
538
|
+
prefixEndVertex: i.prefixEndVertex
|
|
539
|
+
}));
|
|
540
|
+
return {
|
|
541
|
+
rapidPositions: s.rapid.positions,
|
|
542
|
+
rapidPrefixEndVertex: s.rapid.prefixEndVertex,
|
|
543
|
+
buckets: r,
|
|
544
|
+
minPower: 0,
|
|
545
|
+
maxPower: s.maxPower
|
|
546
|
+
};
|
|
547
|
+
}
|
|
548
|
+
function ae(o, e) {
|
|
549
|
+
o.push(e.X, e.Y, e.Z);
|
|
550
|
+
}
|
|
551
|
+
function O(o, e) {
|
|
552
|
+
const t = o.transformedStart ?? o.start, n = o.transformedEnd ?? o.end;
|
|
553
|
+
e.push(t.X, t.Y, t.Z, n.X, n.Y, n.Z);
|
|
554
|
+
}
|
|
555
|
+
function X(o, e, t) {
|
|
556
|
+
const n = Math.max(1, Math.floor(t)), { primary: s, secondary: r } = J(o.plane), i = H(
|
|
557
|
+
o.start[s],
|
|
558
|
+
o.start[r],
|
|
559
|
+
o.center[s],
|
|
560
|
+
o.center[r]
|
|
561
|
+
), a = Math.atan2(
|
|
562
|
+
o.start[r] - o.center[r],
|
|
563
|
+
o.start[s] - o.center[s]
|
|
564
|
+
), c = Math.atan2(
|
|
565
|
+
o.end[r] - o.center[r],
|
|
566
|
+
o.end[s] - o.center[s]
|
|
567
|
+
), u = Math.PI * 2;
|
|
568
|
+
let m = k(a), d = k(c);
|
|
569
|
+
o.motion === "G2" ? m <= d && (m += u) : d <= m && (d += u);
|
|
570
|
+
const x = o.motion === "G2" ? m - d : d - m;
|
|
571
|
+
let l = { ...o.start };
|
|
572
|
+
for (let p = 1; p <= n; p += 1) {
|
|
573
|
+
const v = p / n, A = o.motion === "G2" ? m - x * v : m + x * v, S = Q(o.start, o.end, v);
|
|
574
|
+
S[s] = o.center[s] + i * Math.cos(A), S[r] = o.center[r] + i * Math.sin(A);
|
|
575
|
+
const M = b(l), w = b(S);
|
|
576
|
+
e.push(
|
|
577
|
+
M.X,
|
|
578
|
+
M.Y,
|
|
579
|
+
M.Z,
|
|
580
|
+
w.X,
|
|
581
|
+
w.Y,
|
|
582
|
+
w.Z
|
|
583
|
+
), l = S;
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
function b(o) {
|
|
587
|
+
const e = o.A * Math.PI / 180, t = Math.cos(e), n = Math.sin(e), s = o.Y * t - o.Z * n, r = o.Y * n + o.Z * t;
|
|
588
|
+
return { ...o, Y: s, Z: r };
|
|
589
|
+
}
|
|
590
|
+
function J(o) {
|
|
591
|
+
return o === "G18" ? { primary: "Z", secondary: "X" } : o === "G19" ? { primary: "Y", secondary: "Z" } : { primary: "X", secondary: "Y" };
|
|
592
|
+
}
|
|
593
|
+
function k(o) {
|
|
594
|
+
const e = Math.PI * 2;
|
|
595
|
+
let t = o % e;
|
|
596
|
+
return t < 0 && (t += e), t;
|
|
597
|
+
}
|
|
598
|
+
function H(o, e, t, n) {
|
|
599
|
+
return Math.hypot(o - t, e - n);
|
|
600
|
+
}
|
|
601
|
+
function Q(o, e, t) {
|
|
602
|
+
return {
|
|
603
|
+
X: o.X + (e.X - o.X) * t,
|
|
604
|
+
Y: o.Y + (e.Y - o.Y) * t,
|
|
605
|
+
Z: o.Z + (e.Z - o.Z) * t,
|
|
606
|
+
A: o.A + (e.A - o.A) * t,
|
|
607
|
+
B: o.B + (e.B - o.B) * t,
|
|
608
|
+
C: o.C + (e.C - o.C) * t
|
|
609
|
+
};
|
|
610
|
+
}
|
|
611
|
+
function ee(o) {
|
|
612
|
+
let e = Number.POSITIVE_INFINITY, t = Number.NEGATIVE_INFINITY;
|
|
613
|
+
const n = new L({
|
|
614
|
+
onLinearMove: (s) => {
|
|
615
|
+
const r = s.modals.spindleSpeed;
|
|
616
|
+
r !== null && (e = Math.min(e, r), t = Math.max(t, r));
|
|
617
|
+
},
|
|
618
|
+
onArcMove: (s) => {
|
|
619
|
+
const r = s.modals.spindleSpeed;
|
|
620
|
+
r !== null && (e = Math.min(e, r), t = Math.max(t, r));
|
|
621
|
+
}
|
|
622
|
+
});
|
|
623
|
+
for (const s of o)
|
|
624
|
+
s && n.processLine(s);
|
|
625
|
+
return !Number.isFinite(e) || !Number.isFinite(t) ? { minPower: 0, maxPower: 0 } : { minPower: e, maxPower: t };
|
|
626
|
+
}
|
|
627
|
+
function B(o, e, t, n) {
|
|
628
|
+
if (n <= 1)
|
|
629
|
+
return 0;
|
|
630
|
+
const s = o ?? e;
|
|
631
|
+
if (t <= e)
|
|
632
|
+
return n - 1;
|
|
633
|
+
const r = (s - e) / (t - e), i = Math.floor(r * (n - 1));
|
|
634
|
+
return Math.min(n - 1, Math.max(0, i));
|
|
635
|
+
}
|
|
636
|
+
function R(o, e, t) {
|
|
637
|
+
if (e <= 1)
|
|
638
|
+
return t;
|
|
639
|
+
const n = o / (e - 1);
|
|
640
|
+
return t * n;
|
|
641
|
+
}
|
|
642
|
+
export {
|
|
643
|
+
z as GCodeParser,
|
|
644
|
+
L as GCodeVirtualizer,
|
|
645
|
+
j as buildLaserGeometryFromLinesBatched,
|
|
646
|
+
se as buildLaserVerticesFromLines,
|
|
647
|
+
ie as buildLaserVerticesFromLinesBatched,
|
|
648
|
+
re as buildMovementGeometryFromLinesBatched,
|
|
649
|
+
ne as buildMovementVerticesFromLines,
|
|
650
|
+
oe as buildMovementVerticesFromLinesBatched,
|
|
651
|
+
K as buildToolpathGeometryFromLinesBatched,
|
|
652
|
+
te as buildVerticesFromLines,
|
|
653
|
+
ae as pushXYZ
|
|
654
|
+
};
|
package/dist/react.cjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const w=require("react"),V=require("./GCodeSVGRenderer-DaLc9_L-.cjs");function C(a){const u=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(a){for(const s in a)if(s!=="default"){const i=Object.getOwnPropertyDescriptor(a,s);Object.defineProperty(u,s,i.get?i:{enumerable:!0,get:()=>a[s]})}}return u.default=a,Object.freeze(u)}const o=C(w),G=o.forwardRef(function(u,s){const{id:i,options:l,callbacks:d,className:m,style:f}=u,c=o.useRef(null),r=o.useRef(null);return o.useEffect(()=>{const e=c.current;if(!e)return;const p=new V.GCodeViewer({id:i,container:e,options:l,callbacks:d});return r.current=p,()=>{r.current=null,p.dispose()}},[i]),o.useEffect(()=>{var e;(e=r.current)==null||e.setOptions(l??{})},[l]),o.useEffect(()=>{var e;(e=r.current)==null||e.setCallbacks(d??{})},[d]),o.useImperativeHandle(s,()=>{const e=()=>{const t=r.current;if(!t)throw new Error("GCodeViewer is not ready.");return t};return{get id(){return i},setCallbacks(t){e().setCallbacks(t)},snapCameraToView(t,n){e().snapCameraToView(t,n)},setBitPosition(t,n){e().setBitPosition(t,n)},setBitVisible(t){e().setBitVisible(t)},setToolpathRotationA(t){e().setToolpathRotationA(t)},hideUntilLine(t,n){e().hideUntilLine(t,n)},seekToLine(t,n){e().seekToLine(t,n)},showAll(){e().showAll()},resetColors(){e().resetColors()},loadFromUrl(t,n){return e().loadFromUrl(t,n)},loadFromFile(t){return e().loadFromFile(t)},loadFromText(t){return e().loadFromText(t)},loadFromLines(t){return e().loadFromLines(t)},unload(){e().unload()},setOptions(t){e().setOptions(t)},getOptions(){return e().getOptions()},resize(){e().resize()},focusToModel(){e().focusToModel()},resetCamera(){e().resetCamera()},getBounds(){return e().getBounds()},dispose(){e().dispose()}}},[i]),o.createElement("div",{ref:c,className:m,style:f})}),F=o.forwardRef(function(u,s){const{id:i,options:l,className:d,style:m}=u,f=o.useRef(null),c=o.useRef(null);return o.useEffect(()=>{const r=f.current;if(!r)return;const e=new V.GCodeSVGRenderer(r,l);return c.current=e,()=>{c.current=null,e.dispose()}},[]),o.useEffect(()=>{var r;l&&((r=c.current)==null||r.setOptions(l))},[l]),o.useImperativeHandle(s,()=>{const r=()=>{const e=c.current;if(!e)throw new Error("GCodeSVGRenderer is not ready.");return e};return{loadFromLines:e=>r().loadFromLines(e),loadFromFile:e=>r().loadFromFile(e),loadFromText:e=>r().loadFromText(e),clear:()=>r().clear(),resetView:()=>r().resetView(),setOptions:e=>r().setOptions(e),setProjectionMode:e=>r().setProjectionMode(e),getSVGElement:()=>r().getSVGElement(),dispose:()=>r().dispose()}},[]),o.createElement("div",{ref:f,className:d,style:{width:"100%",height:"100%",...m}})});exports.GCodeSVGVisualizer=F;exports.GCodeVisualizer=G;
|