diff options
Diffstat (limited to 'UVtools.Core/Operations/OperationLayerRemove.cs')
-rw-r--r-- | UVtools.Core/Operations/OperationLayerRemove.cs | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/UVtools.Core/Operations/OperationLayerRemove.cs b/UVtools.Core/Operations/OperationLayerRemove.cs index 25dd621..45816dd 100644 --- a/UVtools.Core/Operations/OperationLayerRemove.cs +++ b/UVtools.Core/Operations/OperationLayerRemove.cs @@ -7,7 +7,10 @@ */ using System; +using System.Collections.Generic; using System.Text; +using Emgu.CV; +using UVtools.Core.FileFormats; namespace UVtools.Core.Operations { @@ -43,5 +46,68 @@ namespace UVtools.Core.Operations #endregion + + #region Methods + + public override bool Execute(FileFormat slicerFile, OperationProgress progress = null) + { + progress ??= new OperationProgress(false); + var layersRemove = new List<uint>(); + for (uint layerIndex = LayerIndexStart; layerIndex <= LayerIndexEnd; layerIndex++) + { + layersRemove.Add(layerIndex); + } + + return RemoveLayers(slicerFile, layersRemove, progress); + } + + public static bool RemoveLayers(FileFormat slicerFile, List<uint> layersRemove, OperationProgress progress = null) + { + if (layersRemove.Count == 0) return false; + + progress ??= new OperationProgress(false); + + progress.Reset("removed layers", (uint)layersRemove.Count); + + var oldLayers = slicerFile.LayerManager.Layers; + float layerHeight = slicerFile.LayerHeight; + + var layers = new Layer[oldLayers.Length - layersRemove.Count]; + + // Re-set + uint newLayerIndex = 0; + for (uint layerIndex = 0; layerIndex < oldLayers.Length; layerIndex++) + { + if (layersRemove.Contains(layerIndex)) continue; + layers[newLayerIndex] = oldLayers[layerIndex]; + layers[newLayerIndex].Index = newLayerIndex; + + // Re-Z + float posZ = layerHeight; + if (newLayerIndex > 0) + { + if (oldLayers[layerIndex - 1].PositionZ == oldLayers[layerIndex].PositionZ) + { + posZ = layers[newLayerIndex - 1].PositionZ; + } + else + { + posZ = (float)Math.Round(layers[newLayerIndex - 1].PositionZ + layerHeight, 2); + } + } + + layers[newLayerIndex].PositionZ = posZ; + layers[newLayerIndex].IsModified = true; + + newLayerIndex++; + progress++; + } + + slicerFile.LayerManager.Layers = layers; + slicerFile.RequireFullEncode = true; + + return true; + } + #endregion } } |