forked from MatterHackers/MatterSlice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLayerDataStorage.cs
480 lines (402 loc) · 17.6 KB
/
LayerDataStorage.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
/*
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 ClipperLib;
using System.Collections.Generic;
namespace MatterHackers.MatterSlice
{
using System;
using System.IO;
using System.Linq;
using Polygon = List<IntPoint>;
using Polygons = List<List<IntPoint>>;
public class LayerDataStorage
{
public List<ExtruderLayers> Extruders = new List<ExtruderLayers>();
public Point3 modelSize, modelMin, modelMax;
public Polygons raftOutline = new Polygons();
public Polygons skirt = new Polygons();
public NewSupport support = null;
public IntPoint wipePoint;
public List<Polygons> wipeShield = new List<Polygons>();
public Polygons wipeTower = new Polygons();
public void CreateIslandData()
{
for (int extruderIndex = 0; extruderIndex < Extruders.Count; extruderIndex++)
{
Extruders[extruderIndex].CreateIslandData();
}
}
public void DumpLayerparts(string filename)
{
LayerDataStorage storage = this;
StreamWriter streamToWriteTo = new StreamWriter(filename);
streamToWriteTo.Write("<!DOCTYPE html><html><body>");
Point3 modelSize = storage.modelSize;
Point3 modelMin = storage.modelMin;
for (int extruderIndex = 0; extruderIndex < storage.Extruders.Count; extruderIndex++)
{
for (int layerNr = 0; layerNr < storage.Extruders[extruderIndex].Layers.Count; layerNr++)
{
streamToWriteTo.Write("<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" style=\"width: 500px; height:500px\">\n");
SliceLayer layer = storage.Extruders[extruderIndex].Layers[layerNr];
for (int i = 0; i < layer.Islands.Count; i++)
{
LayerIsland part = layer.Islands[i];
for (int j = 0; j < part.IslandOutline.Count; j++)
{
streamToWriteTo.Write("<polygon points=\"");
for (int k = 0; k < part.IslandOutline[j].Count; k++)
streamToWriteTo.Write("{0},{1} ".FormatWith((float)(part.IslandOutline[j][k].X - modelMin.x) / modelSize.x * 500, (float)(part.IslandOutline[j][k].Y - modelMin.y) / modelSize.y * 500));
if (j == 0)
streamToWriteTo.Write("\" style=\"fill:gray; stroke:black;stroke-width:1\" />\n");
else
streamToWriteTo.Write("\" style=\"fill:red; stroke:black;stroke-width:1\" />\n");
}
}
streamToWriteTo.Write("</svg>\n");
}
}
streamToWriteTo.Write("</body></html>");
streamToWriteTo.Close();
}
public void GenerateRaftOutlines(int extraDistanceAroundPart_um, ConfigSettings config)
{
LayerDataStorage storage = this;
for (int extruderIndex = 0; extruderIndex < storage.Extruders.Count; extruderIndex++)
{
if (config.ContinuousSpiralOuterPerimeter && extruderIndex > 0)
{
continue;
}
if (storage.Extruders[extruderIndex].Layers.Count < 1)
{
continue;
}
SliceLayer layer = storage.Extruders[extruderIndex].Layers[0];
// let's find the first layer that has something in it for the raft rather than a zero layer
if (layer.Islands.Count == 0 && storage.Extruders[extruderIndex].Layers.Count > 2)
{
layer = storage.Extruders[extruderIndex].Layers[1];
}
for (int partIndex = 0; partIndex < layer.Islands.Count; partIndex++)
{
if (config.ContinuousSpiralOuterPerimeter && partIndex > 0)
{
continue;
}
storage.raftOutline = storage.raftOutline.CreateUnion(layer.Islands[partIndex].IslandOutline.Offset(extraDistanceAroundPart_um));
}
}
storage.raftOutline = storage.raftOutline.CreateUnion(storage.wipeTower.Offset(extraDistanceAroundPart_um));
if (storage.support != null)
{
storage.raftOutline = storage.raftOutline.CreateUnion(storage.support.GetBedOutlines().Offset(extraDistanceAroundPart_um));
}
}
public void GenerateSkirt(int distance, int extrusionWidth_um, int numberOfLoops, int brimCount, int minLength, int initialLayerHeight, ConfigSettings config)
{
LayerDataStorage storage = this;
bool externalOnly = (distance > 0);
List<Polygons> skirtLoops = new List<Polygons>();
Polygons skirtPolygons = GetSkirtBounds(config, storage, externalOnly, distance, extrusionWidth_um, brimCount);
// Find convex hull for the skirt outline
Polygons convexHull = new Polygons(new[] { skirtPolygons.CreateConvexHull() });
// Create skirt loops from the ConvexHull
for (int skirtLoop = 0; skirtLoop < numberOfLoops; skirtLoop++)
{
int offsetDistance = distance + extrusionWidth_um * skirtLoop + extrusionWidth_um / 2;
storage.skirt.AddAll(convexHull.Offset(offsetDistance));
int length = (int)storage.skirt.PolygonLength();
if (skirtLoop + 1 >= numberOfLoops && length > 0 && length < minLength)
{
// add more loops for as long as we have not extruded enough length
numberOfLoops++;
}
}
}
private static Polygons GetSkirtBounds(ConfigSettings config, LayerDataStorage storage, bool externalOnly, int distance, int extrusionWidth_um, int brimCount)
{
bool hasWipeTower = storage.wipeTower.PolygonLength() > 0;
Polygons skirtPolygons = hasWipeTower ? new Polygons(storage.wipeTower) : new Polygons();
if (config.EnableRaft)
{
skirtPolygons = skirtPolygons.CreateUnion(storage.raftOutline);
}
else
{
Polygons allOutlines = new Polygons();
// Loop over every extruder
for (int extrudeIndex = 0; extrudeIndex < storage.Extruders.Count; extrudeIndex++)
{
// Only process the first extruder on spiral vase or
// skip extruders that have empty layers
if (config.ContinuousSpiralOuterPerimeter)
{
SliceLayer layer0 = storage.Extruders[extrudeIndex].Layers[0];
allOutlines.AddAll(layer0.Islands[0]?.IslandOutline);
break;
}
// Add the layers outline to allOutlines
SliceLayer layer = storage.Extruders[extrudeIndex].Layers[0];
allOutlines.AddAll(layer.AllOutlines);
}
if (brimCount > 0)
{
Polygons brimLoops = new Polygons();
// Loop over the requested brimCount creating and unioning a new perimeter for each island
for (int brimIndex = 0; brimIndex < brimCount; brimIndex++)
{
int offsetDistance = extrusionWidth_um * brimIndex + extrusionWidth_um / 2;
Polygons unionedIslandOutlines = new Polygons();
// Grow each island by the current brim distance
foreach (var island in allOutlines)
{
var polygons = new Polygons();
polygons.Add(island);
// Union the island brims
unionedIslandOutlines = unionedIslandOutlines.CreateUnion(polygons.Offset(offsetDistance));
}
// Extend the polygons to account for the brim (ensures convex hull takes this data into account)
brimLoops.AddAll(unionedIslandOutlines);
}
// TODO: This is a quick hack, reuse the skirt data to stuff in the brim. Good enough from proof of concept
storage.skirt.AddAll(brimLoops);
skirtPolygons = skirtPolygons.CreateUnion(brimLoops);
}
else
{
skirtPolygons = skirtPolygons.CreateUnion(allOutlines);
}
if (storage.support != null)
{
skirtPolygons = skirtPolygons.CreateUnion(storage.support.GetBedOutlines());
}
}
return skirtPolygons;
}
public void WriteRaftGCodeIfRequired(GCodeExport gcode, ConfigSettings config)
{
LayerDataStorage storage = this;
if (config.ShouldGenerateRaft())
{
GCodePathConfig raftBaseConfig = new GCodePathConfig("raftBaseConfig");
raftBaseConfig.SetData(config.FirstLayerSpeed, config.RaftBaseExtrusionWidth_um, "SUPPORT");
GCodePathConfig raftMiddleConfig = new GCodePathConfig("raftMiddleConfig");
raftMiddleConfig.SetData(config.RaftPrintSpeed, config.RaftInterfaceExtrusionWidth_um, "SUPPORT");
GCodePathConfig raftSurfaceConfig = new GCodePathConfig("raftMiddleConfig");
raftSurfaceConfig.SetData((config.RaftSurfacePrintSpeed > 0) ? config.RaftSurfacePrintSpeed : config.RaftPrintSpeed, config.RaftSurfaceExtrusionWidth_um, "SUPPORT");
// create the raft base
{
gcode.WriteComment("LAYER:-3");
gcode.WriteComment("RAFT BASE");
GCodePlanner gcodeLayer = new GCodePlanner(gcode, config.TravelSpeed, config.MinimumTravelToCauseRetraction_um, config.PerimeterStartEndOverlapRatio);
if (config.RaftExtruder >= 0)
{
// if we have a specified raft extruder use it
gcodeLayer.SetExtruder(config.RaftExtruder);
}
else if (config.SupportExtruder >= 0)
{
// else preserve the old behavior of using the support extruder if set.
gcodeLayer.SetExtruder(config.SupportExtruder);
}
gcode.setZ(config.RaftBaseThickness_um);
gcode.SetExtrusion(config.RaftBaseThickness_um, config.FilamentDiameter_um, config.ExtrusionMultiplier);
// write the skirt around the raft
gcodeLayer.QueuePolygonsByOptimizer(storage.skirt, raftBaseConfig);
List<Polygons> raftIslands = storage.raftOutline.ProcessIntoSeparatIslands();
foreach (var raftIsland in raftIslands)
{
// write the outline of the raft
gcodeLayer.QueuePolygonsByOptimizer(raftIsland, raftBaseConfig);
Polygons raftLines = new Polygons();
Infill.GenerateLinePaths(raftIsland, raftLines, config.RaftBaseLineSpacing_um, config.InfillExtendIntoPerimeter_um, 0);
// write the inside of the raft base
gcodeLayer.QueuePolygonsByOptimizer(raftLines, raftBaseConfig);
if (config.RetractWhenChangingIslands)
{
gcodeLayer.ForceRetract();
}
}
gcodeLayer.WriteQueuedGCode(config.RaftBaseThickness_um);
}
if (config.RaftFanSpeedPercent > 0)
{
gcode.WriteFanCommand(config.RaftFanSpeedPercent);
}
// raft middle layers
{
gcode.WriteComment("LAYER:-2");
gcode.WriteComment("RAFT MIDDLE");
GCodePlanner gcodeLayer = new GCodePlanner(gcode, config.TravelSpeed, config.MinimumTravelToCauseRetraction_um, config.PerimeterStartEndOverlapRatio);
gcode.setZ(config.RaftBaseThickness_um + config.RaftInterfaceThicknes_um);
gcode.SetExtrusion(config.RaftInterfaceThicknes_um, config.FilamentDiameter_um, config.ExtrusionMultiplier);
Polygons raftLines = new Polygons();
Infill.GenerateLinePaths(storage.raftOutline, raftLines, config.RaftInterfaceLineSpacing_um, config.InfillExtendIntoPerimeter_um, 45);
gcodeLayer.QueuePolygonsByOptimizer(raftLines, raftMiddleConfig);
gcodeLayer.WriteQueuedGCode(config.RaftInterfaceThicknes_um);
}
for (int raftSurfaceIndex = 1; raftSurfaceIndex <= config.RaftSurfaceLayers; raftSurfaceIndex++)
{
gcode.WriteComment("LAYER:-1");
gcode.WriteComment("RAFT SURFACE");
GCodePlanner gcodeLayer = new GCodePlanner(gcode, config.TravelSpeed, config.MinimumTravelToCauseRetraction_um, config.PerimeterStartEndOverlapRatio);
gcode.setZ(config.RaftBaseThickness_um + config.RaftInterfaceThicknes_um + config.RaftSurfaceThickness_um * raftSurfaceIndex);
gcode.SetExtrusion(config.RaftSurfaceThickness_um, config.FilamentDiameter_um, config.ExtrusionMultiplier);
Polygons raftLines = new Polygons();
if (raftSurfaceIndex == config.RaftSurfaceLayers)
{
// make sure the top layer of the raft is 90 degrees offset to the first layer of the part so that it has minimum contact points.
Infill.GenerateLinePaths(storage.raftOutline, raftLines, config.RaftSurfaceLineSpacing_um, config.InfillExtendIntoPerimeter_um, config.InfillStartingAngle + 90);
}
else
{
Infill.GenerateLinePaths(storage.raftOutline, raftLines, config.RaftSurfaceLineSpacing_um, config.InfillExtendIntoPerimeter_um, 90 * raftSurfaceIndex);
}
gcodeLayer.QueuePolygonsByOptimizer(raftLines, raftSurfaceConfig);
gcodeLayer.WriteQueuedGCode(config.RaftInterfaceThicknes_um);
}
}
}
public void CreateWipeTower(int totalLayers, ConfigSettings config)
{
if (config.WipeTowerSize_um < 1
|| LastLayerWithChange(config) == -1)
{
return;
}
extrudersThatHaveBeenPrimed = new bool[config.MaxExtruderCount()];
Polygon wipeTowerShape = new Polygon();
wipeTowerShape.Add(new IntPoint(this.modelMin.x - 3000, this.modelMax.y + 3000));
wipeTowerShape.Add(new IntPoint(this.modelMin.x - 3000, this.modelMax.y + 3000 + config.WipeTowerSize_um));
wipeTowerShape.Add(new IntPoint(this.modelMin.x - 3000 - config.WipeTowerSize_um, this.modelMax.y + 3000 + config.WipeTowerSize_um));
wipeTowerShape.Add(new IntPoint(this.modelMin.x - 3000 - config.WipeTowerSize_um, this.modelMax.y + 3000));
this.wipeTower.Add(wipeTowerShape);
this.wipePoint = new IntPoint(this.modelMin.x - 3000 - config.WipeTowerSize_um / 2, this.modelMax.y + 3000 + config.WipeTowerSize_um / 2);
}
bool[] extrudersThatHaveBeenPrimed = null;
public void GenerateWipeTowerInfill(int extruderIndex, Polygons partOutline, Polygons outputfillPolygons, long extrusionWidth_um, ConfigSettings config)
{
Polygons outlineForExtruder = partOutline.Offset(-extrusionWidth_um * extruderIndex);
long insetPerLoop = extrusionWidth_um * config.MaxExtruderCount();
while (outlineForExtruder.Count > 0)
{
for (int polygonIndex = 0; polygonIndex < outlineForExtruder.Count; polygonIndex++)
{
Polygon newInset = outlineForExtruder[polygonIndex];
newInset.Add(newInset[0]); // add in the last move so it is a solid polygon
outputfillPolygons.Add(newInset);
}
outlineForExtruder = outlineForExtruder.Offset(-insetPerLoop);
}
outputfillPolygons.Reverse();
}
public void PrimeOnWipeTower(int extruderIndex, int layerIndex, GCodePlanner gcodeLayer, GCodePathConfig fillConfig, ConfigSettings config)
{
if (config.WipeTowerSize_um < 1
|| extrudersThatHaveBeenPrimed == null
|| layerIndex > LastLayerWithChange(config) + 1)
{
return;
}
//If we changed extruder, print the wipe/prime tower for this nozzle;
Polygons fillPolygons = new Polygons();
GenerateWipeTowerInfill(extruderIndex, this.wipeTower, fillPolygons, fillConfig.lineWidth_um, config);
gcodeLayer.QueuePolygons(fillPolygons, fillConfig);
extrudersThatHaveBeenPrimed[extruderIndex] = true;
}
int LastLayerWithChange(ConfigSettings config)
{
int numLayers = Extruders[0].Layers.Count;
int firstExtruderWithData = -1;
for (int checkLayer = numLayers - 1; checkLayer >= 0; checkLayer--)
{
for (int extruderToCheck = 0; extruderToCheck < config.MaxExtruderCount(); extruderToCheck++)
{
if((extruderToCheck < Extruders.Count && Extruders[extruderToCheck].Layers[checkLayer].AllOutlines.Count > 0)
|| (config.SupportExtruder == extruderToCheck && support != null && support.HasNormalSupport(checkLayer) )
|| (config.SupportInterfaceExtruder == extruderToCheck && support != null && support.HasInterfaceSupport(checkLayer)) )
{
if(firstExtruderWithData == -1)
{
firstExtruderWithData = extruderToCheck;
}
else
{
if(firstExtruderWithData != extruderToCheck)
{
return checkLayer;
}
}
}
}
}
return -1;
}
public void EnsureWipeTowerIsSolid(int layerIndex, GCodePlanner gcodeLayer, GCodePathConfig fillConfig, ConfigSettings config)
{
if(layerIndex >= LastLayerWithChange(config)
|| extrudersThatHaveBeenPrimed == null)
{
return;
}
// print all of the extruder loops that have not already been printed
for (int extruderIndex = 0; extruderIndex < config.MaxExtruderCount(); extruderIndex++)
{
if (!extrudersThatHaveBeenPrimed[extruderIndex])
{
// write the loops for this extruder, but don't change to it. We are just filling the prime tower.
PrimeOnWipeTower(extruderIndex, 0, gcodeLayer, fillConfig, config);
}
// clear the history of printer extruders for the next layer
extrudersThatHaveBeenPrimed[extruderIndex] = false;
}
}
public void CreateWipeShield(int totalLayers, ConfigSettings config)
{
if (config.WipeShieldDistanceFromShapes_um <= 0)
{
return;
}
for (int layerIndex = 0; layerIndex < totalLayers; layerIndex++)
{
Polygons wipeShield = new Polygons();
for (int extruderIndex = 0; extruderIndex < this.Extruders.Count; extruderIndex++)
{
for (int islandIndex = 0; islandIndex < this.Extruders[extruderIndex].Layers[layerIndex].Islands.Count; islandIndex++)
{
wipeShield = wipeShield.CreateUnion(this.Extruders[extruderIndex].Layers[layerIndex].Islands[islandIndex].IslandOutline.Offset(config.WipeShieldDistanceFromShapes_um));
}
}
this.wipeShield.Add(wipeShield);
}
for (int layerIndex = 0; layerIndex < totalLayers; layerIndex++)
{
this.wipeShield[layerIndex] = this.wipeShield[layerIndex].Offset(-1000).Offset(1000);
}
int offsetAngle = (int)Math.Tan(60.0 * Math.PI / 180) * config.LayerThickness_um;//Allow for a 60deg angle in the wipeShield.
for (int layerIndex = 1; layerIndex < totalLayers; layerIndex++)
{
this.wipeShield[layerIndex] = this.wipeShield[layerIndex].CreateUnion(this.wipeShield[layerIndex - 1].Offset(-offsetAngle));
}
for (int layerIndex = totalLayers - 1; layerIndex > 0; layerIndex--)
{
this.wipeShield[layerIndex - 1] = this.wipeShield[layerIndex - 1].CreateUnion(this.wipeShield[layerIndex].Offset(-offsetAngle));
}
}
}
}