@@ -16,6 +16,26 @@ const translatePosition = (position, newPosition, relative) => {
1616} ;
1717
1818class Toolpath {
19+ g92offset = {
20+ x : 0 ,
21+ y : 0 ,
22+ z : 0
23+ } ;
24+
25+ offsetG92 = ( pos ) => {
26+ return {
27+ x : pos . x + this . g92offset . x ,
28+ y : pos . y + this . g92offset . y ,
29+ z : pos . z + this . g92offset . z ,
30+ } ;
31+ }
32+ offsetAddLine = ( start , end ) => {
33+ this . fn . addLine ( this . modal , this . offsetG92 ( start ) , this . offsetG92 ( end ) ) ;
34+ }
35+ offsetAddArcCurve = ( start , end , center ) => {
36+ this . fn . addArcCurve ( this . modal , this . offsetG92 ( start ) , this . offsetG92 ( end ) , this . offsetG92 ( center ) ) ;
37+ }
38+
1939 position = {
2040 x : 0 ,
2141 y : 0 ,
@@ -92,7 +112,7 @@ class Toolpath {
92112 } ;
93113 const targetPosition = { x : v2 . x , y : v2 . y , z : v2 . z } ;
94114
95- this . fn . addLine ( this . modal , v1 , v2 ) ;
115+ this . offsetAddLine ( v1 , v2 ) ;
96116
97117 // Update position
98118 this . setPosition ( targetPosition . x , targetPosition . y , targetPosition . z ) ;
@@ -128,7 +148,7 @@ class Toolpath {
128148 } ;
129149 const targetPosition = { x : v2 . x , y : v2 . y , z : v2 . z } ;
130150
131- this . fn . addLine ( this . modal , v1 , v2 ) ;
151+ this . offsetAddLine ( v1 , v2 ) ;
132152
133153 // Update position
134154 this . setPosition ( targetPosition . x , targetPosition . y , targetPosition . z ) ;
@@ -212,7 +232,7 @@ class Toolpath {
212232 v0 . y = v1 . y + offsetY ;
213233 }
214234
215- this . fn . addArcCurve ( this . modal , v1 , v2 , v0 ) ;
235+ this . offsetAddArcCurve ( v1 , v2 , v0 ) ;
216236
217237 // Update position
218238 this . setPosition ( targetPosition . x , targetPosition . y , targetPosition . z ) ;
@@ -278,7 +298,7 @@ class Toolpath {
278298 v0 . y = v1 . y + offsetY ;
279299 }
280300
281- this . fn . addArcCurve ( this . modal , v1 , v2 , v0 ) ;
301+ this . offsetAddArcCurve ( v1 , v2 , v0 ) ;
282302
283303 // Update position
284304 this . setPosition ( targetPosition . x , targetPosition . y , targetPosition . z ) ;
@@ -429,21 +449,44 @@ class Toolpath {
429449 // This would set the machine's X coordinate to 10. No physical motion will occur.
430450 // A G92 without coordinates will reset all axes to zero.
431451 'G92' : ( params ) => {
432- const v2 = {
433- x : this . translateX ( params . X , false ) ,
434- y : this . translateY ( params . Y , false ) ,
435- z : this . translateZ ( params . Z , false )
436- } ;
437-
438452 // A G92 without coordinates will reset all axes to zero.
439453 if ( ( params . X === undefined ) && ( params . Y === undefined ) && ( params . Z === undefined ) ) {
440- v2 . x = 0 ;
441- v2 . y = 0 ;
442- v2 . z = 0 ;
454+ this . position . x += this . g92offset . x ;
455+ this . g92offset . x = 0 ;
456+ this . position . y += this . g92offset . y ;
457+ this . g92offset . y = 0 ;
458+ this . position . z += this . g92offset . z ;
459+ this . g92offset . z = 0 ;
460+ } else {
461+ // The calls to translateX/Y/Z() below are necessary for inch/mm conversion
462+ // params.X/Y/Z must be interpreted as absolute positions, hence the "false"
463+ if ( params . X != undefined ) {
464+ const xmm = this . translateX ( params . X , false ) ;
465+ this . g92offset . x += this . position . x - xmm ;
466+ this . position . x = xmm ;
467+ }
468+ if ( params . Y != undefined ) {
469+ const ymm = this . translateY ( params . Y , false ) ;
470+ this . g92offset . y += this . position . y - ymm ;
471+ this . position . y = ymm ;
472+ }
473+ if ( params . Z != undefined ) {
474+ const zmm = this . translateX ( params . Z , false ) ;
475+ this . g92offset . z += this . position . z - zmm ;
476+ this . position . z = zmm ;
477+ }
443478 }
444-
445- // Update position
446- this . setPosition ( v2 . x , v2 . y , v2 . z ) ;
479+ } ,
480+ // G92.1: Cancel G92 offsets
481+ // Parameters
482+ // none
483+ 'G92.1' : ( params ) => {
484+ this . position . x += this . g92offset . x ;
485+ this . g92offset . x = 0 ;
486+ this . position . y += this . g92offset . y ;
487+ this . g92offset . y = 0 ;
488+ this . position . z += this . g92offset . z ;
489+ this . g92offset . z = 0 ;
447490 } ,
448491 // G93: Inverse Time Mode
449492 // In inverse time feed rate mode, an F word means the move should be completed in
@@ -571,6 +614,7 @@ class Toolpath {
571614 addLine = noop ,
572615 addArcCurve = noop
573616 } = { ...options } ;
617+ this . g92offset . x = this . g92offset . y = this . g92offset . z = 0 ;
574618
575619 // Position
576620 if ( position ) {
0 commit comments