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
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
|
/*
* 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.ComponentModel;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Numerics;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Util;
using UVtools.Core.Extensions;
using UVtools.Core.Operations;
namespace UVtools.Core.FileFormats
{
public class ChituboxZipFile : FileFormat
{
#region Constants
public const string GCodeStart = ";START_GCODE_BEGIN{0}" +
"G21 ;Set units to be mm{0}" +
"G90 ;Absolute Positioning{0}" +
"M17 ;Enable motors{0}" +
"G28 Z0 ;Home Z{0}" +
//"G91 ;Relative Positioning{0}" +
"M106 S0 ;Light off{0}" +
";START_GCODE_END{0}{0}";
public const string GCodeEnd = ";END_GCODE_BEGIN{0}" +
"M106 S0 ;Light off{0}" +
"G1 Z{1} F25 ;Raize Z{0}" +
"M18 ;Disable Motors{0}" +
";END_GCODE_END{0}";
#endregion
#region Sub Classes
public class Header
{
// ;(****Build and Slicing Parameters****)
[DisplayName("fileName")] public string Filename { get; set; } = string.Empty;
[DisplayName("machineType")] public string MachineType { get; set; } = "Default";
[DisplayName("estimatedPrintTime")] public float EstimatedPrintTime { get; set; }
[DisplayName("volume")] public float Volume { get; set; }
[DisplayName("resin")] public string Resin { get; set; } = "Normal";
[DisplayName("weight")] public float Weight { get; set; }
[DisplayName("price")] public float Price { get; set; }
[DisplayName("layerHeight")] public float LayerHeight { get; set; }
[DisplayName("resolutionX")] public uint ResolutionX { get; set; }
[DisplayName("resolutionY")] public uint ResolutionY { get; set; }
[DisplayName("machineX")] public float MachineX { get; set; }
[DisplayName("machineY")] public float MachineY { get; set; }
[DisplayName("machineZ")] public float MachineZ { get; set; }
[DisplayName("projectType")] public string ProjectType { get; set; } = "Normal";
[DisplayName("normalExposureTime")] public float LayerExposureTime { get; set; } = 7; // 35s
[DisplayName("bottomLayExposureTime")] public float BottomLayExposureTime { get; set; } = 35; // 35s
[DisplayName("bottomLayerExposureTime")] public float BottomLayerExposureTime { get; set; } = 35; // 35s
[DisplayName("normalDropSpeed")] public float RetractSpeed { get; set; } = 150; // 150 mm/m
[DisplayName("normalLayerLiftSpeed")] public float LiftSpeed { get; set; } = 60; // 60 mm/m
[DisplayName("normalLayerLiftHeight")] public float LiftHeight { get; set; } = 5; // 5 mm
[DisplayName("zSlowUpDistance")] public float ZSlowUpDistance { get; set; }
[DisplayName("bottomLayCount")] public ushort BottomLayCount { get; set; } = 4;
[DisplayName("bottomLayerCount")] public ushort BottomLayerCount { get; set; } = 4;
[DisplayName("mirror")] public byte Mirror { get; set; } // 0/1
[DisplayName("totalLayer")] public uint LayerCount { get; set; }
[DisplayName("bottomLayerLiftHeight")] public float BottomLiftHeight { get; set; } = 5;
[DisplayName("bottomLayerLiftSpeed")] public float BottomLiftSpeed { get; set; } = 60;
[DisplayName("bottomLightOffTime")] public float BottomLightOffTime { get; set; }
[DisplayName("lightOffTime")] public float LightOffTime { get; set; }
[DisplayName("bottomPWMLight")] public byte BottomLightPWM { get; set; } = 255;
[DisplayName("PWMLight")] public byte LightPWM { get; set; } = 255;
[DisplayName("antiAliasLevel")] public byte AntiAliasing { get; set; } = 1;
}
#endregion
#region Properties
public Header HeaderSettings { get; } = new Header();
public override FileFormatType FileType => FileFormatType.Archive;
public override FileExtension[] FileExtensions { get; } = {
new FileExtension("zip", "Chitubox Zip Files")
};
public override Type[] ConvertToFormats { get; } = {
typeof(ChituboxFile),
typeof(PHZFile),
typeof(PWSFile),
typeof(CWSFile),
typeof(ZCodexFile),
typeof(UVJFile)
};
public override PrintParameterModifier[] PrintParameterModifiers { get; } = {
PrintParameterModifier.BottomLayerCount,
PrintParameterModifier.BottomExposureSeconds,
PrintParameterModifier.ExposureSeconds,
PrintParameterModifier.BottomLayerOffTime,
PrintParameterModifier.LayerOffTime,
PrintParameterModifier.BottomLiftHeight,
PrintParameterModifier.BottomLiftSpeed,
PrintParameterModifier.LiftHeight,
PrintParameterModifier.LiftSpeed,
PrintParameterModifier.RetractSpeed,
PrintParameterModifier.BottomLightPWM,
PrintParameterModifier.LightPWM,
};
public override byte ThumbnailsCount { get; } = 2;
public override Size[] ThumbnailsOriginalSize { get; } = {new Size(954, 850), new Size(168, 150)};
public override uint ResolutionX
{
get => HeaderSettings.ResolutionX;
set => HeaderSettings.ResolutionX = value;
}
public override uint ResolutionY
{
get => HeaderSettings.ResolutionY;
set => HeaderSettings.ResolutionY = value;
}
public override float DisplayWidth
{
get => HeaderSettings.MachineX;
set => HeaderSettings.MachineX = value;
}
public override float DisplayHeight
{
get => HeaderSettings.MachineY;
set => HeaderSettings.MachineX = value;
}
public override byte AntiAliasing => HeaderSettings.AntiAliasing;
public override float LayerHeight
{
get => HeaderSettings.LayerHeight;
set => HeaderSettings.LayerHeight = value;
}
public override uint LayerCount
{
set
{
HeaderSettings.LayerCount = LayerCount;
RebuildGCode();
}
}
public override ushort BottomLayerCount
{
get => HeaderSettings.BottomLayerCount;
set => HeaderSettings.BottomLayerCount = HeaderSettings.BottomLayCount = value;
}
public override float BottomExposureTime
{
get => HeaderSettings.BottomLayerExposureTime;
set => HeaderSettings.BottomLayerExposureTime = HeaderSettings.BottomLayExposureTime = value;
}
public override float ExposureTime
{
get => HeaderSettings.LayerExposureTime;
set => HeaderSettings.LayerExposureTime = value;
}
public override float BottomLayerOffTime
{
get => HeaderSettings.BottomLightOffTime;
set => HeaderSettings.BottomLightOffTime = value;
}
public override float LayerOffTime
{
get => HeaderSettings.LightOffTime;
set => HeaderSettings.LightOffTime = value;
}
public override float BottomLiftHeight
{
get => HeaderSettings.BottomLiftHeight;
set => HeaderSettings.BottomLiftHeight = value;
}
public override float LiftHeight
{
get => HeaderSettings.LiftHeight;
set => HeaderSettings.LiftHeight = value;
}
public override float BottomLiftSpeed
{
get => HeaderSettings.BottomLiftSpeed;
set => HeaderSettings.BottomLiftSpeed = value;
}
public override float LiftSpeed
{
get => HeaderSettings.LiftSpeed;
set => HeaderSettings.LiftSpeed = value;
}
public override float RetractSpeed
{
get => HeaderSettings.RetractSpeed;
set => HeaderSettings.RetractSpeed = value;
}
public override byte BottomLightPWM
{
get => HeaderSettings.BottomLightPWM;
set => HeaderSettings.BottomLightPWM = value;
}
public override byte LightPWM
{
get => HeaderSettings.LightPWM;
set => HeaderSettings.LightPWM = value;
}
public override float PrintTime => HeaderSettings.EstimatedPrintTime;
public override float UsedMaterial => HeaderSettings.Weight;
public override float MaterialCost => HeaderSettings.Price;
public override string MaterialName => HeaderSettings.Resin;
public override string MachineName => HeaderSettings.MachineType;
public override object[] Configs => new object[] { HeaderSettings };
public bool IsPHZZip = false;
#endregion
#region Methods
public override void Encode(string fileFullPath, OperationProgress progress = null)
{
base.Encode(fileFullPath, progress);
using (ZipArchive outputFile = ZipFile.Open(fileFullPath, ZipArchiveMode.Create))
{
if (Thumbnails.Length > 0 && !ReferenceEquals(Thumbnails[0], null))
{
using (Stream stream = outputFile.CreateEntry("preview.png").Open())
{
using (var vec = new VectorOfByte())
{
CvInvoke.Imencode(".png", Thumbnails[0], vec);
stream.WriteBytes(vec.ToArray());
stream.Close();
}
}
}
if (Thumbnails.Length > 1 && !ReferenceEquals(Thumbnails[1], null))
{
using (Stream stream = outputFile.CreateEntry("preview_cropping.png").Open())
{
using (var vec = new VectorOfByte())
{
CvInvoke.Imencode(".png", Thumbnails[1], vec);
stream.WriteBytes(vec.ToArray());
stream.Close();
}
}
}
if (!IsPHZZip)
{
RebuildGCode();
outputFile.PutFileContent("run.gcode", GCode.ToString(), ZipArchiveMode.Create);
}
for (uint layerIndex = 0; layerIndex < LayerCount; layerIndex++)
{
progress.Token.ThrowIfCancellationRequested();
Layer layer = this[layerIndex];
outputFile.PutFileContent($"{layerIndex + 1}.png", layer.CompressedBytes,
ZipArchiveMode.Create);
progress++;
}
}
AfterEncode();
}
public override void Decode(string fileFullPath, OperationProgress progress = null)
{
base.Decode(fileFullPath, progress);
if(progress is null) progress = new OperationProgress();
progress.Reset(OperationProgress.StatusGatherLayers, LayerCount);
FileFullPath = fileFullPath;
using (var inputFile = ZipFile.Open(FileFullPath, ZipArchiveMode.Read))
{
var entry = inputFile.GetEntry("run.gcode");
if (!ReferenceEquals(entry, null))
{
//Clear();
//throw new FileLoadException("run.gcode not found", fileFullPath);
using (TextReader tr = new StreamReader(entry.Open()))
{
string line;
GCode = new StringBuilder();
while ((line = tr.ReadLine()) != null)
{
GCode.AppendLine(line);
if (string.IsNullOrEmpty(line)) continue;
if (line[0] != ';')
{
continue;
}
var splitLine = line.Split(':');
if (splitLine.Length < 2) continue;
foreach (var propertyInfo in HeaderSettings.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance))
{
var displayNameAttribute = propertyInfo.GetCustomAttributes(false).OfType<DisplayNameAttribute>().FirstOrDefault();
if (ReferenceEquals(displayNameAttribute, null)) continue;
if (!splitLine[0].Trim(' ', ';').Equals(displayNameAttribute.DisplayName)) continue;
Helpers.SetPropertyValue(propertyInfo, HeaderSettings, splitLine[1].Trim());
}
}
tr.Close();
}
}
else
{
IsPHZZip = true;
}
if (HeaderSettings.LayerCount == 0)
{
foreach (var zipEntry in inputFile.Entries)
{
if(!zipEntry.Name.EndsWith(".png")) continue;
var filename = Path.GetFileNameWithoutExtension(zipEntry.Name);
if (!filename.All(char.IsDigit)) continue;
if (!uint.TryParse(filename, out var layerIndex)) continue;
HeaderSettings.LayerCount = Math.Max(HeaderSettings.LayerCount, layerIndex);
}
}
LayerManager = new LayerManager(HeaderSettings.LayerCount, this);
progress.ItemCount = LayerCount;
var gcode = GCode?.ToString();
for (uint layerIndex = 0; layerIndex < HeaderSettings.LayerCount; layerIndex++)
{
if (progress.Token.IsCancellationRequested) break;
entry = inputFile.GetEntry($"{layerIndex+1}.png");
if (ReferenceEquals(entry, null))
{
Clear();
throw new FileLoadException($"Layer {layerIndex+1} not found", fileFullPath);
}
if (IsPHZZip) // PHZ file
{
LayerManager[layerIndex] = new Layer(layerIndex, entry.Open(), entry.Name);
progress++;
continue;;
}
var startStr = $";LAYER_START:{layerIndex}";
var stripGcode = gcode.Substring(gcode.IndexOf(startStr, StringComparison.InvariantCultureIgnoreCase) + startStr.Length);
stripGcode = stripGcode.Substring(0, stripGcode.IndexOf(";LAYER_END")).Trim(' ', '\n', '\r', '\t');
//var startCurrPos = stripGcode.Remove(0, ";currPos:".Length);
var currPos = Regex.Match(stripGcode, ";currPos:([+-]?([0-9]*[.])?[0-9]+)", RegexOptions.IgnoreCase);
var exposureTime = Regex.Match(stripGcode, "G4 P(\\d+)", RegexOptions.IgnoreCase);
var pwm = Regex.Match(stripGcode, "M106 S(\\d+)", RegexOptions.IgnoreCase);
if (layerIndex < BottomLayerCount)
{
HeaderSettings.BottomLightPWM = byte.Parse(pwm.Groups[1].Value);
}
else
{
HeaderSettings.LightPWM = byte.Parse(pwm.Groups[1].Value);
}
var asd = exposureTime.NextMatch();
var asd1 = currPos.Groups[1].Value;
var asd2 = exposureTime.NextMatch().Groups[1].Value;
var posZ = float.Parse(asd1, CultureInfo.InvariantCulture);
var exp = float.Parse(asd2, CultureInfo.InvariantCulture) / 1000f;
LayerManager[layerIndex] = new Layer(layerIndex, entry.Open(), entry.Name)
{
PositionZ = posZ,
ExposureTime = exp
};
progress++;
}
if (GCode is null) // PHZ file
{
LayerManager.RebuildLayersProperties();
}
if (HeaderSettings.LayerCount > 0 && ResolutionX == 0)
{
using (var mat = this[0].LayerMat)
{
HeaderSettings.ResolutionX = (uint)mat.Width;
HeaderSettings.ResolutionY = (uint)mat.Height;
}
}
entry = inputFile.GetEntry("preview.png");
if (!ReferenceEquals(entry, null))
{
Thumbnails[0] = new Mat();
CvInvoke.Imdecode(entry.Open().ToArray(), ImreadModes.AnyColor, Thumbnails[0]);
}
entry = inputFile.GetEntry("preview_cropping.png");
if (!ReferenceEquals(entry, null))
{
var count = CreatedThumbnailsCount;
Thumbnails[count] = new Mat();
CvInvoke.Imdecode(entry.Open().ToArray(), ImreadModes.AnyColor, Thumbnails[count]);
}
}
LayerManager.GetBoundingRectangle(progress);
}
public override void RebuildGCode()
{
if (IsPHZZip) return;
string arch = Environment.Is64BitOperatingSystem ? "64-bits" : "32-bits";
GCode = new StringBuilder();
GCode.AppendLine($"; {About.Website} {About.Software} {Assembly.GetExecutingAssembly().GetName().Version} {arch} {DateTime.Now}");
foreach (var propertyInfo in HeaderSettings.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance))
{
var displayNameAttribute = propertyInfo.GetCustomAttributes(false).OfType<DisplayNameAttribute>().FirstOrDefault();
if (displayNameAttribute is null) continue;
GCode.AppendLine($";{displayNameAttribute.DisplayName}:{propertyInfo.GetValue(HeaderSettings)}");
}
GCode.AppendLine();
GCode.AppendFormat(GCodeStart, Environment.NewLine);
float lastZPosition = 0;
for (uint layerIndex = 0; layerIndex < LayerCount; layerIndex++)
{
var liftHeight = GetInitialLayerValueOrNormal(layerIndex, HeaderSettings.BottomLiftHeight,
HeaderSettings.LiftHeight);
float liftZHeight = (float)Math.Round(liftHeight + this[layerIndex].PositionZ, 2);
var liftZSpeed = GetInitialLayerValueOrNormal(layerIndex, HeaderSettings.BottomLiftSpeed,
HeaderSettings.LiftSpeed);
var lightOffDelay = GetInitialLayerValueOrNormal(layerIndex, HeaderSettings.BottomLightOffTime,
HeaderSettings.LightOffTime) * 1000;
var pwmValue = GetInitialLayerValueOrNormal(layerIndex, HeaderSettings.BottomLightPWM, HeaderSettings.LightPWM);
var exposureTime = this[layerIndex].ExposureTime * 1000;
GCode.AppendLine($";LAYER_START:{layerIndex}");
GCode.AppendLine($";currPos:{this[layerIndex].PositionZ}");
GCode.AppendLine($"M6054 \"{layerIndex + 1}.png\";show Image");
// Absolute gcode
if (liftHeight > 0 && liftZHeight > this[layerIndex].PositionZ)
{
GCode.AppendLine($"G0 Z{liftZHeight} F{liftZSpeed};Z Lift");
}
if (lastZPosition < this[layerIndex].PositionZ)
{
GCode.AppendLine($"G0 Z{this[layerIndex].PositionZ} F{HeaderSettings.RetractSpeed};Layer position");
}
GCode.AppendLine($"G4 P{lightOffDelay};Before cure delay");
GCode.AppendLine($"M106 S{pwmValue};light on");
GCode.AppendLine($"G4 P{exposureTime};Cure time");
GCode.AppendLine("M106 S0;light off");
GCode.AppendLine(";LAYER_END");
GCode.AppendLine();
lastZPosition = this[layerIndex].PositionZ;
}
GCode.AppendFormat(GCodeEnd, Environment.NewLine, HeaderSettings.MachineZ);
}
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))
{
foreach (var zipentry in outputFile.Entries)
{
if (zipentry.Name.EndsWith(".gcode"))
{
zipentry.Delete();
break;
}
}
if (!IsPHZZip)
{
outputFile.PutFileContent("run.gcode", GCode.ToString(), ZipArchiveMode.Update);
}
}
//Decode(FileFullPath, progress);
}
public override bool Convert(Type to, string fileFullPath, OperationProgress progress = null)
{
if (to == typeof(ChituboxFile))
{
ChituboxFile file = new ChituboxFile
{
LayerManager = LayerManager,
HeaderSettings
=
{
Version = 2,
BedSizeX = HeaderSettings.MachineX,
BedSizeY = HeaderSettings.MachineY,
BedSizeZ = HeaderSettings.MachineZ,
OverallHeightMilimeter = TotalHeight,
BottomExposureSeconds = BottomExposureTime,
BottomLayersCount = BottomLayerCount,
BottomLightPWM = HeaderSettings.BottomLightPWM,
LayerCount = LayerCount,
LayerExposureSeconds = ExposureTime,
LayerHeightMilimeter = LayerHeight,
LayerOffTime = HeaderSettings.LightOffTime,
LightPWM = HeaderSettings.LightPWM,
PrintTime = (uint) HeaderSettings.EstimatedPrintTime,
ProjectorType = HeaderSettings.Mirror,
ResolutionX = ResolutionX,
ResolutionY = ResolutionY,
AntiAliasLevel = ValidateAntiAliasingLevel()
},
PrintParametersSettings =
{
BottomLayerCount = BottomLayerCount,
BottomLiftHeight = HeaderSettings.BottomLiftHeight,
BottomLiftSpeed = HeaderSettings.BottomLiftSpeed,
BottomLightOffDelay = HeaderSettings.BottomLightOffTime,
CostDollars = MaterialCost,
LiftHeight = HeaderSettings.LiftHeight,
LiftSpeed = HeaderSettings.LiftSpeed,
LightOffDelay = HeaderSettings.LightOffTime,
RetractSpeed = HeaderSettings.RetractSpeed,
VolumeMl = UsedMaterial,
WeightG = HeaderSettings.Weight
},
SlicerInfoSettings = { MachineName = MachineName, MachineNameSize = (uint)MachineName.Length }
};
file.SetThumbnails(Thumbnails);
file.Encode(fileFullPath, progress);
return true;
}
if (to == typeof(PHZFile))
{
PHZFile file = new PHZFile
{
LayerManager = LayerManager,
HeaderSettings =
{
Version = 2,
BedSizeX = HeaderSettings.MachineX,
BedSizeY = HeaderSettings.MachineY,
BedSizeZ = HeaderSettings.MachineZ,
OverallHeightMilimeter = TotalHeight,
BottomExposureSeconds = BottomExposureTime,
BottomLayersCount = BottomLayerCount,
BottomLightPWM = HeaderSettings.BottomLightPWM,
LayerCount = LayerCount,
LayerExposureSeconds = ExposureTime,
LayerHeightMilimeter = LayerHeight,
LayerOffTime = HeaderSettings.LightOffTime,
LightPWM = HeaderSettings.LightPWM,
PrintTime = (uint) HeaderSettings.EstimatedPrintTime,
ProjectorType = HeaderSettings.Mirror,
ResolutionX = ResolutionX,
ResolutionY = ResolutionY,
BottomLayerCount = BottomLayerCount,
BottomLiftHeight = HeaderSettings.BottomLiftHeight,
BottomLiftSpeed = HeaderSettings.BottomLiftSpeed,
BottomLightOffDelay = HeaderSettings.BottomLightOffTime,
CostDollars = MaterialCost,
LiftHeight = HeaderSettings.LiftHeight,
LiftSpeed = HeaderSettings.LiftSpeed,
RetractSpeed = HeaderSettings.RetractSpeed,
VolumeMl = UsedMaterial,
AntiAliasLevelInfo = ValidateAntiAliasingLevel(),
WeightG = HeaderSettings.Weight,
MachineName = MachineName,
MachineNameSize = (uint)MachineName.Length
}
};
file.SetThumbnails(Thumbnails);
file.Encode(fileFullPath, progress);
return true;
}
if (to == typeof(PWSFile))
{
PWSFile file = new PWSFile
{
LayerManager = LayerManager,
HeaderSettings =
{
ResolutionX = ResolutionX,
ResolutionY = ResolutionY,
LayerHeight = LayerHeight,
LayerExposureTime = ExposureTime,
LiftHeight = LiftHeight,
LiftSpeed = LiftSpeed / 60,
RetractSpeed = RetractSpeed / 60,
LayerOffTime = HeaderSettings.LightOffTime,
BottomLayersCount = BottomLayerCount,
BottomExposureSeconds = BottomExposureTime,
Price = MaterialCost,
Volume = UsedMaterial,
Weight = HeaderSettings.Weight,
AntiAliasing = ValidateAntiAliasingLevel()
}
};
file.SetThumbnails(Thumbnails);
file.Encode(fileFullPath, progress);
return true;
}
if (to == typeof(ZCodexFile))
{
TimeSpan ts = new TimeSpan(0, 0, (int)PrintTime);
ZCodexFile file = new ZCodexFile
{
ResinMetadataSettings = new ZCodexFile.ResinMetadata
{
MaterialId = 2,
Material = MaterialName,
AdditionalSupportLayerTime = 0,
BottomLayersNumber = BottomLayerCount,
BottomLayersTime = (uint)(BottomExposureTime * 1000),
LayerTime = (uint)(ExposureTime * 1000),
DisableSettingsChanges = false,
LayerThickness = LayerHeight,
PrintTime = (uint)PrintTime,
TotalLayersCount = LayerCount,
TotalMaterialVolumeUsed = UsedMaterial,
TotalMaterialWeightUsed = UsedMaterial,
},
UserSettings = new ZCodexFile.UserSettingsdata
{
Printer = MachineName,
BottomLayersCount = BottomLayerCount,
PrintTime = $"{ts.Hours}h {ts.Minutes}m",
LayerExposureTime = (uint)(ExposureTime * 1000),
BottomLayerExposureTime = (uint)(BottomExposureTime * 1000),
MaterialId = 2,
LayerThickness = $"{LayerHeight} mm",
AntiAliasing = (byte)(AntiAliasing > 1 ? 1 : 0),
CrossSupportEnabled = 1,
ExposureOffTime = (uint)HeaderSettings.LightOffTime,
HollowEnabled = 0,
HollowThickness = 0,
InfillDensity = 0,
IsAdvanced = 0,
MaterialType = MaterialName,
MaterialVolume = UsedMaterial,
MaxLayer = LayerCount - 1,
ModelLiftEnabled = 0,
ModelLiftHeight = 0,
RaftEnabled = 0,
RaftHeight = 0,
RaftOffset = 0,
SupportAdditionalExposureEnabled = 0,
SupportAdditionalExposureTime = 0,
XCorrection = 0,
YCorrection = 0,
ZLiftDistance = HeaderSettings.LiftHeight,
ZLiftFeedRate = HeaderSettings.LiftSpeed,
ZLiftRetractRate = HeaderSettings.RetractSpeed,
},
ZCodeMetadataSettings = new ZCodexFile.ZCodeMetadata
{
PrintTime = (uint)PrintTime,
PrinterName = MachineName,
Materials = new List<ZCodexFile.ZCodeMetadata.MaterialsData>
{
new ZCodexFile.ZCodeMetadata.MaterialsData
{
Name = MaterialName,
ExtruderType = "MAIN",
Id = 0,
Usage = 0,
Temperature = 0
}
},
},
LayerManager = LayerManager
};
float usedMaterial = UsedMaterial / LayerCount;
for (uint layerIndex = 0; layerIndex < LayerCount; layerIndex++)
{
file.ResinMetadataSettings.Layers.Add(new ZCodexFile.ResinMetadata.LayerData
{
Layer = layerIndex,
UsedMaterialVolume = usedMaterial
});
}
file.SetThumbnails(Thumbnails);
file.Encode(fileFullPath, progress);
return true;
}
if (to == typeof(CWSFile))
{
CWSFile defaultFormat = (CWSFile)FindByType(typeof(CWSFile));
CWSFile file = new CWSFile { LayerManager = LayerManager };
file.SliceSettings.Xppm = file.OutputSettings.PixPermmX = (float)Math.Round(ResolutionX / HeaderSettings.MachineX, 3);
file.SliceSettings.Yppm = file.OutputSettings.PixPermmY = (float)Math.Round(ResolutionY / HeaderSettings.MachineY, 3);
file.SliceSettings.Xres = file.OutputSettings.XResolution = (ushort)ResolutionX;
file.SliceSettings.Yres = file.OutputSettings.YResolution = (ushort)ResolutionY;
file.SliceSettings.Thickness = file.OutputSettings.LayerThickness = LayerHeight;
file.SliceSettings.LayersNum = file.OutputSettings.LayersNum = LayerCount;
file.SliceSettings.HeadLayersNum = file.OutputSettings.NumberBottomLayers = BottomLayerCount;
file.SliceSettings.LayersExpoMs = file.OutputSettings.LayerTime = (uint)ExposureTime * 1000;
file.SliceSettings.HeadLayersExpoMs = file.OutputSettings.BottomLayersTime = (uint)BottomExposureTime * 1000;
file.SliceSettings.WaitBeforeExpoMs = (uint)(HeaderSettings.LightOffTime * 1000);
file.SliceSettings.LiftDistance = file.OutputSettings.LiftDistance = LiftHeight;
file.SliceSettings.LiftUpSpeed = file.OutputSettings.ZLiftFeedRate = LiftSpeed;
file.SliceSettings.LiftDownSpeed = file.OutputSettings.ZLiftRetractRate = RetractSpeed;
file.SliceSettings.LiftWhenFinished = defaultFormat.SliceSettings.LiftWhenFinished;
file.OutputSettings.BlankingLayerTime = (uint)(HeaderSettings.LightOffTime * 1000);
//file.OutputSettings.RenderOutlines = false;
//file.OutputSettings.OutlineWidthInset = 0;
//file.OutputSettings.OutlineWidthOutset = 0;
file.OutputSettings.RenderOutlines = false;
//file.OutputSettings.TiltValue = 0;
//file.OutputSettings.UseMainliftGCodeTab = false;
//file.OutputSettings.AntiAliasing = 0;
//file.OutputSettings.AntiAliasingValue = 0;
file.OutputSettings.FlipX = HeaderSettings.Mirror != 0;
file.OutputSettings.FlipY = file.OutputSettings.FlipX;
file.OutputSettings.AntiAliasingValue = ValidateAntiAliasingLevel();
file.OutputSettings.AntiAliasing = file.OutputSettings.AntiAliasingValue > 1;
file.Printer = MachineName.Contains("Bene4 Mono") ||
FileFullPath.Contains("bene4_mono")
? CWSFile.PrinterType.BeneMono : CWSFile.PrinterType.Elfin;
file.Encode(fileFullPath, progress);
return true;
}
if (to == typeof(UVJFile))
{
UVJFile defaultFormat = (UVJFile)FindByType(typeof(UVJFile));
UVJFile file = new UVJFile
{
LayerManager = LayerManager,
JsonSettings = new UVJFile.Settings
{
Properties = new UVJFile.Properties
{
Size = new UVJFile.Size
{
X = (ushort)ResolutionX,
Y = (ushort)ResolutionY,
Millimeter = new UVJFile.Millimeter
{
X = HeaderSettings.MachineX,
Y = HeaderSettings.MachineY,
},
LayerHeight = LayerHeight,
Layers = LayerCount
},
Bottom = new UVJFile.Bottom
{
LiftHeight = HeaderSettings.BottomLiftHeight,
LiftSpeed = HeaderSettings.BottomLiftSpeed,
LightOnTime = BottomExposureTime,
LightOffTime = HeaderSettings.BottomLightOffTime,
LightPWM = HeaderSettings.BottomLightPWM,
RetractSpeed = HeaderSettings.RetractSpeed,
Count = BottomLayerCount
//RetractHeight = LookupCustomValue<float>(Keyword_LiftHeight, defaultFormat.JsonSettings.Properties.Bottom.RetractHeight),
},
Exposure = new UVJFile.Exposure
{
LiftHeight = HeaderSettings.LiftHeight,
LiftSpeed = HeaderSettings.LiftSpeed,
LightOnTime = ExposureTime,
LightOffTime = HeaderSettings.LightOffTime,
LightPWM = HeaderSettings.LightPWM,
RetractSpeed = HeaderSettings.RetractSpeed,
},
AntiAliasLevel = ValidateAntiAliasingLevel()
}
}
};
file.SetThumbnails(Thumbnails);
file.Encode(fileFullPath, progress);
return true;
}
return false;
}
#endregion
}
}
|