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

ZCodexFile.cs « FileFormats « UVtools.Core - github.com/sn4k3/UVtools.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6612d4c47e42003019781d13a697f8de38e617ff (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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
/*
 *                     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.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Text;
using System.Text.RegularExpressions;
using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Util;
using Newtonsoft.Json;
using UVtools.Core.Extensions;
using UVtools.Core.Operations;

namespace UVtools.Core.FileFormats
{
    public class ZCodexFile : FileFormat
    {
        #region Constants

        private const string GCodeStart = "G28\nG21\nG91\nM17\n";
        private const string GCodeKeywordSlice = "<Slice>";
        private const string GCodeKeywordDelayBlank = "<Delay_blank>";
        private const string GCodeKeywordDelayModel = "<Delay_model>";
        private const string GCodeKeywordDelaySupportPart = "<Delay_support_part>";
        private const string GCodeKeywordDelaySupportFull = "<Delay_support_full>";
        private const string FolderImages = "ResinSlicesData";
        private const string FolderImageName = "Slice";
        #endregion

        #region Sub Classes

        public class ResinMetadata
        {
            public class LayerData
            {
                public uint Layer { get; set; }
                public float UsedMaterialVolume { get; set; }

            }

            public string Guid { get; set; } = "07452AC2-7494-4576-BA60-BFEA8815F917";
            public string Material { get; set; }
            public uint MaterialId { get; set; }
            public float LayerThickness { get; set; }
            public uint PrintTime { get; set; }
            public uint LayerTime { get; set; }
            public uint BottomLayersTime { get; set; }
            public uint AdditionalSupportLayerTime { get; set; }
            public ushort BottomLayersNumber { get; set; }
            public uint BlankingLayerTime { get; set; }
            public float TotalMaterialVolumeUsed { get; set; }
            public float TotalMaterialWeightUsed { get; set; }
            public uint TotalLayersCount { get; set; }
            public bool DisableSettingsChanges { get; set; }

            public List<LayerData> Layers { get; set; } = new List<LayerData>();
        }

        public class UserSettingsdata
        {
            public uint MaxLayer { get; set; }
            public string PrintTime { get; set; }
            public float MaterialVolume { get; set; }
            public byte IsAdvanced { get; set; }
            public string Printer { get; set; } = "Zortrax Inkspire";
            public string MaterialType { get; set; }
            public uint MaterialId { get; set; }
            public string LayerThickness { get; set; }
            public byte RaftEnabled { get; set; }
            public float RaftHeight { get; set; }
            public float RaftOffset { get; set; }
            public byte ModelLiftEnabled { get; set; }
            public float ModelLiftHeight { get; set; }
            public byte CrossSupportEnabled { get; set; }
            public uint LayerExposureTime { get; set; }
            //public uint LayerThicknessesDisplayTime { get; set; } arr
            public uint ExposureOffTime { get; set; } = 5;
            public uint BottomLayerExposureTime { get; set; }
            public uint BottomLayersCount { get; set; }
            public byte SupportAdditionalExposureEnabled { get; set; }
            public uint SupportAdditionalExposureTime { get; set; }
            public float ZLiftDistance { get; set; } = 5;
            public float ZLiftRetractRate { get; set; } = 100;
            public float ZLiftFeedRate { get; set; } = 100;
            public byte AntiAliasing { get; set; } = 0;
            public float XCorrection { get; set; }
            public float YCorrection { get; set; }
            public byte HollowEnabled { get; set; }
            public float HollowThickness { get; set; }
            public byte InfillDensity { get; set; }
        }

        public class ZCodeMetadata
        {
            public class MaterialsData
            {
                public string ExtruderType { get; set; }
                public uint Id { get; set; }
                public string Name { get; set; }
                public uint Usage { get; set; }
                public uint Temperature { get; set; }
            }

            public string ZCodexVersion { get; set; } = "2.0.0.0";
            public string SoftwareVersion { get; set; } = "2.12.2.0";
            public string MinFirmwareVersion { get; set; } = "20013";
            public uint PrinterModelEnumId { get; set; } = 40;
            public string PrinterName { get; set; } = "Inkspire";
            public List<MaterialsData> Materials { get; set; }
            public byte HeatbedTemperature { get; set; }
            public byte ChamberTemperature { get; set; }
            public uint CommandCount { get; set; }
            public uint PrintTime { get; set; }
            public float NozzleDiameter { get; set; }
            public string PrintBoundingBox { get; set; }
            public string Pauses { get; set; }
            public string MaterialUsages { get; set; }
        }

        public class LayerData
        {
            public int SupportLayerFileIndex { get; set; } = -1;
            public int LayerFileIndex { get; set; } = -1;
            public ZipArchiveEntry SupportLayerEntry { get; set; }
            public ZipArchiveEntry LayerEntry { get; set; }

            public bool HaveSupportLayer => !ReferenceEquals(SupportLayerEntry, null);
        }

        #endregion

        #region Properties
        public ResinMetadata ResinMetadataSettings { get; set; } = new ResinMetadata();
        public UserSettingsdata UserSettings { get; set; } = new UserSettingsdata();
        public ZCodeMetadata ZCodeMetadataSettings { get; set; } = new ZCodeMetadata();

        public List<LayerData> LayersSettings { get; } = new List<LayerData>();

        public override FileFormatType FileType => FileFormatType.Archive;

        public override FileExtension[] FileExtensions { get; } = {
            new FileExtension("zcodex", "Z-Suite ZCodex Files")
        };

        public override Type[] ConvertToFormats { get; } = null;

        public override PrintParameterModifier[] PrintParameterModifiers { get; } = {
            PrintParameterModifier.BottomLayerCount,
            PrintParameterModifier.BottomExposureSeconds,
            PrintParameterModifier.ExposureSeconds,


            PrintParameterModifier.LiftHeight,
            PrintParameterModifier.RetractSpeed,
            PrintParameterModifier.LiftSpeed,
        };

        public override byte ThumbnailsCount { get; } = 1;

        public override System.Drawing.Size[] ThumbnailsOriginalSize { get; } = {new System.Drawing.Size(320, 180)};

        public override uint ResolutionX
        {
            get => 1440;
            set => throw new NotImplementedException();
        }

        public override uint ResolutionY
        {
            get => 2560;
            set => throw new NotImplementedException();
        }

        public override float DisplayWidth
        {
            get => 74.67f;
            set {}
        }
        public override float DisplayHeight
        {
            get => 132.88f;
            set { }
        }

        public override byte AntiAliasing => UserSettings.AntiAliasing;

        public override float LayerHeight
        {
            get => ResinMetadataSettings.LayerThickness;
            set => ResinMetadataSettings.LayerThickness = value;
        }

        public override uint LayerCount
        {
            set
            {
                UserSettings.MaxLayer = LayerCount - 1;
                ResinMetadataSettings.TotalLayersCount = LayerCount;
                RebuildGCode();
            }
        }

        public override ushort BottomLayerCount
        {
            get => ResinMetadataSettings.BottomLayersNumber;
            set => ResinMetadataSettings.BottomLayersNumber = value;
        }

        public override float BottomExposureTime
        {
            get => (float) Math.Round(UserSettings.BottomLayerExposureTime / 1000f, 2);
            set => UserSettings.BottomLayerExposureTime = (uint) (value * 1000f);
        }

        public override float ExposureTime
        {
            get => (float) Math.Round(UserSettings.LayerExposureTime / 1000f, 2);
            set => UserSettings.LayerExposureTime = (uint) (value * 1000f);
        }

        public override float LiftHeight
        {
            get => UserSettings.ZLiftDistance;
            set => UserSettings.ZLiftDistance = value;
        }

        public override float LiftSpeed
        {
            get => UserSettings.ZLiftFeedRate;
            set => UserSettings.ZLiftFeedRate = value;
        }

        public override float RetractSpeed
        {
            get => UserSettings.ZLiftRetractRate;
            set => UserSettings.ZLiftRetractRate = value;
        }

        public override float PrintTime => ResinMetadataSettings.PrintTime;

        public override float UsedMaterial => ResinMetadataSettings.TotalMaterialVolumeUsed;

        public override float MaterialCost => 0;

        public override string MaterialName => ResinMetadataSettings.Material;

        public override string MachineName => ZCodeMetadataSettings.PrinterName;

        public override object[] Configs => new[] {(object) ResinMetadataSettings, UserSettings, ZCodeMetadataSettings};
        #endregion

        #region Methods

        public override void Clear()
        {
            base.Clear();
            LayersSettings.Clear();
        }

        public override void Encode(string fileFullPath, OperationProgress progress = null)
        {
            base.Encode(fileFullPath, progress);
            using (ZipArchive outputFile = ZipFile.Open(fileFullPath, ZipArchiveMode.Create))
            {
                outputFile.PutFileContent("ResinMetadata", JsonConvert.SerializeObject(ResinMetadataSettings), ZipArchiveMode.Create);
                outputFile.PutFileContent("UserSettingsData", JsonConvert.SerializeObject(UserSettings), ZipArchiveMode.Create);
                outputFile.PutFileContent("ZCodeMetadata", JsonConvert.SerializeObject(ZCodeMetadataSettings), ZipArchiveMode.Create);

                if (CreatedThumbnailsCount > 0)
                {
                    using (Stream stream = outputFile.CreateEntry("Preview.png").Open())
                    {
                        var vec = new VectorOfByte();
                        CvInvoke.Imencode(".png", Thumbnails[0], vec);
                        stream.WriteBytes(vec.ToArray());
                        stream.Close();
                    }
                }

                GCode = new StringBuilder(GCodeStart);

                float lastZPosition = 0;
                for (uint layerIndex = 0; layerIndex < LayerCount; layerIndex++)
                {
                    progress.Token.ThrowIfCancellationRequested();

                    Layer layer = this[layerIndex];
                    GCode.AppendLine($"{GCodeKeywordSlice} {layerIndex}");

                    if (lastZPosition != layer.PositionZ)
                    {
                        if (LiftHeight > 0)
                        {
                            GCode.AppendLine($"G1 Z{LiftHeight} F{LiftSpeed}");
                            GCode.AppendLine($"G1 Z-{Math.Round(LiftHeight - layer.PositionZ + lastZPosition, 2)} F{RetractSpeed}");
                        }
                        else
                        {
                            GCode.AppendLine($"G1 Z{Math.Round(layer.PositionZ- lastZPosition, 2)} F{LiftSpeed}");
                        }
                    }
                    /*else
                    {
                        //GCode.AppendLine($";G1 Z{LiftHeight} F{LiftSpeed}; Already here");
                        //GCode.AppendLine($";G1 Z-{LiftHeight - layer.PositionZ + lastZPosition} F{RetractSpeed}; Already here");
                    }*/

                    //GCode.AppendLine($"G1 Z{LiftHeight} F{LiftSpeed}");
                    //GCode.AppendLine($"G1 Z-{LiftHeight - LayerHeight} F{RetractSpeed}");
                    GCode.AppendLine(GCodeKeywordDelayBlank);
                    GCode.AppendLine("M106 S255");
                    GCode.AppendLine(GCodeKeywordDelayModel);
                    GCode.AppendLine("M106 S0");


                    var layerimagePath = $"{FolderImages}/{FolderImageName}{layerIndex:D5}.png";
                    using (Stream stream = outputFile.CreateEntry(layerimagePath).Open())
                    {
                        //image.Save(stream, Helpers.PngEncoder);
                        var byteArr = this[layerIndex].CompressedBytes;
                        stream.Write(byteArr, 0, byteArr.Length);
                        stream.Close();
                    }

                    lastZPosition = layer.PositionZ;

                    progress++;
                }

                GCode.AppendLine($"G1 Z40.0 F{UserSettings.ZLiftFeedRate}");
                GCode.AppendLine("M18");

                outputFile.PutFileContent("ResinGCodeData", GCode.ToString(), ZipArchiveMode.Create);
            }
            AfterEncode();
        }

        public override void Decode(string fileFullPath, OperationProgress progress = null)
        {
            base.Decode(fileFullPath, progress);

            FileFullPath = fileFullPath;
            using (var inputFile = ZipFile.Open(FileFullPath, ZipArchiveMode.Read))
            {
                var entry = inputFile.GetEntry("ResinMetadata");
                if (ReferenceEquals(entry, null))
                {
                    Clear();
                    throw new FileLoadException("ResinMetadata not found", fileFullPath);
                }

                ResinMetadataSettings = Helpers.JsonDeserializeObject<ResinMetadata>(entry.Open());

                entry = inputFile.GetEntry("UserSettingsData");
                if (ReferenceEquals(entry, null))
                {
                    Clear();
                    throw new FileLoadException("UserSettingsData not found", fileFullPath);
                }

                UserSettings = Helpers.JsonDeserializeObject<UserSettingsdata>(entry.Open());

                entry = inputFile.GetEntry("ZCodeMetadata");
                if (ReferenceEquals(entry, null))
                {
                    Clear();
                    throw new FileLoadException("ZCodeMetadata not found", fileFullPath);
                }

                ZCodeMetadataSettings = Helpers.JsonDeserializeObject<ZCodeMetadata>(entry.Open());

                entry = inputFile.GetEntry("ResinGCodeData");
                if (ReferenceEquals(entry, null))
                {
                    Clear();
                    throw new FileLoadException("ResinGCodeData not found", fileFullPath);
                }

                LayerManager = new LayerManager(ResinMetadataSettings.TotalLayersCount, this);
                GCode = new StringBuilder();
                using (TextReader tr = new StreamReader(entry.Open()))
                {
                    string line;
                    int layerIndex = 0;
                    int layerFileIndex = 0;
                    string layerimagePath = null;
                    float currentHeight = 0;
                    while (!ReferenceEquals(line = tr.ReadLine(), null))
                    {
                        GCode.AppendLine(line);
                        if (line.StartsWith(GCodeKeywordSlice))
                        {
                            layerFileIndex = int.Parse(line.Substring(GCodeKeywordSlice.Length));
                            layerimagePath = $"{FolderImages}/{FolderImageName}{layerFileIndex:D5}.png";
                            if (LayersSettings.Count - 1 < layerIndex) LayersSettings.Add(new LayerData());
                            continue;
                        }

                        if (line.StartsWith(GCodeKeywordDelaySupportPart))
                        {
                            LayersSettings[layerIndex].SupportLayerFileIndex = layerFileIndex;
                            LayersSettings[layerIndex].SupportLayerEntry = inputFile.GetEntry(layerimagePath);
                            continue;
                        }

                        /*
                         *
<Slice> 0
G1 Z5.0 F100.0
G1 Z-4.9 F100.0
<Delay_blank>
M106 S255
<Delay_support_full>
M106 S0
                         */

                        var gcode = GCode.ToString();

                        if (line.StartsWith(GCodeKeywordDelaySupportFull) || line.StartsWith(GCodeKeywordDelayModel))
                        {
                            var startStr = $"{GCodeKeywordSlice} {layerIndex}";
                            var stripGcode = gcode.Substring(gcode.IndexOf(startStr, StringComparison.InvariantCultureIgnoreCase) + startStr.Length).Trim(' ', '\n', '\r', '\t');

                            var currPos = Regex.Match(stripGcode, "G1 Z([+-]?([0-9]*[.])?[0-9]+)", RegexOptions.IgnoreCase);
                            //var exposureTime = Regex.Match(stripGcode, ";<Delay> (\\d+)", RegexOptions.IgnoreCase);
                            /*var pwm = Regex.Match(stripGcode, "M106 S(\\d+)", RegexOptions.IgnoreCase);
                            if (layerIndex < InitialLayerCount)
                            {
                                OutputSettings.BottomLayerLightPWM = byte.Parse(pwm.Groups[1].Value);
                            }
                            else
                            {
                                OutputSettings.LayerLightPWM = byte.Parse(pwm.Groups[1].Value);
                            }*/
                            if (currPos.Success)
                            {
                                var nextMatch = currPos.NextMatch();
                                if (nextMatch.Success)
                                {
                                    currentHeight = (float)Math.Round(currentHeight + float.Parse(currPos.Groups[1].Value) + float.Parse(currPos.NextMatch().Groups[1].Value), 2);
                                }
                                else
                                {
                                    currentHeight = (float)Math.Round(currentHeight + float.Parse(currPos.Groups[1].Value), 2);
                                }
                                
                            }

                            LayersSettings[layerIndex].LayerFileIndex = layerFileIndex;
                            LayersSettings[layerIndex].LayerEntry = inputFile.GetEntry(layerimagePath);
                            this[layerIndex] = new Layer((uint) layerIndex, LayersSettings[layerIndex].LayerEntry.Open(), LayersSettings[layerIndex].LayerEntry.Name)
                            {
                                PositionZ = currentHeight,
                                ExposureTime = GetInitialLayerValueOrNormal((uint) layerIndex, BottomExposureTime, ExposureTime)
                            };
                            layerIndex++;

                            progress++;
                        }
                    }

                    tr.Close();
                }

                entry = inputFile.GetEntry("Preview.png");
                if (!ReferenceEquals(entry, null))
                {
                    using (Stream stream = entry.Open())
                    {
                        
                        CvInvoke.Imdecode(stream.ToArray(), ImreadModes.AnyColor, Thumbnails[0]);
                        stream.Close();
                    }
                }
            }

            LayerManager.GetBoundingRectangle(progress);
        }

        public override void RebuildGCode()
        {
            var gcode = GCode.ToString();
            gcode = Regex.Replace(gcode, @"Z[+]?([0-9]*\.[0-9]+|[0-9]+) F[+]?([0-9]*\.[0-9]+|[0-9]+)",
                $"Z{UserSettings.ZLiftDistance} F{UserSettings.ZLiftFeedRate}");

            gcode = Regex.Replace(gcode, @"Z-[-]?([0-9]*\.[0-9]+|[0-9]+) F[+]?([0-9]*\.[0-9]+|[0-9]+)",
                $"Z-{UserSettings.ZLiftDistance - LayerHeight} F{UserSettings.ZLiftRetractRate}");

            GCode.Clear();
            GCode.Append(gcode);
        }

        public override void SaveAs(string filePath = null, OperationProgress progress = null)
        {
            if (RequireFullEncode)
            {
                if (!string.IsNullOrEmpty(filePath))
                {
                    FileFullPath = filePath;
                }
                Encode(FileFullPath, progress);
                return;
            }

            if (!string.IsNullOrEmpty(filePath))
            {
                File.Copy(FileFullPath, filePath, true);
                FileFullPath = filePath;

            }

            using (var outputFile = ZipFile.Open(FileFullPath, ZipArchiveMode.Update))
            {
                outputFile.PutFileContent("ResinMetadata", JsonConvert.SerializeObject(ResinMetadataSettings), ZipArchiveMode.Update);
                outputFile.PutFileContent("UserSettingsData", JsonConvert.SerializeObject(UserSettings), ZipArchiveMode.Update);
                outputFile.PutFileContent("ZCodeMetadata", JsonConvert.SerializeObject(ZCodeMetadataSettings), ZipArchiveMode.Update);
                outputFile.PutFileContent("ResinGCodeData", GCode.ToString(), ZipArchiveMode.Update);
            }

            //Decode(FileFullPath, progress);
        }

        public override bool Convert(Type to, string fileFullPath, OperationProgress progress = null)
        {
            throw new NotImplementedException();
        }
        #endregion
    }
}