Skip to content

Commit efd1789

Browse files
committed
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.
1 parent f93806e commit efd1789

File tree

2 files changed

+43
-5
lines changed

2 files changed

+43
-5
lines changed

PicoGK_Cli.cs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ public static void WriteSlicesToCliFile( PolySliceStack oSlices,
6666
string strDate = "",
6767
float fUnitsInMM = 0.0f)
6868
{
69+
if ((oSlices.nCount() < 1) || oSlices.oBBox().bIsEmpty())
70+
throw new Exception("No valid slices detected (empty)");
71+
6972
if (fUnitsInMM <= 0.0f)
7073
fUnitsInMM = 1.0f;
7174

@@ -83,12 +86,15 @@ public static void WriteSlicesToCliFile( PolySliceStack oSlices,
8386
oTextWriter.WriteLine("$$LABEL/1,default");
8487
oTextWriter.WriteLine("$$DATE/" + strDate);
8588

89+
// Use X/Y dimenstions of the bounding box
90+
// use 0 as first Z coordinate and last layer's Z coordinate as Z height
91+
8692
string strDim = oSlices.oBBox().vecMin.X.ToString("00000000.00000") + "," +
8793
oSlices.oBBox().vecMin.Y.ToString("00000000.00000") + "," +
88-
oSlices.oBBox().vecMin.Z.ToString("00000000.00000") + "," +
94+
"00000000.00000" + "," +
8995
oSlices.oBBox().vecMax.X.ToString("00000000.00000") + "," +
9096
oSlices.oBBox().vecMax.Y.ToString("00000000.00000") + "," +
91-
oSlices.oBBox().vecMax.Z.ToString("00000000.00000");
97+
oSlices.oSliceAt(oSlices.nCount()-1).fZPos().ToString("00000000.00000");
9298

9399
oTextWriter.WriteLine("$$DIMENSION/{0}", strDim);
94100
oTextWriter.WriteLine("$$LAYERS/{0}", (oSlices.nCount() + 1).ToString("00000"));
@@ -680,9 +686,8 @@ private static bool bExtractParameter( ref string strLine,
680686

681687
public partial class Voxels
682688
{
683-
public void SaveToCliFile( string strFileName,
684-
float fLayerHeight = 0f,
685-
bool bUseAbsXYOrigin = false)
689+
public PolySliceStack oVectorize( float fLayerHeight = 0f,
690+
bool bUseAbsXYOrigin = false)
686691
{
687692
if (fLayerHeight == 0f)
688693
fLayerHeight = Library.fVoxelSizeMM;
@@ -733,6 +738,9 @@ public void SaveToCliFile( string strFileName,
733738
continue;
734739
}
735740

741+
Console.WriteLine($"Slice has {oSlice.nCountours()} contours");
742+
743+
oSlice.Close();
736744
oSlices.Add(oSlice);
737745

738746
fLayerZ += fLayerHeight;
@@ -741,6 +749,14 @@ public void SaveToCliFile( string strFileName,
741749
PolySliceStack oStack = new();
742750
oStack.AddSlices(oSlices);
743751

752+
return oStack;
753+
}
754+
755+
public void SaveToCliFile( string strFileName,
756+
float fLayerHeight = 0f,
757+
bool bUseAbsXYOrigin = false)
758+
{
759+
PolySliceStack oStack = oVectorize(fLayerHeight, bUseAbsXYOrigin);
744760
CliIo.WriteSlicesToCliFile(oStack, strFileName);
745761
}
746762
}

PicoGK_Slice.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,20 @@ public void DetectWinding()
127127

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

130+
/// <summary>
131+
/// Makes sure that the last coordinate is identical to the first
132+
/// coordinate, to close the loop
133+
/// </summary>
134+
public void Close()
135+
{
136+
if (m_oVertices.Count() == 0)
137+
return;
138+
139+
Vector2 vecDist = m_oVertices.First() - m_oVertices.Last();
140+
if (vecDist.Length() > float.Epsilon)
141+
m_oVertices.Add(m_oVertices.First());
142+
}
143+
130144
public void AsSvgPolyline(out string str)
131145
{
132146
str = "<polyline points='";
@@ -199,6 +213,14 @@ public bool bIsEmpty()
199213
return m_oContours.Count() == 0;
200214
}
201215

216+
public void Close()
217+
{
218+
foreach (PolyContour oContour in m_oContours)
219+
{
220+
oContour.Close();
221+
}
222+
}
223+
202224
public void SaveToSvgFile( string strPath,
203225
bool bSolid,
204226
BBox2? oBBoxToUse = null)

0 commit comments

Comments
 (0)