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

MainWindow.GCode.cs « UVtools.WPF - github.com/sn4k3/UVtools.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2b1e003b43aaa2950112d05908c38d1835cfc6cd (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
/*
 *                     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.IO;
using Avalonia;
using Avalonia.Controls;
using MessageBox.Avalonia.Enums;
using UVtools.Core.SystemOS;
using UVtools.WPF.Extensions;
using Helpers = UVtools.WPF.Controls.Helpers;

namespace UVtools.WPF;

public partial class MainWindow
{
    public bool HaveGCode => IsFileLoaded && SlicerFile.SupportsGCode;

    public uint GCodeLines => !HaveGCode ? 0 : SlicerFile.GCode.LineCount;

    public void OnClickRebuildGcode()
    {
        if (!HaveGCode) return;
        var temp = SlicerFile.SuppressRebuildGCode;
        SlicerFile.SuppressRebuildGCode = false;
        SlicerFile.RebuildGCode();
        SlicerFile.SuppressRebuildGCode = temp;
        RaisePropertyChanged(nameof(GCodeLines));

        CanSave = true;
    }

    public async void OnClickGCodeSaveFile()
    {
        if (!HaveGCode) return;

        var dialog = new SaveFileDialog
        {
            Filters = Helpers.IniFileFilter,
            Directory = Path.GetDirectoryName(SlicerFile.FileFullPath),
            InitialFileName = $"{Path.GetFileNameWithoutExtension(SlicerFile.FileFullPath)}_gcode.txt"
        };
        var file = await dialog.ShowAsync(this);

        if (string.IsNullOrEmpty(file)) return;

        try
        {
            await using TextWriter tw = new StreamWriter(file);
            await tw.WriteAsync(SlicerFile.GCodeStr);
            tw.Close();
        }
        catch (Exception e)
        {
            await this.MessageBoxError(e.ToString(), "Error occur while save gcode");
            return;
        }

        var result = await this.MessageBoxQuestion(
            "GCode save was successful. Do you want open the file in the default editor?",
            "GCode save complete");
        if (result != ButtonResult.Yes) return;

        SystemAware.StartProcess(file);
    }

    public void OnClickGCodeSaveClipboard()
    {
        if (!HaveGCode) return;
        Application.Current.Clipboard.SetTextAsync(SlicerFile.GCodeStr);
    }
}