Welcome to mirror list, hosted at ThFree Co, Russian Federation.

OperationLayerClone.cs « Operations « UVtools.Core - github.com/sn4k3/UVtools.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9c591b95bd0fd116c5828d4d8befc392ae569eed (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
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
/*
 *                     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.Linq;
using System.Text;
using UVtools.Core.FileFormats;
using UVtools.Core.Layers;

namespace UVtools.Core.Operations;

[Serializable]
public sealed class OperationLayerClone : Operation
{
    #region Members
    private uint _clones = 1;
    private bool _keepSamePositionZ;

    #endregion

    #region Overrides

    public override LayerRangeSelection StartLayerRangeSelection => LayerRangeSelection.Current;
    public override bool CanROI => false;
    public override bool PassActualLayerIndex => true;
    public override string IconClass => "fa-solid fa-clone";
    public override string Title => "Clone layers";
    public override string Description =>
        "Clone layers.\n\n" +
        "Useful to increase the height of the model or add additional structure by duplicating layers. For example, can be used to increase the raft height for added stability.";
    public override string ConfirmationText =>
        $"clone layers {LayerIndexStart} through {LayerIndexEnd}, {Clones} time{(Clones != 1 ? "s" : "")}?";

    public override string ProgressTitle =>
        $"Cloning layers {LayerIndexStart} through {LayerIndexEnd}, {Clones} time{(Clones != 1 ? "s" : "")}";

    public override string ProgressAction => "Cloned layers";

    public override bool CanCancel => false;

    public override bool CanHaveProfiles => false;

    public override string? ValidateInternally()
    {
        var sb = new StringBuilder();
        if (Clones <= 0)
        {
            sb.AppendLine("Clones must be a positive number");
        }

        return sb.ToString();
    }

    public override string ToString()
    {
        var result = $"[Clones: {Clones}]" + LayerRangeString;
        if (!string.IsNullOrEmpty(ProfileName)) result = $"{ProfileName}: {result}";
        return result;
    }

    #endregion

    #region Properties

    /// <summary>
    /// Gets or sets if cloned layers will keep same position z or get the height rebuilt
    /// </summary>
    public bool KeepSamePositionZ
    {
        get => _keepSamePositionZ;
        set => RaiseAndSetIfChanged(ref _keepSamePositionZ, value);
    }

    /// <summary>
    /// Gets or sets the number of clones
    /// </summary>
    public uint Clones
    {
        get => _clones;
        set
        {
            if(!RaiseAndSetIfChanged(ref _clones, value)) return;
            RaisePropertyChanged(nameof(ExtraLayers));
        }
    }

    public uint ExtraLayers => (uint)Math.Max(0, ((int)LayerIndexEnd - LayerIndexStart + 1) * _clones);

    #endregion

    #region Constructor

    public OperationLayerClone() { }

    public OperationLayerClone(FileFormat slicerFile) : base(slicerFile) { }

    #endregion

    #region Methods

    void Increment()
    {

    }

    protected override bool ExecuteInternally(OperationProgress progress)
    {
        uint totalClones = (LayerIndexEnd - LayerIndexStart + 1) * Clones;
        progress.Reset(ProgressAction, totalClones);

        var oldLayers = SlicerFile.ToArray();

        SlicerFile.Init(SlicerFile.LayerCount + totalClones);

        //var newLayers = new Layer[SlicerFile.LayerCount + totalClones];
        uint newLayerIndex = 0;
        float incrementedPositionZ = 0;
        for (uint layerIndex = 0; layerIndex < oldLayers.Length; layerIndex++)
        {
            SlicerFile[newLayerIndex++] = oldLayers[layerIndex];

            if (!_keepSamePositionZ && incrementedPositionZ > 0)
            {
                oldLayers[layerIndex].PositionZ += incrementedPositionZ;
            }
            
            if (layerIndex < LayerIndexStart || layerIndex > LayerIndexEnd) continue;
            float increment = SlicerFile[layerIndex].RelativePositionZ;
            for (uint i = 0; i < _clones; i++)
            {
                SlicerFile[newLayerIndex] = oldLayers[layerIndex].Clone();

                if (!_keepSamePositionZ)
                {
                    incrementedPositionZ += increment;
                    SlicerFile[newLayerIndex].PositionZ += increment * (i + 1);
                }

                newLayerIndex++;
                progress++;
            }
        }

        SlicerFile.SuppressRebuildPropertiesWork(() =>
        {
            SlicerFile.Layers = SlicerFile.Layers; // Reassign for update
        });


        return !progress.Token.IsCancellationRequested;
    }

    #endregion
}