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
|
/*
* 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.ComponentModel;
using System.Linq;
using System.Text;
using MoreLinq.Extensions;
using UVtools.Core.Extensions;
using UVtools.Core.FileFormats;
namespace UVtools.Core.Operations
{
[Serializable]
public sealed class OperationDynamicLifts : Operation
{
#region Enums
public enum DynamicLiftsLightOffDelaySetMode : byte
{
[Description("Set the light-off with an extra delay")]
UpdateWithExtraDelay,
[Description("Set the light-off without an extra delay")]
UpdateWithoutExtraDelay,
[Description("Set the light-off to zero")]
SetToZero,
}
#endregion
#region Members
private float _minBottomLiftHeight;
private float _maxBottomLiftHeight;
private float _minLiftHeight;
private float _maxLiftHeight;
private float _minBottomLiftSpeed;
private float _maxBottomLiftSpeed;
private float _minLiftSpeed;
private float _maxLiftSpeed;
private float _lightOffDelayBottomExtraTime = 3;
private float _lightOffDelayExtraTime = 2.5f;
private DynamicLiftsLightOffDelaySetMode _lightOffDelaySetMode = DynamicLiftsLightOffDelaySetMode.UpdateWithExtraDelay;
#endregion
#region Overrides
public override string Title => "Dynamic lifts";
public override string Description =>
"Generate dynamic lift height and speeds for each layer given it mass.\n" +
"Larger masses requires more lift height and less speed while smaller masses can go with shorter lift height and more speed.\n" +
"If you have a raft, start after it layer number to not influence the calculations.\n" +
"Note: Only few printers support this. Running this on an unsupported printer will cause no harm.";
public override string ConfirmationText =>
$"generate dynamic lifts from layers {LayerIndexStart} through {LayerIndexEnd}?";
public override string ProgressTitle =>
$"Generating dynamic lifts from layers {LayerIndexStart} through {LayerIndexEnd}";
public override string ProgressAction => "Generated lifts";
public override string ValidateInternally()
{
var sb = new StringBuilder();
if (_minBottomLiftHeight > _maxBottomLiftHeight)
{
sb.AppendLine("Minimum bottom lift height can't be higher than the maximum.");
}
if (_minBottomLiftSpeed > _maxBottomLiftSpeed)
{
sb.AppendLine("Minimum bottom lift speed can't be higher than the maximum.");
}
if (_minLiftHeight > _maxLiftHeight)
{
sb.AppendLine("Minimum lift height can't be higher than the maximum.");
}
if (_minLiftSpeed > _maxLiftSpeed)
{
sb.AppendLine("Minimum lift speed can't be higher than the maximum.");
}
if (_minBottomLiftHeight == _maxBottomLiftHeight &&
_minBottomLiftSpeed == _maxBottomLiftSpeed &&
_minLiftHeight == _maxLiftHeight &&
_minLiftSpeed == _maxLiftSpeed)
{
sb.AppendLine("The selected min/max settings are all equal and will not produce a change.");
}
return sb.ToString();
}
public override string ToString()
{
var result =
$"[Bottom height: {_minBottomLiftHeight}/{_maxBottomLiftHeight}mm]" +
$" [Bottom speed: {_minBottomLiftSpeed}/{_maxBottomLiftSpeed}mm/min]" +
$" [Height: {_minLiftHeight}/{_maxLiftHeight}mm]" +
$" [Speed: {_minLiftSpeed}/{_maxLiftSpeed}mm/min]" +
$" [Light-off: {_lightOffDelaySetMode} {_lightOffDelayBottomExtraTime}/{_lightOffDelayExtraTime}s]" +
LayerRangeString;
if (!string.IsNullOrEmpty(ProfileName)) result = $"{ProfileName}: {result}";
return result;
}
protected override void OnPropertyChanged(PropertyChangedEventArgs e)
{
base.OnPropertyChanged(e);
if (e.PropertyName is nameof(LayerRangeCount))
{
RaisePropertyChanged(nameof(IsBottomLayersEnabled));
RaisePropertyChanged(nameof(IsNormalLayersEnabled));
}
}
#endregion
#region Properties
public bool IsBottomLayersEnabled => LayerIndexStart < SlicerFile.BottomLayerCount;
public bool IsNormalLayersEnabled => LayerIndexEnd >= SlicerFile.BottomLayerCount;
public float MinBottomLiftHeight
{
get => _minBottomLiftHeight;
set => RaiseAndSetIfChanged(ref _minBottomLiftHeight, (float)Math.Round(value, 2));
}
public float MaxBottomLiftHeight
{
get => _maxBottomLiftHeight;
set => RaiseAndSetIfChanged(ref _maxBottomLiftHeight, (float)Math.Round(value, 2));
}
public float MinLiftHeight
{
get => _minLiftHeight;
set => RaiseAndSetIfChanged(ref _minLiftHeight, (float)Math.Round(value, 2));
}
public float MaxLiftHeight
{
get => _maxLiftHeight;
set => RaiseAndSetIfChanged(ref _maxLiftHeight, (float)Math.Round(value, 2));
}
public float MinBottomLiftSpeed
{
get => _minBottomLiftSpeed;
set => RaiseAndSetIfChanged(ref _minBottomLiftSpeed, (float)Math.Round(value, 2));
}
public float MaxBottomLiftSpeed
{
get => _maxBottomLiftSpeed;
set => RaiseAndSetIfChanged(ref _maxBottomLiftSpeed, (float)Math.Round(value, 2));
}
public float MinLiftSpeed
{
get => _minLiftSpeed;
set => RaiseAndSetIfChanged(ref _minLiftSpeed, (float)Math.Round(value, 2));
}
public float MaxLiftSpeed
{
get => _maxLiftSpeed;
set => RaiseAndSetIfChanged(ref _maxLiftSpeed, (float)Math.Round(value, 2));
}
public DynamicLiftsLightOffDelaySetMode LightOffDelaySetMode
{
get => _lightOffDelaySetMode;
set => RaiseAndSetIfChanged(ref _lightOffDelaySetMode, value);
}
public float LightOffDelayBottomExtraTime
{
get => _lightOffDelayBottomExtraTime;
set => RaiseAndSetIfChanged(ref _lightOffDelayBottomExtraTime, (float)Math.Round(value, 2));
}
public float LightOffDelayExtraTime
{
get => _lightOffDelayExtraTime;
set => RaiseAndSetIfChanged(ref _lightOffDelayExtraTime, (float)Math.Round(value, 2));
}
//public uint MinBottomLayerPixels => SlicerFile.Where(layer => layer.IsBottomLayer && !layer.IsEmpty && layer.Index >= LayerIndexStart && layer.Index <= LayerIndexEnd).Max(layer => layer.NonZeroPixelCount);
public uint MinBottomLayerPixels => (from layer in SlicerFile
where layer.IsBottomLayer
where !layer.IsEmpty
where layer.Index >= LayerIndexStart
where layer.Index <= LayerIndexEnd
select layer.NonZeroPixelCount).Min();
//public uint MinNormalLayerPixels => SlicerFile.Where(layer => layer.IsNormalLayer && !layer.IsEmpty && layer.Index >= LayerIndexStart && layer.Index <= LayerIndexEnd).Max(layer => layer.NonZeroPixelCount);
public uint MinNormalLayerPixels => (from layer in SlicerFile
where layer.IsNormalLayer
where !layer.IsEmpty
where layer.Index >= LayerIndexStart
where layer.Index <= LayerIndexEnd
select layer.NonZeroPixelCount).Min();
//public uint MaxBottomLayerPixels => SlicerFile.Where(layer => layer.IsBottomLayer && layer.Index >= LayerIndexStart && layer.Index <= LayerIndexEnd).Max(layer => layer.NonZeroPixelCount);
public uint MaxBottomLayerPixels => (from layer in SlicerFile
where layer.IsBottomLayer
where !layer.IsEmpty
where layer.Index >= LayerIndexStart
where layer.Index <= LayerIndexEnd
select layer.NonZeroPixelCount).Max();
//public uint MaxNormalLayerPixels => SlicerFile.Where(layer => layer.IsNormalLayer && layer.Index >= LayerIndexStart && layer.Index <= LayerIndexEnd).Max(layer => layer.NonZeroPixelCount);
public uint MaxNormalLayerPixels => (from layer in SlicerFile
where layer.IsNormalLayer
where !layer.IsEmpty
where layer.Index >= LayerIndexStart
where layer.Index <= LayerIndexEnd
select layer.NonZeroPixelCount).Max();
#endregion
#region Constructor
public OperationDynamicLifts()
{ }
public OperationDynamicLifts(FileFormat slicerFile) : base(slicerFile)
{ }
public override void InitWithSlicerFile()
{
base.InitWithSlicerFile();
if(_minBottomLiftHeight <= 0) _minBottomLiftHeight = SlicerFile.BottomLiftHeight;
if (_maxBottomLiftHeight <= 0 || _maxBottomLiftHeight < _minBottomLiftHeight) _maxBottomLiftHeight = _minBottomLiftHeight;
if (_minLiftHeight <= 0) _minLiftHeight = SlicerFile.LiftHeight;
if (_maxLiftHeight <= 0 || _maxLiftHeight < _minLiftHeight) _maxLiftHeight = _minLiftHeight;
if (_minBottomLiftSpeed <= 0) _minBottomLiftSpeed = SlicerFile.BottomLiftSpeed;
if (_maxBottomLiftSpeed <= 0 || _maxBottomLiftSpeed < _minBottomLiftSpeed) _maxBottomLiftSpeed = _minBottomLiftSpeed;
if(_minLiftSpeed <= 0) _minLiftSpeed = SlicerFile.LiftSpeed;
if (_maxLiftSpeed <= 0 || _maxLiftSpeed < _minLiftSpeed) _maxLiftSpeed = _minLiftSpeed;
RaisePropertyChanged(nameof(IsBottomLayersEnabled));
RaisePropertyChanged(nameof(IsNormalLayersEnabled));
}
#endregion
#region Methods
protected override bool ExecuteInternally(OperationProgress progress)
{
uint minBottomPixels = 0;
uint minNormalPixels = 0;
uint maxBottomPixels = 0;
uint maxNormalPixels = 0;
try
{
minBottomPixels = MinBottomLayerPixels;
}
catch
{
}
try
{
minNormalPixels = MinNormalLayerPixels;
}
catch
{
}
try
{
maxBottomPixels = MaxBottomLayerPixels;
}
catch
{
}
try
{
maxNormalPixels = MaxNormalLayerPixels;
}
catch
{
}
float liftHeight;
float liftSpeed;
uint max = (from layer in SlicerFile where !layer.IsBottomLayer where !layer.IsEmpty where layer.Index >= LayerIndexStart where layer.Index <= LayerIndexEnd select layer).Aggregate<Layer, uint>(0, (current, layer) => Math.Max(layer.NonZeroPixelCount, current));
for (uint layerIndex = LayerIndexStart; layerIndex <= LayerIndexEnd; layerIndex++)
{
progress.Token.ThrowIfCancellationRequested();
var layer = SlicerFile[layerIndex];
// Height
// min - largestpixelcount
// x - pixelcount
// Speed
// max - minpixelCount
// x - pixelcount
if (layer.IsBottomLayer)
{
liftHeight = (_maxBottomLiftHeight * layer.NonZeroPixelCount / maxBottomPixels).Clamp(_minBottomLiftHeight, _maxBottomLiftHeight);
liftSpeed = (_maxBottomLiftSpeed - (_maxBottomLiftSpeed * layer.NonZeroPixelCount / maxNormalPixels)).Clamp(_minBottomLiftSpeed, _maxBottomLiftSpeed);
}
else
{
liftHeight = (_maxLiftHeight * layer.NonZeroPixelCount / maxNormalPixels).Clamp(_minLiftHeight, _maxLiftHeight);
liftSpeed = (_maxLiftSpeed - (_maxLiftSpeed * layer.NonZeroPixelCount / maxNormalPixels)).Clamp(_minLiftSpeed, _maxLiftSpeed);
}
layer.LiftHeight = (float) Math.Round(liftHeight, 2);
layer.LiftSpeed = (float) Math.Round(liftSpeed, 2);
switch (_lightOffDelaySetMode)
{
case DynamicLiftsLightOffDelaySetMode.UpdateWithExtraDelay:
layer.SetLightOffDelay(layer.IsBottomLayer ? _lightOffDelayBottomExtraTime : _lightOffDelayExtraTime);
break;
case DynamicLiftsLightOffDelaySetMode.UpdateWithoutExtraDelay:
layer.SetLightOffDelay();
break;
case DynamicLiftsLightOffDelaySetMode.SetToZero:
layer.LightOffDelay = 0;
break;
default:
throw new NotImplementedException();
}
progress++;
}
SlicerFile.UpdatePrintTime();
return !progress.Token.IsCancellationRequested;
}
public Layer GetSmallestLayer(bool isBottom)
{
return SlicerFile.Where((layer, index) => !layer.IsEmpty && layer.IsBottomLayer == isBottom && index >= LayerIndexStart && index <= LayerIndexEnd).MinBy(layer => layer.NonZeroPixelCount).FirstOrDefault();
}
public Layer GetLargestLayer(bool isBottom)
{
return SlicerFile.Where((layer, index) => !layer.IsEmpty && layer.IsBottomLayer == isBottom && index >= LayerIndexStart && index <= LayerIndexEnd).MaxBy(layer => layer.NonZeroPixelCount).FirstOrDefault();
}
#endregion
#region Equality
private bool Equals(OperationDynamicLifts other)
{
return _minBottomLiftHeight.Equals(other._minBottomLiftHeight) && _maxBottomLiftHeight.Equals(other._maxBottomLiftHeight) && _minLiftHeight.Equals(other._minLiftHeight) && _maxLiftHeight.Equals(other._maxLiftHeight) && _minBottomLiftSpeed.Equals(other._minBottomLiftSpeed) && _maxBottomLiftSpeed.Equals(other._maxBottomLiftSpeed) && _minLiftSpeed.Equals(other._minLiftSpeed) && _maxLiftSpeed.Equals(other._maxLiftSpeed) && _lightOffDelayBottomExtraTime.Equals(other._lightOffDelayBottomExtraTime) && _lightOffDelayExtraTime.Equals(other._lightOffDelayExtraTime) && _lightOffDelaySetMode == other._lightOffDelaySetMode;
}
public override bool Equals(object obj)
{
return ReferenceEquals(this, obj) || obj is OperationDynamicLifts other && Equals(other);
}
public override int GetHashCode()
{
var hashCode = new HashCode();
hashCode.Add(_minBottomLiftHeight);
hashCode.Add(_maxBottomLiftHeight);
hashCode.Add(_minLiftHeight);
hashCode.Add(_maxLiftHeight);
hashCode.Add(_minBottomLiftSpeed);
hashCode.Add(_maxBottomLiftSpeed);
hashCode.Add(_minLiftSpeed);
hashCode.Add(_maxLiftSpeed);
hashCode.Add(_lightOffDelayBottomExtraTime);
hashCode.Add(_lightOffDelayExtraTime);
hashCode.Add((int) _lightOffDelaySetMode);
return hashCode.ToHashCode();
}
#endregion
}
}
|