A bit of an updated working (work in progress) code snippet for reading the currently active gcode line
(I took another look at dezsoe's Regular Expression and found a regular expression viewer to understand the info I was given)
http://regexstorm.net/testerThis code gets the currently active line number
Reads the line with StreamReader
splits the string up into a 2 column array [number of rows is dependant upon the gcode split]
And stores the Alpha code [G,A,B,C,X,Y,Z,I,J,K,S,F,etc] in column [i,0] and the split gcode [G1,G2,G3,X123.234,Y-123.456 etc] in column [i,1]
The message box bits are just to display the stored values as the mocro progresses so that you can see what values are obtained and where they are located
You may ask why do you need the "alpha code" well the plan is to use a switch statement to assign the gcode snippets into a 26 position array, that way they can always be recovered and tested for (they could also be put into #Vars..... similar to Macro B.... although depends what you want to use them for...
- Code: Select all
// get gcode line
string strGcode = AS3.Getfield(866); // read the current [active] gcode line number
string strCurrGcodeFilename = exec.Getgcodefilename(); // Get path + filename of current loaded gcode file
MessageBox.Show(strGcode + "\n" + strCurrGcodeFilename);
int myLineNumber = Convert.ToInt32(strGcode);
string line = null;
using( System.IO.Stream fileStream = System.IO.File.Open(strCurrGcodeFilename, System.IO.FileMode.Open) )
{
using (System.IO.StreamReader reader = new System.IO.StreamReader(fileStream) )
{
for( int i = 0; i < myLineNumber; ++i )
{
line = reader.ReadLine();
}
MessageBox.Show(line);
}
}
string pattern1;
pattern1 = @"([abcdefghijklpqrsxyz]\s*)";
System.Text.RegularExpressions.MatchCollection matches1 = System.Text.RegularExpressions.Regex.Matches(line, pattern1, System.Text.RegularExpressions.RegexOptions.IgnoreCase);
string[,] gCode = new string[matches1.Count,2];
for (int i = 0; i < matches1.Count; i++)
{
gCode[i,0] = matches1[i].Value;
}
int length1 = gCode.GetLength(0);
MessageBox.Show("Array Rows " + Convert.ToString(length1));
for( int i = 0; i < length1; ++i )
{
MessageBox.Show(gCode[i,0]);
}
string pattern;
pattern = @"([abcdefhijklpqrsxyz]\s*[+-]?([0-9]+([\.,][0-9]*)?|[\.,][0-9]+)\s*)|([g]\s*\d+([\.][0-9])*\s*)";
System.Text.RegularExpressions.MatchCollection matches = System.Text.RegularExpressions.Regex.Matches(line, pattern, System.Text.RegularExpressions.RegexOptions.IgnoreCase);
for (int i = 0; i < matches.Count; i++)
{
gCode[i,1] = matches[i].Value;
}
for( int i = 0; i < length1; ++i )
{
MessageBox.Show(gCode[i,1]);
}
for( int i = 0; i < length1; ++i )
{
MessageBox.Show("Row " + i + " " + gCode[i,0] + " " + gCode [i,1]);
}
A suggestion is to try this on a number of gcode files / formats as some do not repeat the modal commands like G1, G2 and G3, or the feedrate (or spindle speed) hence you'll just recover the X,Y,Z,I,J,K,A,B,C accordingly and you may need to use other uccnc fields to establish what the current modal commands actually are, current stored feed rate (or spindle speed) should you want to change and reset these later. As discussed earlier you'll also need to check out if the machine is in absolute or incremental mode
{ actualdistancemode } before moving it back to another location .
Useful fields will therefore be:
Field # 877 Activemodal
Field # 867 Setfeedrate
Field # 869 Setspindlespeed [if required .... not for plasma obviously .... unless used for THC voltage / set cut ampage via RS485 interface maybe??]