diff --git a/src/emc/task/emctask.cc b/src/emc/task/emctask.cc index b598bd12b52..0ac5c22f3d1 100644 --- a/src/emc/task/emctask.cc +++ b/src/emc/task/emctask.cc @@ -14,6 +14,7 @@ ********************************************************************/ #include +#include // fabs() #include // rtapi_strlcpy() #include // struct stat #include // stat() @@ -548,6 +549,17 @@ int emcTaskPlanOpen(const char *file) emcStatus->task.readLine = 0; } + // Reset canon length units to machine default before re-opening. + // Without this, G20/G21 from the previous run persists in + // canon.lengthUnits, causing FROM_PROG_LEN() to convert differently + // on re-run for G-code commands that appear before G20/G21. + double units = GET_EXTERNAL_LENGTH_UNITS(); + if (fabs(units - 1.0 / 25.4) < 1.0e-3) { + USE_LENGTH_UNITS(CANON_UNITS_INCHES); + } else if (fabs(units - 1.0) < 1.0e-3) { + USE_LENGTH_UNITS(CANON_UNITS_MM); + } + int retval = interp.open(file); if (retval > INTERP_MIN_ERROR) { print_interp_error(retval);