forked from MatterHackers/MatterSlice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExtruderLayers.cs
240 lines (193 loc) · 8.96 KB
/
ExtruderLayers.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
/*
This file is part of MatterSlice. A command line utility for
generating 3D printing GCode.
Copyright (c) 2015, 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 Polygons = List<List<IntPoint>>;
public class ExtruderLayers
{
public List<SliceLayer> Layers = new List<SliceLayer>();
private static readonly double cleanDistance_um = 10;
public void CreateIslandData()
{
for (int layerIndex = 0; layerIndex < Layers.Count; layerIndex++)
{
Layers[layerIndex].CreateIslandData();
}
}
public void GenerateTopAndBottoms(int layerIndex, int extrusionWidth_um, int outerPerimeterWidth_um, int downLayerCount, int upLayerCount)
{
ExtruderLayers extruder = this;
SliceLayer layer = extruder.Layers[layerIndex];
for (int islandIndex = 0; islandIndex < layer.Islands.Count; islandIndex++)
{
LayerIsland island = layer.Islands[islandIndex];
// this is the entire extrusion width to make sure we are outside of the extrusion line
Polygons lastInset = island.InsetToolPaths[island.InsetToolPaths.Count - 1];
Polygons insetWithOffset = lastInset.Offset(-extrusionWidth_um);
Polygons infillOutlines = new Polygons(insetWithOffset);
// calculate the bottom outlines
if (downLayerCount > 0)
{
Polygons bottomOutlines = new Polygons(insetWithOffset);
if (layerIndex - 1 >= 0)
{
bottomOutlines = RemoveIslandsFromPolygons(extruder.Layers[layerIndex - 1].Islands, island.BoundingBox, bottomOutlines);
bottomOutlines.RemoveSmallAreas(extrusionWidth_um);
}
infillOutlines = infillOutlines.CreateDifference(bottomOutlines);
infillOutlines = Clipper.CleanPolygons(infillOutlines, cleanDistance_um);
island.SolidBottomToolPaths = bottomOutlines;
}
// calculate the top outlines
if (upLayerCount > 0)
{
Polygons topOutlines = new Polygons(insetWithOffset);
topOutlines = topOutlines.CreateDifference(island.SolidBottomToolPaths);
topOutlines = Clipper.CleanPolygons(topOutlines, cleanDistance_um);
for (int insetIndex = 0; insetIndex < island.InsetToolPaths.Count - 1; insetIndex++)
{
// Add thin wall filling by taking the area between the insets.
Polygons largerInset = island.InsetToolPaths[insetIndex].Offset(-extrusionWidth_um / 2);
Polygons smallerInset = island.InsetToolPaths[insetIndex + 1].Offset(extrusionWidth_um / 2);
Polygons thinWalls = largerInset.CreateDifference(smallerInset).Offset(-extrusionWidth_um/4);
if (thinWalls.Count > 0)
{
topOutlines.AddAll(thinWalls);
}
}
if (layerIndex + 1 < extruder.Layers.Count)
{
// Remove the top layer that is above this one to get only the data that is a top layer on this layer.
topOutlines = RemoveIslandsFromPolygons(extruder.Layers[layerIndex + 1].Islands, island.BoundingBox, topOutlines);
}
topOutlines.RemoveSmallAreas(extrusionWidth_um);
infillOutlines = infillOutlines.CreateDifference(topOutlines);
infillOutlines = Clipper.CleanPolygons(infillOutlines, cleanDistance_um);
island.SolidTopToolPaths = topOutlines;
}
// calculate the solid infill outlines
if (upLayerCount > 1 || downLayerCount > 1)
{
Polygons solidInfillOutlines = new Polygons(insetWithOffset);
solidInfillOutlines = solidInfillOutlines.CreateDifference(island.SolidBottomToolPaths);
solidInfillOutlines = Clipper.CleanPolygons(solidInfillOutlines, cleanDistance_um);
solidInfillOutlines = solidInfillOutlines.CreateDifference(island.SolidTopToolPaths);
solidInfillOutlines = Clipper.CleanPolygons(solidInfillOutlines, cleanDistance_um);
int upEnd = layerIndex + upLayerCount + 1;
if (upEnd <= extruder.Layers.Count && layerIndex - downLayerCount >= 0)
{
Polygons totalPartsToRemove = new Polygons(insetWithOffset);
int upStart = layerIndex + 2;
for (int layerToTest = upStart; layerToTest < upEnd; layerToTest++)
{
totalPartsToRemove = AddIslandsToPolygons(extruder.Layers[layerToTest].Islands, island.BoundingBox, totalPartsToRemove);
totalPartsToRemove = Clipper.CleanPolygons(totalPartsToRemove, cleanDistance_um);
}
int downStart = layerIndex - 1;
int downEnd = layerIndex - downLayerCount;
for (int layerToTest = downStart; layerToTest >= downEnd; layerToTest--)
{
totalPartsToRemove = AddIslandsToPolygons(extruder.Layers[layerToTest].Islands, island.BoundingBox, totalPartsToRemove);
totalPartsToRemove = Clipper.CleanPolygons(totalPartsToRemove, cleanDistance_um);
}
solidInfillOutlines = solidInfillOutlines.CreateDifference(totalPartsToRemove);
solidInfillOutlines.RemoveSmallAreas(extrusionWidth_um);
solidInfillOutlines = Clipper.CleanPolygons(solidInfillOutlines, cleanDistance_um);
}
island.SolidInfillToolPaths = solidInfillOutlines;
infillOutlines = infillOutlines.CreateDifference(solidInfillOutlines);
}
infillOutlines.RemoveSmallAreas(extrusionWidth_um);
infillOutlines = Clipper.CleanPolygons(infillOutlines, cleanDistance_um);
island.InfillToolPaths = infillOutlines;
}
}
public void InitializeLayerData(Slicer slicer, ConfigSettings config)
{
for (int layerIndex = 0; layerIndex < slicer.layers.Count; layerIndex++)
{
if (config.outputOnlyFirstLayer && layerIndex > 0)
{
break;
}
Layers.Add(new SliceLayer());
Layers[layerIndex].LayerZ = slicer.layers[layerIndex].Z;
Layers[layerIndex].AllOutlines = slicer.layers[layerIndex].PolygonList;
Layers[layerIndex].AllOutlines = Layers[layerIndex].AllOutlines.GetCorrectedWinding();
}
}
public bool OnlyHasBottom(int layerToCheck)
{
return Layers[layerToCheck].Islands.Count == 1
&& Layers[layerToCheck].Islands[0].SolidBottomToolPaths.Count == 1
&& Layers[layerToCheck].Islands[0].SolidTopToolPaths.Count == 0
&& Layers[layerToCheck].Islands[0].SolidInfillToolPaths.Count == 0
&& Layers[layerToCheck].Islands[0].InfillToolPaths.Count == 0;
}
public bool OnlyHasInfill(int layerToCheck)
{
return Layers[layerToCheck].Islands.Count == 1
&& Layers[layerToCheck].Islands[0].SolidBottomToolPaths.Count == 0
&& Layers[layerToCheck].Islands[0].SolidTopToolPaths.Count == 0
&& Layers[layerToCheck].Islands[0].SolidInfillToolPaths.Count == 0
&& Layers[layerToCheck].Islands[0].InfillToolPaths.Count == 1;
}
public bool OnlyHasSolidInfill(int layerToCheck)
{
return Layers[layerToCheck].Islands.Count == 1
&& Layers[layerToCheck].Islands[0].SolidBottomToolPaths.Count == 0
&& Layers[layerToCheck].Islands[0].SolidTopToolPaths.Count == 0
&& Layers[layerToCheck].Islands[0].SolidInfillToolPaths.Count == 1
&& Layers[layerToCheck].Islands[0].InfillToolPaths.Count == 0;
}
public bool OnlyHasTop(int layerToCheck)
{
return Layers[layerToCheck].Islands.Count == 1
&& Layers[layerToCheck].Islands[0].SolidBottomToolPaths.Count == 0
&& Layers[layerToCheck].Islands[0].SolidTopToolPaths.Count == 1
&& Layers[layerToCheck].Islands[0].SolidInfillToolPaths.Count == 0
&& Layers[layerToCheck].Islands[0].InfillToolPaths.Count == 0;
}
private static Polygons AddIslandsToPolygons(List<LayerIsland> islands, Aabb boundsToConsider, Polygons polysToAddTo)
{
Polygons polysToIntersect = new Polygons();
for (int islandIndex = 0; islandIndex < islands.Count; islandIndex++)
{
if (boundsToConsider.Hit(islands[islandIndex].BoundingBox))
{
polysToIntersect = polysToIntersect.CreateUnion(islands[islandIndex].InsetToolPaths[islands[islandIndex].InsetToolPaths.Count - 1]);
polysToIntersect = Clipper.CleanPolygons(polysToIntersect, cleanDistance_um);
}
}
polysToAddTo = polysToAddTo.CreateIntersection(polysToIntersect);
return polysToAddTo;
}
private static Polygons RemoveIslandsFromPolygons(List<LayerIsland> islands, Aabb boundsToConsider, Polygons polygonsToSubtractFrom)
{
for (int islandIndex = 0; islandIndex < islands.Count; islandIndex++)
{
if (boundsToConsider.Hit(islands[islandIndex].BoundingBox))
{
polygonsToSubtractFrom = polygonsToSubtractFrom.CreateDifference(islands[islandIndex].InsetToolPaths[islands[islandIndex].InsetToolPaths.Count - 1]);
polygonsToSubtractFrom = Clipper.CleanPolygons(polygonsToSubtractFrom, cleanDistance_um);
}
}
return polygonsToSubtractFrom;
}
}
}