-
Notifications
You must be signed in to change notification settings - Fork 113
/
GCodeFile.cs
487 lines (393 loc) · 11.1 KB
/
GCodeFile.cs
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
using HelixToolkit.Wpf;
using OpenCNCPilot.GCode.GCodeCommands;
using OpenCNCPilot.Properties;
using OpenCNCPilot.Util;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Windows.Media.Media3D;
namespace OpenCNCPilot.GCode
{
class GCodeFile
{
public ReadOnlyCollection<Command> Toolpath;
public string FileName = string.Empty;
public Vector3 Min { get; private set; }
public Vector3 Max { get; private set; }
public Vector3 Size { get; private set; }
public Vector3 MinFeed { get; private set; }
public Vector3 MaxFeed { get; private set; }
public Vector3 SizeFeed { get; private set; }
public bool ContainsMotion { get; private set; } = false;
public double TravelDistance { get; private set; } = 0;
public TimeSpan TotalTime { get; private set; } = TimeSpan.Zero;
public List<string> Warnings = new List<string>();
private GCodeFile(List<Command> toolpath)
{
for (int i = 0; i < toolpath.Count; i++)
{
Command c = toolpath[i];
if (c is Motion)
{
Motion m = (Motion)c;
if (m.Start == m.End)
{
Warnings.Add($"ignoring zero-length move from line number {m.LineNumber}");
toolpath.RemoveAt(i--);
}
}
}
Toolpath = new ReadOnlyCollection<Command>(toolpath);
Vector3 min = Vector3.MaxValue, max = Vector3.MinValue;
Vector3 minfeed = Vector3.MaxValue, maxfeed = Vector3.MinValue;
foreach (Command c in Toolpath)
{
if (c is Line)
{
Line l = (Line)c;
// StartValid should be sufficient, keep PositionValid just to be sure
if (!l.StartValid || l.PositionValid.Any(isValid => !isValid))
continue;
}
if (c is Motion)
{
ContainsMotion = true;
Motion m = (Motion)c;
TravelDistance += m.Length;
if (m is Line && !((Line)m).Rapid && ((Line)m).Feed > 0.0)
TotalTime += TimeSpan.FromMinutes(m.Length / m.Feed);
min = Vector3.ElementwiseMin(min, m.End);
max = Vector3.ElementwiseMax(max, m.End);
min = Vector3.ElementwiseMin(min, m.Start);
max = Vector3.ElementwiseMax(max, m.Start);
if (m is Line && (m as Line).Rapid)
continue;
minfeed = Vector3.ElementwiseMin(minfeed, m.End);
maxfeed = Vector3.ElementwiseMax(maxfeed, m.End);
minfeed = Vector3.ElementwiseMin(minfeed, m.Start);
maxfeed = Vector3.ElementwiseMax(maxfeed, m.Start);
}
}
Max = max;
Min = min;
Vector3 size = Max - Min;
for (int i = 0; i < 3; i++)
{
if (size[i] < 0)
size[i] = 0;
}
Size = size;
MaxFeed = maxfeed;
MinFeed = minfeed;
Vector3 sizefeed = MaxFeed - MinFeed;
for (int i = 0; i < 3; i++)
{
if (sizefeed[i] < 0)
sizefeed[i] = 0;
}
SizeFeed = sizefeed;
}
public static GCodeFile Load(string path)
{
GCodeParser.Reset();
GCodeParser.ParseFile(path);
GCodeFile gcodeFile = new GCodeFile(GCodeParser.Commands) { FileName = path.Substring(path.LastIndexOf('\\') + 1) };
gcodeFile.Warnings.InsertRange(0, GCodeParser.Warnings);
return gcodeFile;
}
public static GCodeFile FromList(IEnumerable<string> file)
{
GCodeParser.Reset();
GCodeParser.Parse(file);
GCodeFile gcodeFile = new GCodeFile(GCodeParser.Commands) { FileName = "output.nc" };
gcodeFile.Warnings.InsertRange(0, GCodeParser.Warnings);
return gcodeFile;
}
public static GCodeFile Empty
{
get
{
return new GCodeFile(new List<Command>());
}
}
public void Save(string path)
{
File.WriteAllLines(path, GetGCode());
}
public void GetModel(LinesVisual3D line, LinesVisual3D rapid, LinesVisual3D arc)
{
var sw = System.Diagnostics.Stopwatch.StartNew();
Point3DCollection linePoints = new Point3DCollection();
Point3DCollection rapidPoints = new Point3DCollection();
Point3DCollection arcPoints = new Point3DCollection();
foreach (Command c in Toolpath)
{
var l = c as Line;
if (l != null)
{
if (!l.StartValid)
continue;
if (l.Rapid)
{
rapidPoints.Add(l.Start.ToPoint3D());
rapidPoints.Add(l.End.ToPoint3D());
}
else
{
linePoints.Add(l.Start.ToPoint3D());
linePoints.Add(l.End.ToPoint3D());
}
continue;
}
var a = c as Arc;
if (a != null)
{
foreach (Motion sub in a.Split(Settings.Default.ViewportArcSplit))
{
arcPoints.Add(sub.Start.ToPoint3D());
arcPoints.Add(sub.End.ToPoint3D());
}
}
}
line.Points = linePoints;
rapid.Points = rapidPoints;
arc.Points = arcPoints;
sw.Stop();
Console.WriteLine("Generating the toolpath model took {0} ms", sw.ElapsedMilliseconds);
}
public List<string> GetGCode()
{
List<string> GCode = new List<string>(Toolpath.Count + 1) { "G90 G91.1 G21 G17" };
NumberFormatInfo nfi = new NumberFormatInfo();
nfi.NumberDecimalSeparator = "."; //prevent problems with international versions of windows (eg Germany would write 25.4 as 25,4 which is not compatible with standard GCode)
ParserState State = new ParserState();
var xyz = "XYZ";
foreach (Command c in Toolpath)
{
if (c is Motion)
{
Motion m = c as Motion;
if (m.Feed != State.Feed)
{
GCode.Add(string.Format(nfi, "F{0:0.###}", m.Feed));
State.Feed = m.Feed;
}
}
if (c is Line)
{
Line l = c as Line;
string code = l.Rapid ? "G0" : "G1";
for (int i = 0; i < 3; i++)
{
if (!l.PositionValid[i])
continue;
if (!l.StartValid || State.Position[i] != l.End[i])
code += string.Format(nfi, " {0}{1:0.###}", xyz[i], l.End[i]);
}
GCode.Add(code);
State.Position = l.End;
continue;
}
if (c is Arc)
{
Arc a = c as Arc;
if (State.Plane != a.Plane)
{
switch (a.Plane)
{
case ArcPlane.XY:
GCode.Add("G17");
break;
case ArcPlane.YZ:
GCode.Add("G19");
break;
case ArcPlane.ZX:
GCode.Add("G18");
break;
}
State.Plane = a.Plane;
}
string code = a.Direction == ArcDirection.CW ? "G2" : "G3";
if (State.Position.X != a.End.X)
code += string.Format(nfi, " X{0:0.###}", a.End.X);
if (State.Position.Y != a.End.Y)
code += string.Format(nfi, " Y{0:0.###}", a.End.Y);
if (State.Position.Z != a.End.Z)
code += string.Format(nfi, " Z{0:0.###}", a.End.Z);
Vector3 Center = new Vector3(a.U, a.V, 0).RollComponents((int)a.Plane) - State.Position;
if (Center.X != 0 && a.Plane != ArcPlane.YZ)
code += string.Format(nfi, " I{0:0.###}", Center.X);
if (Center.Y != 0 && a.Plane != ArcPlane.ZX)
code += string.Format(nfi, " J{0:0.###}", Center.Y);
if (Center.Z != 0 && a.Plane != ArcPlane.XY)
code += string.Format(nfi, " K{0:0.###}", Center.Z);
GCode.Add(code);
State.Position = a.End;
continue;
}
if (c is MCode)
{
int code = ((MCode)c).Code;
if (!Settings.Default.GCodeIncludeMEnd)
{
if (code == 2 || code == 30)
continue;
}
GCode.Add($"M{code}");
continue;
}
if (c is Spindle)
{
if (!Settings.Default.GCodeIncludeSpindle)
continue;
GCode.Add(string.Format(nfi, "S{0}", ((Spindle)c).Speed));
continue;
}
if (c is Dwell)
{
if (!Settings.Default.GCodeIncludeDwell)
continue;
GCode.Add(string.Format(nfi, "G4 P{0}", ((Dwell)c).Seconds));
continue;
}
}
return GCode;
}
public GCodeFile Split(double length)
{
List<Command> newFile = new List<Command>();
foreach (Command c in Toolpath)
{
if (c is Motion)
{
newFile.AddRange(((Motion)c).Split(length));
}
else
{
newFile.Add(c);
}
}
return new GCodeFile(newFile);
}
public GCodeFile ArcsToLines(double length)
{
List<Command> newFile = new List<Command>();
foreach (Command c in Toolpath)
{
if (c is Arc)
{
foreach (Arc segment in ((Arc)c).Split(length).Cast<Arc>())
{
Line l = new Line();
l.Start = segment.Start;
l.End = segment.End;
l.Feed = segment.Feed;
l.Rapid = false;
l.PositionValid = new bool[] { true, true, true };
l.StartValid = true;
newFile.Add(l);
}
}
else
{
newFile.Add(c);
}
}
return new GCodeFile(newFile);
}
public GCodeFile ApplyHeightMap(HeightMap map)
{
double segmentLength = Math.Min(map.GridX, map.GridY);
List<Command> newToolPath = new List<Command>();
foreach (Command command in Toolpath)
{
if (command is Motion)
{
Motion m = (Motion)command;
if (m is Arc)
{
Arc a = m as Arc;
if (a.Plane != ArcPlane.XY)
throw new Exception("GCode contains arcs in YZ or XZ plane (G18/19), can't apply height map. Use 'Arcs to Lines' if you really need this.");
}
if (m is Line)
{
Line l = (Line)m;
// do not split up or modify any lines that are rapid or not fully defined
if (!l.StartValid || l.PositionValid.Any(isValid => !isValid) || l.Rapid)
{
newToolPath.Add(l);
continue;
}
}
foreach (Motion subMotion in m.Split(segmentLength))
{
subMotion.Start.Z += map.InterpolateZ(subMotion.Start.X, subMotion.Start.Y);
subMotion.End.Z += map.InterpolateZ(subMotion.End.X, subMotion.End.Y);
newToolPath.Add(subMotion);
}
}
else
{
newToolPath.Add(command);
continue;
}
}
return new GCodeFile(newToolPath);
}
public GCodeFile RotateCW()
{
List<Command> newFile = new List<Command>();
foreach (Command oldCommand in Toolpath)
{
if (oldCommand is Motion)
{
Motion oldMotion = (Motion)oldCommand;
Motion newMotion;
if (oldCommand is Arc)
{
Arc oldArc = (Arc)oldMotion;
Arc newArc = new Arc();
// would be possible, but I'm too lazy to implement this properly
if (oldArc.Plane != ArcPlane.XY)
throw new Exception("GCode contains arcs in YZ or XZ plane (G18/19), can't rotate gcode. Use 'Arcs to Lines' if you really need this.");
newArc.Direction = oldArc.Direction;
newArc.Plane = oldArc.Plane;
newArc.U = oldArc.V;
newArc.V = -oldArc.U;
newMotion = newArc;
}
else if (oldCommand is Line)
{
Line oldLine = (Line)oldMotion;
Line newLine = new Line();
newLine.Rapid = oldLine.Rapid;
newLine.PositionValid[0] = oldLine.PositionValid[1];
newLine.PositionValid[1] = oldLine.PositionValid[0];
newLine.PositionValid[2] = oldLine.PositionValid[2];
newLine.StartValid = oldLine.StartValid;
newMotion = newLine;
}
else
throw new Exception("this shouldn't happen, please contact the autor on GitHub");
newMotion.Start = oldMotion.Start;
newMotion.End = oldMotion.End;
newMotion.Start.X = oldMotion.Start.Y;
newMotion.Start.Y = -oldMotion.Start.X;
newMotion.End.X = oldMotion.End.Y;
newMotion.End.Y = -oldMotion.End.X;
newMotion.Feed = oldMotion.Feed;
newFile.Add(newMotion);
}
else
{
newFile.Add(oldCommand);
}
}
return new GCodeFile(newFile);
}
}
}