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
|
/*
* GNU AFFERO GENERAL PUBLIC LICENSE
* Version 3, 19 November 2007
* Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
* Everyone is permitted to copy and distribute verbatim copies
* of this license document, but changing it is not allowed.
*/
using System;
using System.Collections.Generic;
using UVtools.Core.FileFormats;
using UVtools.Core.Operations;
namespace UVtools.Core.GCode
{
public class GCodeLayer
{
private float? _positionZ;
private float? _waitTimeBeforeCure;
private float? _exposureTime;
private float? _waitTimeAfterCure;
private float? _liftHeight;
private float? _liftSpeed;
private float? _liftHeight2;
private float? _liftSpeed2;
private float? _waitTimeAfterLift;
private float? _retractSpeed;
private float? _retractHeight2;
private float? _retractSpeed2;
public enum GCodeLastParsedLine : byte
{
LayerIndex,
}
public bool IsValid => LayerIndex.HasValue;
public FileFormat SlicerFile { get; }
public List<(float Pos, float Speed)> Movements = new();
public uint? LayerIndex { get; set; }
public float? PositionZ
{
get => _positionZ;
set => _positionZ = value;
}
public float PreviousPositionZ { get; set; }
public float? WaitTimeBeforeCure
{
get => _waitTimeBeforeCure;
set => _waitTimeBeforeCure = value is null ? null : (float)Math.Round(value.Value, 2);
}
public float? ExposureTime
{
get => _exposureTime;
set => _exposureTime = value is null ? null : (float)Math.Round(value.Value, 2);
}
public float? WaitTimeAfterCure
{
get => _waitTimeAfterCure;
set => _waitTimeAfterCure = value is null ? null : (float)Math.Round(value.Value, 2);
}
public float? LiftHeight
{
get => _liftHeight;
set => _liftHeight = value is null ? null : Layer.RoundHeight(value.Value);
}
public float? LiftSpeed
{
get => _liftSpeed;
set => _liftSpeed = value is null ? null : (float)Math.Round(value.Value, 2);
}
public float LiftHeightTotal => Layer.RoundHeight((LiftHeight ?? 0) + (LiftHeight2 ?? 0));
public float? LiftHeight2
{
get => _liftHeight2;
set => _liftHeight2 = value is null ? null : Layer.RoundHeight(value.Value);
}
public float? LiftSpeed2
{
get => _liftSpeed2;
set => _liftSpeed2 = value is null ? null : (float)Math.Round(value.Value, 2);
}
public float? WaitTimeAfterLift
{
get => _waitTimeAfterLift;
set => _waitTimeAfterLift = value is null ? null : (float)Math.Round(value.Value, 2);
}
public float? RetractSpeed
{
get => _retractSpeed;
set => _retractSpeed = value is null ? null : (float)Math.Round(value.Value, 2);
}
public float? RetractHeight2
{
get => _retractHeight2;
set => _retractHeight2 = value is null ? null : Layer.RoundHeight(value.Value);
}
public float? RetractSpeed2
{
get => _retractSpeed2;
set => _retractSpeed2 = value is null ? null : (float)Math.Round(value.Value, 2);
}
public byte? LightPWM { get; set; }
public bool IsExposing => LightPWM.HasValue && !IsAfterLightOff;
public bool IsExposed => LightPWM.HasValue && IsAfterLightOff;
public byte LightOffCount { get; set; }
public bool IsAfterLightOff => LightOffCount > 0;
public GCodeLayer(FileFormat slicerFile)
{
SlicerFile = slicerFile;
}
public void Init()
{
PreviousPositionZ = PositionZ ?? 0;
Movements.Clear();
LayerIndex = null;
PositionZ = null;
WaitTimeBeforeCure = null;
ExposureTime = null;
WaitTimeAfterCure = null;
LiftHeight = null;
LiftSpeed = null;
LiftHeight2 = null;
LiftSpeed2 = null;
WaitTimeAfterLift = null;
RetractSpeed = null;
RetractHeight2 = null;
RetractSpeed2 = null;
LightPWM = null;
LightOffCount = 0;
}
public void AssignMovements(GCodeBuilder.GCodePositioningTypes positionType)
{
if (Movements.Count == 0) return;
var currentZ = PreviousPositionZ;
PositionZ = null;
LiftHeight = null;
LiftSpeed = null;
LiftHeight2 = null;
LiftSpeed2 = null;
RetractSpeed = null;
RetractHeight2 = null;
RetractSpeed2 = null;
for (int i = 0; i < Movements.Count; i++)
{
var (pos, speed) = Movements[i];
float heightRaw;
switch (positionType)
{
case GCodeBuilder.GCodePositioningTypes.Absolute:
heightRaw = Layer.RoundHeight(pos - currentZ);
currentZ = pos;
break;
case GCodeBuilder.GCodePositioningTypes.Partial:
heightRaw = pos;
currentZ = Layer.RoundHeight(currentZ + pos);
break;
default:
throw new ArgumentOutOfRangeException(nameof(positionType));
}
// Fail-safe check
if (currentZ < PreviousPositionZ)
throw new NotSupportedException("GCode parsing error: Attempting to crash the print on the LCD with negative position.\n" +
"Do not attempt to print this file!");
// Is position Z
if (i == Movements.Count - 1)
{
PositionZ = currentZ;
if (LiftHeight.HasValue)
{
RetractSpeed = speed; // A lift exists, set to retract speed of this move
}
continue;
}
if (heightRaw == 0) continue;
var height = Math.Abs(heightRaw);
if (heightRaw > 0) // Is a lift
{
if (!LiftHeight.HasValue)
{
LiftHeight = height;
LiftSpeed = speed;
continue;
}
LiftHeight2 ??= 0;
LiftHeight2 += height;
LiftSpeed2 = speed;
continue;
}
if(!LiftHeight.HasValue) continue; // Fail-safe: Retract without a lift? Skip
// Is a extra retract (2)
RetractHeight2 ??= 0;
RetractHeight2 += height;
RetractSpeed2 = speed;
}
if (Movements.Count == 1) // Only 1 move, this is the PositionZ only
{
LiftSpeed = Movements[0].Speed;
return;
}
// Sanitize
if (PositionZ.HasValue && LiftHeight.HasValue && !IsExposed) // Lift before exposure order, need to remove layer height as offset
{
var liftHeight = Layer.RoundHeight(LiftHeight.Value - (PositionZ.Value - PreviousPositionZ));
if(liftHeight <= 0) return; // Something not right or not the correct moment, skip
LiftHeight = liftHeight;
}
if (RetractHeight2.HasValue) // Need to fix the propose of this value
{
RetractHeight2 = Layer.RoundHeight(LiftHeightTotal - RetractHeight2.Value);
(RetractSpeed, RetractSpeed2) = (RetractSpeed2, RetractSpeed);
}
if (LiftHeight.HasValue && RetractHeight2.HasValue) // Sanitize RetractHeight2 value
{
RetractHeight2 = Math.Clamp(RetractHeight2.Value, 0, LiftHeightTotal);
}
}
/// <summary>
/// Set gathered data to the layer
/// </summary>
public void SetLayer(bool reinit = false)
{
if (!IsValid) return;
uint layerIndex = LayerIndex.Value;
var layer = SlicerFile[layerIndex];
PositionZ ??= PreviousPositionZ;
layer.PositionZ = PositionZ.Value;
layer.WaitTimeBeforeCure = WaitTimeBeforeCure ?? 0;
layer.ExposureTime = ExposureTime ?? SlicerFile.GetInitialLayerValueOrNormal(layerIndex, SlicerFile.BottomExposureTime, SlicerFile.ExposureTime);
layer.WaitTimeAfterCure = WaitTimeAfterCure ?? 0;
layer.LiftHeight = LiftHeight ?? 0;
layer.LiftSpeed = LiftSpeed ?? SlicerFile.GetInitialLayerValueOrNormal(layerIndex, SlicerFile.BottomLiftSpeed, SlicerFile.LiftSpeed);
layer.LiftHeight2 = LiftHeight2 ?? 0;
layer.LiftSpeed2 = LiftSpeed2 ?? SlicerFile.GetInitialLayerValueOrNormal(layerIndex, SlicerFile.BottomLiftSpeed2, SlicerFile.LiftSpeed2);
layer.WaitTimeAfterLift = WaitTimeAfterLift ?? 0;
layer.RetractSpeed = RetractSpeed ?? SlicerFile.GetInitialLayerValueOrNormal(layerIndex, SlicerFile.BottomRetractSpeed, SlicerFile.RetractSpeed);
layer.RetractHeight2 = RetractHeight2 ?? 0;
layer.RetractSpeed2 = RetractSpeed2 ?? SlicerFile.GetInitialLayerValueOrNormal(layerIndex, SlicerFile.BottomRetractSpeed2, SlicerFile.RetractSpeed2);
layer.LightPWM = LightPWM ?? 0;//SlicerFile.GetInitialLayerValueOrNormal(layerIndex, SlicerFile.BottomLightPWM, SlicerFile.LightPWM);
if (SlicerFile.GCode.SyncMovementsWithDelay) // Dirty fix of the value
{
var syncTime = OperationCalculator.LightOffDelayC.CalculateSeconds(layer, 1.5f);
if (syncTime < layer.WaitTimeBeforeCure)
{
layer.WaitTimeBeforeCure = (float) Math.Round(layer.WaitTimeBeforeCure - syncTime, 2);
}
}
if(reinit) Init();
}
}
}
|