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

OperationCalculator.cs « Operations « UVtools.Core - github.com/sn4k3/UVtools.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2e85d33b065954ecd3c52a8ded239d8a05643dbb (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
/*
 *                     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.Drawing;
using UVtools.Core.Objects;

namespace UVtools.Core.Operations
{
    public class OperationCalculator : Operation
    {
        public override string Title => "Calculator";
        public override string Description => null;

        public override string ConfirmationText => null;

        public override string ProgressTitle => null;

        public override string ProgressAction => null;

        public override Enumerations.LayerRangeSelection LayerRangeSelection => Enumerations.LayerRangeSelection.None;
        public override bool CanROI => false;

        public MillimetersToPixels CalcMillimetersToPixels { get; set; }
        public LightOffDelayC CalcLightOffDelay { get; set; }

        public OperationCalculator()
        {
        }

        public abstract class Calculation : BindableBase
        {
            public abstract string Description { get; }
            public abstract string Formula { get; }
        }

        public sealed class MillimetersToPixels : Calculation
        {
            private uint _resolutionX;
            private uint _resolutionY;
            private decimal _displayWidth;
            private decimal _displayHeight;
            private decimal _millimeters = 1;

            public override string Description => "Converts from Millimeters to Pixels";
            public override string Formula => "Pixels = Resolution / Display * Millimeters";

            public MillimetersToPixels(Size resolution, SizeF display, decimal millimeters = 1)
            {
                _resolutionX = (uint) resolution.Width;
                _resolutionY = (uint) resolution.Height;

                _displayWidth = (decimal) display.Width;
                _displayHeight = (decimal) display.Height;

                _millimeters = millimeters;
            }

            public uint ResolutionX
            {
                get => _resolutionX;
                set
                {
                    if(!RaiseAndSetIfChanged(ref _resolutionX, value)) return;
                    RaisePropertyChanged(nameof(PixelsPerMillimeterX));
                    RaisePropertyChanged(nameof(PixelsX));
                }
            }

            public uint ResolutionY
            {
                get => _resolutionY;
                set
                {
                    if(!RaiseAndSetIfChanged(ref _resolutionY, value)) return;
                    RaisePropertyChanged(nameof(PixelsPerMillimeterY));
                    RaisePropertyChanged(nameof(PixelsY));
                }
            }

            public decimal DisplayWidth
            {
                get => _displayWidth;
                set
                {
                    if(!RaiseAndSetIfChanged(ref _displayWidth, value)) return;
                    RaisePropertyChanged(nameof(PixelsPerMillimeterX));
                    RaisePropertyChanged(nameof(PixelsX));
                }
            }

            public decimal DisplayHeight
            {
                get => _displayHeight;
                set
                {
                    if(!RaiseAndSetIfChanged(ref _displayHeight, value)) return;
                    RaisePropertyChanged(nameof(PixelsPerMillimeterY));
                    RaisePropertyChanged(nameof(PixelsY));
                }
            }

            public decimal Millimeters
            {
                get => _millimeters;
                set
                {
                    if(!RaiseAndSetIfChanged(ref _millimeters, value)) return;
                    RaisePropertyChanged(nameof(PixelsX));
                    RaisePropertyChanged(nameof(PixelsY));
                }
            }

            public decimal PixelsPerMillimeterX => DisplayWidth > 0 ? Math.Round(ResolutionX / DisplayWidth, 3) : 0;
            public decimal PixelsPerMillimeterY => DisplayHeight > 0 ? Math.Round(ResolutionY / DisplayHeight, 3) : 0;

            public decimal PixelsX => PixelsPerMillimeterX * Millimeters;
            public decimal PixelsY => PixelsPerMillimeterY * Millimeters;

            
        }

        public sealed class LightOffDelayC : Calculation
        {
            private decimal _liftHeight;
            private decimal _bottomLiftHeight;
            private decimal _liftSpeed;
            private decimal _bottomLiftSpeed;
            private decimal _retractSpeed;
            private decimal _bottomRetractSpeed;
            private decimal _waitTime = 2.5m;
            private decimal _bottomWaitTime = 3m;

            public override string Description =>
                "Calculates the required light-off delay (Moving time from the build plate + additional time for resin to stabilize) given The lifting height, speed and retract to wait x seconds before cure a new layer.\n" +
                "Light-off delay is crucial for gaining higher-resolution and sharper prints.\n" +
                "When the build plate retracts, it is important to allow enough time for the resin to stabilize before the UV lights turn on. This would ideally be around 2-3s.";

            public override string Formula => "Light-off delay = Lifting height / (Lifting speed / 60) + Lifting height / (Retract speed / 60) + Desired wait seconds";

            public decimal LiftHeight
            {
                get => _liftHeight;
                set
                {
                    if(!RaiseAndSetIfChanged(ref _liftHeight, value)) return;
                    RaisePropertyChanged(nameof(LightOffDelay));
                }
            }

            public decimal BottomLiftHeight
            {
                get => _bottomLiftHeight;
                set
                {
                    if(!RaiseAndSetIfChanged(ref _bottomLiftHeight, value)) return;
                    RaisePropertyChanged(nameof(BottomLightOffDelay));
                }
            }

            public decimal LiftSpeed
            {
                get => _liftSpeed;
                set
                {
                    if(!RaiseAndSetIfChanged(ref _liftSpeed, value)) return;
                    RaisePropertyChanged(nameof(LightOffDelay));
                }
            }

            public decimal BottomLiftSpeed
            {
                get => _bottomLiftSpeed;
                set
                {
                    if(!RaiseAndSetIfChanged(ref _bottomLiftSpeed, value)) return;
                    RaisePropertyChanged(nameof(BottomLightOffDelay));
                }
            }

            public decimal RetractSpeed
            {
                get => _retractSpeed;
                set
                {
                    if(!RaiseAndSetIfChanged(ref _retractSpeed, value)) return;
                    RaisePropertyChanged(nameof(LightOffDelay));
                }
            }

            public decimal BottomRetractSpeed
            {
                get => _bottomRetractSpeed;
                set
                {
                    if (!RaiseAndSetIfChanged(ref _bottomRetractSpeed, value)) return;
                    RaisePropertyChanged(nameof(BottomLightOffDelay));
                }
            }

            public decimal WaitTime
            {
                get => _waitTime;
                set
                {
                    if(!RaiseAndSetIfChanged(ref _waitTime, value)) return;
                    RaisePropertyChanged(nameof(LightOffDelay));
                }
            }

            public decimal BottomWaitTime
            {
                get => _bottomWaitTime;
                set
                {
                    if (!RaiseAndSetIfChanged(ref _bottomWaitTime, value)) return;
                    RaisePropertyChanged(nameof(BottomLightOffDelay));
                }
            }

            public decimal LightOffDelay => Calculate(_liftHeight, _liftSpeed, _retractSpeed, _waitTime);

            public decimal BottomLightOffDelay => Calculate(_bottomLiftHeight, _bottomLiftSpeed, _bottomRetractSpeed, _bottomWaitTime);

            public LightOffDelayC()
            {
            }

            public LightOffDelayC(decimal liftHeight, decimal bottomLiftHeight, decimal liftSpeed, decimal bottomLiftSpeed, decimal retractSpeed, decimal bottomRetractSpeed, decimal waitTime = 2.5m, decimal bottomWaitTime = 3m)
            {
                _liftHeight = liftHeight;
                _bottomLiftHeight = bottomLiftHeight;
                _liftSpeed = liftSpeed;
                _bottomLiftSpeed = bottomLiftSpeed;
                _retractSpeed = retractSpeed;
                _bottomRetractSpeed = bottomRetractSpeed;
                _waitTime = waitTime;
                _bottomWaitTime = bottomWaitTime;
            }

            public static decimal Calculate(decimal liftHeight, decimal liftSpeed, decimal retract, decimal waitTime = 0)
            {
                try
                {
                    return Math.Round(liftHeight / (liftSpeed / 60m) + liftHeight / (retract / 60m) + waitTime, 2);
                }
                catch (Exception)
                {
                    return 0;
                }
            }

            public static float Calculate(float liftHeight, float liftSpeed, float retract, float waitTime = 0)
            {
                try
                {
                    return (float) Math.Round(liftHeight / (liftSpeed / 60f) + liftHeight / (retract / 60f) + waitTime, 2);
                }
                catch (Exception)
                {
                    return 0;
                }
                
            }
        }
    }
}