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

ScriptTimelapseSample.cs « UVtools.ScriptSample - github.com/sn4k3/UVtools.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1f7ceb91f94422a2b07ae9f4693df3cd438e3f06 (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
/*
 *                     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 UVtools.Core.Extensions;
using UVtools.Core.Layers;
using UVtools.Core.Scripting;

namespace UVtools.ScriptSample;

/// <summary>
/// Change layer properties to random values
/// </summary>
public class ScriptTimelapseSample : ScriptGlobals
{
    readonly ScriptNumericalInput<float> InputPositionZ = new()
    {
        Label = "Z position to lift to",
        Unit = "mm",
        Minimum = 0.01f,
        Maximum = 1000,
        Increment = 1f,
        Value = 0.5f,
        DecimalPlates = 2
    };

    readonly ScriptNumericalInput<ushort> InputRaiseEveryLayerN = new()
    {
        Label = "Raise every",
        Unit = "layer(s)",
        Minimum = 1,
        Maximum = 1000,
        Increment = 1,
        Value = 10,
        DecimalPlates = 0
    };

    readonly ScriptNumericalInput<float> InputWaitTime = new()
    {
        Label = "Time to wait on still position",
        Unit = "s",
        ToolTip = "Note: Not always possible to wait in some cases",
        Minimum = 0,
        Maximum = 30,
        Increment = 1,
        Value = 2,
        DecimalPlates = 2
    };

    readonly ScriptToggleSwitchInput InputUseVirtualLayer = new()
    {
        OnText = "Use blank layers to go to the target height",
        OffText = "Use lift movement to go to the target height",
        ToolTip = "Use this option if you printer is unable to use large lifts or waits after lift"
    };

    readonly ScriptNumericalInput<float> InputLiftSpeed = new()
    {
        Label = "Virtual layer lift speed",
        Unit = "mm/min",
        Minimum = 50,
        Maximum = 1000,
        Increment = 10,
        Value = 200,
        DecimalPlates = 2
    };

    readonly ScriptNumericalInput<float> InputRetractSpeed = new()
    {
        Label = "Virtual layer retract speed",
        Unit = "mm/min",
        Minimum = 50,
        Maximum = 1000,
        Increment = 10,
        Value = 200,
        DecimalPlates = 2
    };

    /// <summary>
    /// Set configurations here, this function trigger just after load a script
    /// </summary>
    public void ScriptInit()
    {
        Script.Name = "Timelapse position setter";
        Script.Description = "Raises the build platform to a set position to take a timelapse photo every n layers.\n" +
                             "Do not execute this script twice!";
        Script.Author = "Tiago Conceição";
        Script.Version = new Version(0, 1);
        Script.MinimumVersionToRun = new Version(3, 0, 0);

        InputPositionZ.Value = (float)Math.Round(SlicerFile.PrintHeight + 1, 2);
        InputPositionZ.Minimum = (float) Math.Round(SlicerFile.PrintHeight + 0.1, 2);
        Script.UserInputs.Add(InputPositionZ);
        Script.UserInputs.Add(InputRaiseEveryLayerN);
        Script.UserInputs.Add(InputWaitTime);

        if (!SlicerFile.SupportsGCode)
        {
            InputUseVirtualLayer.Value = true;
        }

        if (SlicerFile.CanUseLayerLiftHeight)
        {
            Script.UserInputs.Add(InputUseVirtualLayer);
        }
        else
        {
            InputUseVirtualLayer.Value = true; // Must use layer height
        }

        Script.UserInputs.Add(InputLiftSpeed);
        Script.UserInputs.Add(InputRetractSpeed);
    }

    /// <summary>
    /// Validate user inputs here, this function trigger when user click on execute
    /// </summary>
    /// <returns>A error message, empty or null if validation passes.</returns>
    public string? ScriptValidate()
    {
        if (!SlicerFile.SupportPerLayerSettings) return "This script is not compatible with your printer / file format";
        if (InputPositionZ.Value <= SlicerFile.PrintHeight) return $"{InputPositionZ.Label} must be greater than {SlicerFile.PrintHeight}mm";
        return null;
    }

    /// <summary>
    /// Execute the script, this function trigger when when user click on execute and validation passes
    /// </summary>
    /// <returns>True if executes successfully to the end, otherwise false.</returns>
    public bool ScriptExecute()
    {
        if (InputUseVirtualLayer.Value)
        {
            using var mat = EmguExtensions.InitMat(SlicerFile.Resolution);
            var pixelPos = SlicerFile.BoundingRectangle.Center();
            mat.SetByte(pixelPos.X, pixelPos.Y, 1); // Print a very fade pixel to ignore empty layer detection
            var layer = new Layer(SlicerFile.LayerCount, mat, SlicerFile)
            {
                PositionZ = InputPositionZ.Value,
                ExposureTime = SlicerFile.SupportsGCode ? 0 : 0.05f,
                LiftSpeed = InputLiftSpeed.Value,
                RetractSpeed = InputRetractSpeed.Value
            };

            if (InputWaitTime.Value > 0)
            {
                if (SlicerFile.CanUseWaitTimeBeforeCure)
                {
                    layer.WaitTimeBeforeCure = InputWaitTime.Value;
                }
                else
                {
                    layer.ExposureTime = InputWaitTime.Value; 
                }
            }


            SlicerFile.SuppressRebuildPropertiesWork(() =>
            {
                uint createdLayers = 0;
                for (uint layerIndex = Math.Max(1, Operation.LayerIndexStart + InputRaiseEveryLayerN.Value); layerIndex <= Operation.LayerIndexEnd; layerIndex += InputRaiseEveryLayerN.Value)
                {
                    SlicerFile.Insert((int)(layerIndex + createdLayers), layer.Clone());
                    createdLayers++;
                    Progress.ProcessedItems = layerIndex;
                }
            });
                
        }
        else
        {
            for (uint layerIndex = Math.Max(1, Operation.LayerIndexStart + InputRaiseEveryLayerN.Value - 1); layerIndex <= Operation.LayerIndexEnd; layerIndex += InputRaiseEveryLayerN.Value)
            {
                var layer = SlicerFile[layerIndex];
                layer.LiftHeightTotal = Math.Max(SlicerFile.LiftHeightTotal, InputPositionZ.Value - layer.PositionZ);
                if (SlicerFile.CanUseLayerWaitTimeAfterLift && InputWaitTime.Value > 0)
                {
                    layer.WaitTimeAfterLift = InputWaitTime.Value;
                }
                Progress.ProcessedItems = layerIndex;
            }
        }

        // return true if not cancelled by user
        return !Progress.Token.IsCancellationRequested;
    }
}