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
|
/*
* 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.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Xml.Serialization;
using UVtools.Core.Extensions;
using UVtools.Core.Layers;
using UVtools.Core.Operations;
namespace UVtools.Core.FileFormats
{
#region Sub Classes
[Serializable]
[XmlRoot(ElementName = "root")]
public class VDARoot
{
[Serializable]
public class VDAFileInfo
{
[Serializable]
public class VDAVersion
{
public ushort Major { get; set; } = 1;
public ushort Minor { get; set; } = 2;
}
[Serializable]
public class VDAWritten
{
[Serializable]
[XmlRoot(ElementName = "By")]
public class VDABy
{
[XmlAttribute]
public string ApplicationName { get; set; } = About.Software;
[XmlAttribute]
public string ApplicationVersion { get; set; } = About.VersionStr;
public override string ToString()
{
return $"{ApplicationName} v{ApplicationVersion}";
}
public void Reset()
{
ApplicationName = About.Software;
ApplicationVersion = About.VersionStr;
}
}
public VDABy By { get; set; } = new();
public string When { get; set; } = DateTime.UtcNow.ToString("u");
public void Reset()
{
When = DateTime.UtcNow.ToString("u");
By.Reset();
}
}
public VDAVersion Version { get; set; } = new();
public VDAWritten Written { get; set; } = new();
}
[Serializable]
public class VDASlices
{
public ushort Count { get; set; } = 1;
[XmlElement("thickness")]
public float LayerHeight { get; set; }
[XmlElement("startHeight")]
public float StartHeight { get; set; }
[XmlElement("endHeight")]
public float EndHeight { get; set; }
[XmlElement("layersCount")]
public uint LayerCount { get; set; }
}
[Serializable]
public class VDAMachines
{
public string FileType { get; set; } = "ZIP File";
public string Resolution { get; set; } = "1920*1080P";
public string PixelXSize { get; set; } = "50um";
public string PixelYSize { get; set; } = "50um";
[XmlElement("Anti-Aliasing")]
public byte AntiAliasing { get; set; } = 1;
public float XLength { get; set; }
public float YWidth { get; set; }
public float ZHeight { get; set; }
}
public class VDALayer
{
[XmlElement("Index")]
public uint Index { get; set; }
[XmlElement("zvalue")]
public float ZPosition { get; set; }
[XmlElement("filename")]
public string Filename { get; set; }
public VDALayer()
{
}
public VDALayer(uint index, float zPosition, string filename)
{
Index = index;
ZPosition = zPosition;
Filename = filename;
}
}
public VDAFileInfo FileInfo { get; set; } = new();
public VDASlices Slices { get; set; } = new();
public VDAMachines Machines { get; set; } = new();
public List<VDALayer> Layers { get; set; } = new();
}
#endregion
public class VDAFile : FileFormat
{
#region Constants
#endregion
#region Properties
public VDARoot ManifestFile { get; set; } = new ();
public override FileFormatType FileType => FileFormatType.Archive;
public override FileExtension[] FileExtensions { get; } = {
new(typeof(VDAFile), "zip", "Voxeldance Additive Zip")
};
public override uint ResolutionX
{
get
{
var resolution = ManifestFile.Machines.Resolution.Split('*', StringSplitOptions.TrimEntries);
if (resolution.Length < 2) return 0;
uint.TryParse(resolution[0], out var xRes);
return xRes;
}
set
{
ManifestFile.Machines.Resolution = $"{value}*{ResolutionY}P";
RaisePropertyChanged();
}
}
public override uint ResolutionY
{
get
{
var resolution = ManifestFile.Machines.Resolution.Split('*', StringSplitOptions.TrimEntries);
if (resolution.Length < 2) return 0;
resolution[1] = resolution[1].TrimEnd('P');
uint.TryParse(resolution[1], out var yRes);
return yRes;
}
set
{
ManifestFile.Machines.Resolution = $"{ResolutionX}*{value}P";
RaisePropertyChanged();
}
}
public override float DisplayWidth
{
get
{
if (ManifestFile.Machines.XLength > 0) return ManifestFile.Machines.XLength;
var umStr= ManifestFile.Machines.PixelXSize.Replace("um", string.Empty, StringComparison.OrdinalIgnoreCase);
if (ushort.TryParse(umStr, out var um) && um > 0)
{
return (float) Math.Round(ResolutionX * um / 1000f, 2);
}
return ManifestFile.Machines.XLength;
}
set
{
ManifestFile.Machines.XLength = (float) Math.Round(value, 2);
ManifestFile.Machines.PixelXSize = $"{Math.Round(value / ResolutionX * 1000, 2)}um";
RaisePropertyChanged();
}
}
public override float DisplayHeight
{
get
{
if (ManifestFile.Machines.YWidth > 0) return ManifestFile.Machines.YWidth;
var umStr = ManifestFile.Machines.PixelYSize.Replace("um", string.Empty, StringComparison.OrdinalIgnoreCase);
if (ushort.TryParse(umStr, out var um) && um > 0)
{
return (float)Math.Round(ResolutionY * um / 1000f, 2);
}
return ManifestFile.Machines.YWidth;
}
set
{
ManifestFile.Machines.YWidth = (float)Math.Round(value, 2);
ManifestFile.Machines.PixelYSize = $"{Math.Round(value / ResolutionY * 1000, 2)}um";
RaisePropertyChanged();
}
}
public override float MachineZ
{
get => ManifestFile.Machines.ZHeight > 0 ? ManifestFile.Machines.ZHeight : base.MachineZ;
set
{
ManifestFile.Machines.ZHeight = value;
RaisePropertyChanged();
}
}
public override byte AntiAliasing
{
get => ManifestFile.Machines.AntiAliasing;
set => base.AntiAliasing = ManifestFile.Machines.AntiAliasing = value.Clamp(1, 16);
}
public override float LayerHeight
{
get => ManifestFile.Slices.LayerHeight;
set
{
ManifestFile.Slices.LayerHeight = Layer.RoundHeight(value);
RaisePropertyChanged();
}
}
public override uint LayerCount
{
get => base.LayerCount;
set => base.LayerCount = ManifestFile.Slices.LayerCount = base.LayerCount;
}
public override object[] Configs => new object[] {
ManifestFile.FileInfo.Version,
ManifestFile.FileInfo.Written,
ManifestFile.Machines,
ManifestFile.Slices };
#endregion
#region Constructor
public VDAFile()
{ }
#endregion
#region Methods
public override bool CanProcess(string fileFullPath)
{
if(!base.CanProcess(fileFullPath)) return false;
try
{
using var zip = ZipFile.Open(fileFullPath, ZipArchiveMode.Read);
if (zip.Entries.Any(entry => entry.Name.EndsWith(".xml"))) return true;
}
catch (Exception e)
{
Debug.WriteLine(e);
}
return false;
}
protected override void EncodeInternally(OperationProgress progress)
{
using var outputFile = ZipFile.Open(FileFullPath, ZipArchiveMode.Create);
var manifestFilename = Filename.
Replace($".{FileExtensions[0].Extension}{TemporaryFileAppend}", ".xml").
Replace($".{FileExtensions[0].Extension}", ".xml");
for (uint layerIndex = 0; layerIndex < LayerCount; layerIndex++)
{
progress.Token.ThrowIfCancellationRequested();
var layer = this[layerIndex];
var filename = $"{layerIndex + 1}".PadLeft(4, '0') + ".png";
outputFile.PutFileContent(filename, layer.CompressedBytes, ZipArchiveMode.Create);
progress++;
}
UpdateManifest();
XmlSerializer serializer = new(ManifestFile.GetType());
XmlSerializerNamespaces ns = new();
ns.Add("", "");
var entry = outputFile.CreateEntry(manifestFilename);
using var stream = entry.Open();
serializer.Serialize(stream, ManifestFile, ns);
}
protected override void DecodeInternally(OperationProgress progress)
{
using (var inputFile = ZipFile.Open(FileFullPath, ZipArchiveMode.Read))
{
var entry = inputFile.Entries.FirstOrDefault(zipEntry => zipEntry.Name.EndsWith(".xml"));
if (entry is null)
{
Clear();
throw new FileLoadException($".xml manifest not found", FileFullPath);
}
try
{
var serializer = new XmlSerializer(ManifestFile.GetType());
using var stream = entry.Open();
ManifestFile = (VDARoot)serializer.Deserialize(stream);
}
catch (Exception e)
{
Clear();
throw new FileLoadException($"Unable to deserialize '{entry.Name}'\n{e}", FileFullPath);
}
LayerManager.Init(ManifestFile.Slices.LayerCount, DecodeType == FileDecodeType.Partial);
progress.Reset(OperationProgress.StatusDecodeLayers, LayerCount);
for (uint layerIndex = 0; layerIndex < LayerCount; layerIndex++)
{
if (progress.Token.IsCancellationRequested) break;
var filename = $"{layerIndex + 1}".PadLeft(4, '0')+".png";
entry = inputFile.GetEntry(filename);
if (entry is null)
{
Clear();
throw new FileLoadException($"Layer {filename} not found", FileFullPath);
}
if (DecodeType == FileDecodeType.Full)
{
using var stream = entry.Open();
this[layerIndex] = new Layer(layerIndex, stream, LayerManager);
}
progress++;
}
}
LayerManager.GetBoundingRectangle(progress);
}
protected override void PartialSaveInternally(OperationProgress progress)
{
using var outputFile = ZipFile.Open(FileFullPath, ZipArchiveMode.Update);
bool deleted;
do
{
deleted = false;
foreach (var zipEntry in outputFile.Entries)
{
if (!zipEntry.Name.EndsWith(".xml")) continue;
zipEntry.Delete();
deleted = true;
break;
}
} while (deleted);
var manifestFilename = Path.GetFileName(FileFullPath).
Replace($".{FileExtensions[0].Extension}{TemporaryFileAppend}", ".xml").
Replace($".{FileExtensions[0].Extension}", ".xml");
UpdateManifest();
XmlSerializer serializer = new(ManifestFile.GetType());
XmlSerializerNamespaces ns = new();
ns.Add("", "");
var entry = outputFile.CreateEntry(manifestFilename);
using var stream = entry.Open();
serializer.Serialize(stream, ManifestFile, ns);
}
public void UpdateManifest()
{
ManifestFile.FileInfo.Written.Reset();
ManifestFile.Slices.StartHeight = FirstLayer.PositionZ;
ManifestFile.Slices.EndHeight = LastLayer.PositionZ;
ManifestFile.Layers.Clear();
for (uint layerIndex = 0; layerIndex < LayerCount; layerIndex++)
{
var layer = this[layerIndex];
ManifestFile.Layers.Add(new VDARoot.VDALayer(layerIndex, layer.PositionZ, layer.FormatFileName(4, false)));
}
}
#endregion
}
}
|