D
D
Dmitry Shalimov2016-12-07 15:20:11
Programming
Dmitry Shalimov, 2016-12-07 15:20:11

How to properly process text in C#?

Task:
This text

...
X13.41Y-24.94
G00Z5.00
G00X7.28Y-33.17
G01Z0.00F600
G02X6.45Y-32.08I3.88J3.86
...

You must add the G5 parameter to the beginning of lines starting with coordinates (X or Y), and separate the parameters with a space. What would happen like this.
...
G5 X13.1 Y-24.94
G0 Z5.00
G0 X7.28 Y-33.17
G1 Z0.00
G2X 6.45 Y-32.08 I3.88 J3.86
...

I use:
Regex regex = new Regex(@"\.");
textBox1.Text = regex.Replace(textBox1.Text, ",");
Regex regzero = new Regex(@"F600");
textBox1.Text = regzero.Replace(textBox1.Text, "");

Regex regGX = new Regex(@"^X");
textBox1.Text = regzero.Replace(textBox1.Text, "G5X");
Regex regX = new Regex(@"X");
textBox1.Text = regzero.Replace(textBox1.Text, " X");
Regex regY = new Regex(@"Y");
textBox1.Text = regzero.Replace(textBox1.Text, " Y");
Regex regI = new Regex(@"I");
textBox1.Text = regzero.Replace(textBox1.Text, " I");
Regex regJ = new Regex(@"J");
textBox1.Text = regzero.Replace(textBox1.Text, " J");

Works out only the first 2 expressions...
How can I solve this problem and what is my mistake?
PS
In fact, this is a program block that does not work, ideally, you need to multiply the value of each parameter by 1000 and replace X and Y with the difference with their last values ​​(translating absolute coordinates into relative ones). ΔX=X1-X0, Y respectively. And replace the lines G1 Z0.00 and G0 Z5.00 with M98P0002 and M98P0003. It should come out like this
...
G5 X13100 Y-24940 //assume these are the start coordinates
M98P0002 //replace G00Z5.00 with M98P0002
G0 X-5820 Y-9230 //the position of these coordinates relative to previous
M98P0003 //replace G01Z0.00 with M98P0003
G2X - 830 Y-1090 I3880 J3860 //parameters I and J do not need to be changed, only *1000
...

No comments
...
G5 X13100 Y-24940
M98P0002
G0 X-5820 Y-9230
M98P0003
G2X -830 Y-1090 I3880 J3860
...

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Rsa97, 2016-12-07
@Akui_Aru

Regex regex = new Regex(@"X(-?\d+)\.(\d+)Y(-?\d+)\.(\d+).*?G00Z(-?\d+)\.(\d+).*?G00X(-?\d+)\.(\d+)Y(-?\d+)\.(\d+).*?G01Z(-?\d+)\.(\d+).*?G02X(-?\d+)\.(\d+)Y(-?\d+)\.(\d+)I(-?\d+)\.(\d+)J(-?\d+)\.(\d+)", RegexOptions.Singleline);
textBox1.Text = regex.Replace(textBox1.Text, "G5 X$1,$2 Y$3,$4\nG0 Z$5,$6\nG0 X$7,$8 Y$9,$10\nG1 Z$11,$12\nG2 X$13,$14 Y$15,$16 I$17,$18 J$19,$20");

D
Dmitry Shalimov, 2016-12-07
@Akui_Aru

You must add the G5 parameter to the beginning of lines starting with coordinates (X or Y), and separate the parameters with a space.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question