mirror of
https://github.com/riegera2412/UVtools.git
synced 2026-07-14 04:17:41 +02:00
097bbb867d
* **PrusaSlicer:**
* **In this release is recommended to discard your printer and refresh it with uvtools updated printer or replace notes over**
* (Add) FILEFORMAT_XXX variable to auto-convert to that file format once open in UVtools
* (Update) Print profiles fields with new PrusaSlicer version
* (Remove) LayerOffDelay from printer notes and use only the LightOffDelay variable instead, both were being used, to avoid redundacy LayerOffDelay was dropped. Please update your printer accordingly!
* (Remove) FLIP_XY compability from printers
* (Remove) AntiAlias variable from printers
* **(Add) Settings - Automations:**
* Auto save the file after apply any automation(s)
* Auto convert SL1 files to the target format when possible and load it back
* Auto set the extra 'light-off delay' based on lift height and speed.
* (Add) Allow all and future formats to convert between them without knowing each other (Abstraction)
* (Add) XYResolution and XYResolutionUm property to file formats
* (Add) Calculator - Optimal model tilt: Calculates the optimal model tilt angle for printing and to minimize the visual layer effect
* (Add) Bottom layer count to the status bar
* **(Add) FileFormat propertiers:**
* MirrorDisplay: If images need to be mirrored on lcd to print on the correct orientation (If available)
* MaxPrintHeight: The maximum Z build volume of the printer (If available)
* (Add) ZCodex: Print paramenter light-off delay"
* (Add) SL1: Implement missing keys: host_type, physical_printer_settings_id and support_small_pillar_diameter_percent
* (Change) File formats: Round all setters floats to 2 decimals
* (Change) Island Repair: "Remove Islands Below Equal Pixels" limit from 255 to 65535 (#124)
* (Change) LightOffTime variables to LayerOffDelay
* (Fix) Files with upper case extensions doesn't load in
* **(Fix) SL1:**
* Prevent error when bottle volume is 0
* bool values were incorrectly parsed
* (Fix) **ZIP:**
* Material volume was set to grams
* Bed Y was not being set
532 lines
19 KiB
C#
532 lines
19 KiB
C#
/*
|
|
* 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 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 UVJFile : FileFormat
|
|
{
|
|
#region Constants
|
|
|
|
private const string FileConfigName = "config.json";
|
|
private const string FolderImageName = "slice";
|
|
private const string FolderPreviewName = "preview";
|
|
private const string FilePreviewHugeName = "preview/huge.png";
|
|
private const string FilePreviewTinyName = "preview/tiny.png";
|
|
#endregion
|
|
|
|
#region Sub Classes
|
|
|
|
public class Millimeter
|
|
{
|
|
public float X { get; set; }
|
|
public float Y { get; set; }
|
|
}
|
|
|
|
public class Size
|
|
{
|
|
public ushort X { get; set; }
|
|
public ushort Y { get; set; }
|
|
|
|
public Millimeter Millimeter { get; set; } = new Millimeter();
|
|
|
|
public uint Layers { get; set; }
|
|
public float LayerHeight { get; set; }
|
|
|
|
public override string ToString()
|
|
{
|
|
return $"{nameof(X)}: {X}, {nameof(Y)}: {Y}, {nameof(Millimeter)}: {Millimeter}, {nameof(Layers)}: {Layers}, {nameof(LayerHeight)}: {LayerHeight}";
|
|
}
|
|
}
|
|
|
|
public class Exposure
|
|
{
|
|
public float LightOnTime { get; set; }
|
|
public float LightOffTime { get; set; }
|
|
public byte LightPWM { get; set; } = 255;
|
|
public float LiftHeight { get; set; } = 5;
|
|
public float LiftSpeed { get; set; } = 100;
|
|
public float RetractHeight { get; set; }
|
|
public float RetractSpeed { get; set; } = 100;
|
|
|
|
public override string ToString()
|
|
{
|
|
return $"{nameof(LightOnTime)}: {LightOnTime}, {nameof(LightOffTime)}: {LightOffTime}, {nameof(LightPWM)}: {LightPWM}, {nameof(LiftHeight)}: {LiftHeight}, {nameof(LiftSpeed)}: {LiftSpeed}, {nameof(RetractHeight)}: {RetractHeight}, {nameof(RetractSpeed)}: {RetractSpeed}";
|
|
}
|
|
}
|
|
|
|
public class Bottom
|
|
{
|
|
public float LightOnTime { get; set; }
|
|
public float LightOffTime { get; set; }
|
|
public byte LightPWM { get; set; } = 255;
|
|
public float LiftHeight { get; set; } = 5;
|
|
public float LiftSpeed { get; set; } = 100;
|
|
public float RetractHeight { get; set; }
|
|
public float RetractSpeed { get; set; } = 100;
|
|
public ushort Count { get; set; }
|
|
|
|
public override string ToString()
|
|
{
|
|
return $"{nameof(LightOnTime)}: {LightOnTime}, {nameof(LightOffTime)}: {LightOffTime}, {nameof(LightPWM)}: {LightPWM}, {nameof(LiftHeight)}: {LiftHeight}, {nameof(LiftSpeed)}: {LiftSpeed}, {nameof(RetractHeight)}: {RetractHeight}, {nameof(RetractSpeed)}: {RetractSpeed}, {nameof(Count)}: {Count}";
|
|
}
|
|
}
|
|
|
|
public class LayerData
|
|
{
|
|
public float Z { get; set; }
|
|
public Exposure Exposure { get; set; }
|
|
|
|
public override string ToString()
|
|
{
|
|
return $"{nameof(Z)}: {Z}, {nameof(Exposure)}: {Exposure}";
|
|
}
|
|
}
|
|
|
|
public class Properties
|
|
{
|
|
public Size Size { get; set; } = new Size();
|
|
public Exposure Exposure { get; set; } = new Exposure();
|
|
public Bottom Bottom { get; set; } = new Bottom();
|
|
public byte AntiAliasLevel { get; set; } = 1;
|
|
|
|
public override string ToString()
|
|
{
|
|
return $"{nameof(Size)}: {Size}, {nameof(Exposure)}: {Exposure}, {nameof(Bottom)}: {Bottom}, {nameof(AntiAliasLevel)}: {AntiAliasLevel}";
|
|
}
|
|
}
|
|
|
|
public class Settings
|
|
{
|
|
public Properties Properties { get; set; } = new Properties();
|
|
public List<LayerData> Layers { get; set; } = new List<LayerData>();
|
|
|
|
public override string ToString()
|
|
{
|
|
return $"{nameof(Properties)}: {Properties}, {nameof(Layers)}: {Layers.Count}";
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Properties
|
|
public Settings JsonSettings { get; set; } = new();
|
|
|
|
public override FileFormatType FileType => FileFormatType.Archive;
|
|
|
|
public override FileExtension[] FileExtensions { get; } = {
|
|
new("uvj", "UVJ")
|
|
};
|
|
|
|
public override PrintParameterModifier[] PrintParameterModifiers { get; } = {
|
|
PrintParameterModifier.BottomLayerCount,
|
|
PrintParameterModifier.BottomExposureSeconds,
|
|
PrintParameterModifier.ExposureSeconds,
|
|
|
|
PrintParameterModifier.BottomLightOffDelay,
|
|
PrintParameterModifier.LightOffDelay,
|
|
PrintParameterModifier.BottomLiftHeight,
|
|
PrintParameterModifier.BottomLiftSpeed,
|
|
PrintParameterModifier.LiftHeight,
|
|
PrintParameterModifier.LiftSpeed,
|
|
PrintParameterModifier.RetractSpeed,
|
|
|
|
PrintParameterModifier.BottomLightPWM,
|
|
PrintParameterModifier.LightPWM,
|
|
};
|
|
|
|
public override byte ThumbnailsCount { get; } = 2;
|
|
|
|
public override System.Drawing.Size[] ThumbnailsOriginalSize { get; } = {new System.Drawing.Size(400, 400), new System.Drawing.Size(800, 480) };
|
|
|
|
public override uint ResolutionX
|
|
{
|
|
get => JsonSettings.Properties.Size.X;
|
|
set
|
|
{
|
|
JsonSettings.Properties.Size.X = (ushort) value;
|
|
RaisePropertyChanged();
|
|
}
|
|
}
|
|
|
|
public override uint ResolutionY
|
|
{
|
|
get => JsonSettings.Properties.Size.Y;
|
|
set
|
|
{
|
|
JsonSettings.Properties.Size.Y = (ushort) value;
|
|
RaisePropertyChanged();
|
|
}
|
|
}
|
|
|
|
public override float DisplayWidth
|
|
{
|
|
get => JsonSettings.Properties.Size.Millimeter.X;
|
|
set
|
|
{
|
|
JsonSettings.Properties.Size.Millimeter.X = (float) Math.Round(value, 2);
|
|
RaisePropertyChanged();
|
|
}
|
|
}
|
|
|
|
public override float DisplayHeight
|
|
{
|
|
get => JsonSettings.Properties.Size.Millimeter.Y;
|
|
set
|
|
{
|
|
JsonSettings.Properties.Size.Millimeter.Y = (float)Math.Round(value, 2);
|
|
RaisePropertyChanged();
|
|
}
|
|
}
|
|
|
|
public override bool MirrorDisplay { get; set; }
|
|
|
|
public override byte AntiAliasing
|
|
{
|
|
get => JsonSettings.Properties.AntiAliasLevel;
|
|
set
|
|
{
|
|
JsonSettings.Properties.AntiAliasLevel = value.Clamp(1, 16);
|
|
RaisePropertyChanged();
|
|
}
|
|
|
|
}
|
|
|
|
public override bool SupportPerLayerSettings => true;
|
|
|
|
public override float LayerHeight
|
|
{
|
|
get => JsonSettings.Properties.Size.LayerHeight;
|
|
set
|
|
{
|
|
JsonSettings.Properties.Size.LayerHeight = (float)Math.Round(value, 2);
|
|
RaisePropertyChanged();
|
|
}
|
|
}
|
|
|
|
public override uint LayerCount
|
|
{
|
|
set
|
|
{
|
|
JsonSettings.Properties.Size.Layers = LayerCount;
|
|
RaisePropertyChanged();
|
|
RaisePropertyChanged(nameof(NormalLayerCount));
|
|
}
|
|
}
|
|
|
|
public override ushort BottomLayerCount
|
|
{
|
|
get => JsonSettings.Properties.Bottom.Count;
|
|
set
|
|
{
|
|
JsonSettings.Properties.Bottom.Count = value;
|
|
RaisePropertyChanged();
|
|
}
|
|
}
|
|
|
|
public override float BottomExposureTime
|
|
{
|
|
get => JsonSettings.Properties.Bottom.LightOnTime;
|
|
set
|
|
{
|
|
JsonSettings.Properties.Bottom.LightOnTime = (float)Math.Round(value, 2);
|
|
RaisePropertyChanged();
|
|
}
|
|
}
|
|
|
|
public override float ExposureTime
|
|
{
|
|
get => JsonSettings.Properties.Exposure.LightOnTime;
|
|
set
|
|
{
|
|
JsonSettings.Properties.Exposure.LightOnTime = (float)Math.Round(value, 2);
|
|
RaisePropertyChanged();
|
|
}
|
|
}
|
|
|
|
public override float BottomLightOffDelay
|
|
{
|
|
get => JsonSettings.Properties.Bottom.LightOffTime;
|
|
set
|
|
{
|
|
JsonSettings.Properties.Bottom.LightOffTime = (float)Math.Round(value, 2);
|
|
RaisePropertyChanged();
|
|
}
|
|
}
|
|
|
|
public override float LightOffDelay
|
|
{
|
|
get => JsonSettings.Properties.Exposure.LightOffTime;
|
|
set
|
|
{
|
|
JsonSettings.Properties.Exposure.LightOffTime = (float)Math.Round(value, 2);
|
|
RaisePropertyChanged();
|
|
}
|
|
}
|
|
|
|
public override float BottomLiftHeight
|
|
{
|
|
get => JsonSettings.Properties.Bottom.LiftHeight;
|
|
set
|
|
{
|
|
JsonSettings.Properties.Bottom.LiftHeight = (float)Math.Round(value, 2);
|
|
RaisePropertyChanged();
|
|
}
|
|
}
|
|
|
|
public override float LiftHeight
|
|
{
|
|
get => JsonSettings.Properties.Exposure.LiftHeight;
|
|
set
|
|
{
|
|
JsonSettings.Properties.Exposure.LiftHeight = (float)Math.Round(value, 2);
|
|
RaisePropertyChanged();
|
|
}
|
|
}
|
|
|
|
public override float BottomLiftSpeed
|
|
{
|
|
get => JsonSettings.Properties.Bottom.LiftSpeed;
|
|
set
|
|
{
|
|
JsonSettings.Properties.Bottom.LiftSpeed = (float)Math.Round(value, 2);
|
|
RaisePropertyChanged();
|
|
}
|
|
}
|
|
|
|
public override float LiftSpeed
|
|
{
|
|
get => JsonSettings.Properties.Exposure.LiftSpeed;
|
|
set
|
|
{
|
|
JsonSettings.Properties.Exposure.LiftSpeed = (float)Math.Round(value, 2);
|
|
RaisePropertyChanged();
|
|
}
|
|
}
|
|
|
|
public override float RetractSpeed
|
|
{
|
|
get => JsonSettings.Properties.Exposure.RetractSpeed;
|
|
set
|
|
{
|
|
JsonSettings.Properties.Exposure.RetractSpeed = (float)Math.Round(value, 2);
|
|
RaisePropertyChanged();
|
|
}
|
|
}
|
|
|
|
public override byte BottomLightPWM
|
|
{
|
|
get => JsonSettings.Properties.Bottom.LightPWM;
|
|
set
|
|
{
|
|
JsonSettings.Properties.Bottom.LightPWM = value;
|
|
RaisePropertyChanged();
|
|
}
|
|
}
|
|
|
|
public override byte LightPWM
|
|
{
|
|
get => JsonSettings.Properties.Exposure.LightPWM;
|
|
set
|
|
{
|
|
JsonSettings.Properties.Exposure.LightPWM = value;
|
|
RaisePropertyChanged();
|
|
}
|
|
}
|
|
|
|
public override object[] Configs => new[] {(object) JsonSettings.Properties.Size, JsonSettings.Properties.Size.Millimeter, JsonSettings.Properties.Bottom, JsonSettings.Properties.Exposure};
|
|
#endregion
|
|
|
|
#region Methods
|
|
|
|
public override void Clear()
|
|
{
|
|
base.Clear();
|
|
JsonSettings.Layers = new List<LayerData>();
|
|
}
|
|
|
|
public override void Encode(string fileFullPath, OperationProgress progress = null)
|
|
{
|
|
progress ??= new OperationProgress();
|
|
progress.Reset(OperationProgress.StatusEncodeLayers, LayerCount);
|
|
|
|
base.Encode(fileFullPath, progress);
|
|
|
|
// Redo layer data
|
|
JsonSettings.Layers.Clear();
|
|
for (uint layerIndex = 0; layerIndex < LayerCount; layerIndex++)
|
|
{
|
|
var layer = this[layerIndex];
|
|
JsonSettings.Layers.Add(new LayerData
|
|
{
|
|
Z = layer.PositionZ,
|
|
Exposure = new Exposure
|
|
{
|
|
LiftHeight = layer.LiftHeight,
|
|
LiftSpeed = layer.LiftSpeed,
|
|
RetractHeight = layer.LiftHeight+1,
|
|
RetractSpeed = layer.RetractSpeed,
|
|
LightOffTime = layer.LightOffDelay,
|
|
LightOnTime = layer.ExposureTime,
|
|
LightPWM = layer.LightPWM
|
|
}
|
|
});
|
|
}
|
|
|
|
using (ZipArchive outputFile = ZipFile.Open(fileFullPath, ZipArchiveMode.Create))
|
|
{
|
|
outputFile.PutFileContent(FileConfigName, JsonConvert.SerializeObject(JsonSettings), ZipArchiveMode.Create);
|
|
|
|
if (CreatedThumbnailsCount > 0)
|
|
{
|
|
using (Stream stream = outputFile.CreateEntry(FilePreviewTinyName).Open())
|
|
{
|
|
using (var vec = new VectorOfByte())
|
|
{
|
|
CvInvoke.Imencode(".png", Thumbnails[0], vec);
|
|
stream.WriteBytes(vec.ToArray());
|
|
stream.Close();
|
|
}
|
|
}
|
|
}
|
|
|
|
if (CreatedThumbnailsCount > 1)
|
|
{
|
|
using (Stream stream = outputFile.CreateEntry(FilePreviewHugeName).Open())
|
|
{
|
|
using (var vec = new VectorOfByte())
|
|
{
|
|
CvInvoke.Imencode(".png", Thumbnails[1], vec);
|
|
stream.WriteBytes(vec.ToArray());
|
|
stream.Close();
|
|
}
|
|
}
|
|
}
|
|
|
|
for (uint layerIndex = 0; layerIndex < LayerCount; layerIndex++)
|
|
{
|
|
progress.Token.ThrowIfCancellationRequested();
|
|
|
|
Layer layer = this[layerIndex];
|
|
|
|
var layerimagePath = $"{FolderImageName}/{layerIndex:D8}.png";
|
|
outputFile.PutFileContent(layerimagePath, 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(FileConfigName);
|
|
if (ReferenceEquals(entry, null))
|
|
{
|
|
Clear();
|
|
throw new FileLoadException($"{FileConfigName} not found", fileFullPath);
|
|
}
|
|
|
|
JsonSettings = Helpers.JsonDeserializeObject<Settings>(entry.Open());
|
|
|
|
LayerManager = new LayerManager(JsonSettings.Properties.Size.Layers, this);
|
|
|
|
entry = inputFile.GetEntry(FilePreviewTinyName);
|
|
if (!ReferenceEquals(entry, null))
|
|
{
|
|
using (Stream stream = entry.Open())
|
|
{
|
|
Mat image = new Mat();
|
|
CvInvoke.Imdecode(stream.ToArray(), ImreadModes.AnyColor, image);
|
|
Thumbnails[0] = image;
|
|
stream.Close();
|
|
}
|
|
}
|
|
|
|
entry = inputFile.GetEntry(FilePreviewHugeName);
|
|
if (!ReferenceEquals(entry, null))
|
|
{
|
|
using (Stream stream = entry.Open())
|
|
{
|
|
Mat image = new Mat();
|
|
CvInvoke.Imdecode(stream.ToArray(), ImreadModes.AnyColor, image);
|
|
Thumbnails[1] = image;
|
|
stream.Close();
|
|
}
|
|
}
|
|
|
|
for (uint layerIndex = 0; layerIndex < LayerCount; layerIndex++)
|
|
{
|
|
entry = inputFile.GetEntry($"{FolderImageName}/{layerIndex:D8}.png");
|
|
if (ReferenceEquals(entry, null)) continue;
|
|
|
|
this[layerIndex] = new Layer(layerIndex, entry.Open(), LayerManager)
|
|
{
|
|
PositionZ = JsonSettings.Layers.Count >= layerIndex ? JsonSettings.Layers[(int) layerIndex].Z : GetHeightFromLayer(layerIndex),
|
|
LiftHeight = JsonSettings.Layers.Count >= layerIndex ? JsonSettings.Layers[(int)layerIndex].Exposure.LiftHeight : GetInitialLayerValueOrNormal(layerIndex, BottomLiftHeight, LiftHeight),
|
|
LiftSpeed = JsonSettings.Layers.Count >= layerIndex ? JsonSettings.Layers[(int)layerIndex].Exposure.LiftSpeed : GetInitialLayerValueOrNormal(layerIndex, BottomLiftSpeed, LiftSpeed),
|
|
RetractSpeed = JsonSettings.Layers.Count >= layerIndex ? JsonSettings.Layers[(int)layerIndex].Exposure.RetractSpeed : RetractSpeed,
|
|
LightOffDelay = JsonSettings.Layers.Count >= layerIndex ? JsonSettings.Layers[(int)layerIndex].Exposure.LightOffTime : GetInitialLayerValueOrNormal(layerIndex, BottomLightOffDelay, LightOffDelay),
|
|
ExposureTime = JsonSettings.Layers.Count >= layerIndex ? JsonSettings.Layers[(int)layerIndex].Exposure.LightOnTime : GetInitialLayerValueOrNormal(layerIndex, BottomExposureTime, ExposureTime),
|
|
LightPWM = JsonSettings.Layers.Count >= layerIndex ? JsonSettings.Layers[(int)layerIndex].Exposure.LightPWM : GetInitialLayerValueOrNormal(layerIndex, BottomLightPWM, LightPWM),
|
|
};
|
|
}
|
|
|
|
progress.ProcessedItems++;
|
|
}
|
|
|
|
LayerManager.GetBoundingRectangle(progress);
|
|
}
|
|
|
|
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(FileConfigName, JsonConvert.SerializeObject(JsonSettings), ZipArchiveMode.Update);
|
|
}
|
|
|
|
//Decode(FileFullPath, progress);
|
|
}
|
|
#endregion
|
|
}
|
|
}
|