Created
April 7, 2022 17:30
-
-
Save kamicane/bcf9a80af2fcd0792eecd88f3117b5ba to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using UVtools.Core; | |
using UVtools.Core.Extensions; | |
using UVtools.Core.Scripting; | |
namespace UVtools.ScriptSample; | |
public class LayerDuper : ScriptGlobals | |
{ | |
readonly ScriptNumericalInput<ushort> NumClonesInput = new() | |
{ | |
Label = "Duplicates", | |
Unit = "layer(s)", | |
Minimum = 1, | |
Maximum = 1000, | |
Increment = 1, | |
Value = 10, | |
DecimalPlates = 0 | |
}; | |
readonly ScriptNumericalInput<ushort> EveryLayerInput = new() | |
{ | |
Label = "Every", | |
Unit = "layer(s)", | |
Minimum = 1, | |
Maximum = 1000, | |
Increment = 1, | |
Value = 2, | |
DecimalPlates = 0 | |
}; | |
public void ScriptInit() | |
{ | |
Script.Name = "Layer Duper"; | |
Script.Description = "duplicate layers"; | |
Script.Author = "kamicane"; | |
Script.Version = new Version(0, 1); | |
Script.UserInputs.Add(NumClonesInput); | |
Script.UserInputs.Add(EveryLayerInput); | |
} | |
public string? ScriptValidate() | |
{ | |
return null; | |
} | |
public bool ScriptExecute() | |
{ | |
int numClones = NumClonesInput.Value; | |
int sliceIndex = 0; | |
for (int i = 0; i < numClones; i++) { | |
var clone = SlicerFile[sliceIndex].Clone(); | |
SlicerFile.Insert(sliceIndex, clone); | |
sliceIndex+=EveryLayerInput.Value + 1; | |
} | |
// return true if not cancelled by user | |
return !Progress.Token.IsCancellationRequested; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Calling
SlicerFile.Insert
in a loop is not efficient due it's allocation and rebuild need.Consider using
SlicerFile.ReallocateStart(numClones)
before loop, and then useSlicerFile[sliceIndex] = clone
The reallocate method will not rebuild properties nor recompute layer Z pos, to attain that effect call after the loop:
SlicerFile.RebuildLayersProperties()
Edit: However that solution will only work if
EveryLayerInput.Value == 1
To be most efficient and work with any
EveryLayerInput.Value
do:Alternative, but previous still better:
That will take a lot of overhead if loop is large