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

Program.cs « UVtools.WPF - github.com/sn4k3/UVtools.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d6dee910af6fd78de8ba76c25c4caca91cef0aef (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
using System;
using System.Diagnostics;
using System.Globalization;
using System.Threading.Tasks;
using Avalonia;
using Projektanker.Icons.Avalonia;
using Projektanker.Icons.Avalonia.FontAwesome;
using Projektanker.Icons.Avalonia.MaterialDesign;
using UVtools.Core.SystemOS;

namespace UVtools.WPF;

#nullable enable

public static class Program
{
    private static bool _isDebug;

    public static string[] Args = Array.Empty<string>();
    
    public static bool IsDebug
    {
        get
        {
#if DEBUG
            return true;
#else
            return _isDebug;
#endif
        }
        set => _isDebug = value;
    }

    public static bool IsCrashReport;

    public static Stopwatch ProgramStartupTime = null!;
    // Initialization code. Don't use any Avalonia, third-party APIs or any
    // SynchronizationContext-reliant code before AppMain is called: things aren't initialized
    // yet and stuff might break.
    [STAThread]
    public static void Main(string[] args)
    {
        CultureInfo.DefaultThreadCurrentCulture = CultureInfo.InvariantCulture;
        CultureInfo.DefaultThreadCurrentUICulture = CultureInfo.InvariantCulture;

        ProgramStartupTime = Stopwatch.StartNew();
        Args = args;
        try
        {
            if (ConsoleArguments.ParseArgs(args)) return;
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
            return;
        }
        
        if (Args.Length >= 1)
        {
            switch (Args[0])
            {
                case "--debug":
                    IsDebug = true;
                    break;
                case "--crash-report" when Args.Length >= 3:
                    IsCrashReport = true;
                    break;
            }
        } 

        /*using var mat = CvInvoke.Imread(@"D:\layer0.png", ImreadModes.Grayscale);
        var contours = mat.FindContours(out var hierarchy, RetrType.Tree, ChainApproxMethod.ChainApproxTc89Kcos);

        var triangulator = new Incremental();

        var poly = new Polygon();

        // Generate mesh.
        var points = new List<Vertex>();

        for (int i = 0; i < contours.Size; i++)
        for (int x = 0; x < contours[i].Size; x++)
        {
            points.Add(new Vertex(contours[i][x].X, contours[i][x].Y));
        }
        
        //var mesh = triangulator.Triangulate(points, new Configuration());
        //var triangles = mesh.Triangles.ToArray();


        using var stl = new STLMeshFile(@"D:\test.stl", FileMode.Create);
        stl.BeginWrite();

        var groups = EmguContours.GetPositiveContoursInGroups(contours, hierarchy);

        foreach (var group in groups)
        {
            var z = 0;
            for (int i = 0; i < group.Size; i++)
            {
                var list = new List<Vertex>();

                
                for (int x = 0; x < contours[i].Size; x++)
                {
                    //list.Add(contours[i][x]);
                    list.Add(new Vertex(contours[i][x].X, contours[i][x].Y));
                }

                poly.Add(new Contour(list), i > 0);

                /*using var sub = new Subdiv2D(list.ToArray());
                var triangles = sub.GetDelaunayTriangles();

                foreach (var triangle2Df in triangles)
                {
                    stl.WriteTriangle(
                        new Vector3(triangle2Df.V0.X, triangle2Df.V0.Y, z),
                        new Vector3(triangle2Df.V1.X, triangle2Df.V1.Y, z),
                        new Vector3(triangle2Df.V2.X, triangle2Df.V2.Y, z),
                        new Vector3(triangle2Df.Centeroid.X, triangle2Df.Centeroid.Y, z)
                    );
                }*/



                //z++;
           /* }

            
            //break;
        }

        var mesh = poly.Triangulate(new ConstraintOptions());
        for (int i = 0; i < 50; i++)
        {
            foreach (var meshTriangle in mesh.Triangles)
            {
                stl.WriteTriangle(
                    new Vector3((float)meshTriangle.GetVertex(0).X, (float)meshTriangle.GetVertex(0).Y, i),
                    new Vector3((float)meshTriangle.GetVertex(1).X, (float)meshTriangle.GetVertex(1).Y, i),
                    new Vector3((float)meshTriangle.GetVertex(2).X, (float)meshTriangle.GetVertex(2).Y, i),
                    new Vector3(1f, 1f, i)
                );
            }
        }
        

        stl.EndWrite();
        return;*/

        /*Slicer slicer = new(Size.Empty, SizeF.Empty, "D:\\Cube100x100x100.stl");
        var slices = slicer.SliceModel(0.05f);
    
        foreach (var slice in slices)
        {
            using var mat = EmguExtensions.InitMat(new Size(1000, 1000));
            var contour = slice.Value.ToContour();
            using var vec = new VectorOfPoint(contour);
            CvInvoke.FillPoly(mat, vec, EmguExtensions.WhiteColor, LineType.AntiAlias);
            mat.Save(@$"D:\SLICE\{slice.Key}.png");
        }*/

        // PrusaSlicer to Machine.cs
        //var machines = Machine.GetMachinesFromPrusaSlicer();
        //var machinesText = Machine.GenerateMachinePresetsFromPrusaSlicer();

        // Add the event handler for handling non-UI thread exceptions to the event.
        AppDomain.CurrentDomain.UnhandledException += (sender, e) => HandleUnhandledException("Non-UI", (Exception)e.ExceptionObject);
        TaskScheduler.UnobservedTaskException += (sender, e) => HandleUnhandledException("Task", e.Exception);
        //AppDomain.CurrentDomain.FirstChanceException += CurrentDomainOnFirstChanceException;
        
        try
        {
            BuildAvaloniaApp().StartWithClassicDesktopLifetime(args);
        }
        catch (Exception e)
        {
            HandleUnhandledException("Application", e);
        }
        

        // Closing
    }

    private static void HandleUnhandledException(string category, Exception ex)
    {
        ErrorLog.AppendLine($"Fatal {category} Error", ex.ToString());

        if (!IsCrashReport)
        {
            try
            {
                string? file = null;
                if (App.SlicerFile is not null)
                {
                    file = $"{App.SlicerFile.Filename}  [Version: {App.SlicerFile.Version}] [Class: {App.SlicerFile.GetType().Name}]";
                }
                SystemAware.StartThisApplication($"--crash-report \"{category}\" \"{ex}\" \"{file}\"");
                //var errorMsg = $"An application error occurred. Please contact the administrator with the following information:\n\n{ex}";
                //await App.MainWindow.MessageBoxError(errorMsg, "Fatal Non-UI Error");
            }
            catch (Exception exception)
            {
                Debug.WriteLine(exception);
                Console.WriteLine(exception);
            }
        }

        Environment.Exit(-1);
    }

    // Avalonia configuration, don't remove; also used by visual designer.
    public static AppBuilder BuildAvaloniaApp()
        => AppBuilder.Configure<App>()
            .UsePlatformDetect()
            .With(new Win32PlatformOptions { AllowEglInitialization = false/*, UseWgl = true*/})
            .With(new X11PlatformOptions { UseGpu = true/*, UseEGL = true*/ })
            .With(new MacOSPlatformOptions { ShowInDock = true })
            .With(new AvaloniaNativePlatformOptions { UseGpu = true })
            .WithIcons(container => container
                .Register<FontAwesomeIconProvider>()
                .Register<MaterialDesignIconProvider>())
            //.UseSkia()
            .LogToTrace();
}