forked from MatterHackers/MatterSlice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNewSupport.cs
545 lines (448 loc) · 19.4 KB
/
NewSupport.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
/*
This file is part of MatterSlice. A command line utility for
generating 3D printing GCode.
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 System.Collections.Generic;
using MSClipperLib;
// TODO:
// split the part into multiple areas for support pillars
// Create extra upward support for small features (tip of a rotated box)
// sparse write the support layers so they are easier to remove
// make column reduced support work
// DONE:
// check frost morn, should have support under unsupported parts
// make sure all air gapped layers are written after ALL extruder normal layers
// Make the on model material be air gapped
// Offset the output data to account for nozzle diameter (currently they are just the outlines not the extrude positions)
// Make skirt consider support outlines
// Make raft consider support outlines
// Make sure we work correctly with the support extruder set.
// Make from bed only work (no internal support)
// Fix extra extruder material on top of interface layer
namespace MatterHackers.MatterSlice
{
using Polygon = List<IntPoint>;
using Polygons = List<List<IntPoint>>;
public class NewSupport
{
//List<Polygons> pushedUpTopOutlines = new List<Polygons>();
internal List<Polygons> airGappedBottomOutlines = new List<Polygons>();
internal List<Polygons> allPartOutlines = new List<Polygons>();
internal List<Polygons> allPotentialSupportOutlines = new List<Polygons>();
internal List<Polygons> allRequiredSupportOutlines = new List<Polygons>();
internal List<Polygons> easyGrabDistanceOutlines = new List<Polygons>();
internal List<Polygons> insetPartOutlines = new List<Polygons>();
internal List<Polygons> interfaceLayers = new List<Polygons>();
internal List<Polygons> supportOutlines = new List<Polygons>();
private static double cleanDistance_um = 10;
private double grabDistanceMm;
public NewSupport(ConfigSettings config, List<ExtruderLayers> Extruders, double grabDistanceMm)
{
cleanDistance_um = config.ExtrusionWidth_um / 10;
long supportWidth_um = (long)(config.ExtrusionWidth_um * (100 - config.SupportPercent) / 100);
this.grabDistanceMm = grabDistanceMm;
// create starting support outlines
allPartOutlines = CalculateAllPartOutlines(config, Extruders);
insetPartOutlines = CreateInsetPartOutlines(allPartOutlines, config.ExtrusionWidth_um / 2);
allPotentialSupportOutlines = FindAllPotentialSupportOutlines(insetPartOutlines, supportWidth_um);
allRequiredSupportOutlines = RemoveSelfSupportedSections(allPotentialSupportOutlines, supportWidth_um);
if (!config.GenerateInternalSupport)
{
allRequiredSupportOutlines = RemoveSupportFromInternalSpaces(allRequiredSupportOutlines, insetPartOutlines);
}
easyGrabDistanceOutlines = ExpandToEasyGrabDistance(allRequiredSupportOutlines, (int)(grabDistanceMm * 1000));
//pushedUpTopOutlines = PushUpTops(easyGrabDistanceOutlines, numLayers, config);
interfaceLayers = CreateInterfaceLayers(easyGrabDistanceOutlines, config.SupportInterfaceLayers);
interfaceLayers = ClipToXyDistance(interfaceLayers, insetPartOutlines, config);
supportOutlines = AccumulateDownPolygons(config, easyGrabDistanceOutlines, insetPartOutlines);
supportOutlines = ClipToXyDistance(supportOutlines, insetPartOutlines, config);
// remove the interface layers from the normal support layers
supportOutlines = CalculateDifferencePerLayer(supportOutlines, interfaceLayers);
airGappedBottomOutlines = CreateAirGappedBottomLayers(supportOutlines, insetPartOutlines);
// remove the airGappedBottomOutlines layers from the normal support layers
supportOutlines = CalculateDifferencePerLayer(supportOutlines, airGappedBottomOutlines);
}
public Polygons GetBedOutlines()
{
return supportOutlines[0].CreateUnion(interfaceLayers[0]);
}
public Polygons GetRequiredSupportAreas(int layerIndex)
{
layerIndex--;
if (layerIndex < interfaceLayers.Count && layerIndex >= 0)
{
if (interfaceLayers[layerIndex].Count > 0)
{
return interfaceLayers[layerIndex];
}
else if (layerIndex < supportOutlines.Count)
{
return supportOutlines[layerIndex];
}
}
return new Polygons();
}
public bool HasInterfaceSupport(int layerIndex)
{
return interfaceLayers[layerIndex].Count > 0;
}
public bool HasNormalSupport(int layerIndex)
{
return supportOutlines[layerIndex].Count > 0;
}
public void QueueAirGappedBottomLayer(ConfigSettings config, GCodePlanner gcodeLayer, int layerIndex, GCodePathConfig supportNormalConfig)
{
// normal support
Polygons currentAirGappedBottoms = airGappedBottomOutlines[layerIndex];
currentAirGappedBottoms = currentAirGappedBottoms.Offset(-config.ExtrusionWidth_um / 2);
List<Polygons> supportIslands = currentAirGappedBottoms.ProcessIntoSeparatIslands();
foreach (Polygons islandOutline in supportIslands)
{
// force a retract if changing islands
if (config.RetractWhenChangingIslands)
{
gcodeLayer.ForceRetract();
}
Polygons islandInfillLines = new Polygons();
// render a grid of support
if (config.GenerateSupportPerimeter)
{
Polygons outlines = Clipper.CleanPolygons(islandOutline, config.ExtrusionWidth_um / 4);
gcodeLayer.QueuePolygonsByOptimizer(outlines, supportNormalConfig);
}
Polygons infillOutline = islandOutline.Offset(-config.ExtrusionWidth_um / 2);
switch (config.SupportType)
{
case ConfigConstants.SUPPORT_TYPE.GRID:
Infill.GenerateGridInfill(config, infillOutline, islandInfillLines, config.SupportInfillStartingAngle, config.SupportLineSpacing_um);
break;
case ConfigConstants.SUPPORT_TYPE.LINES:
Infill.GenerateLineInfill(config, infillOutline, islandInfillLines, config.SupportInfillStartingAngle, config.SupportLineSpacing_um);
break;
}
gcodeLayer.QueuePolygonsByOptimizer(islandInfillLines, supportNormalConfig);
}
}
public bool QueueInterfaceSupportLayer(ConfigSettings config, GCodePlanner gcodeLayer, int layerIndex, GCodePathConfig supportInterfaceConfig)
{
// interface
bool outputPaths = false;
Polygons currentInterfaceOutlines2 = interfaceLayers[layerIndex].Offset(-config.ExtrusionWidth_um / 2);
if (currentInterfaceOutlines2.Count > 0)
{
List<Polygons> interfaceIslands = currentInterfaceOutlines2.ProcessIntoSeparatIslands();
foreach (Polygons interfaceOutline in interfaceIslands)
{
// force a retract if changing islands
if (config.RetractWhenChangingIslands)
{
gcodeLayer.ForceRetract();
}
// make a border if layer 0
if (layerIndex == 0)
{
Polygons infillOutline = interfaceOutline.Offset(-supportInterfaceConfig.lineWidth_um / 2);
Polygons outlines = Clipper.CleanPolygons(infillOutline, config.ExtrusionWidth_um / 4);
if (gcodeLayer.QueuePolygonsByOptimizer(outlines, supportInterfaceConfig))
{
outputPaths = true;
}
}
Polygons supportLines = new Polygons();
Infill.GenerateLineInfill(config, interfaceOutline, supportLines, config.InfillStartingAngle + 90, config.ExtrusionWidth_um);
if (gcodeLayer.QueuePolygonsByOptimizer(supportLines, supportInterfaceConfig))
{
outputPaths = true;
}
}
}
return outputPaths;
}
public bool QueueNormalSupportLayer(ConfigSettings config, GCodePlanner gcodeLayer, int layerIndex, GCodePathConfig supportNormalConfig)
{
// normal support
Polygons currentSupportOutlines = supportOutlines[layerIndex];
currentSupportOutlines = currentSupportOutlines.Offset(-supportNormalConfig.lineWidth_um / 2);
List<Polygons> supportIslands = currentSupportOutlines.ProcessIntoSeparatIslands();
bool outputPaths = false;
foreach (Polygons islandOutline in supportIslands)
{
// force a retract if changing islands
if (config.RetractWhenChangingIslands)
{
gcodeLayer.ForceRetract();
}
Polygons islandInfillLines = new Polygons();
// render a grid of support
if (config.GenerateSupportPerimeter || layerIndex == 0)
{
Polygons outlines = Clipper.CleanPolygons(islandOutline, config.ExtrusionWidth_um / 4);
if(gcodeLayer.QueuePolygonsByOptimizer(outlines, supportNormalConfig))
{
outputPaths = true;
}
}
Polygons infillOutline = islandOutline.Offset(-supportNormalConfig.lineWidth_um / 2);
if (layerIndex == 0)
{
// on the first layer print this as solid
Infill.GenerateLineInfill(config, infillOutline, islandInfillLines, config.SupportInfillStartingAngle, config.ExtrusionWidth_um);
}
else
{
switch (config.SupportType)
{
case ConfigConstants.SUPPORT_TYPE.GRID:
Infill.GenerateGridInfill(config, infillOutline, islandInfillLines, config.SupportInfillStartingAngle, config.SupportLineSpacing_um);
break;
case ConfigConstants.SUPPORT_TYPE.LINES:
Infill.GenerateLineInfill(config, infillOutline, islandInfillLines, config.SupportInfillStartingAngle, config.SupportLineSpacing_um);
break;
}
}
if (gcodeLayer.QueuePolygonsByOptimizer(islandInfillLines, supportNormalConfig))
{
outputPaths |= true;
}
}
return outputPaths;
}
private static List<Polygons> AccumulateDownPolygons(ConfigSettings config, List<Polygons> inputPolys, List<Polygons> allPartOutlines)
{
int numLayers = inputPolys.Count;
long nozzleSize = config.ExtrusionWidth_um;
long areaToTryAndBe = 20 * 20 * nozzleSize * nozzleSize; // 10 x 10 mm approximately (assuming .5 nozzle)
List<Polygons> allDownOutlines = CreateEmptyPolygons(numLayers);
for (int layerIndex = numLayers - 2; layerIndex >= 0; layerIndex--)
{
Polygons aboveRequiredSupport = inputPolys[layerIndex + 1];
// get all the polygons above us
Polygons accumulatedAbove = allDownOutlines[layerIndex + 1].CreateUnion(aboveRequiredSupport);
// experimental and not working well enough yet
if (config.MinimizeSupportColumns)
{
// reduce the amount of support material used
for (int i = accumulatedAbove.Count - 1; i >= 0; i--)
{
Polygon polygon = accumulatedAbove[i];
double polyArea = polygon.Area();
if (polyArea > areaToTryAndBe)
{
Polygons offsetPolygons = new Polygons() { polygon }.Offset(-config.ExtrusionWidth_um / 2);
accumulatedAbove.RemoveAt(i);
foreach (Polygon polyToAdd in offsetPolygons)
{
accumulatedAbove.Insert(i, polyToAdd);
}
}
else if (polyArea < areaToTryAndBe * .9)
{
Polygons offsetPolygons = new Polygons() { polygon }.Offset(config.ExtrusionWidth_um / 2);
accumulatedAbove.RemoveAt(i);
foreach (Polygon polyToAdd in offsetPolygons)
{
accumulatedAbove.Insert(i, polyToAdd);
}
}
}
}
// add in the support on this level
Polygons curRequiredSupport = inputPolys[layerIndex];
Polygons totalSupportThisLayer = accumulatedAbove.CreateUnion(curRequiredSupport);
// remove the solid polygons on this level
Polygons remainingAbove = totalSupportThisLayer.CreateDifference(allPartOutlines[layerIndex]);
allDownOutlines[layerIndex] = Clipper.CleanPolygons(remainingAbove, cleanDistance_um);
}
return allDownOutlines;
}
private static List<Polygons> CalculateAllPartOutlines(ConfigSettings config, List<ExtruderLayers> Extruders)
{
int numLayers = Extruders[0].Layers.Count;
List<Polygons> allPartOutlines = CreateEmptyPolygons(numLayers);
foreach (var extruder in Extruders)
{
// calculate the combined outlines for everything
for (int layerIndex = numLayers - 1; layerIndex >= 0; layerIndex--)
{
allPartOutlines[layerIndex] = allPartOutlines[layerIndex].CreateUnion(extruder.Layers[layerIndex].AllOutlines);
}
}
return allPartOutlines;
}
private static List<Polygons> CalculateDifferencePerLayer(List<Polygons> inputPolys, List<Polygons> outlinesToRemove)
{
int numLayers = inputPolys.Count;
List<Polygons> diferenceLayers = CreateEmptyPolygons(numLayers);
for (int layerIndex = numLayers - 2; layerIndex >= 0; layerIndex--)
{
Polygons curRequiredSupport = inputPolys[layerIndex];
Polygons totalSupportThisLayer = curRequiredSupport.CreateDifference(outlinesToRemove[layerIndex]);
diferenceLayers[layerIndex] = Clipper.CleanPolygons(totalSupportThisLayer, cleanDistance_um);
}
return diferenceLayers;
}
private static List<Polygons> ClipToXyDistance(List<Polygons> inputPolys, List<Polygons> allPartOutlines, ConfigSettings config)
{
int numLayers = inputPolys.Count;
List<Polygons> clippedToXyOutlines = CreateEmptyPolygons(numLayers);
for (int layerIndex = numLayers - 2; layerIndex >= 0; layerIndex--)
{
Polygons curRequiredSupport = inputPolys[layerIndex];
Polygons expandedlayerPolys = allPartOutlines[layerIndex].Offset(config.SupportXYDistance_um);
Polygons totalSupportThisLayer = curRequiredSupport.CreateDifference(expandedlayerPolys);
clippedToXyOutlines[layerIndex] = Clipper.CleanPolygons(totalSupportThisLayer, cleanDistance_um);
}
return clippedToXyOutlines;
}
private static List<Polygons> CreateAirGappedBottomLayers(List<Polygons> inputPolys, List<Polygons> allPartOutlines)
{
int numLayers = inputPolys.Count;
List<Polygons> airGappedBottoms = CreateEmptyPolygons(numLayers);
for (int layerIndex = 1; layerIndex < numLayers; layerIndex++)
{
Polygons belowOutlines = allPartOutlines[layerIndex - 1];
Polygons curRequiredSupport = inputPolys[layerIndex];
Polygons airGapArea = belowOutlines.CreateIntersection(curRequiredSupport);
airGappedBottoms[layerIndex] = airGapArea;
}
return airGappedBottoms;
}
private static List<Polygons> CreateEmptyPolygons(int numLayers)
{
List<Polygons> polygonsList = new List<Polygons>();
for (int i = 0; i < numLayers; i++)
{
polygonsList.Add(new Polygons());
}
return polygonsList;
}
private static List<Polygons> CreateInsetPartOutlines(List<Polygons> inputPolys, long insetAmount_um)
{
int numLayers = inputPolys.Count;
List<Polygons> allInsetOutlines = CreateEmptyPolygons(numLayers);
// calculate all the non-supported areas
for (int layerIndex = 0; layerIndex < numLayers; layerIndex++)
{
Polygons insetPolygons = Clipper.CleanPolygons(inputPolys[layerIndex].Offset(-insetAmount_um), cleanDistance_um);
List<Polygons> insetIslands = insetPolygons.ProcessIntoSeparatIslands();
foreach (Polygons insetOutline in insetIslands)
{
insetOutline.RemoveSmallAreas(insetAmount_um * 2);
foreach (var islandPart in insetOutline)
{
allInsetOutlines[layerIndex].Add(islandPart);
}
}
}
return allInsetOutlines;
}
private static List<Polygons> CreateInterfaceLayers(List<Polygons> inputPolys, int numInterfaceLayers)
{
int numLayers = inputPolys.Count;
List<Polygons> allInterfaceLayers = CreateEmptyPolygons(numLayers);
if (numInterfaceLayers > 0)
{
for (int layerIndex = 0; layerIndex < numLayers; layerIndex++)
{
Polygons accumulatedAbove = inputPolys[layerIndex].DeepCopy();
for (int addIndex = layerIndex + 1; addIndex < Math.Min(layerIndex + numInterfaceLayers, numLayers - 2); addIndex++)
{
accumulatedAbove = accumulatedAbove.CreateUnion(inputPolys[addIndex]);
accumulatedAbove = Clipper.CleanPolygons(accumulatedAbove, cleanDistance_um);
}
allInterfaceLayers[layerIndex] = accumulatedAbove;
}
}
return allInterfaceLayers;
}
private static List<Polygons> ExpandToEasyGrabDistance(List<Polygons> inputPolys, long grabDistance_um)
{
int numLayers = inputPolys.Count;
List<Polygons> easyGrabDistanceOutlines = CreateEmptyPolygons(numLayers);
for (int layerIndex = numLayers - 1; layerIndex >= 0; layerIndex--)
{
Polygons curLayerPolys = inputPolys[layerIndex];
easyGrabDistanceOutlines[layerIndex] = Clipper.CleanPolygons(curLayerPolys.Offset(grabDistance_um), cleanDistance_um);
}
return easyGrabDistanceOutlines;
}
private static List<Polygons> FindAllPotentialSupportOutlines(List<Polygons> inputPolys, long supportWidth_um)
{
int numLayers = inputPolys.Count;
List<Polygons> allPotentialSupportOutlines = CreateEmptyPolygons(numLayers);
// calculate all the non-supported areas
for (int layerIndex = numLayers - 2; layerIndex >= 0; layerIndex--)
{
Polygons aboveLayerPolys = inputPolys[layerIndex + 1];
Polygons curLayerPolys = inputPolys[layerIndex].Offset(supportWidth_um);
Polygons areasNeedingSupport = aboveLayerPolys.CreateDifference(curLayerPolys);
allPotentialSupportOutlines[layerIndex] = Clipper.CleanPolygons(areasNeedingSupport, cleanDistance_um);
}
return allPotentialSupportOutlines;
}
private static List<Polygons> PushUpTops(List<Polygons> inputPolys, ConfigSettings config)
{
int numLayers = inputPolys.Count;
return inputPolys;
int layersFor2Mm = 2000 / config.LayerThickness_um;
List<Polygons> pushedUpPolys = CreateEmptyPolygons(numLayers);
for (int layerIndex = numLayers - 1; layerIndex >= 0; layerIndex--)
{
for (int layerToAddToIndex = Math.Min(layerIndex + layersFor2Mm, numLayers - 1); layerToAddToIndex >= 0; layerToAddToIndex--)
{
}
Polygons curLayerPolys = inputPolys[layerIndex];
pushedUpPolys[layerIndex] = Clipper.CleanPolygons(curLayerPolys.Offset(config.ExtrusionWidth_um + config.SupportXYDistance_um), cleanDistance_um);
}
return pushedUpPolys;
}
private static List<Polygons> RemoveSelfSupportedSections(List<Polygons> inputPolys, long supportWidth_um)
{
int numLayers = inputPolys.Count;
List<Polygons> allRequiredSupportOutlines = CreateEmptyPolygons(numLayers);
// calculate all the non-supported areas
for (int layerIndex = numLayers - 1; layerIndex > 0; layerIndex--)
{
if (inputPolys[layerIndex - 1].Count > 0)
{
if (inputPolys[layerIndex].Count > 0)
{
Polygons expandedLayerBellow = inputPolys[layerIndex - 1].Offset(supportWidth_um);
allRequiredSupportOutlines[layerIndex] = inputPolys[layerIndex].CreateDifference(expandedLayerBellow);
allRequiredSupportOutlines[layerIndex] = Clipper.CleanPolygons(allRequiredSupportOutlines[layerIndex], cleanDistance_um);
}
}
else
{
allRequiredSupportOutlines[layerIndex] = inputPolys[layerIndex].DeepCopy();
}
}
return allRequiredSupportOutlines;
}
private static List<Polygons> RemoveSupportFromInternalSpaces(List<Polygons> inputPolys, List<Polygons> allPartOutlines)
{
int numLayers = inputPolys.Count;
Polygons accumulatedLayers = new Polygons();
for (int layerIndex = 0; layerIndex < numLayers; layerIndex++)
{
accumulatedLayers = accumulatedLayers.CreateUnion(allPartOutlines[layerIndex]);
accumulatedLayers = Clipper.CleanPolygons(accumulatedLayers, cleanDistance_um);
inputPolys[layerIndex] = inputPolys[layerIndex].CreateDifference(accumulatedLayers);
inputPolys[layerIndex] = Clipper.CleanPolygons(inputPolys[layerIndex], cleanDistance_um);
}
return inputPolys;
}
}
}