Skip to content

Commit

Permalink
v0.6.1.1
Browse files Browse the repository at this point in the history
* (Add) Allow chitubox, phz, pws, pw0 files convert to cws
* (Add) Allow convert between cbddlp, ctb and photon
* (Add) Allow convert between pws and pw0
* (Improvement) Layers can now have modified heights and independent parameters (#9)
* (Improvement) UVtools now generate better gcode and detect the lack of Lift and same z position and optimize the commands
* (Fix) zcodex: Wasn't reporting layer decoding progress
  • Loading branch information
sn4k3 committed Jul 4, 2020
1 parent 08fe746 commit 2c514fe
Show file tree
Hide file tree
Showing 31 changed files with 1,547 additions and 1,112 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# Changelog

## 02/07/2020 - v0.6.1.1

* (Add) Allow chitubox, phz, pws, pw0 files convert to cws
* (Add) Allow convert between cbddlp, ctb and photon
* (Add) Allow convert between pws and pw0
* (Improvement) Layers can now have modified heights and independent parameters (#9)
* (Improvement) UVtools now generate better gcode and detect the lack of Lift and same z position and optimize the commands
* (Fix) zcodex: Wasn't reporting layer decoding progress


## 02/07/2020 - v0.6.1.0

* (Add) Thumbnail image can now saved to clipboard
Expand Down
1 change: 1 addition & 0 deletions UVtools.Cmd/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Threading;
using System.Threading.Tasks;
using UVtools.Core;
using UVtools.Core.FileFormats;

namespace UVtools.Cmd
{
Expand Down
96 changes: 87 additions & 9 deletions UVtools.Core/CWSFile.cs → UVtools.Core/FileFormats/CWSFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
using UVtools.Core.Extensions;
using UVtools.Core.Operations;

namespace UVtools.Core
namespace UVtools.Core.FileFormats
{
public class CWSFile : FileFormat
{
Expand Down Expand Up @@ -290,15 +291,65 @@ public override void Decode(string fileFullPath, OperationProgress progress = nu

LayerManager = new LayerManager(OutputSettings.LayersNum);

var gcode = GCode.ToString();
float currentHeight = 0;

foreach (var zipArchiveEntry in inputFile.Entries)
{
if (!zipArchiveEntry.Name.EndsWith(".png")) continue;

// - .png - 4 numbers
int layerSize = OutputSettings.LayersNum.ToString().Length;
string layerStr = zipArchiveEntry.Name.Substring(zipArchiveEntry.Name.Length - 4 - layerSize, layerSize);
uint iLayer = uint.Parse(layerStr);
LayerManager[iLayer] = new Layer(iLayer, zipArchiveEntry.Open(), zipArchiveEntry.Name);
uint layerIndex = uint.Parse(layerStr);

var startStr = $"{GCodeKeywordSlice} {layerIndex}";
var stripGcode = gcode.Substring(gcode.IndexOf(startStr, StringComparison.InvariantCultureIgnoreCase) + startStr.Length);
stripGcode = stripGcode.Substring(0, stripGcode.IndexOf(GCodeKeywordDelay, stripGcode.IndexOf(GCodeKeywordSlice))).Trim(' ', '\n', '\r', '\t');
//var startCurrPos = stripGcode.Remove(0, ";currPos:".Length);

/*
*
;<Slice> 0
M106 S255
;<Delay> 45000
M106 S0
;<Slice> Blank
G1 Z4 F120
G1 Z-3.9 F120
;<Delay> 45000
*/

var currPos = Regex.Match(stripGcode, "G1 Z([+-]?([0-9]*[.])?[0-9]+)", RegexOptions.IgnoreCase);
var exposureTime = Regex.Match(stripGcode, ";<Delay> (\\d+)", RegexOptions.IgnoreCase);
/*var pwm = Regex.Match(stripGcode, "M106 S(\\d+)", RegexOptions.IgnoreCase);
if (layerIndex < InitialLayerCount)
{
OutputSettings.BottomLayerLightPWM = byte.Parse(pwm.Groups[1].Value);
}
else
{
OutputSettings.LayerLightPWM = byte.Parse(pwm.Groups[1].Value);
}*/

if (currPos.Success)
{
var nextMatch = currPos.NextMatch();
if (nextMatch.Success)
{
currentHeight = (float)Math.Round(currentHeight + float.Parse(currPos.Groups[1].Value) + float.Parse(currPos.NextMatch().Groups[1].Value), 2);
}
else
{
currentHeight = (float)Math.Round(currentHeight + float.Parse(currPos.Groups[1].Value), 2);
}
}

LayerManager[layerIndex] = new Layer(layerIndex, zipArchiveEntry.Open(), zipArchiveEntry.Name)
{
PositionZ = currentHeight,
ExposureTime = float.Parse(exposureTime.Groups[1].Value) / 1000f
};
}
}

Expand All @@ -318,24 +369,35 @@ public override object GetValueFromPrintParameterModifier(PrintParameterModifier

public override bool SetValueFromPrintParameterModifier(PrintParameterModifier modifier, string value)
{
void UpdateLayers()
{
for (uint layerIndex = 0; layerIndex < LayerCount; layerIndex++)
{
this[layerIndex].ExposureTime = GetInitialLayerValueOrNormal(layerIndex, InitialExposureTime, LayerExposureTime);
}
}

if (ReferenceEquals(modifier, PrintParameterModifier.InitialLayerCount))
{
SliceSettings.HeadLayersNum =
OutputSettings.NumberBottomLayers = value.Convert<ushort>();
UpdateLayers();
UpdateGCode();
return true;
}
if (ReferenceEquals(modifier, PrintParameterModifier.InitialExposureSeconds))
{
SliceSettings.HeadLayersExpoMs =
OutputSettings.BottomLayersTime = value.Convert<uint>()*1000;
UpdateLayers();
UpdateGCode();
return true;
}
if (ReferenceEquals(modifier, PrintParameterModifier.ExposureSeconds))
{
SliceSettings.LayersExpoMs =
OutputSettings.LayerTime = value.Convert<uint>() * 1000;
UpdateLayers();
UpdateGCode();
return true;
}
Expand Down Expand Up @@ -453,17 +515,33 @@ private void UpdateGCode()
GCode.AppendLine();
GCode.AppendFormat(GCodeStart, Environment.NewLine);

float lastZPosition = 0;

for (uint layerIndex = 0; layerIndex < LayerCount; layerIndex++)
{
//Layer layer = this[layerIndex];
Layer layer = this[layerIndex];
GCode.AppendLine($"{GCodeKeywordSlice} {layerIndex}");
GCode.AppendLine($"M106 S{GetInitialLayerValueOrNormal(layerIndex, OutputSettings.BottomLayerLightPWM, OutputSettings.LayerLightPWM)}");
GCode.AppendLine($"{GCodeKeywordDelay} {GetInitialLayerValueOrNormal(layerIndex, SliceSettings.HeadLayersExpoMs, SliceSettings.LayersExpoMs)}");
GCode.AppendLine($"{GCodeKeywordDelay} {layer.ExposureTime}");
GCode.AppendLine("M106 S0");
GCode.AppendLine(GCodeKeywordSliceBlank);
GCode.AppendLine($"G1 Z{LiftHeight} F{LiftSpeed}");
GCode.AppendLine($"G1 Z-{LiftHeight - LayerHeight} F{RetractSpeed}");
GCode.AppendLine($"{GCodeKeywordDelay} {GetInitialLayerValueOrNormal(layerIndex, SliceSettings.HeadLayersExpoMs, SliceSettings.LayersExpoMs)}");

if (lastZPosition != layer.PositionZ)
{
if (LiftHeight > 0)
{
GCode.AppendLine($"G1 Z{LiftHeight} F{LiftSpeed}");
GCode.AppendLine($"G1 Z-{LiftHeight - layer.PositionZ + lastZPosition} F{RetractSpeed}");
}
else
{
GCode.AppendLine($"G1 Z{layer.PositionZ - lastZPosition} F{LiftSpeed}");
}
}

GCode.AppendLine($"{GCodeKeywordDelay} {layer.ExposureTime}");

lastZPosition = layer.PositionZ;
}

GCode.AppendFormat(GCodeEnd, Environment.NewLine, SliceSettings.LiftWhenFinished);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@
using Emgu.CV;
using Emgu.CV.CvEnum;
using UVtools.Core.Extensions;
using UVtools.Core.Operations;

namespace UVtools.Core
namespace UVtools.Core.FileFormats
{
public class ChituboxFile : FileFormat
{
Expand Down Expand Up @@ -508,15 +509,16 @@ public LayerData()
public LayerData(ChituboxFile parent, uint layerIndex)
{
Parent = parent;
LayerPositionZ = parent.GetHeightFromLayer(layerIndex);
LayerPositionZ = parent[layerIndex].PositionZ;
LayerExposure = parent[layerIndex].ExposureTime;

LayerOffTimeSeconds = layerIndex < parent.HeaderSettings.BottomLayersCount
? parent.PrintParametersSettings.BottomLightOffDelay
: parent.PrintParametersSettings.LightOffDelay;
LayerOffTimeSeconds = parent.GetInitialLayerValueOrNormal(layerIndex,
parent.PrintParametersSettings.BottomLightOffDelay,
parent.PrintParametersSettings.LightOffDelay);

LayerExposure = layerIndex < parent.HeaderSettings.BottomLayersCount
/*LayerExposure = layerIndex < parent.HeaderSettings.BottomLayersCount
? parent.HeaderSettings.BottomExposureSeconds
: parent.HeaderSettings.LayerExposureSeconds;
: parent.HeaderSettings.LayerExposureSeconds;*/
}

public Mat Decode(uint layerIndex, bool consumeData = true)
Expand All @@ -531,9 +533,6 @@ public Mat Decode(uint layerIndex, bool consumeData = true)

public static Mat DecodeCbddlpImage(ChituboxFile parent, uint layerIndex)
{
//Mat image = new Mat(new Size((int)parent.HeaderSettings.ResolutionX, (int)parent.HeaderSettings.ResolutionY), DepthType.Cv8U, 1);
//var bytes = image.GetBytesBlank();
//var image = EmguExtensions.CreateMat(out var bytes, new Size((int)parent.HeaderSettings.ResolutionX, (int)parent.HeaderSettings.ResolutionY));
var image = new Mat(new Size((int)parent.HeaderSettings.ResolutionX, (int)parent.HeaderSettings.ResolutionY), DepthType.Cv8U, 1);
var span = image.GetPixelSpan<byte>();

Expand Down Expand Up @@ -909,10 +908,12 @@ public byte[] Read(byte[] input)

public override Type[] ConvertToFormats { get; } =
{
typeof(ChituboxFile),
typeof(ChituboxZipFile),
typeof(PWSFile),
typeof(PHZFile),
typeof(ZCodexFile),
typeof(CWSFile),
};

public override PrintParameterModifier[] PrintParameterModifiers { get; } =
Expand Down Expand Up @@ -1013,6 +1014,10 @@ public override void Encode(string fileFullPath, OperationProgress progress = nu
HeaderSettings.EncryptionKey = (uint)rnd.Next(byte.MaxValue, int.MaxValue);
}
}
else
{
HeaderSettings.EncryptionKey = 0;
}

uint currentOffset = (uint)Helpers.Serializer.SizeOf(HeaderSettings);
LayersDefinitions = new LayerData[HeaderSettings.AntiAliasLevel, HeaderSettings.LayerCount];
Expand Down Expand Up @@ -1099,7 +1104,7 @@ public override void Encode(string fileFullPath, OperationProgress progress = nu
var layerData = LayersDefinitions[aaIndex, layerIndex];
LayerData layerDataHash = null;

if (!IsCbtFile && HeaderSettings.EncryptionKey == 0)
if (!IsCbtFile /*&& HeaderSettings.EncryptionKey == 0*/)
{
string hash = Helpers.ComputeSHA1Hash(layerData.EncodedRle);
if (LayersHash.TryGetValue(hash, out layerDataHash))
Expand Down Expand Up @@ -1258,7 +1263,12 @@ public override void Decode(string fileFullPath, OperationProgress progress = nu

using (var image = LayersDefinitions[0, layerIndex].Decode((uint) layerIndex))
{
this[layerIndex] = new Layer((uint) layerIndex, image);
this[layerIndex] = new Layer((uint) layerIndex, image)
{
PositionZ = LayersDefinitions[0, layerIndex].LayerPositionZ,
ExposureTime = LayersDefinitions[0, layerIndex].LayerExposure
};

lock (progress.Mutex)
{
progress++;
Expand Down Expand Up @@ -1299,8 +1309,10 @@ void UpdateLayers()
for (uint layerIndex = 0; layerIndex < HeaderSettings.LayerCount; layerIndex++)
{
// Bottom : others
LayersDefinitions[aaIndex, layerIndex].LayerExposure = layerIndex < HeaderSettings.BottomLayersCount ? HeaderSettings.BottomExposureSeconds : HeaderSettings.LayerExposureSeconds;
LayersDefinitions[aaIndex, layerIndex].LayerOffTimeSeconds = layerIndex < HeaderSettings.BottomLayersCount ? PrintParametersSettings.BottomLightOffDelay : PrintParametersSettings.LightOffDelay;
this[layerIndex].ExposureTime =
LayersDefinitions[aaIndex, layerIndex].LayerExposure = GetInitialLayerValueOrNormal(layerIndex, HeaderSettings.BottomExposureSeconds, HeaderSettings.LayerExposureSeconds);

LayersDefinitions[aaIndex, layerIndex].LayerOffTimeSeconds = GetInitialLayerValueOrNormal(layerIndex, PrintParametersSettings.BottomLightOffDelay, HeaderSettings.LayerOffTime);
}
}
}
Expand Down Expand Up @@ -1427,6 +1439,66 @@ public override void SaveAs(string filePath = null, OperationProgress progress =

public override bool Convert(Type to, string fileFullPath, OperationProgress progress = null)
{
if (to == typeof(ChituboxFile))
{
if (Path.GetExtension(FileFullPath).Equals(Path.GetExtension(fileFullPath)))
{
return false;
}
ChituboxFile file = new ChituboxFile
{
LayerManager = LayerManager,
HeaderSettings =
{
ResolutionX = ResolutionX,
ResolutionY = ResolutionY,
BedSizeX = HeaderSettings.BedSizeX,
BedSizeY = HeaderSettings.BedSizeY,
BedSizeZ = HeaderSettings.BedSizeZ,
ProjectorType = HeaderSettings.ProjectorType,
LayerCount = LayerCount,
AntiAliasLevel = ValidateAntiAliasingLevel(),
BottomLightPWM = (byte) HeaderSettings.BottomLightPWM,
LightPWM = (byte) HeaderSettings.LightPWM,
LayerOffTime = HeaderSettings.LayerOffTime,
PrintTime = HeaderSettings.PrintTime,
BottomExposureSeconds = HeaderSettings.BottomExposureSeconds,
BottomLayersCount = HeaderSettings.BottomLayersCount,
//EncryptionKey = HeaderSettings.EncryptionKey,
LayerExposureSeconds = HeaderSettings.LayerExposureSeconds,
LayerHeightMilimeter = HeaderSettings.LayerHeightMilimeter,
OverallHeightMilimeter = HeaderSettings.OverallHeightMilimeter,
},
PrintParametersSettings =
{
LiftSpeed = PrintParametersSettings.LiftSpeed,
LiftHeight = PrintParametersSettings.LiftHeight,
BottomLiftSpeed = PrintParametersSettings.BottomLiftSpeed,
RetractSpeed = PrintParametersSettings.RetractSpeed,
BottomLightOffDelay = PrintParametersSettings.BottomLightOffDelay,
LightOffDelay = PrintParametersSettings.BottomLightOffDelay,
BottomLayerCount = PrintParametersSettings.BottomLayerCount,
VolumeMl = PrintParametersSettings.VolumeMl,
BottomLiftHeight = PrintParametersSettings.BottomLiftHeight,
CostDollars = PrintParametersSettings.CostDollars,
WeightG = PrintParametersSettings.WeightG
},
SlicerInfoSettings =
{
AntiAliasLevel = SlicerInfoSettings.AntiAliasLevel,
MachineName = SlicerInfoSettings.MachineName,
//EncryptionMode = SlicerInfoSettings.EncryptionMode,
MachineNameSize = SlicerInfoSettings.MachineNameSize,
},
Thumbnails = Thumbnails,
};

//file.SetThumbnails(Thumbnails);
file.Encode(fileFullPath, progress);

return true;
}

if (to == typeof(ChituboxZipFile))
{
ChituboxZipFile file = new ChituboxZipFile
Expand Down Expand Up @@ -1643,6 +1715,45 @@ public override bool Convert(Type to, string fileFullPath, OperationProgress pro
return true;
}

if (to == typeof(CWSFile))
{
CWSFile defaultFormat = (CWSFile)FindByType(typeof(CWSFile));
CWSFile file = new CWSFile { LayerManager = LayerManager };

file.SliceSettings.Xppm = file.OutputSettings.PixPermmX = (float)Math.Round(ResolutionX / HeaderSettings.BedSizeX, 3);
file.SliceSettings.Yppm = file.OutputSettings.PixPermmY = (float)Math.Round(ResolutionY / HeaderSettings.BedSizeY, 3);
file.SliceSettings.Xres = file.OutputSettings.XResolution = (ushort)ResolutionX;
file.SliceSettings.Yres = file.OutputSettings.YResolution = (ushort)ResolutionY;
file.SliceSettings.Thickness = file.OutputSettings.LayerThickness = LayerHeight;
file.SliceSettings.LayersNum = file.OutputSettings.LayersNum = LayerCount;
file.SliceSettings.HeadLayersNum = file.OutputSettings.NumberBottomLayers = InitialLayerCount;
file.SliceSettings.LayersExpoMs = file.OutputSettings.LayerTime = (uint)LayerExposureTime * 1000;
file.SliceSettings.HeadLayersExpoMs = file.OutputSettings.BottomLayersTime = (uint)InitialExposureTime * 1000;
file.SliceSettings.WaitBeforeExpoMs = (uint)(PrintParametersSettings.LightOffDelay * 1000);
file.SliceSettings.LiftDistance = file.OutputSettings.LiftDistance = LiftHeight;
file.SliceSettings.LiftUpSpeed = file.OutputSettings.ZLiftFeedRate = LiftSpeed;
file.SliceSettings.LiftDownSpeed = file.OutputSettings.ZLiftRetractRate = RetractSpeed;
file.SliceSettings.LiftWhenFinished = defaultFormat.SliceSettings.LiftWhenFinished;

file.OutputSettings.BlankingLayerTime = (uint) (PrintParametersSettings.LightOffDelay * 1000);
//file.OutputSettings.RenderOutlines = false;
//file.OutputSettings.OutlineWidthInset = 0;
//file.OutputSettings.OutlineWidthOutset = 0;
file.OutputSettings.RenderOutlines = false;
//file.OutputSettings.TiltValue = 0;
//file.OutputSettings.UseMainliftGCodeTab = false;
//file.OutputSettings.AntiAliasing = 0;
//file.OutputSettings.AntiAliasingValue = 0;
file.OutputSettings.FlipX = HeaderSettings.ProjectorType != 0;
file.OutputSettings.FlipY = file.OutputSettings.FlipX;
file.OutputSettings.AntiAliasingValue = ValidateAntiAliasingLevel();
file.OutputSettings.AntiAliasing = file.OutputSettings.AntiAliasingValue > 1;

file.Encode(fileFullPath, progress);

return true;
}

return false;
}
#endregion
Expand Down
Loading

0 comments on commit 2c514fe

Please sign in to comment.