blob: 45816dd759f166e07a625988fd7ba5b51f21392d (
plain)
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
|
/*
* 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.Text;
using Emgu.CV;
using UVtools.Core.FileFormats;
namespace UVtools.Core.Operations
{
[Serializable]
public sealed class OperationLayerRemove : Operation
{
#region Overrides
public override Enumerations.LayerRangeSelection StartLayerRangeSelection => Enumerations.LayerRangeSelection.Current;
public override bool CanROI => false;
public override bool PassActualLayerIndex => true;
public override string Title => "Remove layers";
public override string Description =>
"Remove Layers in a given range.";
public override string ConfirmationText =>
$"remove layers {LayerIndexStart} through {LayerIndexEnd}?";
public override string ProgressTitle =>
$"Removing layers {LayerIndexStart} through {LayerIndexEnd}";
public override string ProgressAction => "Removed layers";
public override bool CanCancel => false;
public override bool CanHaveProfiles => false;
#endregion
#region Properties
#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
}
}
|