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

ScriptCloneSettings.cs « UVtools.ScriptSample - github.com/sn4k3/UVtools.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a9435223e17cb9b7a8313eed75228404584e1f70 (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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
/*
 *                     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 System.Collections.Generic;
using UVtools.Core.FileFormats;
using System.Diagnostics;
using System.Linq;

namespace UVtools.ScriptSample;

/// <summary>
/// Performs a black inset around objects
/// </summary>
public class ScriptCloneSettings : ScriptGlobals
{
    readonly ScriptCheckBoxInput Recursive = new()
    {
        Label = "Recursive",
        ToolTip = "If unchecked, only files in the same folder are modified; if checked, files in all sub-folders are modified too",
        Value = true
    };

    readonly ScriptCheckBoxInput Report = new()
    {
        Label = "Generate report file",
        ToolTip = "Optionally generate a report file in the same directory as the open file",
        Value = true
    };

    readonly ScriptCheckBoxInput OpenReport = new()
    {
        Label = "Open report file",
        ToolTip = "Optionally open the result file when completed",
        Value = true
    };

    readonly ScriptOpenFolderDialogInput FolderPath = new()
    {
        Label = "Folder path",
        ToolTip = "The folder path to process",
    };

    private enum ResultStatus
    {
        Exception,
        UnknownFileType,
        DifferentMachineTypes,
        Success,
        Unchanged,
    }

    /// <summary>
    /// Set configurations here, this function trigger just after load a script
    /// </summary>
    public void ScriptInit()
    {
        Script.Name = "Clone Settings";
        Script.Description = "Copies the print settings from the current file to all files in the same directory and, optionally, recursively below it";
        Script.Author = "Gina Venolia";
        Script.Version = new Version(0, 2);
        Script.UserInputs.Add(Recursive);
        Script.UserInputs.Add(Report);
        Script.UserInputs.Add(OpenReport);
        Script.UserInputs.Add(FolderPath);
        FolderPath.Value = SlicerFile.DirectoryPath;
    }

    /// <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.CanCancel = true;

        // Gather the list of directories to operate on
        Progress.Reset("Processing files...");

        var directoryPaths = new List<string> {Path.GetDirectoryName(SlicerFile.FileFullPath)!};

        if (Recursive.Value)
        {
            for (var i = 0; i < directoryPaths.Count; i++)
            {
                directoryPaths.AddRange(Directory.EnumerateDirectories(directoryPaths[i]));
            }
        }

        // Gather the list of files to operate on
        var filePaths = new List<string>();
        foreach (var directoryPath in directoryPaths)
        {
            filePaths.AddRange(Directory.EnumerateFiles(directoryPath));
        }

        // Except for the file we started with
        var normalizedFilePath = Path.GetFullPath(SlicerFile.FileFullPath!);
        filePaths.RemoveAll(x => Path.GetFullPath(x) == normalizedFilePath);

        // Process the files
        Progress.Reset("Processing files...", (uint)filePaths.Count, 0);

        var results = new List<Tuple<ResultStatus, string, List<string>?>>();
        foreach (var filePath in filePaths)
        {
            Progress.ThrowIfCancellationRequested();

            Tuple<ResultStatus, string, List<string>?> result;
            try
            {
                result = ProcessFile(filePath);
            }
            catch (Exception ex)
            {
                var deets = new List<string>
                {
                    ex.GetType().Name,
                    ex.Message
                };
                result = new Tuple<ResultStatus, string, List<string>?>(ResultStatus.Exception, filePath, deets);
            }

            results.Add(result);

            Progress++;
        }

        // Generate the report
        if (Report.Value)
        {
            var reportDirectory = Path.GetDirectoryName(SlicerFile.FileFullPath);
            var reportFilePath = Path.Combine(reportDirectory!, "CloneSettingsReport.txt");
            using (var writer = new StreamWriter(reportFilePath))
            {
                writer.WriteLine(Script.Name + " " + Script.Version.ToString() + " by " + Script.Author);
                writer.WriteLine("Source file: " + SlicerFile.FileFullPath);
                writer.WriteLine(DateTime.Now.ToLongDateString() + ", " + DateTime.Now.ToLongTimeString());
                writer.WriteLine("Recursive: " + Recursive.Value.ToString());
                writer.WriteLine("Directories: " + directoryPaths.Count);
                writer.WriteLine("Files: " + filePaths.Count);
                writer.WriteLine();

                foreach (var statusGroup in results.GroupBy(x => x.Item1))
                {
                    writer.WriteLine(statusGroup.Key.ToString());
                    foreach (var result in statusGroup.OrderBy(x => x.Item2))
                    {
                        writer.WriteLine("\t" + result.Item2);
                        if (result.Item3 is not null)
                        {
                            foreach (var detail in result.Item3)
                            {
                                writer.WriteLine("\t\t" + detail);
                            }
                        }
                    }
                }
            }

            if (OpenReport.Value)
            {
                var startInfo = new ProcessStartInfo(reportFilePath)
                {
                    UseShellExecute = true
                };
                Process.Start(startInfo);
            }
        }

        // return true if not cancelled by user
        return true;
    }

    private Tuple<ResultStatus, string, List<string>?> ProcessFile(string filePath)
    {
        // Determine the type of file
        var file = FileFormat.FindByExtensionOrFilePath(filePath, true);
        if (file is null)
        {
            return new Tuple<ResultStatus, string, List<string>?>(ResultStatus.UnknownFileType, filePath, null);
        }

        // Load the file
        file.Decode(filePath);
        if (string.Compare(file.MachineName, SlicerFile.MachineName, true) != 0)
        {
            var deets = new List<string>
            {
                "Source machine type: " + SlicerFile.MachineName,
                "Destination machine type: " + file.MachineName
            };
            return new Tuple<ResultStatus, string, List<string>?>(ResultStatus.DifferentMachineTypes, filePath, deets);
        }

        // TODO: Validate that some parameters are the same in both files?

        // Change the parameters
        var changed = false;
        var details = new List<string>();

        if (file.CanUseBottomLayerCount && file.BottomLayerCount != SlicerFile.BottomLayerCount)
        {
            details.Add($"Bottom Layer Count: {file.BottomLayerCount} to {SlicerFile.BottomLayerCount}");
            file.BottomLayerCount = SlicerFile.BottomLayerCount;
            changed = true;
        }

        if (file.CanUseBottomLightOffDelay && file.BottomLightOffDelay != SlicerFile.BottomLightOffDelay)
        {
            details.Add($"Bottom Light Off Delay: {file.BottomLightOffDelay} to {SlicerFile.BottomLightOffDelay}");
            file.BottomLightOffDelay = SlicerFile.BottomLightOffDelay;
            changed = true;
        }

        if (file.CanUseLightOffDelay && file.LightOffDelay != SlicerFile.LightOffDelay)
        {
            details.Add($"Light Off Delay: {file.LightOffDelay} to {SlicerFile.LightOffDelay}");
            file.LightOffDelay = SlicerFile.LightOffDelay;
            changed = true;
        }

        if (file.CanUseBottomWaitTimeBeforeCure && file.BottomWaitTimeBeforeCure != SlicerFile.BottomWaitTimeBeforeCure)
        {
            details.Add($"Bottom Wait Time Before Cure: {file.BottomWaitTimeBeforeCure} to {SlicerFile.BottomWaitTimeBeforeCure}");
            file.BottomWaitTimeBeforeCure = SlicerFile.BottomWaitTimeBeforeCure;
            changed = true;
        }

        if (file.CanUseWaitTimeBeforeCure && file.WaitTimeBeforeCure != SlicerFile.WaitTimeBeforeCure)
        {
            details.Add($"Wait Time Before Cure: {file.WaitTimeBeforeCure} to {SlicerFile.WaitTimeBeforeCure}");
            file.WaitTimeBeforeCure = SlicerFile.WaitTimeBeforeCure;
            changed = true;
        }

        if (file.CanUseBottomExposureTime && file.BottomExposureTime != SlicerFile.BottomExposureTime)
        {
            details.Add($"Bottom Exposure Time: {file.BottomExposureTime} to {SlicerFile.BottomExposureTime}");
            file.BottomExposureTime = SlicerFile.BottomExposureTime;
            changed = true;
        }

        if (file.CanUseExposureTime && file.ExposureTime != SlicerFile.ExposureTime)
        {
            details.Add($"Exposure Time: {file.ExposureTime} to {SlicerFile.ExposureTime}");
            file.ExposureTime = SlicerFile.ExposureTime;
            changed = true;
        }

        if (file.CanUseBottomWaitTimeAfterCure && file.BottomWaitTimeAfterCure != SlicerFile.BottomWaitTimeAfterCure)
        {
            details.Add($"Bottom Wait Time After Cure: {file.BottomWaitTimeAfterCure} to {SlicerFile.BottomWaitTimeAfterCure}");
            file.BottomWaitTimeAfterCure = SlicerFile.BottomWaitTimeAfterCure;
            changed = true;
        }

        if (file.CanUseWaitTimeAfterCure && file.WaitTimeAfterCure != SlicerFile.WaitTimeAfterCure)
        {
            details.Add($"Wait Time After Cure: {file.WaitTimeAfterCure} to {SlicerFile.WaitTimeAfterCure}");
            file.WaitTimeAfterCure = SlicerFile.WaitTimeAfterCure;
            changed = true;
        }

        if (file.CanUseBottomLiftHeight && file.BottomLiftHeight != SlicerFile.BottomLiftHeight)
        {
            details.Add($"Bottom Lift Height: {file.BottomLiftHeight} to {SlicerFile.BottomLiftHeight}");
            file.BottomLiftHeight = SlicerFile.BottomLiftHeight;
            changed = true;
        }

        if (file.CanUseBottomLiftSpeed && file.BottomLiftSpeed != SlicerFile.BottomLiftSpeed)
        {
            details.Add($"Bottom Lift Speed: {file.BottomLiftSpeed} to {SlicerFile.BottomLiftSpeed}");
            file.BottomLiftSpeed = SlicerFile.BottomLiftSpeed;
            changed = true;
        }

        if (file.CanUseLiftHeight && file.LiftHeight != SlicerFile.LiftHeight)
        {
            details.Add($"Lift Height: {file.LiftHeight} to {SlicerFile.LiftHeight}");
            file.LiftHeight = SlicerFile.LiftHeight;
            changed = true;
        }

        if (file.CanUseLiftSpeed && file.LiftSpeed != SlicerFile.LiftSpeed)
        {
            details.Add($"Lift Speed: {file.LiftSpeed} to {SlicerFile.LiftSpeed}");
            file.LiftSpeed = SlicerFile.LiftSpeed;
            changed = true;
        }

        if (file.CanUseBottomLiftHeight2 && file.BottomLiftHeight2 != SlicerFile.BottomLiftHeight2)
        {
            details.Add($"Bottom Lift Height 2: {file.BottomLiftHeight2} to {SlicerFile.BottomLiftHeight2}");
            file.BottomLiftHeight2 = SlicerFile.BottomLiftHeight2;
            changed = true;
        }

        if (file.CanUseBottomLiftSpeed2 && file.BottomLiftSpeed2 != SlicerFile.BottomLiftSpeed2)
        {
            details.Add($"Bottom Lift Speed2: {file.BottomLiftSpeed2} to {SlicerFile.BottomLiftSpeed2}");
            file.BottomLiftSpeed2 = SlicerFile.BottomLiftSpeed2;
            changed = true;
        }

        if (file.CanUseLiftHeight2 && file.LiftHeight2 != SlicerFile.LiftHeight2)
        {
            details.Add($"Lift Height 2: {file.LiftHeight2} to {SlicerFile.LiftHeight2}");
            file.LiftHeight2 = SlicerFile.LiftHeight2;
            changed = true;
        }

        if (file.CanUseLiftSpeed2 && file.LiftSpeed2 != SlicerFile.LiftSpeed2)
        {
            details.Add($"Lift Speed 2: {file.LiftSpeed2} to {SlicerFile.LiftSpeed2}");
            file.LiftSpeed2 = SlicerFile.LiftSpeed2;
            changed = true;
        }

        if (file.CanUseBottomWaitTimeAfterLift && file.BottomWaitTimeAfterLift != SlicerFile.BottomWaitTimeAfterLift)
        {
            details.Add($"Bottom Wait Time After Lift: {file.BottomWaitTimeAfterLift} to {SlicerFile.BottomWaitTimeAfterLift}");
            file.BottomWaitTimeAfterLift = SlicerFile.BottomWaitTimeAfterLift;
            changed = true;
        }

        if (file.CanUseWaitTimeAfterLift && file.WaitTimeAfterLift != SlicerFile.WaitTimeAfterLift)
        {
            details.Add($"Wait Time After Lift: {file.WaitTimeAfterLift} to {SlicerFile.WaitTimeAfterLift}");
            file.WaitTimeAfterLift = SlicerFile.WaitTimeAfterLift;
            changed = true;
        }

        if (file.CanUseBottomRetractSpeed && file.BottomRetractSpeed != SlicerFile.BottomRetractSpeed)
        {
            details.Add($"Bottom Retract Speed: {file.BottomRetractSpeed} to {SlicerFile.BottomRetractSpeed}");
            file.BottomRetractSpeed = SlicerFile.BottomRetractSpeed;
            changed = true;
        }

        if (file.CanUseRetractSpeed && file.RetractSpeed != SlicerFile.RetractSpeed)
        {
            details.Add($"Retract Speed: {file.RetractSpeed} to {SlicerFile.RetractSpeed}");
            file.RetractSpeed = SlicerFile.RetractSpeed;
            changed = true;
        }

        if (file.CanUseBottomRetractHeight2 && file.BottomRetractHeight2 != SlicerFile.BottomRetractHeight2)
        {
            details.Add($"Bottom Retract Height 2: {file.BottomRetractHeight2} to {SlicerFile.BottomRetractHeight2}");
            file.BottomRetractHeight2 = SlicerFile.BottomRetractHeight2;
            changed = true;
        }

        if (file.CanUseBottomRetractSpeed2 && file.BottomRetractSpeed2 != SlicerFile.BottomRetractSpeed2)
        {
            details.Add($"Bottom Retract Speed2: {file.BottomRetractSpeed2} to {SlicerFile.BottomRetractSpeed2}");
            file.BottomRetractSpeed2 = SlicerFile.BottomRetractSpeed2;
            changed = true;
        }

        if (file.CanUseRetractHeight2 && file.RetractHeight2 != SlicerFile.RetractHeight2)
        {
            details.Add($"Retract Height 2: {file.RetractHeight2} to {SlicerFile.RetractHeight2}");
            file.RetractHeight2 = SlicerFile.RetractHeight2;
            changed = true;
        }

        if (file.CanUseRetractSpeed2 && file.RetractSpeed2 != SlicerFile.RetractSpeed2)
        {
            details.Add($"Retract Speed 2: {file.RetractSpeed2} to {SlicerFile.RetractSpeed2}");
            file.RetractSpeed2 = SlicerFile.RetractSpeed2;
            changed = true;
        }

        if (file.CanUseBottomLightPWM && file.BottomLightPWM != SlicerFile.BottomLightPWM)
        {
            details.Add($"Bottom Light PWM: {file.BottomLightPWM} to {SlicerFile.BottomLightPWM}");
            file.BottomLightPWM = SlicerFile.BottomLightPWM;
            changed = true;
        }

        if (file.CanUseLightPWM && file.LightPWM != SlicerFile.LightPWM)
        {
            details.Add($"Light PWM: {file.LightPWM} to {SlicerFile.LightPWM}");
            file.LightPWM = SlicerFile.LightPWM;
            changed = true;
        }

        // if (file.CanUseXXX && file.XXX != SlicerFile.XXX)
        // {
        //     details.Add($"XXX: {file.XXX} to {SlicerFile.XXX}");
        //     file.XXX = SlicerFile.XXX;
        //     changed = true;
        // }

        // Bail out if not changed
        if (!changed)
        {
            return new Tuple<ResultStatus, string, List<string>?>(ResultStatus.Unchanged, filePath, null);
        }

        // Save the changed file
        file.Save();

        return new Tuple<ResultStatus, string, List<string>?>(ResultStatus.Success, filePath, details);
    }

}