Skip to content

Commit

Permalink
CLI enhancements
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
LinKayser committed Aug 3, 2024
1 parent f93806e commit efd1789
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 5 deletions.
26 changes: 21 additions & 5 deletions PicoGK_Cli.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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"));
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -733,6 +738,9 @@ public void SaveToCliFile( string strFileName,
continue;
}

Console.WriteLine($"Slice has {oSlice.nCountours()} contours");

oSlice.Close();
oSlices.Add(oSlice);

fLayerZ += fLayerHeight;
Expand All @@ -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);
}
}
Expand Down
22 changes: 22 additions & 0 deletions PicoGK_Slice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,20 @@ public void DetectWinding()

public List<Vector2> oVertices() { return m_oVertices; }

/// <summary>
/// Makes sure that the last coordinate is identical to the first
/// coordinate, to close the loop
/// </summary>
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 = "<polyline points='";
Expand Down Expand Up @@ -199,6 +213,14 @@ public bool bIsEmpty()
return m_oContours.Count() == 0;
}

public void Close()
{
foreach (PolyContour oContour in m_oContours)
{
oContour.Close();
}
}

public void SaveToSvgFile( string strPath,
bool bSolid,
BBox2? oBBoxToUse = null)
Expand Down

0 comments on commit efd1789

Please sign in to comment.