"NC code" text file parsing with C#
Hi falks!
I need to parse a text file including an NC code which is a simple CNC tool motion determinator. In the file, each line shows the instantaneous positon of the tool tip. For example, the first line is X0 Y0 Z0 F200 and second line X10 Y10 Z10 this means that the tool is at (0,0,0) and will go to (10,10,10) with a speed of 200 m/min. This motion will take some time. Depending on the machine tool specificaion (user will enter), according to sampling time entered by user (0.1 second for example) succesive points will be determined between (0,0,0) and (10,10,10) one point in each 0.1 second. I have to parse the text file and give a list as an output showing the position of the tool at eah multiples of the sampling time. A sample NC code is:
G1X0Y0Z0F200
X10.Y10.Z10.
X21.Y16.Z8.
Y10.Z5.
G1 means linear motion. I will use only linear motion. Blanks between the characters are not important. However, to specify X position of tool as 10 it should be written as "X10." or "X10.0". X10 means 10*sampling time (e.g 10*0,1=1). Any caharacter which is not writen in a line means that it is the same as the last written form in earlier lineas. My output will be like
X Y Z
0 0 0 0
0,1 1 1 1
0,2 2 2 2
0,3 . . .
. . . .
. . . .
. . . .
I need help!
Martin DoddPosted Aug 7, 2009, 7:33 AM
First read file into string, call it myNC, then
string myNC = "G1X0Y0Z0F200 X10.Y10.Z10. X21.Y16.Z8. Y10.Z5.";
string output = string.Empty;
output = processNC(myNC);
The function processNC takes the form (I have added comment for your information):
public string processNC(string myNC)
{
string[] myComp = myNC.Split(" ");
System.Text.StringBuilder sb = new System.Text.StringBuilder();
string previousX = string.Empty;
string previousY = string.Empty;
string previousZ = string.Empty;
string previousF = string.Empty;
string output = string.Empty;
foreach (string comp in myComp) {
string temp = comp;
//Establish Starting point
if (comp.IndexOf("G1") != -1) {
sb.Append("X Y Z ");
comp = comp.Substring(comp.IndexOf("X"));
}
//Get first X posn
if (comp.IndexOf("Y") > 0) {
temp = comp;
comp = (double.Parse(temp.Substring(1, temp.IndexOf("Y") - 1)) * 0.1).ToString();
previousX = comp.ToString();
sb.Append(previousX + " ");
comp = temp.Substring(temp.IndexOf("Y"));
}
else {
sb.Append(previousX + " ");
}
//Get first Y posn
if (comp.IndexOf("Z") > 0) {
temp = comp;
comp = (double.Parse(temp.Substring(1, temp.IndexOf("Z") - 1)) * 0.1).ToString();
previousY = comp.ToString();
sb.Append(previousY + " ");
comp = temp.Substring(temp.IndexOf("Z"));
}
else {
sb.Append(previousY + " ");
}
//Get first Z posn
if (comp.IndexOf("F") > 0) {
temp = comp;
comp = (double.Parse(temp.Substring(1, temp.IndexOf("F") - 1)) * 0.1).ToString();
previousZ = comp.ToString();
sb.Append(previousZ + " ");
comp = temp.Substring(temp.IndexOf("F"));
//What is left of the string after getting z posn from F must be the value proceed F eg. 200 in the example
temp = comp;
comp = (double.Parse(temp.Substring(1)) * 0.1).ToString();
previousF = comp;
sb.Append(previousF + ",");
}
else {
//If Z value exists but no F value need to identify Z
if (comp.Length > 2) {
temp = comp;
comp = (double.Parse(temp.Substring(1)) * 0.1).ToString();
sb.Append(comp + " ");
previousZ = comp;
}
else {
//No Z value, use the previousZ value from line before
sb.Append(previousZ + " ");
}
}
//Case where no Z value is specified
if (comp == previousZ) {
sb.Append(previousF + ",");
}
//X Y Z 0 0 0 0 0,1 1 1 1 0,2 2 2 2 0,3
}
output = sb.ToString();
output = output.Substring(0, output.Length - 1);
return output;
}
The out put takes the form:
//OUTPUT from function with example provided
//X Y Z 0 0 0 20,1 1 1 20,2.1 1.6 0.8 20,2.1 1 0.5 20
Please contact me if you have any question. I hope this answer your question. Regards,
Martin