Script to determine minimum and maximum values of X/Y/Z values in G-Codes for CNC machines

Script to determine minimum and maximum values of X/Y/Z values in G-Codes for CNC machines

1
NewbieNewbie
1

    Aug 15, 2016#1

    Hello,

    I'm new to UltraEdit. I use it to edit G-Codes for CNC Machines. Our machines have 3 axis. X,Y,Z. The code tells the machine where to go to. I want to know if there is a way to find the minimum and maximum values for each axis. I've used other editors that have something like:

    Min/Max:

    Code: Select all

    X-5.  X5.
    Y-6.  Y4.
    Z-10.  Z6.
    Here's a sample of the code that we feed to machine.

    Code: Select all

    Y26.906 Z0.03
    G94
    G01 Y28.25 Z-0.3301 F210.0
    X-65.856 Z-1.0
    X-68.356
    Y23.75
    X-63.356
    Y28.25
    X-65.856
    Y25.75
    G00 Z10.0
    Thanks in advance.

    Thanks.

    6,681583
    Grand MasterGrand Master
    6,681583

      Aug 16, 2016#2

      Below is the code for an UltraEdit script for this special task.
      • Copy the code and paste it into a new ASCII file and save the file for example with name AxisGetMinMax.js for example into %APPDATA%\IDMComp\UltraEdit\scripts (on Windows).
      • Add this script to Script List with or without a hotkey or chord for quick execution by key by
        clicking in menu Scripting on menu item Scripts on using traditional menus, or
        clicking in menu Advanced on menu item All scripts on using contemporary menus, or
        clicking on ribbon tab Advanced in group Script on item All scripts on using ribbon mode.
      To run the script
      • use the hotkey or chord if one is assigned to the script, or
      • click on script name in menu Scripting on using traditional menus, or
        click on script name in menu Advanced in submenu Play script on using contemporary menus, or
        click on ribbon tab Advanced in group Script on down arrow of item Play script and click in opened pop-up menu on the script name on using ribbon mode, or
      • double click on the script name in Script List view opened with
        a click in menu Scripting on Script List or in menu View in submenu Views/Lists on Script List on using traditional menus, or
        a click in menu Layout on menu item Script list on using contemporary menus, or
        a click ribbon tab Layout in group Views on item Script list on using ribbon mode.
      The script can be executed just on a selection in active file or on entire active file if there is nothing currently selected in active file.

      #.# is output for minimum and maximum value for an axis if there is no value found for an axis is current selection or entire file.

      Code: Select all

      function GetMinMax (sAxis, nIndexMinMax)
      {
         var fVal;         // Current axis value as floating point.
         var fMin;         // Current minimum value as floating point.
         var fMax;         // Current maximum value as floating point.
      
         var sVal;         // Current axis value as string.
         var sMin = "#.#"; // Current minimum value as string.
         var sMax = "#.#"; // Current maximum value as string.
      
         var oSearchRegExp = new RegExp("\\b" + sAxis + "[-+]?[\\d.]+","gi");
      
         // Get all axis values loaded into an array of strings.
         var asValues = g_sAllData.match(oSearchRegExp);
      
         // Process all axis values.
         if (asValues != null)
         {
            for (var nValueIndex = 0; nValueIndex < asValues.length; nValueIndex++)
            {
               // Get value string without X, Y and Z.
               sVal = asValues[nValueIndex].substr(1);
               // Convert the string to a floating point value.
               fVal = parseFloat(sVal);
               if (!isNaN(fVal))    // Is this a valid floating point value?
               {
                  if (fMin != null) // Is there already a minimum/maximum value?
                  {
                     if (fVal < fMin)
                     {
                        fMin = fVal;
                        sMin = sVal;
                     }
                     if (fVal > fMax)
                     {
                        fMax = fVal;
                        sMax = sVal;
                     }
                  }
                  else  // First valid value for current axis.
                  {
                     fMin = fMax = fVal;
                     sMin = sMax = sVal;
                  }
               }
            }
         }
      
         // Store the minimum and maximum values as strings in global arrays.
         g_asMin[nIndexMinMax] = sMin;
         g_asMax[nIndexMinMax] = sMax;
      }
      
      
      if (UltraEdit.document.length > 0)  // Is any file opened?
      {
         var g_sAllData = "";
      
         // Define environment for this script.
         UltraEdit.insertMode();
         if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
         else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
      
         if (UltraEdit.activeDocument.isSel())  // Is something selected?
         {
            // Get everything of selected block into a global string variable.
            g_sAllData = UltraEdit.activeDocument.selection;
         }
         else  // Process entire file.
         {
            // Get position of caret in file.
            var nCurrentLine = UltraEdit.activeDocument.currentLineNum;
            var nCurrentColumn = UltraEdit.activeDocument.currentColumnNum;
      
            // Starting with UE for Windows v16.00 / UES v10.00 the property
            // currentColumnNum contains the column number with number 1 for
            // first column in line. Former versions return 0 for first column.
            // This difference must be compensated by adding 1 depending on
            // version of UltraEdit / UEStudio.
            if (typeof(UltraEdit.activeDocumentIdx) == "undefined") nCurrentColumn++;
      
            // Select everything in file which moves caret to top of file.
            UltraEdit.activeDocument.selectAll();
            if (UltraEdit.activeDocument.isSel())  // Is the file not empty?
            {
               // Get everything in active file loaded into a global string variable.
               g_sAllData = UltraEdit.activeDocument.selection;
            }
            // Cancel the selection and move caret back to initial position.
            UltraEdit.activeDocument.gotoLine(nCurrentLine,nCurrentColumn);
         }
      
         if (g_sAllData.length > 0)    // Is the active file not empty?
         {
            // Create two arrays for 3 minimum / maximum value strings.
            var g_asMin = new Array(3);
            var g_asMax = new Array(3);
      
            GetMinMax("X",0);    // Determine minimum and maximum X values.
            GetMinMax("Y",1);    // Determine minimum and maximum Y values.
            GetMinMax("Z",2);    // Determine minimum and maximum Z values.
      
            // All 3 minimum values as well as all 3 maximum values should be
            // output aligned centered on decimal point or right aligned if all
            // 3 minimum or all 3 maximum values don't have a decimal point.
            var nMinMaxIndex;
            var sIntegerDigits = "";
            var sDecimalPlaces = "";
            var nMaxIntegerDigits = 0;
            var nMaxDecimalPlaces = 0;
            var nIntegerDigits, nDecimalPlaces;
      
            // First determine maximum number of characters before decimal point
            // and maximum number of characters from decimal point to value end.
            for (nMinMaxIndex = 0; nMinMaxIndex < g_asMin.length; nMinMaxIndex++)
            {
               nIntegerDigits = g_asMin[nMinMaxIndex].indexOf('.');
               if (nIntegerDigits < 0)    // Is there no decimal point?
               {
                  nIntegerDigits = g_asMin[nMinMaxIndex].length;
               }
               if (nIntegerDigits > nMaxIntegerDigits)
               {
                  nMaxIntegerDigits = nIntegerDigits;
               }
               nDecimalPlaces = g_asMin[nMinMaxIndex].length - nIntegerDigits;
               if (nDecimalPlaces > nMaxDecimalPlaces)
               {
                  nMaxDecimalPlaces = nDecimalPlaces;
               }
            }
      
            // Build strings just containing spaces for the alignment.
            while (nMaxIntegerDigits)
            {
               sIntegerDigits += " ";
               nMaxIntegerDigits--;
            }
            while (nMaxDecimalPlaces)
            {
               sDecimalPlaces += " ";
               nMaxDecimalPlaces--;
            }
      
            // Now the alignment can be made on the 3 minimum values.
            for (nMinMaxIndex = 0; nMinMaxIndex < g_asMin.length; nMinMaxIndex++)
            {
               nIntegerDigits = g_asMin[nMinMaxIndex].indexOf('.');
               if (nIntegerDigits < 0)    // Is there no decimal point?
               {
                  nIntegerDigits = g_asMin[nMinMaxIndex].length;
               }
               nDecimalPlaces = g_asMin[nMinMaxIndex].length - nIntegerDigits;
               g_asMin[nMinMaxIndex] = sIntegerDigits.substr(nIntegerDigits) +
                                       g_asMin[nMinMaxIndex] +
                                       sDecimalPlaces.substr(nDecimalPlaces);
            }
      
            // Do the alignment also for the maximum values whereby appending
            // spaces after the decimal places is not needed for the maximum
            // values as there is nothing else output after each maximum value.
            sIntegerDigits = "";
            nMaxIntegerDigits = 0;
      
            for (nMinMaxIndex = 0; nMinMaxIndex < g_asMax.length; nMinMaxIndex++)
            {
               nIntegerDigits = g_asMax[nMinMaxIndex].indexOf('.');
               if (nIntegerDigits < 0)    // Is there no decimal point?
               {
                  nIntegerDigits = g_asMax[nMinMaxIndex].length;
               }
               if (nIntegerDigits > nMaxIntegerDigits)
               {
                  nMaxIntegerDigits = nIntegerDigits;
               }
            }
      
            // Build strings just containing spaces for the alignment.
            while (nMaxIntegerDigits)
            {
               sIntegerDigits += " ";
               nMaxIntegerDigits--;
            }
      
            // Now the decimal point or right alignment
            // can be made on the 3 maximum values.
            for (nMinMaxIndex = 0; nMinMaxIndex < g_asMax.length; nMinMaxIndex++)
            {
               nIntegerDigits = g_asMax[nMinMaxIndex].indexOf('.');
               if (nIntegerDigits < 0)    // Is there no decimal point?
               {
                  nIntegerDigits = g_asMax[nMinMaxIndex].length;
               }
               g_asMax[nMinMaxIndex] = sIntegerDigits.substr(nIntegerDigits) +
                                       g_asMax[nMinMaxIndex];
            }
      
            // The next two lines should be commented before making modifications
            // on script code below and testing the modified script. The error
            // messages of the JavaScript interpreter written to output window
            // are suppressed by disabling showing status and are not visible
            // anymore after clearing output window.
            UltraEdit.outputWindow.showStatus = false;
            UltraEdit.outputWindow.clear();
      
            // Output the 6 values into output window which is opened automatically.
            UltraEdit.outputWindow.write("Min. / max. X = " + g_asMin[0] + " / " + g_asMax[0]);
            UltraEdit.outputWindow.write("Min. / max. Y = " + g_asMin[1] + " / " + g_asMax[1]);
            UltraEdit.outputWindow.write("Min. / max. Z = " + g_asMin[2] + " / " + g_asMax[2]);
            if (UltraEdit.outputWindow.visible == false)
            {
               UltraEdit.outputWindow.showWindow(true);
            }
         }
      }
      
      The output is written well formatted and aligned to the output window which is automatically opened if currently not visible.

      The output for the input example is:

      Code: Select all

      Min. / max. X = -68.356 / -63.356
      Min. / max. Y =  23.75  /  28.25
      Min. / max. Z =  -1.0   /  10.0
      
      Tirekicker wrote:I've used other editors that have something like ...
      I would be really interested in which editors have such a feature for G-Codes built-in. Are those editors specific designed for G-Codes or are they general text editors like UltraEdit.

      Which editors have such a minimum and maximum determining feature built-in without using a third-party plugin or script?
      Best regards from an UC/UE/UES for Windows user from Austria