Welcome to mirror list, hosted at ThFree Co, Russian Federation.

OperationTimelapse.cs « Operations « UVtools.Core - github.com/sn4k3/UVtools.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 084ea579e6e3327e73c3e3ee2f7278b7ff9d2611 (plain)
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
/*
 *                     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 System.ComponentModel;
using System.Linq;
using System.Text;
using UVtools.Core.FileFormats;
using UVtools.Core.Layers;

namespace UVtools.Core.Operations
{
    [Serializable]
    public class OperationTimelapse : Operation
    {
        #region Constants
        #endregion

        #region Enums
        public enum TimelapseRaiseMode
        {
            [Description("Lift height: Use the lift sequence to raise to the set postion (Faster)")]
            LiftHeight,

            [Description("Virtual layer: Print a blank layer to simulate and raise to the set position (Slower)")]
            VirtualLayer,
        }
        #endregion

        #region Members
        private decimal _raisePositionZ;
        private bool _outputDummyPixel = true;
        private decimal _raiseEachNthHeight = 1;
        private TimelapseRaiseMode _raiseMode = TimelapseRaiseMode.LiftHeight;
        private decimal _waitTimeAfterLift = 1;
        private decimal _exposureTime = 1;
        private bool _useCustomLift;
        private decimal _slowLiftHeight = 3;
        private decimal _liftSpeed;
        private decimal _liftSpeed2;
        private decimal _slowRetractHeight = 3;
        private decimal _retractSpeed;
        private decimal _retractSpeed2;

        #endregion

        #region Overrides

        public override Enumerations.LayerRangeSelection StartLayerRangeSelection => Enumerations.LayerRangeSelection.Normal;

        public override string Title => "Timelapse";
        public override string Description =>
            "Raise the build platform to a set position every odd-even height to be able to take a photo and create a time-lapse video of the print.\n" +
            "You will require external hardware to take the photos, and create the time-lapse video by your own.\n" +
            "NOTE: Only use this tool once. It will delay the total print time significantly.";

        public override string ConfirmationText =>
            $"raise the platform at every odd-even {_raiseEachNthHeight}mm to Z={_raisePositionZ}mm";

        public override string ProgressTitle => "Raising layers";

        public override string ProgressAction => "Raised layer";

        public override string ValidateSpawn()
        {
            if(!SlicerFile.CanUseLayerPositionZ && !SlicerFile.CanUseLayerLiftHeight)
            {
                return NotSupportedMessage;
            }

            return null;
        }

        public override string ValidateInternally()
        {
            var sb = new StringBuilder();

            if (!ValidateSpawn(out var message))
            {
                sb.AppendLine(message);
            }

            if ((_raiseMode == TimelapseRaiseMode.LiftHeight && !SlicerFile.CanUseLayerLiftHeight) ||
                (_raiseMode == TimelapseRaiseMode.VirtualLayer && !SlicerFile.CanUseLayerPositionZ))
            {
                return $"The raise method {_raiseMode} is not compatible with this printer / file format, please choose other method.";
            }
            
            return sb.ToString();
        }

        public override string ToString()
        {
            var result = $"[Mode: {_raiseMode}] [Z={_raisePositionZ}mm] [Raise each: {_raiseEachNthHeight}mm]";
            if (!string.IsNullOrEmpty(ProfileName)) result = $"{ProfileName}: {result}";
            return result;
        }
        #endregion

        #region Properties

        /// <summary>
        /// Gets the minimum possible
        /// </summary>
        public float MinimumPositionZ => Layer.RoundHeight(SlicerFile.PrintHeight + SlicerFile.LayerHeight);

        public TimelapseRaiseMode RaiseMode
        {
            get => _raiseMode;
            set
            {
                if(!RaiseAndSetIfChanged(ref _raiseMode, value)) return;
                RaisePropertyChanged(nameof(IsLiftHeightMode));
                RaisePropertyChanged(nameof(IsVirtualLayerMode));
            }
        }

        public bool IsLiftHeightMode => _raiseMode is TimelapseRaiseMode.LiftHeight;
        public bool IsVirtualLayerMode => _raiseMode is TimelapseRaiseMode.VirtualLayer;

        /// <summary>
        /// Sets or gets the Z position to raise to
        /// </summary>
        public decimal RaisePositionZ
        {
            get => _raisePositionZ;
            set => RaiseAndSetIfChanged(ref _raisePositionZ, Layer.RoundHeight(Math.Clamp(value, 10, 10000)));
        }

        /// <summary>
        /// True to output a dummy pixel on bounding rectangle position to avoid empty layer and blank image, otherwise set to false
        /// </summary>
        public bool OutputDummyPixel 
        {
            get => _outputDummyPixel; 
            set => RaiseAndSetIfChanged(ref _outputDummyPixel, value); 
        }

        /// <summary>
        /// Gets or sets the alternating height in millimeters to raise when, it will raise only at each defined millimeters and skip the same next millimeters
        /// </summary>
        public decimal RaiseEachNthHeight
        {
            get => _raiseEachNthHeight;
            set => RaiseAndSetIfChanged(ref _raiseEachNthHeight, Math.Max(0, value));
        }

        public ushort RaiseEachNthLayers
        {
            get
            {
                if (_raiseEachNthHeight == 0) return 1;
                return (ushort)Math.Max(1, _raiseEachNthHeight / (decimal)SlicerFile.LayerHeight);
            }
        }

        public decimal WaitTimeAfterLift
        {
            get => _waitTimeAfterLift;
            set => RaiseAndSetIfChanged(ref _waitTimeAfterLift, Math.Max(0, value));
        }

        public decimal ExposureTime
        {
            get => _exposureTime;
            set => RaiseAndSetIfChanged(ref _exposureTime, Math.Max(0, value));
        }

        public bool UseCustomLift
        {
            get => _useCustomLift;
            set => RaiseAndSetIfChanged(ref _useCustomLift, value);
        }

        public decimal SlowLiftHeight
        {
            get => _slowLiftHeight;
            set => RaiseAndSetIfChanged(ref _slowLiftHeight, Math.Max(0, value));
        }

        public decimal LiftSpeed
        {
            get => _liftSpeed;
            set => RaiseAndSetIfChanged(ref _liftSpeed, Math.Max(0, value));
        }

        public decimal LiftSpeed2
        {
            get => _liftSpeed2;
            set => RaiseAndSetIfChanged(ref _liftSpeed2, Math.Max(0, value));
        }

        public decimal SlowRetractHeight
        {
            get => _slowRetractHeight;
            set => RaiseAndSetIfChanged(ref _slowRetractHeight, Math.Max(0, value));
        }

        public decimal RetractSpeed
        {
            get => _retractSpeed;
            set => RaiseAndSetIfChanged(ref _retractSpeed, Math.Max(0, value));
        }

        public decimal RetractSpeed2
        {
            get => _retractSpeed2;
            set => RaiseAndSetIfChanged(ref _retractSpeed2, Math.Max(0, value));
        }

        #endregion

        #region Constructor

        public OperationTimelapse()
        {
        }

        public OperationTimelapse(FileFormat slicerFile) : base(slicerFile) 
        {
            if (_raisePositionZ <= 0) _raisePositionZ = (decimal)SlicerFile.MachineZ;
            if(_exposureTime <= 0) _exposureTime = SlicerFile.SupportsGCode ? 0 : 0.05M;

            if (_liftSpeed <= 0) _liftSpeed = (decimal) SlicerFile.LiftSpeed;
            if (_liftSpeed2 <= 0) _liftSpeed2 = (decimal) SlicerFile.MaximumSpeed;

            if (SlicerFile.CanUseLayerRetractSpeed2) // TSMC
            {
                if (_retractSpeed <= 0) _retractSpeed = (decimal)SlicerFile.MaximumSpeed;
                if (_retractSpeed2 <= 0) _retractSpeed2 = (decimal)SlicerFile.RetractSpeed2;
            }
            else
            {
                if (_retractSpeed <= 0) _retractSpeed = (decimal)SlicerFile.RetractSpeed;
            }
            
        }

        #endregion

        #region Equality

        protected bool Equals(OperationTimelapse other)
        {
            return _raisePositionZ == other._raisePositionZ && _outputDummyPixel == other._outputDummyPixel && _raiseEachNthHeight == other._raiseEachNthHeight && _raiseMode == other._raiseMode && _waitTimeAfterLift == other._waitTimeAfterLift && _exposureTime == other._exposureTime && _useCustomLift == other._useCustomLift && _slowLiftHeight == other._slowLiftHeight && _liftSpeed == other._liftSpeed && _liftSpeed2 == other._liftSpeed2 && _slowRetractHeight == other._slowRetractHeight && _retractSpeed == other._retractSpeed && _retractSpeed2 == other._retractSpeed2;
        }

        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj)) return false;
            if (ReferenceEquals(this, obj)) return true;
            if (obj.GetType() != this.GetType()) return false;
            return Equals((OperationTimelapse) obj);
        }

        public override int GetHashCode()
        {
            var hashCode = new HashCode();
            hashCode.Add(_raisePositionZ);
            hashCode.Add(_outputDummyPixel);
            hashCode.Add(_raiseEachNthHeight);
            hashCode.Add((int) _raiseMode);
            hashCode.Add(_waitTimeAfterLift);
            hashCode.Add(_exposureTime);
            hashCode.Add(_useCustomLift);
            hashCode.Add(_slowLiftHeight);
            hashCode.Add(_liftSpeed);
            hashCode.Add(_liftSpeed2);
            hashCode.Add(_slowRetractHeight);
            hashCode.Add(_retractSpeed);
            hashCode.Add(_retractSpeed2);
            return hashCode.ToHashCode();
        }

        #endregion

        #region Methods

        public void OptimizeRaisePositionZ()
        {
            RaisePositionZ = (decimal) Math.Min(SlicerFile.MachineZ, SlicerFile.PrintHeight + 5);
        }

        public void MaxRaisePositionZ()
        {
            RaisePositionZ = (decimal) SlicerFile.MachineZ;
        }

        protected override bool ExecuteInternally(OperationProgress progress)
        {
            var virtualLayers = new List<uint>();
            float checkpointHeight = SlicerFile[0].PositionZ;
            for (uint layerIndex = LayerIndexStart; layerIndex <= LayerIndexEnd; layerIndex++)
            {
                progress++;
                var layer = SlicerFile[layerIndex];
                if (_raiseEachNthHeight > 0 && (decimal)Layer.RoundHeight(layer.PositionZ - checkpointHeight) < _raiseEachNthHeight) continue;
                if ((decimal)layer.PositionZ >= _raisePositionZ) break; // pass the target height, do not continue
                checkpointHeight = layer.PositionZ;

                switch (_raiseMode)
                {
                    case TimelapseRaiseMode.LiftHeight:
                        if (_useCustomLift)
                        {
                            layer.LiftSpeed = (float)_liftSpeed;
                            layer.RetractSpeed = (float)_retractSpeed;

                            if (SlicerFile.CanUseLayerLiftHeight2)
                            {
                                layer.LiftHeight = (float)_slowLiftHeight;
                            }

                            if (SlicerFile.CanUseLayerLiftSpeed2)
                            {
                                layer.LiftSpeed2 = (float)_liftSpeed2;
                            }

                            if (SlicerFile.CanUseLayerRetractHeight2)
                            {
                                layer.RetractHeight2 = (float)_slowRetractHeight;
                            }

                            if (SlicerFile.CanUseLayerRetractSpeed2)
                            {
                                layer.RetractSpeed2 = (float)_retractSpeed2;
                            }
                        }

                        if (SlicerFile.CanUseLayerLiftHeight2 && (layer.LiftHeight2 > 0 || _useCustomLift && _slowLiftHeight > 0)) // TSMC
                        {
                            layer.LiftHeight2 = Math.Max(0, (float)_raisePositionZ - layer.PositionZ - layer.LiftHeight);
                        }
                        else
                        {
                            layer.LiftHeightTotal = Math.Max(layer.LiftHeightTotal, (float)_raisePositionZ - layer.PositionZ);
                        }

                        if (SlicerFile.CanUseLayerWaitTimeAfterLift && _waitTimeAfterLift > 0)
                        {
                            layer.WaitTimeAfterLift = (float) _waitTimeAfterLift;
                        }

                        break;
                    case TimelapseRaiseMode.VirtualLayer:
                        virtualLayers.Add(layerIndex);
                        break;
                    default:
                        throw new ArgumentOutOfRangeException(nameof(RaiseMode));
                }
            }

            if (virtualLayers.Count > 0 && _raiseMode == TimelapseRaiseMode.VirtualLayer)
            {
                using var mat = _outputDummyPixel
                    ? SlicerFile.CreateMatWithDummyPixel()
                    : SlicerFile.CreateMat();

                var layer = new Layer(SlicerFile.LayerCount, mat, SlicerFile)
                {
                    PositionZ = (float) _raisePositionZ,
                    ExposureTime = (float) _exposureTime,
                    // This layer does not require a lift procedure
                    LiftHeightTotal = SlicerFile.SupportsGCode ? 0 : 0.1f, 
                    LiftSpeed = SlicerFile.MaximumSpeed,
                    LiftSpeed2 = SlicerFile.MaximumSpeed,
                    RetractSpeed = SlicerFile.MaximumSpeed,
                    RetractSpeed2 = SlicerFile.MaximumSpeed,
                    RetractHeight2 = 0
                };

                layer.SetNoDelays();

                /*if (_useCustomLift)
                {
                    layer.LiftSpeed = (float) _liftSpeed;
                    layer.RetractSpeed = (float) _retractSpeed;

                    if (SlicerFile.CanUseLayerLiftSpeed2)
                    {
                        layer.LiftSpeed2 = (float) _liftSpeed2;
                    }

                    if (SlicerFile.CanUseLayerRetractSpeed2)
                    {
                        layer.RetractSpeed2 = (float) _retractSpeed2;
                    }
                }*/

                var layers = SlicerFile.ToList();
                for (int i = 0; i < virtualLayers.Count; i++)
                {
                    /*var newLayer = layer.Clone();
                    if (!_useCustomLift)
                    {
                        var intersectLayer = Math.Clamp(virtualLayers[i] + i, 0, SlicerFile.LastLayerIndex);
                        SlicerFile[intersectLayer].CopyLiftTo(newLayer);
                        // This layer does not require a lift procedure
                        newLayer.LiftHeightTotal = SlicerFile.SupportsGCode ? 0 : 0.1f;
                        newLayer.RetractHeight2 = 0;
                    }*/
                    layers.Insert((int)(virtualLayers[i] + i), layer.Clone());
                }
                SlicerFile.SuppressRebuildPropertiesWork(() => SlicerFile.LayerManager.Layers = layers.ToArray());
            }

            return !progress.Token.IsCancellationRequested;
        }

        #endregion
    }
}