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

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

namespace UVtools.ScriptSample;

/// <summary>
/// Change layer properties to random values
/// </summary>
public class ScriptCustomGCode : ScriptGlobals
{
    /// <summary>
    /// Set configurations here, this function trigger just after load a script
    /// </summary>
    public void ScriptInit()
    {
        Script.Name = "Custo gcode generator";
        Script.Description = "Generates custom gcode and saves the file";
        Script.Author = "Tiago Conceição";
        Script.Version = new Version(0, 1);
    }

    /// <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 SlicerFile.SupportsGCode ? null : "GCode is not supported on this file";
    }

    /// <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()
    {
        var gcode = SlicerFile.GCode!;
        gcode.Clear();

        float pos = 1;
        //float layerHeight = 0.025f;
        //float liftHeight = 4.5f;
        float feedrate = gcode.ConvertFromMillimetersPerMinute(150);
        float lightoff = gcode.ConvertFromSeconds(1f);

        gcode.AppendStartGCode();
        //gcode.AppendShowImageM6054(gcode.GetShowImageString(0));
        //gcode.AppendWaitG4(gcode.ConvertFromSeconds(2));
        //gcode.AppendTurnLightM106(255);
        gcode.AppendWaitG4(gcode.ConvertFromSeconds(1));
        //gcode.AppendTurnLightM106(0);
        gcode.AppendLiftMoveG0(20, feedrate, pos, feedrate);
        gcode.AppendWaitG4(gcode.ConvertFromSeconds(5));

        // 0.025 test
        /*gcode.AppendComment("0.025 layer height simulated print test");
        for (int i = 0; i < 50; i++)
        {
            pos = Layer.RoundHeight(pos + layerHeight);
            var liftPos = Layer.RoundHeight(pos + liftHeight);
            gcode.AppendLiftMoveG0(liftPos, feedrate, pos, feedrate, lightoff);
        }*/

        // 0.01 test
        /*gcode.AppendComment("0.01 layer height simulated print test");
        pos = 1;
        layerHeight = 0.01f;


        gcode.AppendMoveG0(pos, feedrate);
        gcode.AppendWaitG4(gcode.ConvertFromSeconds(5));
        for (int i = 0; i < 50; i++)
        {
            pos = Layer.RoundHeight(pos + layerHeight);
            var liftPos = Layer.RoundHeight(pos + liftHeight);
            gcode.AppendLiftMoveG0(liftPos, feedrate, pos, feedrate, lightoff);
        }*/

        // 0.001 test
        /*gcode.AppendComment("0.001 layer height simulated print test");
        pos = 1;
        layerHeight = 0.001f;
        liftHeight = 1;

        gcode.AppendMoveG0(pos, feedrate);
        gcode.AppendWaitG4(gcode.ConvertFromSeconds(5));
        for (int i = 0; i < 50; i++)
        {
            pos = Layer.RoundHeight(pos + layerHeight);
            var liftPos = Layer.RoundHeight(pos + liftHeight);
            gcode.AppendLiftMoveG0(liftPos, feedrate, pos, feedrate, lightoff);
        }*/


        /*// 0.05 backlash test
        gcode.AppendComment("0.05 backlash test");
        pos = 1;
        layerHeight = 0.02f;

        gcode.AppendMoveG0(pos, feedrate);
        //gcode.AppendWaitG4(gcode.ConvertFromSeconds(5));
        for (int i = 0; i < 50; i++)
        {
            var liftPos = Layer.RoundHeight(pos + layerHeight);
            gcode.AppendMoveG0(liftPos, feedrate);
            gcode.AppendWaitG4(lightoff);
            gcode.AppendMoveG0(pos, feedrate);
            gcode.AppendWaitG4(lightoff);
        }
        */

        gcode.AppendLiftMoveG0(20, gcode.ConvertFromMillimetersPerMinute(150), 1, gcode.ConvertFromMillimetersPerMinute(150));
        gcode.AppendWaitG4(lightoff);
        gcode.AppendLiftMoveG0(20, gcode.ConvertFromMillimetersPerMinute(180), 1.5f, gcode.ConvertFromMillimetersPerMinute(180));
        gcode.AppendWaitG4(lightoff);
        gcode.AppendLiftMoveG0(20, gcode.ConvertFromMillimetersPerMinute(195), 2, gcode.ConvertFromMillimetersPerMinute(195));
        gcode.AppendWaitG4(lightoff);
        gcode.AppendLiftMoveG0(20, gcode.ConvertFromMillimetersPerMinute(200), 2.5f, gcode.ConvertFromMillimetersPerMinute(200));
        gcode.AppendWaitG4(lightoff);
        gcode.AppendLiftMoveG0(20, gcode.ConvertFromMillimetersPerMinute(250), 3f, gcode.ConvertFromMillimetersPerMinute(250));
        gcode.AppendWaitG4(lightoff);
        gcode.AppendLiftMoveG0(20, gcode.ConvertFromMillimetersPerMinute(300), 3.5f, gcode.ConvertFromMillimetersPerMinute(300));
        gcode.AppendWaitG4(lightoff);

        gcode.AppendMoveG0(100, feedrate);
        //gcode.AppendTurnMotors(false);



        SlicerFile.SuppressRebuildGCode = true;
        SlicerFile.Save(Progress);
        SlicerFile.SuppressRebuildGCode = false;

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