forked from MatterHackers/MatterSlice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMultiExtruders.cs
212 lines (185 loc) · 7.97 KB
/
MultiExtruders.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
/*
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 MSClipperLib;
using System.Collections.Generic;
namespace MatterHackers.MatterSlice
{
using System;
using System.Linq;
using Polygons = List<List<IntPoint>>;
public class BooleanProcessing
{
int currentExtruder = 0;
int numberOfOpens = 0;
public List<ExtruderLayers> FinalLayers { get; internal set; }
List<int> layersToRemove = new List<int>();
Stack<int> extruderIndexStack = new Stack<int>();
enum BooleanType { None, Union, Difference, Intersection };
public BooleanProcessing(List<ExtruderLayers> extruders, string booleanOpperations)
{
int parseIndex = 0;
int totalLayers = extruders[0].Layers.Count;
while (parseIndex < booleanOpperations.Length)
{
BooleanType typeToDo = BooleanType.None;
switch (booleanOpperations[parseIndex])
{
case '(': // start union
case '[': // start intersection
case '{': // start difference
numberOfOpens++;
parseIndex++;
break;
case ')': // end union
typeToDo = BooleanType.Union;
parseIndex++;
break;
case '}': // end difference
typeToDo = BooleanType.Difference;
parseIndex++;
break;
case ']': // end intersection
typeToDo = BooleanType.Intersection;
parseIndex++;
break;
case ',':
parseIndex++;
break;
default:
// get the number for the operand index
int skipCount = 0;
extruderIndexStack.Push(GetNextNumber(booleanOpperations, parseIndex, out skipCount));
parseIndex += skipCount;
break;
}
if (typeToDo != BooleanType.None)
{
numberOfOpens--;
int extruderBIndex = extruderIndexStack.Pop();
int extruderAIndex = extruderIndexStack.Pop();
if (extruders[extruderBIndex].Layers.Count != extruders[extruderAIndex].Layers.Count ||
extruders[extruderAIndex].Layers.Count != totalLayers)
{
throw new Exception("These should be the same.");
}
for (int layerIndex = 0; layerIndex < totalLayers; layerIndex++)
{
SliceLayer layerA = extruders[extruderAIndex].Layers[layerIndex];
SliceLayer layerB = extruders[extruderBIndex].Layers[layerIndex];
DoLayerBooleans(layerA, layerB, typeToDo);
}
layersToRemove.Add(extruderBIndex);
extruderIndexStack.Push(extruderAIndex);
if (numberOfOpens == 0)
{
currentExtruder++;
}
}
}
layersToRemove.Sort();
for (int i = layersToRemove.Count - 1; i >= 0; i--)
{
extruders.RemoveAt(layersToRemove[i]);
}
}
private int GetNextNumber(string numberString, int index, out int skipCount)
{
string digits = new string(numberString.Substring(index).TakeWhile(c => Char.IsDigit(c)).ToArray());
skipCount = digits.Length;
int result;
if (Int32.TryParse(digits, out result))
{
return result;
}
throw new FormatException("not a number");
}
private static void DoLayerBooleans(SliceLayer layersA, SliceLayer layersB, BooleanType booleanType)
{
switch (booleanType)
{
case BooleanType.Union:
if (layersB.AllOutlines.Count == 0)
{
// do nothing we will keep the content of A
}
else if (layersA.AllOutlines.Count == 0)
{
// there is nothing in A so set it to the content of B
layersA.AllOutlines = layersB.AllOutlines;
}
else
{
layersA.AllOutlines = layersA.AllOutlines.CreateUnion(layersB.AllOutlines);
}
break;
case BooleanType.Difference:
layersA.AllOutlines = layersA.AllOutlines.CreateDifference(layersB.AllOutlines);
break;
case BooleanType.Intersection:
layersA.AllOutlines = layersA.AllOutlines.CreateIntersection(layersB.AllOutlines);
break;
}
}
}
public static class MultiExtruders
{
public static void ProcessBooleans(List<ExtruderLayers> allPartsLayers, string booleanOpperations)
{
BooleanProcessing processor = new BooleanProcessing(allPartsLayers, booleanOpperations);
}
public static void RemoveExtruderIntersections(List<ExtruderLayers> extruders)
{
//Go trough all the extruders, and remove the previous extruders outlines from our own outline, so we never have overlapped areas.
for (int extruderIndex = extruders.Count - 1; extruderIndex >= 0; extruderIndex--)
{
for (int otherExtuderIndex = extruderIndex - 1; otherExtuderIndex >= 0; otherExtuderIndex--)
{
for (int layerIndex = 0; layerIndex < extruders[extruderIndex].Layers.Count; layerIndex++)
{
SliceLayer layerToRemoveFrom = extruders[extruderIndex].Layers[layerIndex];
SliceLayer layerToRemove = extruders[otherExtuderIndex].Layers[layerIndex];
layerToRemoveFrom.AllOutlines = layerToRemoveFrom.AllOutlines.CreateDifference(layerToRemove.AllOutlines);
}
}
}
}
//Expand each layer a bit and then keep the extra overlapping parts that overlap with other extruders.
//This generates some overlap in dual extrusion, for better bonding in touching parts.
public static void OverlapMultipleExtrudersSlightly(List<ExtruderLayers> extruders, int overlapUm)
{
if (extruders.Count < 2 || overlapUm <= 0)
{
return;
}
for (int layerIndex = 0; layerIndex < extruders[0].Layers.Count; layerIndex++)
{
Polygons fullLayer = new Polygons();
for (int extruderIndex = 0; extruderIndex < extruders.Count; extruderIndex++)
{
SliceLayer layer1 = extruders[extruderIndex].Layers[layerIndex];
fullLayer = fullLayer.CreateUnion(layer1.AllOutlines.Offset(20));
}
fullLayer = fullLayer.Offset(-20);
for (int extruderIndex = 0; extruderIndex < extruders.Count; extruderIndex++)
{
SliceLayer layer1 = extruders[extruderIndex].Layers[layerIndex];
layer1.AllOutlines = fullLayer.CreateIntersection(layer1.AllOutlines.Offset(overlapUm / 2));
}
}
}
}
}