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

OperationRaiseOnPrintFinish.cs « Operations « UVtools.Core - github.com/sn4k3/UVtools.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 65c9e6968f53083d70ef78843b734b0b03d2cae9 (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
/*
 *                     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.Text;
using UVtools.Core.Extensions;
using UVtools.Core.FileFormats;
using UVtools.Core.Layers;

namespace UVtools.Core.Operations;

[Serializable]
public class OperationRaiseOnPrintFinish : Operation
{
    #region Constants
    #endregion

    #region Members
    private decimal _positionZ;
    private decimal _waitTime = 1200; // 20m
    private bool _outputDummyPixel = true;

    #endregion

    #region Overrides

    public override LayerRangeSelection StartLayerRangeSelection => LayerRangeSelection.None;
    public override string IconClass => "fa-solid fa-level-up-alt";
    public override string Title => "Raise platform on print finish";
    public override string Description =>
        "Raise the build platform to a set position after finish the print.\n\n" +
        "NOTE: Only use this tool once and if your printer firmware don't already raise the build platform after finish the print.\n" +
        "This will create a \"empty\" layer on end to simulate a print at a defined height.\n" +
        "Not compatible with all printers, still it won't cause any harm if printer don't support this strategy.";

    public override string ConfirmationText =>
        $"raise the platform on print finish to Z={_positionZ}mm";

    public override string ProgressTitle =>
        $"Inserting dummy layer on end";

    public override string ProgressAction => "Inserted layer";

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

        if (SlicerFile.LayerCount >= 2)
        {
            var layerHeight = SlicerFile.LastLayer!.LayerHeight;
            var criteria = Math.Max((float) Layer.MaximumHeight, SlicerFile.LayerHeight);

            if (layerHeight > criteria)
            {
                return $"With a difference of {layerHeight}mm between the last two layers, it looks like this tool had already been applied.\n" +
                       $"The difference must be less or equal to {criteria}mm in order to run this tool.";
            }
        }

        return null;
    }

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

        if (!ValidateSpawn(out var message))
        {
            sb.AppendLine(message);
        }
        if((float)_positionZ < SlicerFile.PrintHeight)
        {
            sb.AppendLine($"Can't raise to {_positionZ}mm, because it's below the maximum print height of {SlicerFile.PrintHeight}mm.");
        }
        else if ((float)_positionZ == SlicerFile.PrintHeight)
        {
            sb.AppendLine($"Raise to {_positionZ}mm will have no effect because it's the same height as last layer of {SlicerFile.PrintHeight}mm.");
        }
            
        return sb.ToString();
    }

    public override string ToString()
    {
        var result = $"[Z={_positionZ}mm] [Wait: {_waitTime}s] [Dummy pixel: {_outputDummyPixel}]";
        if (!string.IsNullOrEmpty(ProfileName)) result = $"{ProfileName}: {result}";
        return result;
    }
    #endregion

    #region Properties

    public float MinimumPositionZ => Layer.RoundHeight(SlicerFile.PrintHeight + SlicerFile.LayerHeight);
    public float MediumPositionZ => Layer.RoundHeight(MinimumPositionZ + (MaximumPositionZ - MinimumPositionZ) / 2);
    public float MaximumPositionZ => Math.Max(MinimumPositionZ, Layer.RoundHeight(SlicerFile.MachineZ));

    /// <summary>
    /// Gets or sets the Z position to raise to
    /// </summary>
    public decimal PositionZ
    {
        get => _positionZ;
        set => RaiseAndSetIfChanged(ref _positionZ, Layer.RoundHeight(value));
    }

    /// <summary>
    /// <para>Gets or sets the ensured wait time to stay still on the desired position.</para>
    /// <para>This is useful if the printer firmware always move to top and you want to stay still on the set position for at least the desired time.</para>
    /// <para>Note: The print time calculation will take this wait into consideration and display a longer print time.</para>
    /// </summary>
    public decimal WaitTime
    {
        get => _waitTime;
        set => RaiseAndSetIfChanged(ref _waitTime, Math.Round(Math.Max(0, value), 2));
    }

    /// <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); 
    }
    #endregion

    #region Constructor

    public OperationRaiseOnPrintFinish() 
    {
        //_outputDummyPixel = !SlicerFile.SupportsGCode;
    }

    public OperationRaiseOnPrintFinish(FileFormat slicerFile) : base(slicerFile)
    { }

    public override void InitWithSlicerFile()
    {
        if (_positionZ <= 0) _positionZ = (decimal)SlicerFile.MachineZ;
    }

    #endregion

    #region Equality

    protected bool Equals(OperationRaiseOnPrintFinish other)
    {
        return _positionZ == other._positionZ && _outputDummyPixel == other._outputDummyPixel && _waitTime == other._waitTime;
    }

    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((OperationRaiseOnPrintFinish) obj);
    }

    public override int GetHashCode()
    {
        return HashCode.Combine(_positionZ, _outputDummyPixel, _waitTime);
    }

    #endregion

    #region Methods

    public void SetToMinimumPosition() => PositionZ = (decimal)MinimumPositionZ;
    public void SetToMediumPosition() => PositionZ = (decimal)MediumPositionZ;
    public void SetToMaximumPosition() => PositionZ = (decimal)MaximumPositionZ;

    public void SetWaitTime(decimal time) => WaitTime = time;

    protected override bool ExecuteInternally(OperationProgress progress)
    {
        var layer = SlicerFile.LastLayer!.Clone();
        layer.PositionZ = (float)_positionZ;
        layer.ExposureTime = SlicerFile.SupportsGCode ? 0 : 0.05f; // Very low exposure time
        layer.LightPWM = 0; // Try to disable light if possible
        layer.SetNoDelays();
        using var newMat = _outputDummyPixel 
            ? SlicerFile.CreateMatWithDummyPixel(layer.BoundingRectangle.Center())
            : SlicerFile.CreateMat();
               
        layer.LayerMat = newMat;

        if(_waitTime > 0) layer.SetWaitTimeBeforeCureOrLightOffDelay((float)_waitTime);

        SlicerFile.SuppressRebuildPropertiesWork(() =>
        {
            SlicerFile.Append(layer);
        });
        return true;
        //return !progress.Token.IsCancellationRequested;
    }
    #endregion
}