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

ScriptVATClean.cs « UVtools.ScriptSample - github.com/sn4k3/UVtools.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 065c86e28d6989f541d833d1e0285face13c8428 (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
/*
 *                     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 Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Structure;
using UVtools.Core.Extensions;
using UVtools.Core.Scripting;

namespace UVtools.ScriptSample;

/// <summary>
/// Change layer properties to random values
/// </summary>
public class ScriptVATClean : ScriptGlobals
{
    private readonly ScriptNumericalInput<ushort> InputInset = new()
    {
        Label = "Resolution inset",
        ToolTip = "Inset image resolution by this value to create a black border",
        Unit = "px",
        Minimum = 0,
        Maximum = ushort.MaxValue,
        Increment = 1
    };

    private readonly ScriptNumericalInput<float> InputExposureTime = new()
    {
        Label = "Exposure time",
        ToolTip = "Time to exposure the layer",
        Unit = "s",
        Minimum = 0,
        Maximum = 50,
        DecimalPlates = 2,
        Increment = 1
    };

    /// <summary>
    /// Set configurations here, this function trigger just after load a script
    /// </summary>
    public void ScriptInit()
    {
        Script.Name = "Create a file to clean VAT exposing 1 layer";
        Script.Description = "Print this file to clean your VAT by exposing 1 layer and peel it off.\n" +
                             "1) Load a file for your printer that you previous printed into UVtools\n" +
                             "2) Configure and run this script\n" +
                             "3) Go to File -> Save As, and give it a new name\n" +
                             "4) Remove head/plate\n" +
                             "5) Place a plastic spatula in the VAT at an angle with the handle laying on the top of the VAT frame\n" +
                             "6) Print the created file\n" +
                             "7) When print finish slowly peel the layer with the spatula";
        Script.Author = "Tiago Conceição";
        Script.Version = new Version(0, 2);
        Script.MinimumVersionToRun = new Version(3, 0, 0);


        InputInset.Maximum = (ushort) (Math.Max(SlicerFile.ResolutionX, SlicerFile.ResolutionY) / 2 - 2);
        InputExposureTime.Value = SlicerFile.ExposureTime * 2;

        Script.UserInputs.Add(InputInset);
        Script.UserInputs.Add(InputExposureTime);
    }

    /// <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()
    {
        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()
    {
        Progress.Reset("Generating layers", 1); // Sets the progress name and number of items to process

        var layer = SlicerFile[0];
        layer.PositionZ = SlicerFile.MachineZ;          // Send head to top if possible

        using var mat = EmguExtensions.InitMat(SlicerFile.Resolution);
        CvInvoke.Rectangle(mat, new Rectangle(
            new Point(InputInset.Value, InputInset.Value),
            new Size((int) (SlicerFile.ResolutionX - InputInset.Value*2)-1, (int) (SlicerFile.ResolutionY - InputInset.Value*2)-1)
        ), EmguExtensions.WhiteColor, -1, LineType.FourConnected);
        layer.LayerMat = mat;

        SlicerFile.SuppressRebuildPropertiesWork(() =>
        {
            SlicerFile.BottomLayerCount = 1;
                
            SlicerFile.Layers = new[] { layer };
        });

        SlicerFile.BottomExposureTime =
            SlicerFile.ExposureTime = InputExposureTime.Value;

        SlicerFile.BottomLiftSpeed =
            SlicerFile.LiftSpeed =
                SlicerFile.RetractSpeed = 200;

        SlicerFile.BottomLiftHeight =
            SlicerFile.LiftHeight = 1;

        Progress++;

        SlicerFile.SetThumbnails(GetThumbnail());
            
        // return true if not cancelled by user
        return !Progress.Token.IsCancellationRequested;
    }

    public Mat GetThumbnail()
    {
        Mat thumbnail = EmguExtensions.InitMat(new Size(400, 200), 3);
        var fontFace = FontFace.HersheyDuplex;
        var fontScale = 1;
        var fontThickness = 2;
        const byte xSpacing = 45;
        const byte ySpacing = 45;
        CvInvoke.PutText(thumbnail, "UVtools", new Point(140, 35), fontFace, fontScale, new MCvScalar(255, 27, 245), fontThickness + 1);
        CvInvoke.Line(thumbnail, new Point(xSpacing, 0), new Point(xSpacing, ySpacing + 5), new MCvScalar(255, 27, 245), 3);
        CvInvoke.Line(thumbnail, new Point(xSpacing, ySpacing + 5), new Point(thumbnail.Width - xSpacing, ySpacing + 5), new MCvScalar(255, 27, 245), 3);
        CvInvoke.Line(thumbnail, new Point(thumbnail.Width - xSpacing, 0), new Point(thumbnail.Width - xSpacing, ySpacing + 5), new MCvScalar(255, 27, 245), 3);
        CvInvoke.PutText(thumbnail, "VAT Clean Utility", new Point(xSpacing, ySpacing * 2), fontFace, fontScale, new MCvScalar(0, 255, 255), fontThickness);
        CvInvoke.PutText(thumbnail, $"Exposure time: {SlicerFile.ExposureTime}s", new Point(xSpacing, ySpacing * 3), fontFace, fontScale, EmguExtensions.WhiteColor, fontThickness);
        CvInvoke.PutText(thumbnail, $"Use the spatula in!", new Point(xSpacing, ySpacing * 4), fontFace, fontScale, EmguExtensions.WhiteColor, fontThickness);

        return thumbnail;
    }
}