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: 7b378adb4533b3fed0df7bd79ad57d886324454d (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
/*
 *                     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 Emgu.CV;
using UVtools.Core.Extensions;
using UVtools.Core.FileFormats;
using UVtools.Core.Layers;

namespace UVtools.Core.Operations
{
    [Serializable]
    public class OperationRaiseOnPrintFinish : Operation
    {
        #region Constants
        public const byte DummyPixelBrightness = 128;
        #endregion

        #region Members
        private decimal _positionZ;

        private bool _outputDummyPixel = true;
        #endregion

        #region Overrides
        public override Enumerations.LayerRangeSelection StartLayerRangeSelection => Enumerations.LayerRangeSelection.None;

        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.CanUseLayerLiftHeight)
            {
                return NotSupportedMessage;
            }

            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] [Dummy pixel: {_outputDummyPixel}]";
            if (!string.IsNullOrEmpty(ProfileName)) result = $"{ProfileName}: {result}";
            return result;
        }
        #endregion

        #region Properties

        public float MinimumPositionZ => Layer.RoundHeight(SlicerFile.PrintHeight + SlicerFile.LayerHeight);

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

        /// <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) 
        {
            if (_positionZ <= 0) _positionZ = (decimal)SlicerFile.MachineZ;
        }

        #endregion

        #region Equality

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

        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);
        }

        #endregion

        #region Methods

        protected override bool ExecuteInternally(OperationProgress progress)
        {
            return Execute(null, null);
            //return !progress.Token.IsCancellationRequested;
        }

        public override bool Execute(Mat mat, params object[] arguments)
        {
            var layer = SlicerFile.LastLayer.Clone();
            layer.PositionZ = (float)_positionZ;
            layer.ExposureTime = 0.05f; // Very low exposure time
            layer.LightPWM = 0; // Try to disable light if possible
            layer.SetNoDelays();
            using var newMat = EmguExtensions.InitMat(SlicerFile.Resolution);
            if(_outputDummyPixel)
            {
                newMat.SetByte(newMat.GetPixelPos(layer.BoundingRectangle.Center()), DummyPixelBrightness);
            }

            layer.LayerMat = newMat;

            SlicerFile.SuppressRebuildPropertiesWork(() =>
            {
                SlicerFile.LayerManager.Append(layer);
                return true;
            });
            
            return true;
        }

        #endregion
    }
}