From efd1789dd5c9106c14b0fff8a9109cbf60f88e01 Mon Sep 17 00:00:00 2001 From: Lin Kayser Date: Sat, 3 Aug 2024 21:51:42 +0400 Subject: [PATCH] CLI enhancements CLI Contours are now explicitly closed by adding the first vertex to the end of the contour. Z extent of CLI bounding box is now correctly calculated from 0 (first layer) to position of last layer (instead of using the real world bounding box of the voxels. --- PicoGK_Cli.cs | 26 +++++++++++++++++++++----- PicoGK_Slice.cs | 22 ++++++++++++++++++++++ 2 files changed, 43 insertions(+), 5 deletions(-) diff --git a/PicoGK_Cli.cs b/PicoGK_Cli.cs index 40b0487..82d1da3 100644 --- a/PicoGK_Cli.cs +++ b/PicoGK_Cli.cs @@ -66,6 +66,9 @@ public static void WriteSlicesToCliFile( PolySliceStack oSlices, string strDate = "", float fUnitsInMM = 0.0f) { + if ((oSlices.nCount() < 1) || oSlices.oBBox().bIsEmpty()) + throw new Exception("No valid slices detected (empty)"); + if (fUnitsInMM <= 0.0f) fUnitsInMM = 1.0f; @@ -83,12 +86,15 @@ public static void WriteSlicesToCliFile( PolySliceStack oSlices, oTextWriter.WriteLine("$$LABEL/1,default"); oTextWriter.WriteLine("$$DATE/" + strDate); + // Use X/Y dimenstions of the bounding box + // use 0 as first Z coordinate and last layer's Z coordinate as Z height + string strDim = oSlices.oBBox().vecMin.X.ToString("00000000.00000") + "," + oSlices.oBBox().vecMin.Y.ToString("00000000.00000") + "," + - oSlices.oBBox().vecMin.Z.ToString("00000000.00000") + "," + + "00000000.00000" + "," + oSlices.oBBox().vecMax.X.ToString("00000000.00000") + "," + oSlices.oBBox().vecMax.Y.ToString("00000000.00000") + "," + - oSlices.oBBox().vecMax.Z.ToString("00000000.00000"); + oSlices.oSliceAt(oSlices.nCount()-1).fZPos().ToString("00000000.00000"); oTextWriter.WriteLine("$$DIMENSION/{0}", strDim); oTextWriter.WriteLine("$$LAYERS/{0}", (oSlices.nCount() + 1).ToString("00000")); @@ -680,9 +686,8 @@ private static bool bExtractParameter( ref string strLine, public partial class Voxels { - public void SaveToCliFile( string strFileName, - float fLayerHeight = 0f, - bool bUseAbsXYOrigin = false) + public PolySliceStack oVectorize( float fLayerHeight = 0f, + bool bUseAbsXYOrigin = false) { if (fLayerHeight == 0f) fLayerHeight = Library.fVoxelSizeMM; @@ -733,6 +738,9 @@ public void SaveToCliFile( string strFileName, continue; } + Console.WriteLine($"Slice has {oSlice.nCountours()} contours"); + + oSlice.Close(); oSlices.Add(oSlice); fLayerZ += fLayerHeight; @@ -741,6 +749,14 @@ public void SaveToCliFile( string strFileName, PolySliceStack oStack = new(); oStack.AddSlices(oSlices); + return oStack; + } + + public void SaveToCliFile( string strFileName, + float fLayerHeight = 0f, + bool bUseAbsXYOrigin = false) + { + PolySliceStack oStack = oVectorize(fLayerHeight, bUseAbsXYOrigin); CliIo.WriteSlicesToCliFile(oStack, strFileName); } } diff --git a/PicoGK_Slice.cs b/PicoGK_Slice.cs index 167bce1..5c8dd17 100644 --- a/PicoGK_Slice.cs +++ b/PicoGK_Slice.cs @@ -127,6 +127,20 @@ public void DetectWinding() public List oVertices() { return m_oVertices; } + /// + /// Makes sure that the last coordinate is identical to the first + /// coordinate, to close the loop + /// + public void Close() + { + if (m_oVertices.Count() == 0) + return; + + Vector2 vecDist = m_oVertices.First() - m_oVertices.Last(); + if (vecDist.Length() > float.Epsilon) + m_oVertices.Add(m_oVertices.First()); + } + public void AsSvgPolyline(out string str) { str = "