-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGCodeParser.cs
More file actions
243 lines (210 loc) · 9.73 KB
/
GCodeParser.cs
File metadata and controls
243 lines (210 loc) · 9.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
using System.Linq;
using System.Text.RegularExpressions;
using System.Windows;
namespace NCHops;
public enum MoveType { Rapid, Line, ArcCW, ArcCCW }
public record Move(MoveType Type, double X, double Y,
double Xe = 0, double Ye = 0, double I = 0, double J = 0, int LineNumber = 0, double ToolWidthMm = 0, double FeedRate = 0);
public record SideMove(string Cmd, double X, double Z, int LineNumber = 0);
public record DrillHole(double X, double Y, int LineNumber);
public static class GCodeParser
{
// Matches typical G-code words with signed decimals (e.g. X12, Y-3.5, Z.25).
private static readonly Regex TokenRx = new(
@"[XYZIJKZFASRT]-?(?:\d+(?:\.\d*)?|\.\d+)",
RegexOptions.Compiled | RegexOptions.IgnoreCase);
private static string StripComment(string line)
{
var i = line.IndexOf('(');
return i >= 0 ? line[..i].Trim() : line;
}
private static Dictionary<char, double> ParseWords(string line)
{
var dict = new Dictionary<char, double>();
foreach (Match m in TokenRx.Matches(line))
{
var key = char.ToUpperInvariant(m.Value[0]);
if (double.TryParse(
m.Value[1..],
System.Globalization.NumberStyles.Float,
System.Globalization.CultureInfo.InvariantCulture,
out var value))
{
dict[key] = value;
}
}
return dict;
}
private static readonly Regex RxG0 = new(@"^G0($|[^0-9])", RegexOptions.Compiled);
private static readonly Regex RxG1 = new(@"^G1($|[^0-9])", RegexOptions.Compiled);
private static readonly Regex RxG2 = new(@"^G2($|[^0-9])", RegexOptions.Compiled);
private static readonly Regex RxG3 = new(@"^G3($|[^0-9])", RegexOptions.Compiled);
// Compute effective tool cut-width at a given depth.
// angle = full included tip angle (degrees); 180 = flat end mill.
private static double EffectiveToolWidth(double diam, double angleDeg, double z)
{
if (diam <= 0) return 0;
if (angleDeg >= 180) return diam;
double halfRad = angleDeg / 2.0 * Math.PI / 180.0;
double vWidth = 2.0 * Math.Abs(z) * Math.Tan(halfRad);
return Math.Min(vWidth, diam);
}
private static readonly System.Text.RegularExpressions.Regex RxToolComment = new(
@"\(TOOL\s+D=([\d.]+)\s+ANGLE=([\d.]+)\)",
System.Text.RegularExpressions.RegexOptions.Compiled |
System.Text.RegularExpressions.RegexOptions.IgnoreCase);
public static List<Move> ParseTopView(string text)
{
var moves = new List<Move>();
double x = 0, y = 0, curZ = 0, curF = 0;
double toolD = 0, toolAngle = 180;
bool hasPos = false;
int ln = 0;
var inv = System.Globalization.CultureInfo.InvariantCulture;
foreach (var raw in text.Split('\n'))
{
ln++;
var trimmed = raw.Trim();
// Parse TOOL comment (full line, before stripping)
var tm = RxToolComment.Match(trimmed);
if (tm.Success)
{
toolD = double.Parse(tm.Groups[1].Value, inv);
toolAngle = double.Parse(tm.Groups[2].Value, inv);
}
var line = StripComment(trimmed);
if (string.IsNullOrEmpty(line)) continue;
MoveType? mode = null;
if (line.StartsWith("G00") || RxG0.IsMatch(line)) mode = MoveType.Rapid;
else if (line.StartsWith("G01") || RxG1.IsMatch(line)) mode = MoveType.Line;
else if (line.StartsWith("G02") || RxG2.IsMatch(line)) mode = MoveType.ArcCW;
else if (line.StartsWith("G03") || RxG3.IsMatch(line)) mode = MoveType.ArcCCW;
if (mode == null) continue;
var vals = ParseWords(line);
double prevZ = curZ;
if (vals.TryGetValue('Z', out var nz)) curZ = nz;
if (vals.TryGetValue('F', out var nf)) curF = nf;
// Bei V-Bit mit simultaner X/Y/Z-Bewegung: Start- und End-Z können sehr
// unterschiedlich sein. Breite = Maximum der beiden Enden, damit der breitere
// Schnittbereich korrekt dargestellt wird (z. B. aufsteigender Move von tief→0).
double tw = mode == MoveType.Rapid ? 0 : Math.Max(
EffectiveToolWidth(toolD, toolAngle, prevZ),
EffectiveToolWidth(toolD, toolAngle, curZ));
if (mode is MoveType.Rapid or MoveType.Line)
{
bool hasX = vals.TryGetValue('X', out var nx);
bool hasY = vals.TryGetValue('Y', out var ny);
if (hasX) x = nx;
if (hasY) y = ny;
hasPos = true;
// Z-Only-Moves (z.B. G01 Z-22) haben keine XY-Bewegung →
// in der Topansicht nicht zeichnen
if (hasX || hasY)
moves.Add(new Move(mode.Value, x, y, LineNumber: ln, ToolWidthMm: tw, FeedRate: curF));
}
else if (hasPos)
{
double xs = x, ys = y;
double xe = vals.TryGetValue('X', out var vx) ? vx : xs;
double ye = vals.TryGetValue('Y', out var vy) ? vy : ys;
double I = vals.TryGetValue('I', out var vi) ? vi : 0;
double J = vals.TryGetValue('J', out var vj) ? vj : 0;
moves.Add(new Move(mode.Value, xs, ys, xe, ye, I, J, ln, tw, curF));
x = xe; y = ye;
}
}
return moves;
}
public static List<DrillHole> ParseDrillPoints(string text)
{
var holes = new List<DrillHole>();
double x = 0, y = 0;
int ln = 0;
foreach (var raw in text.Split('\n'))
{
ln++;
var line = StripComment(raw.Trim());
if (string.IsNullOrEmpty(line)) continue;
bool hasX = false, hasY = false, hasZ = false;
double z = 0;
var vals = ParseWords(line);
if (vals.TryGetValue('X', out var vx)) { x = vx; hasX = true; }
if (vals.TryGetValue('Y', out var vy)) { y = vy; hasY = true; }
if (vals.TryGetValue('Z', out var vz)) { z = vz; hasZ = true; }
if (hasZ && !hasX && !hasY && z < 0)
{
if (!holes.Any(h => Math.Abs(h.X - x) < 0.0001 && Math.Abs(h.Y - y) < 0.0001))
holes.Add(new DrillHole(x, y, ln));
}
}
return holes;
}
public static List<SideMove> ParseSideView(string text)
{
var moves = new List<SideMove>();
var inv = System.Globalization.CultureInfo.InvariantCulture;
var style = System.Globalization.NumberStyles.Float;
double x = 0, y = 0, z = 0;
int ln = 0;
foreach (var raw in text.Split('\n'))
{
ln++;
var line = StripComment(raw.Trim());
if (string.IsNullOrEmpty(line)) continue;
string? cmd = null;
double? nx = null, ny = null, nz = null, ni = null, nj = null;
foreach (var part in line.Split(' '))
{
var up = part.ToUpper();
if (up.StartsWith("G00") || up == "G0") cmd = "G0";
else if (up.StartsWith("G01") || up == "G1") cmd = "G1";
else if (up.StartsWith("G02") || up == "G2") cmd = "G2";
else if (up.StartsWith("G03") || up == "G3") cmd = "G3";
else if (up.StartsWith("X") && double.TryParse(up[1..], style, inv, out var vx)) nx = vx;
else if (up.StartsWith("Y") && double.TryParse(up[1..], style, inv, out var vy)) ny = vy;
else if (up.StartsWith("Z") && double.TryParse(up[1..], style, inv, out var vz)) nz = vz;
else if (up.StartsWith("I") && double.TryParse(up[1..], style, inv, out var vi)) ni = vi;
else if (up.StartsWith("J") && double.TryParse(up[1..], style, inv, out var vj)) nj = vj;
}
if (cmd == null) continue;
if (cmd is "G2" or "G3" && (ni.HasValue || nj.HasValue))
{
// Bogen: X-Verlauf approximieren (zeigt z.B. Vollkreise korrekt in der Seitenansicht)
double xe = nx ?? x, ye = ny ?? y, ze = nz ?? z;
double cx = x + (ni ?? 0), cy = y + (nj ?? 0);
double r = Math.Sqrt((x - cx) * (x - cx) + (y - cy) * (y - cy));
if (r > 1e-9)
{
double startA = Math.Atan2(y - cy, x - cx);
double endA = Math.Atan2(ye - cy, xe - cx);
bool cw = cmd == "G2";
bool full = Math.Abs(xe - x) < 1e-6 && Math.Abs(ye - y) < 1e-6;
if (full)
endA = cw ? startA - 2 * Math.PI : startA + 2 * Math.PI;
else
{
if ( cw && endA > startA) endA -= 2 * Math.PI;
if (!cw && endA < startA) endA += 2 * Math.PI;
}
int steps = Math.Max(4, (int)(Math.Abs(endA - startA) / (Math.PI / 18)));
for (int s = 1; s <= steps; s++)
{
double a = startA + (endA - startA) * s / steps;
double xi = cx + r * Math.Cos(a);
double zi = z + (ze - z) * s / steps;
moves.Add(new SideMove("G1", xi, zi, ln));
}
}
x = xe; y = ye; z = ze;
}
else
{
if (nx.HasValue) x = nx.Value;
if (ny.HasValue) y = ny.Value;
if (nz.HasValue) z = nz.Value;
moves.Add(new SideMove(cmd, x, z, ln));
}
}
return moves;
}
}