From bdcd58f93d4f15951b68de6995e31d7d5b203e3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tiago=20Concei=C3=A7=C3=A3o?= Date: Wed, 7 Apr 2021 04:12:48 +0100 Subject: v2.8.3 * File formats: Sanitize and check layers on encoding/saving file, will thrown a error and prevent the save if found any * GCode Parser: Do not sanitize the lack of lift height on a file to allow read files back with no lift's on the layers * CWS: Zips containing files without numbers was interrupting the decode method on first cath (#180) * Tool - Change resolution: Only manipulate the layer image if the new resolution is different from the image resolution --- CHANGELOG.md | 7 +++++ UVtools.Core/FileFormats/CWSFile.cs | 2 +- UVtools.Core/FileFormats/FileFormat.cs | 20 +++----------- UVtools.Core/GCode/GCodeBuilder.cs | 4 +-- UVtools.Core/Layer/LayerManager.cs | 31 ++++++++++++++++++++++ .../Operations/OperationChangeResolution.cs | 23 +++++++++------- UVtools.Core/UVtools.Core.csproj | 2 +- UVtools.ScriptSample/ScriptInsetSample.cs | 2 +- UVtools.WPF/MainWindow.axaml.cs | 3 +-- UVtools.WPF/UVtools.WPF.csproj | 2 +- 10 files changed, 62 insertions(+), 34 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e1d1a8c..81741cf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # Changelog +## 07/04/2021 - v2.8.3 + +* File formats: Sanitize and check layers on encoding/saving file, will thrown a error and prevent the save if found any +* GCode Parser: Do not sanitize the lack of lift height on a file to allow read files back with no lift's on the layers +* CWS: Zips containing files without numbers was interrupting the decode method on first cath (#180) +* Tool - Change resolution: Only manipulate the layer image if the new resolution is different from the image resolution + ## 05/04/2021 - v2.8.2 * **Operations:** diff --git a/UVtools.Core/FileFormats/CWSFile.cs b/UVtools.Core/FileFormats/CWSFile.cs index f8863bb..3f6e1d7 100644 --- a/UVtools.Core/FileFormats/CWSFile.cs +++ b/UVtools.Core/FileFormats/CWSFile.cs @@ -790,7 +790,7 @@ namespace UVtools.Core.FileFormats layerIndexStr = $"{layerStr[i]}{layerIndexStr}"; } - if (string.IsNullOrEmpty(layerIndexStr)) return; + if (string.IsNullOrEmpty(layerIndexStr)) continue; if (!uint.TryParse(layerIndexStr, out var layerIndex)) continue; using var stream = pngEntry.Open(); this[layerIndex] = new Layer(layerIndex, stream, LayerManager); diff --git a/UVtools.Core/FileFormats/FileFormat.cs b/UVtools.Core/FileFormats/FileFormat.cs index c36790d..d5cdbbf 100644 --- a/UVtools.Core/FileFormats/FileFormat.cs +++ b/UVtools.Core/FileFormats/FileFormat.cs @@ -15,7 +15,6 @@ using System.Drawing; using System.IO; using System.Linq; using System.Reflection; -using System.Text; using System.Threading.Tasks; using Emgu.CV; using Emgu.CV.CvEnum; @@ -1300,6 +1299,8 @@ namespace UVtools.Core.FileFormats progress ??= new OperationProgress(); progress.Reset(OperationProgress.StatusEncodeLayers, LayerCount); + _layerManager.Sanitize(); + FileFullPath = fileFullPath; if (File.Exists(fileFullPath)) @@ -1351,23 +1352,8 @@ namespace UVtools.Core.FileFormats "Lower and fix your layer height on slicer to avoid precision errors.", fileFullPath); } - // Sanitize - for (uint layerIndex = 0; layerIndex < LayerCount; layerIndex++) - { - // Check for null layers - if(this[layerIndex] is null) throw new FileLoadException($"Layer {layerIndex} was defined but doesn't contain a valid image.", fileFullPath); - if(layerIndex <= 0) continue; - // Check for bigger position z than it successor - if(this[layerIndex-1].PositionZ > this[layerIndex].PositionZ) throw new FileLoadException($"Layer {layerIndex-1} ({this[layerIndex - 1].PositionZ}mm) have a higher Z position than the successor layer {layerIndex} ({this[layerIndex].PositionZ}mm).\n", fileFullPath); - } - - // Fix 0mm positions at layer 0 - if(this[0].PositionZ == 0) + if (_layerManager.Sanitize()) { - for (uint layerIndex = 0; layerIndex < LayerCount; layerIndex++) - { - this[layerIndex].PositionZ += LayerHeight; - } Save(progress); } } diff --git a/UVtools.Core/GCode/GCodeBuilder.cs b/UVtools.Core/GCode/GCodeBuilder.cs index 211832d..ebe238b 100644 --- a/UVtools.Core/GCode/GCodeBuilder.cs +++ b/UVtools.Core/GCode/GCodeBuilder.cs @@ -625,8 +625,8 @@ namespace UVtools.Core.GCode gcode = gcode.Substring(gcode.IndexOf(startStr, StringComparison.InvariantCultureIgnoreCase) + startStr.Length + 1); var endStrIndex = gcode.IndexOf(endStr, StringComparison.Ordinal); var stripGcode = endStrIndex > 0 ? gcode.Substring(0, endStrIndex) : gcode;/*.Trim(' ', '\n', '\r', '\t');*/ - - float liftHeight = slicerFile.GetInitialLayerValueOrNormal(layerIndex, slicerFile.BottomLiftHeight, slicerFile.LiftHeight); + + float liftHeight = 0;// this allow read back no lifts slicerFile.GetInitialLayerValueOrNormal(layerIndex, slicerFile.BottomLiftHeight, slicerFile.LiftHeight); float liftSpeed = slicerFile.GetInitialLayerValueOrNormal(layerIndex, slicerFile.BottomLiftSpeed, slicerFile.LiftSpeed); float retractSpeed = slicerFile.RetractSpeed; float lightOffDelay = 0; diff --git a/UVtools.Core/Layer/LayerManager.cs b/UVtools.Core/Layer/LayerManager.cs index e601878..1862223 100644 --- a/UVtools.Core/Layer/LayerManager.cs +++ b/UVtools.Core/Layer/LayerManager.cs @@ -393,6 +393,37 @@ namespace UVtools.Core #region Methods + /// + /// Sanitize layers and thrown exception if a severe problem is found + /// + /// True if one or more corrections has been applied, otherwise false + public bool Sanitize() + { + bool appliedCorrections = false; + + for (uint layerIndex = 0; layerIndex < LayerCount; layerIndex++) + { + // Check for null layers + if (this[layerIndex] is null) throw new InvalidDataException($"Layer {layerIndex} was defined but doesn't contain a valid image."); + if (layerIndex <= 0) continue; + // Check for bigger position z than it successor + if (this[layerIndex - 1].PositionZ > this[layerIndex].PositionZ) throw new InvalidDataException($"Layer {layerIndex - 1} ({this[layerIndex - 1].PositionZ}mm) have a higher Z position than the successor layer {layerIndex} ({this[layerIndex].PositionZ}mm).\n"); + } + + // Fix 0mm positions at layer 0 + if (this[0].PositionZ == 0) + { + for (uint layerIndex = 0; layerIndex < LayerCount; layerIndex++) + { + this[layerIndex].PositionZ = Layer.RoundHeight(this[layerIndex].PositionZ + SlicerFile.LayerHeight); + } + + appliedCorrections = true; + } + + return appliedCorrections; + } + /// /// Rebuild layer properties based on slice settings /// diff --git a/UVtools.Core/Operations/OperationChangeResolution.cs b/UVtools.Core/Operations/OperationChangeResolution.cs index e89aeab..334833b 100644 --- a/UVtools.Core/Operations/OperationChangeResolution.cs +++ b/UVtools.Core/Operations/OperationChangeResolution.cs @@ -158,20 +158,25 @@ namespace UVtools.Core.Operations { progress.ItemCount = SlicerFile.LayerCount; var boundingRectangle = SlicerFile.BoundingRectangle; + var newSize = new Size((int) NewResolutionX, (int) NewResolutionY); Parallel.For(0, SlicerFile.LayerCount, layerIndex => { if (progress.Token.IsCancellationRequested) return; using var mat = SlicerFile[layerIndex].LayerMat; - using var matRoi = new Mat(mat, boundingRectangle); - using var matDst = new Mat(new Size((int)NewResolutionX, (int)NewResolutionY), mat.Depth, mat.NumberOfChannels); - using var matDstRoi = new Mat(matDst, - new Rectangle((int)(NewResolutionX / 2 - boundingRectangle.Width / 2), - (int)NewResolutionY / 2 - boundingRectangle.Height / 2, - boundingRectangle.Width, boundingRectangle.Height)); - matRoi.CopyTo(matDstRoi); - //Execute(mat); - SlicerFile[layerIndex].LayerMat = matDst; + + if (mat.Size != newSize) + { + using var matRoi = new Mat(mat, boundingRectangle); + using var matDst = new Mat(newSize, mat.Depth, mat.NumberOfChannels); + using var matDstRoi = new Mat(matDst, + new Rectangle((int) (NewResolutionX / 2 - boundingRectangle.Width / 2), + (int) NewResolutionY / 2 - boundingRectangle.Height / 2, + boundingRectangle.Width, boundingRectangle.Height)); + matRoi.CopyTo(matDstRoi); + //Execute(mat); + SlicerFile[layerIndex].LayerMat = matDst; + } progress.LockAndIncrement(); }); diff --git a/UVtools.Core/UVtools.Core.csproj b/UVtools.Core/UVtools.Core.csproj index 6893a21..e0cf444 100644 --- a/UVtools.Core/UVtools.Core.csproj +++ b/UVtools.Core/UVtools.Core.csproj @@ -10,7 +10,7 @@ https://github.com/sn4k3/UVtools https://github.com/sn4k3/UVtools MSLA/DLP, file analysis, calibration, repair, conversion and manipulation - 2.8.2 + 2.8.3 Copyright © 2020 PTRTECH UVtools.png AnyCPU;x64 diff --git a/UVtools.ScriptSample/ScriptInsetSample.cs b/UVtools.ScriptSample/ScriptInsetSample.cs index 9bfdcba..015fe04 100644 --- a/UVtools.ScriptSample/ScriptInsetSample.cs +++ b/UVtools.ScriptSample/ScriptInsetSample.cs @@ -90,7 +90,7 @@ namespace UVtools.ScriptSample Progress.Reset("Inset layers", Operation.LayerRangeCount); // Sets the progress name and number of items to process // Loop user selected layers in parallel, this will put each core of CPU working here on parallel - Parallel.For(Operation.LayerIndexStart, Operation.LayerIndexEnd, layerIndex => + Parallel.For(Operation.LayerIndexStart, Operation.LayerIndexEnd+1, layerIndex => { if (Progress.Token.IsCancellationRequested) return; // Abort operation, user requested cancellation diff --git a/UVtools.WPF/MainWindow.axaml.cs b/UVtools.WPF/MainWindow.axaml.cs index 2c5ec87..5e4b3d6 100644 --- a/UVtools.WPF/MainWindow.axaml.cs +++ b/UVtools.WPF/MainWindow.axaml.cs @@ -11,7 +11,6 @@ using Avalonia.Input; using Avalonia.Interactivity; using Avalonia.Markup.Xaml; using Avalonia.Threading; -using Emgu.CV; using MessageBox.Avalonia.Enums; using System; using System.Collections.Generic; @@ -1142,7 +1141,7 @@ namespace UVtools.WPF if (mat.Size != SlicerFile.Resolution) { await this.MessageBoxWaring($"Layer image resolution of {mat.Size} mismatch with printer resolution of {SlicerFile.Resolution}.\n" + - "Printing this file can lead to problems or malformed model, please verify your slicing settings;\n" + + "Printing this file can lead to problems or malformed model, please verify your slicer printer settings;\n" + "Processing this file with some of the tools can lead to program crash or misfunction;\n" + "If you used PrusaSlicer to slice this file, you must use it with compatible UVtools printer profiles (Help - Install profiles into PrusaSlicer).", "File and layer resolution mismatch!"); diff --git a/UVtools.WPF/UVtools.WPF.csproj b/UVtools.WPF/UVtools.WPF.csproj index e6a7ed6..192c8c1 100644 --- a/UVtools.WPF/UVtools.WPF.csproj +++ b/UVtools.WPF/UVtools.WPF.csproj @@ -12,7 +12,7 @@ LICENSE https://github.com/sn4k3/UVtools Git - 2.8.2 + 2.8.3 -- cgit v1.2.3