Hi
I want to write a program to convert CNC GCode files.
My problem is this;
From the following code; N230G01X78.54Y34.75f300.
I need to extract the letter X and the numbers 78.54, change X to B, perform a calculation and then pass back to the origional string.
To get this; N230G01B90.0Y34.75f300.
While the code I wrote below works fine, the value of X may not be followed by a Y value (which I am using to mark the end of the X value) or any other value and the numbers are not always the same length. eg.
N230G01X674.887f300.
N230G01X4.3
So, is there a way to extract the numbers after X or search for either 'Y' or 'F' or 'D' or 'H' or ''
I have tried to use sets without sucsess.
I'm a raw beginer using Turbo Delphi and any help will be very much appreciated. Thanks.
I want to write a program to convert CNC GCode files.
My problem is this;
From the following code; N230G01X78.54Y34.75f300.
I need to extract the letter X and the numbers 78.54, change X to B, perform a calculation and then pass back to the origional string.
To get this; N230G01B90.0Y34.75f300.
While the code I wrote below works fine, the value of X may not be followed by a Y value (which I am using to mark the end of the X value) or any other value and the numbers are not always the same length. eg.
N230G01X674.887f300.
N230G01X4.3
So, is there a way to extract the numbers after X or search for either 'Y' or 'F' or 'D' or 'H' or ''
I have tried to use sets without sucsess.
I'm a raw beginer using Turbo Delphi and any help will be very much appreciated. Thanks.
Code:
procedure TForm2.Button1Click(Sender: TObject);
Var
source, target, Numbers, Newaxis, NewBlock : string;
Xposition, EndPosition, Length : integer;
Angle, Radius, FormatedAngle : Double;
begin
edit1.Text:= ansiuppercase(edit1.Text); //Change text in edit1 to uppercase.
source := edit1.Text; //Variable 'Source' gets edit1.text
//Variable 'Radius' gets edit2.text converted from a string to floating point number.
Radius := strtofloat(edit2.text);
//look for position of 'X' in 'Source'. Stores position as Integer in variable Xposition.
Xposition:= ansipos('X',source);
//*************************************************************************
//My problem is here
//finds 'Y' position.
//Would like to find 'Y' or 'F' or ''.
//Alternativly, find 'Y' and number after.
Endposition:= ansipos('Y', source);
//*************************************************************************
//Subtract X position from Y position to find length of target string.
Length := (Endposition - Xposition);
//Extract target string.
Target:= ansimidstr(source, Xposition, Length);
Numbers := ansimidstr(target,2,8); //Extract the numbers
//Angle = numbers*180/Pi/Radius
//Calculate Angle.
Angle := strtofloat(Numbers)*180/Pi/(Radius);
//Format angle to 3 decimal places.
FormatedAngle := strtofloat(Format('%.3f', [Angle]));
//Check to see if formated angle conains a decimal point.
if AnsiContainsStr(FloatToStr(FormatedAngle), '.')
then
//If yes, then add 'B' before angle
NewAxis := Concat('B' + FloatToStr(FormatedAngle))
else
//If No, then add 'B' before angle and a decmal point and '0' after.
NewAxis := Concat('B' + FloatToStr(FormatedAngle) + '.0');
//Replace target from old block with Newaxis.
NewBlock := AnsiReplaceStr(source,(Target), (NewAxis));
if
Xposition=0
then
showmessage('X not found')
else
//Display variables at diferent stages in labels.
Label1.Caption:=('Target ='+' ' +(target));
Label2.Caption:=('Extract Numbers ='+' ' +(numbers));
Label3.Caption:=('Value of Pi ='+' '+FloatToStr(Pi));
Label4.Caption:=('4th axis Angle ='+' ' +floattostr(angle));
Label6.Caption:=('Formated Angle ='+' ' +floattostr(FormatedAngle));
Label7.Caption:=('New Axis ='+' '+(NewAxis));
Label8.Caption:=(NewBlock);
end;