forked from MatterHackers/MatterSlice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GCodePlanner.cs
712 lines (617 loc) · 21 KB
/
GCodePlanner.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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
/*
This file is part of MatterSlice. A commandline utility for
generating 3D printing GCode.
Copyright (C) 2013 David Braam
Copyright (c) 2014, Lars Brubaker
MatterSlice is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using ClipperLib;
using System.Collections.Generic;
namespace MatterHackers.MatterSlice
{
using Polygon = List<IntPoint>;
using Polygons = List<List<IntPoint>>;
//The GCodePlanner class stores multiple moves that are planned.
// It facilitates the avoidCrossingPerimeters to keep the head inside the print.
// It also keeps track of the print time estimate for this planning so speed adjustments can be made for the minimum-layer-time.
public class GCodePlanner
{
private bool alwaysRetract;
private int currentExtruderIndex;
private double extraTime;
private int extrudeSpeedFactor;
private bool forceRetraction;
private GCodeExport gcodeExport = new GCodeExport();
public long CurrentZ { get { return gcodeExport.CurrentZ; } }
public IntPoint LastPosition
{
get; private set;
}
private AvoidCrossingPerimeters outerPerimetersToAvoidCrossing;
private List<GCodePath> paths = new List<GCodePath>();
private int retractionMinimumDistance_um;
private double totalPrintTime;
private GCodePathConfig travelConfig = new GCodePathConfig();
private int travelSpeedFactor;
public GCodePlanner(GCodeExport gcode, int travelSpeed, int retractionMinimumDistance_um)
{
this.gcodeExport = gcode;
travelConfig = new GCodePathConfig(travelSpeed, 0, "travel");
LastPosition = gcode.GetPositionXY();
outerPerimetersToAvoidCrossing = null;
extrudeSpeedFactor = 100;
travelSpeedFactor = 100;
extraTime = 0.0;
totalPrintTime = 0.0;
forceRetraction = false;
alwaysRetract = false;
currentExtruderIndex = gcode.GetExtruderIndex();
this.retractionMinimumDistance_um = retractionMinimumDistance_um;
}
public void ForceMinimumLayerTime(double minTime, int minimumPrintingSpeed)
{
Point3 lastPosition = gcodeExport.GetPosition();
double travelTime = 0.0;
double extrudeTime = 0.0;
for (int n = 0; n < paths.Count; n++)
{
GCodePath path = paths[n];
for (int pointIndex = 0; pointIndex < path.points.Count; pointIndex++)
{
Point3 currentPosition = path.points[pointIndex];
double thisTime = (lastPosition - currentPosition).LengthMm() / (double)(path.config.speed);
if (path.config.lineWidth_um != 0)
{
extrudeTime += thisTime;
}
else
{
travelTime += thisTime;
}
lastPosition = currentPosition;
}
}
double totalTime = extrudeTime + travelTime;
if (totalTime < minTime && extrudeTime > 0.0)
{
double minExtrudeTime = minTime - travelTime;
if (minExtrudeTime < 1)
{
minExtrudeTime = 1;
}
double factor = extrudeTime / minExtrudeTime;
for (int n = 0; n < paths.Count; n++)
{
GCodePath path = paths[n];
if (path.config.lineWidth_um == 0)
{
continue;
}
int speed = (int)(path.config.speed * factor);
if (speed < minimumPrintingSpeed)
{
factor = (double)(minimumPrintingSpeed) / (double)(path.config.speed);
}
}
//Only slow down with the minimum time if that will be slower then a factor already set. First layer slowdown also sets the speed factor.
if (factor * 100 < getExtrudeSpeedFactor())
{
SetExtrudeSpeedFactor((int)(factor * 100));
}
else
{
factor = getExtrudeSpeedFactor() / 100.0;
}
if (minTime - (extrudeTime / factor) - travelTime > 0.1)
{
//TODO: Use up this extra time (circle around the print?)
this.extraTime = minTime - (extrudeTime / factor) - travelTime;
}
this.totalPrintTime = (extrudeTime / factor) + travelTime;
}
else
{
this.totalPrintTime = totalTime;
}
}
public void ForceRetract()
{
forceRetraction = true;
}
public int getExtruder()
{
return currentExtruderIndex;
}
public int getExtrudeSpeedFactor()
{
return this.extrudeSpeedFactor;
}
[Flags]
enum Altered { remove = 1, merged = 2 };
public List<List<Point3>> GetPathsWithOverlapsRemoved(List<Point3> perimeter, int overlapMergeAmount_um)
{
// make a copy that has every point duplicatiod (so that we have them as segments).
List<Point3> polySegments = new List<Point3>(perimeter.Count * 2);
for (int i = 0; i < perimeter.Count - 1; i++)
{
Point3 point = perimeter[i];
Point3 nextPoint = perimeter[i + 1];
polySegments.Add(point);
polySegments.Add(nextPoint);
}
Altered[] markedAltered = new Altered[polySegments.Count/2];
int segmentCount = polySegments.Count / 2;
// now walk every segment and check if there is another segment that is similar enough to merge them together
for (int firstSegmentIndex = 0; firstSegmentIndex < segmentCount; firstSegmentIndex++)
{
int firstPointIndex = firstSegmentIndex * 2;
for (int checkSegmentIndex = firstSegmentIndex + 1; checkSegmentIndex < segmentCount; checkSegmentIndex++)
{
int checkPointIndex = checkSegmentIndex * 2;
// The first point of start and the last point of check (the path will be coming back on itself).
long startDelta = (polySegments[firstPointIndex] - polySegments[checkPointIndex + 1]).Length();
// if the segmets are similar enough
if (startDelta < overlapMergeAmount_um)
{
// The last point of start and the first point of check (the path will be coming back on itself).
long endDelta = (polySegments[firstPointIndex + 1] - polySegments[checkPointIndex]).Length();
if (endDelta < overlapMergeAmount_um)
{
// move the first segments points to the average of the merge positions
polySegments[firstPointIndex] = (polySegments[firstPointIndex] + polySegments[checkPointIndex + 1]) / 2; // the start
polySegments[firstPointIndex + 1] = (polySegments[firstPointIndex + 1] + polySegments[checkPointIndex]) / 2; // the end
markedAltered[firstSegmentIndex] = Altered.merged;
// mark this segment for removal
markedAltered[checkSegmentIndex] = Altered.remove;
// We only expect to find one match for each segment, so move on to the next segment
break;
}
}
}
}
// Check for perimeter edeges that need to be removed that are the u turns of sectons that go back on themselves.
// __________
// |__________| -> |--------| the 2 vertical sections should be removed
for (int segmentIndex = 0; segmentIndex < segmentCount; segmentIndex++)
{
int prevSegmentIndex = (int)((uint)(segmentIndex - 1) % (uint)segmentCount);
int nextSegmentIndex = (segmentIndex + 1) % segmentCount;
if ((markedAltered[nextSegmentIndex] == Altered.merged && markedAltered[prevSegmentIndex] == Altered.remove)
|| (markedAltered[nextSegmentIndex] == Altered.remove && markedAltered[prevSegmentIndex] == Altered.merged))
{
markedAltered[segmentIndex] = Altered.remove;
}
}
// remove the marked segments
for (int segmentIndex = segmentCount - 1; segmentIndex >= 0; segmentIndex--)
{
int pointIndex = segmentIndex * 2;
if (markedAltered[segmentIndex] == Altered.remove)
{
polySegments.RemoveRange(pointIndex, 2);
}
}
// go through the polySegmets and create a new polygon for every connected set of segmets
List<List<Point3>> separatedPolygons = new List<List<Point3>>();
List<Point3> currentPolygon = new List<Point3>();
separatedPolygons.Add(currentPolygon);
// put in the first point
for (int segmentIndex = 0; segmentIndex < polySegments.Count; segmentIndex += 2)
{
// add the start point
currentPolygon.Add(polySegments[segmentIndex]);
// if the next segment is not connected to this one
if (segmentIndex < polySegments.Count - 2
&& polySegments[segmentIndex + 1] != polySegments[segmentIndex + 2])
{
// add the end point
currentPolygon.Add(polySegments[segmentIndex + 1]);
// create a new polygon
currentPolygon = new List<Point3>();
separatedPolygons.Add(currentPolygon);
}
}
// add the end point
currentPolygon.Add(polySegments[polySegments.Count - 1]);
return separatedPolygons;
}
public int getTravelSpeedFactor()
{
return this.travelSpeedFactor;
}
public void MoveInsideTheOuterPerimeter(int distance)
{
if (outerPerimetersToAvoidCrossing == null || outerPerimetersToAvoidCrossing.PointIsInsideBoundary(LastPosition))
{
return;
}
IntPoint p = LastPosition;
if (outerPerimetersToAvoidCrossing.MovePointInsideBoundary(ref p, distance))
{
//Move inside again, so we move out of tight 90deg corners
outerPerimetersToAvoidCrossing.MovePointInsideBoundary(ref p, distance);
if (outerPerimetersToAvoidCrossing.PointIsInsideBoundary(p))
{
QueueTravel(p);
//Make sure the that any retraction happens after this move, not before it by starting a new move path.
ForceNewPathStart();
}
}
}
public void SetAlwaysRetract(bool alwaysRetract)
{
this.alwaysRetract = alwaysRetract;
}
public bool SetExtruder(int extruder)
{
if (extruder == currentExtruderIndex)
{
return false;
}
currentExtruderIndex = extruder;
return true;
}
public void SetExtrudeSpeedFactor(int speedFactor)
{
if (speedFactor < 1) speedFactor = 1;
this.extrudeSpeedFactor = speedFactor;
}
public void SetOuterPerimetersToAvoidCrossing(Polygons polygons)
{
if (polygons != null)
{
outerPerimetersToAvoidCrossing = new AvoidCrossingPerimeters(polygons);
}
else
{
outerPerimetersToAvoidCrossing = null;
}
}
public void SetTravelSpeedFactor(int speedFactor)
{
if (speedFactor < 1) speedFactor = 1;
this.travelSpeedFactor = speedFactor;
}
public void QueueExtrusionMove(IntPoint destination, GCodePathConfig config)
{
GetLatestPathWithConfig(config).points.Add(new Point3(destination, CurrentZ));
LastPosition = destination;
}
public void WriteQueuedGCode(int layerThickness)
{
GCodePathConfig lastConfig = null;
int extruderIndex = gcodeExport.GetExtruderIndex();
for (int pathIndex = 0; pathIndex < paths.Count; pathIndex++)
{
GCodePath path = paths[pathIndex];
if (extruderIndex != path.extruderIndex)
{
extruderIndex = path.extruderIndex;
gcodeExport.SwitchExtruder(extruderIndex);
}
else if (path.Retract)
{
gcodeExport.WriteRetraction();
}
if (path.config != travelConfig && lastConfig != path.config)
{
gcodeExport.WriteComment("TYPE:{0}".FormatWith(path.config.gcodeComment));
lastConfig = path.config;
}
double speed = path.config.speed;
if (path.config.lineWidth_um != 0)
{
// Only apply the extrudeSpeedFactor to extrusion moves
speed = speed * extrudeSpeedFactor / 100;
}
else
{
speed = speed * travelSpeedFactor / 100;
}
if (path.points.Count == 1
&& path.config != travelConfig
&& (gcodeExport.GetPositionXY() - path.points[0].XYPoint).ShorterThen(path.config.lineWidth_um * 2))
{
//Check for lots of small moves and combine them into one large line
Point3 nextPosition = path.points[0];
int i = pathIndex + 1;
while (i < paths.Count && paths[i].points.Count == 1 && (nextPosition - paths[i].points[0]).ShorterThen(path.config.lineWidth_um * 2))
{
nextPosition = paths[i].points[0];
i++;
}
if (paths[i - 1].config == travelConfig)
{
i--;
}
if (i > pathIndex + 2)
{
nextPosition = gcodeExport.GetPosition();
for (int x = pathIndex; x < i - 1; x += 2)
{
long oldLen = (nextPosition - paths[x].points[0]).Length();
Point3 newPoint = (paths[x].points[0] + paths[x + 1].points[0]) / 2;
long newLen = (gcodeExport.GetPosition() - newPoint).Length();
if (newLen > 0)
{
gcodeExport.WriteMove(newPoint, speed, (int)(path.config.lineWidth_um * oldLen / newLen));
}
nextPosition = paths[x + 1].points[0];
}
gcodeExport.WriteMove(paths[i - 1].points[0], speed, path.config.lineWidth_um);
pathIndex = i - 1;
continue;
}
}
bool spiralize = path.config.spiralize;
if (spiralize)
{
//Check if we are the last spiralize path in the list, if not, do not spiralize.
for (int m = pathIndex + 1; m < paths.Count; m++)
{
if (paths[m].config.spiralize)
{
spiralize = false;
}
}
}
if (spiralize) // if we are still in spiralize mode
{
//If we need to spiralize then raise the head slowly by 1 layer as this path progresses.
double totalLength = 0;
long z = gcodeExport.GetPositionZ();
IntPoint currentPosition = gcodeExport.GetPositionXY();
for (int pointIndex = 0; pointIndex < path.points.Count; pointIndex++)
{
IntPoint nextPosition = path.points[pointIndex].XYPoint;
totalLength += (currentPosition - nextPosition).LengthMm();
currentPosition = nextPosition;
}
double length = 0.0;
currentPosition = gcodeExport.GetPositionXY();
for (int i = 0; i < path.points.Count; i++)
{
IntPoint nextPosition = path.points[i].XYPoint;
length += (currentPosition - nextPosition).LengthMm();
currentPosition = nextPosition;
Point3 nextExtrusion = path.points[i];
nextExtrusion.z = (int)(z + layerThickness * length / totalLength + .5);
gcodeExport.WriteMove(nextExtrusion, speed, path.config.lineWidth_um);
}
}
else
{
// This is test code to remove double drawn small perimeter lines.
if (RemoveDoubleDrawPerimeterLines(path, speed))
{
return;
}
else
{
//TrimPerimeterIfNeeded(path);
for (int i = 0; i < path.points.Count; i++)
{
gcodeExport.WriteMove(path.points[i], speed, path.config.lineWidth_um);
}
}
}
}
gcodeExport.UpdateTotalPrintTime();
}
private bool RemoveDoubleDrawPerimeterLines(GCodePath path, double speed)
{
return false;
if (path.config.lineWidth_um > 0
&& path.points.Count > 2 // If the count is not greater than 2 there is no way it can ovelap itself.
&& gcodeExport.GetPosition() == path.points[path.points.Count - 1])
{
List<List<Point3>> pathsWithOverlapsRemoved = GetPathsWithOverlapsRemoved(path.points, path.config.lineWidth_um / 2);
if (pathsWithOverlapsRemoved.Count > 0)
{
for (int polygonIndex = 0; polygonIndex < pathsWithOverlapsRemoved.Count; polygonIndex++)
{
int startIndex = 0;
List<Point3> polygon = pathsWithOverlapsRemoved[polygonIndex];
if (polygonIndex > 0)
{
gcodeExport.WriteMove(polygon[0], travelConfig.speed, 0);
startIndex = 1; // We skip the first point in the next extrusion, because we just moved to it.
}
for (int pointIndex = startIndex; pointIndex < polygon.Count; pointIndex++)
{
gcodeExport.WriteMove(polygon[pointIndex], speed, path.config.lineWidth_um);
}
}
}
}
}
public void QueuePolygon(Polygon polygon, int startIndex, GCodePathConfig config)
{
IntPoint currentPosition = polygon[startIndex];
if (!config.spiralize
&& (LastPosition.X != currentPosition.X
|| LastPosition.Y != currentPosition.Y))
{
QueueTravel(currentPosition);
}
if (config.closedLoop)
{
for (int positionIndex = 1; positionIndex < polygon.Count; positionIndex++)
{
IntPoint destination = polygon[(startIndex + positionIndex) % polygon.Count];
QueueExtrusionMove(destination, config);
currentPosition = destination;
}
// We need to actually close the polygon so go back to the first point
if (polygon.Count > 2)
{
QueueExtrusionMove(polygon[startIndex], config);
}
}
else // we are not closed
{
if (startIndex == 0)
{
for (int positionIndex = 1; positionIndex < polygon.Count; positionIndex++)
{
IntPoint destination = polygon[positionIndex];
QueueExtrusionMove(destination, config);
currentPosition = destination;
}
}
else
{
for (int positionIndex = polygon.Count - 1; positionIndex >= 1; positionIndex--)
{
IntPoint destination = polygon[(startIndex + positionIndex) % polygon.Count];
QueueExtrusionMove(destination, config);
currentPosition = destination;
}
}
}
}
public void QueuePolygonsByOptimizer(Polygons polygons, GCodePathConfig config)
{
IslandOrderOptimizer orderOptimizer = new IslandOrderOptimizer(LastPosition);
orderOptimizer.AddPolygons(polygons);
orderOptimizer.Optimize(config);
for (int i = 0; i < orderOptimizer.bestIslandOrderIndex.Count; i++)
{
int polygonIndex = orderOptimizer.bestIslandOrderIndex[i];
QueuePolygon(polygons[polygonIndex], orderOptimizer.startIndexInPolygon[polygonIndex], config);
}
}
public void QueueTravel(IntPoint positionToMoveTo)
{
GCodePath path = GetLatestPathWithConfig(travelConfig);
if (forceRetraction)
{
path.Retract = true;
forceRetraction = false;
}
else if (outerPerimetersToAvoidCrossing != null)
{
List<IntPoint> pointList = new List<IntPoint>();
if (outerPerimetersToAvoidCrossing.CreatePathInsideBoundary(LastPosition, positionToMoveTo, pointList))
{
long lineLength_um = 0;
// we can stay inside so move within the boundary
for (int pointIndex = 0; pointIndex < pointList.Count; pointIndex++)
{
path.points.Add(new Point3(pointList[pointIndex], CurrentZ));
if (pointIndex > 0)
{
lineLength_um += (pointList[pointIndex] - pointList[pointIndex - 1]).Length();
}
}
// If the internal move is very long (20 mm), do a retraction anyway
if (lineLength_um > retractionMinimumDistance_um)
{
path.Retract = true;
}
}
else
{
if ((LastPosition - positionToMoveTo).LongerThen(retractionMinimumDistance_um))
{
// We are moving relatively far and are going to cross a boundary so do a retraction.
path.Retract = true;
}
}
}
else if (alwaysRetract)
{
if ((LastPosition - positionToMoveTo).LongerThen(retractionMinimumDistance_um))
{
path.Retract = true;
}
}
path.points.Add(new Point3(positionToMoveTo, CurrentZ));
LastPosition = positionToMoveTo;
}
private static void TrimPerimeterIfNeeded(GCodePath path)
{
if (path.config.gcodeComment == "WALL-OUTER" || path.config.gcodeComment == "WALL-INNER")
{
long currentDistance = 0;
long targetDistance = (long)(path.config.lineWidth_um * .90);
if (path.points.Count > 1)
{
for (int pointIndex = path.points.Count - 1; pointIndex > 0; pointIndex--)
{
// Calculate distance between 2 points
currentDistance = (path.points[pointIndex] - path.points[pointIndex - 1]).Length();
// If distance exceeds clip distance:
// - Sets the new last path point
if (currentDistance > targetDistance)
{
long newDistance = currentDistance - targetDistance;
Point3 dir = path.points[pointIndex] - path.points[pointIndex - 1] * newDistance / currentDistance;
Point3 clippedEndpoint = path.points[pointIndex - 1] + dir;
path.points[pointIndex] = clippedEndpoint;
break;
}
else if (currentDistance == targetDistance)
{
// Pops off last point because it is at the limit distance
path.points.RemoveAt(path.points.Count - 1);
break;
}
else
{
// Pops last point and reduces distance remaining to target
targetDistance = targetDistance - currentDistance;
path.points.RemoveAt(path.points.Count - 1);
}
}
}
}
}
private void ForceNewPathStart()
{
if (paths.Count > 0)
{
paths[paths.Count - 1].done = true;
}
}
private GCodePath GetLatestPathWithConfig(GCodePathConfig config)
{
if (paths.Count > 0
&& paths[paths.Count - 1].config == config
&& !paths[paths.Count - 1].done)
{
return paths[paths.Count - 1];
}
paths.Add(new GCodePath());
GCodePath ret = paths[paths.Count - 1];
ret.Retract = false;
ret.config = config;
ret.extruderIndex = currentExtruderIndex;
ret.done = false;
return ret;
}
internal class GCodePath
{
internal GCodePathConfig config;
internal bool done;
internal int extruderIndex;
internal List<Point3> points = new List<Point3>();
internal bool Retract { get; set; }
//Path is finished, no more moves should be added, and a new path should be started instead of any appending done to this one.
}
public Polygon MakeCloseSegmentsMergable(Polygon perimeter, int distanceNeedingAdd)
{
throw new System.NotImplementedException();
}
}
}