Click here to Skip to main content
15,906,766 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All, I've had some great help here to get to grips with C#
But i've run into a challenge.. a project that seemed easy has turned out to be quite a challenge..

The Challenge:
Get a Line of G-Code, for instance :

G01 X0.1106 Y-6.9902

Then.. Take the Value after "Y" and do a calculation with that, put that back in the place of the original value, and change the "Y" to "A".

This is for converting G-Code CNC programs from flat to wrapped around a cilinder.
Note!.. there could be other code after the Y-Value.. so that should not be extracted from the original string. (there could be for instance a Z movement in the same line of Code)

So.. how do I take this:

G01 X0.1106 Y-6.9902

and turn it into

G01 X0.1106 A-31.3859

The Y value is in millimeters and the A value is in degrees, i just calculate the circumference of the cilinder that i want to machine, and divide 360 by that number to get a°/mm conversion constant for that particular part
Also the Value must be rounded to no more than 4 decimals, as any more is of no use for my CNC machine, and it only takes up memory

Thanks in advance

What I have tried:

I've tried different things.. but none of them seem to work, or even do anything.. i just got started in C#
Posted
Updated 2-Jun-22 7:25am

First extract the data you need - there are two basic ways to do that:
1) Use string.FindFirst to get the position of the "Y" and then use Substring to extract the data after it.
This is a bit dangerous, because if that prefix "G01" could ever contain a "Y" your code will break.
2) Use a Regex:
(?!.+?\sX[+-]?\d+\.\d+\sY)([+-]?\d+\.\d+)$
That will extract the number at the end only.

Either way, then use double.TryParse to convert that to a numeric value.
Process the value as needed, and use Regex.Replace with the same regex and the new value as a string to replace it.
 
Share this answer
 
Thanks!. that indeed did the trick, Someone else also pointed in the direction of RegEx, and he seems to be able to just write it down like nothing.. to me it still looks like a monkey has been hitting on the keyboard..

private void ConvertBttn_Click(object sender, EventArgs e)
        {
            if (ImportBox.Text != "")
            {
                String[] lines = ImportBox.Text.Split('\n');
                for (int i = 0; i < ImportBox.Lines.Length; i++)
                {
                    String Converted = lines[i];
                    String Mask = "";
                    Mask = "";
                    Mask += "(?<BEGIN>.*?)";
                    Mask += @"(?<AS>Y)";
                    Mask += @"(?<VALUE>-?\d{1,5}(.\d{1,5})?)";
                    Mask += "(?<REST>[^\r\n]*)?.*";

                    Match MatchResult = Regex.Match(lines[i], Mask);
                    if (MatchResult.Success)
                    {
                        String b = MatchResult.Groups["BEGIN"].ToString();
                        String a = MatchResult.Groups["AS"].ToString();
                        String v = MatchResult.Groups["VALUE"].ToString();
                        double Val = double.Parse(v);
                        String r = MatchResult.Groups["REST"].ToString();
                        Converted = b + "A" + Math.Round(Val * -anglepermm, 4).ToString() + r;
                    }
                    ExportBox.Text = ExportBox.Text + Converted + Environment.NewLine;
                }
            }
        }


This is what i came up with.
(I still haven't tested it on the actual CNC machine, but the preview in the simulation looks right)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900