Creating STL from vertices and faces

Hi all,

I got a list with vertices and a list with faces.
In each list the X, Y and Z or 3 items after each other.
Same with the faces. For example. In the face list a triangle is faces(0), faces(1) and faces(2).

I got this code to convert it to a STL.
But something is going wrong and I am just staring blind to a screen…

If someone got the time please take a quick look:

Dim x As Integer = 0
Dim y As Integer = 1
Dim z As Integer = 2
Dim filename As String = “testScan.stl”
Dim stlString As New StringBuilder
stlString.AppendLine("solid " & filename)
For i = 0 To Faces.Count - 1 Step 3

Dim face1 As Integer = Faces(i)
Dim face2 As Integer = Faces(i + 1)
Dim face3 As Integer = Faces(i + 2)
Dim p1() As Single = {vertices(face1 + x), vertices(face1 + y), vertices(face1 + z)}
Dim p2() As Single = {vertices(face2 + x), vertices(face2 + y), vertices(face2 + z)}
Dim p3() As Single = {vertices(face3 + x), vertices(face3 + y), vertices(face3 + z)}
stlString.AppendLine("facet normal 0.0 0.0 0.0")
stlString.AppendLine("        outer loop")
stlString.AppendLine("                vertex " & p1(x).ToString.Replace(",", ".") & " " & p1(y).ToString.Replace(",", ".") & " " & p1(z).ToString.Replace(",", "."))
stlString.AppendLine("                vertex " & p2(x).ToString.Replace(",", ".") & " " & p2(y).ToString.Replace(",", ".") & " " & p2(z).ToString.Replace(",", "."))
stlString.AppendLine("                vertex " & p3(x).ToString.Replace(",", ".") & " " & p3(y).ToString.Replace(",", ".") & " " & p3(z).ToString.Replace(",", "."))
stlString.AppendLine("        endloop")
stlString.AppendLine("endfacet")

Next
stlString.AppendLine("endsolid " & filename)

ok, not my playground i’m a pythonista but should that not be something like:

vertices(face1)(x)

I assume face1 is an index referring to the vertex at index. From that you need to take the value at index x y or z

I start to doubt myself now but leave the above be maybe it still gets you on track

-Willem

I dont exactly know why, but I had to duplicate the Faces(i) * 3 and everything started working :stuck_out_tongue:

Thanks for looking at it anyways :slight_smile:

1 Like