forked from mcneel/MOVED-rhinoscript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExportCustomSTL.rvb
102 lines (85 loc) · 3.27 KB
/
ExportCustomSTL.rvb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' ExportCustomSTL.rvb -- November 2010
' If this code works, it was written by Dale Fugier.
' If not, I don't know who wrote it.
' Works with Rhino 4.0.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Option Explicit
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' The one and only ExportCustomSTL procedure
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub ExportCustomSTL
' Declare local variables
Dim arrMeshes, strMesh
Dim strFileName, objFSO, objStream
Dim arrVertices, arrFaces, arrFace, arrNormals, arrNormal
Dim i, j
' Pick some meshes to export
arrMeshes = Rhino.GetObjects("Select solid meshes to export", 32, True, True)
If IsNull(arrMeshes) Then Exit Sub
' Make sure solid meshes are selected
For i = 0 To UBound(arrMeshes)
strMesh = arrMeshes(i)
If Rhino.IsMeshClosed(strMesh) = False Then
Rhino.Print "At least one of the selected meshes is not closed. Export canceled."
Exit Sub
End If
Next
' Get the name of the file to create
strFileName = Rhino.SaveFileName("Export STL", "STL File (*.stl)|*.stl||")
If IsNull(strFileName) Then Exit Sub
' Get the file system object
Set objFSO = CreateObject("Scripting.FileSystemObject")
' Create a new text file
On Error Resume Next
Set objStream = objFSO.CreateTextFile(strFileName, True)
If Err Then
MsgBox Err.Description
Exit Sub
End If
Call Rhino.EnableRedraw(False)
' Write the meshes out
For i = 0 To UBound(arrMeshes)
strMesh = arrMeshes(i)
' Since STL facets are all triangles, check to see if we
' need to temporarily convert the mesh to all triangles
' for exporting
If Rhino.MeshQuadCount(strMesh) > 0 Then
strMesh = Rhino.CopyObject(strMesh)
Rhino.MeshQuadsToTriangles(strMesh)
arrVertices = Rhino.MeshVertices(strMesh)
arrFaces = Rhino.MeshFaceVertices(strMesh)
arrNormals = Rhino.MeshFaceNormals(strMesh)
Call Rhino.DeleteObject(strMesh)
Else
arrVertices = Rhino.MeshVertices(strMesh)
arrFaces = Rhino.MeshFaceVertices(strMesh)
arrNormals = Rhino.MeshFaceNormals(strMesh)
End If
' Write out the mesh
Call objStream.WriteLine("solid OBJECT")
For j = 0 To UBound(arrFaces)
arrFace = arrFaces(j)
arrNormal = arrNormals(j)
Call objStream.WriteLine(" facet normal " & FormatPoint(arrNormal))
Call objStream.WriteLine(" outer loop")
Call objStream.WriteLine(" vertex " & FormatPoint(arrVertices(arrFace(0))))
Call objStream.WriteLine(" vertex " & FormatPoint(arrVertices(arrFace(1))))
Call objStream.WriteLine(" vertex " & FormatPoint(arrVertices(arrFace(2))))
Call objStream.WriteLine(" endloop")
Call objStream.WriteLine(" endfacet")
Next
Call objStream.WriteLine("endsolid OBJECT")
Next
' Close the file
objStream.Close
Call Rhino.EnableRedraw(True)
End Sub
' Returns a formatted point string
Function FormatPoint(arrPoint)
Dim x, y, z
x = FormatNumber(arrPoint(0), 7)
y = FormatNumber(arrPoint(1), 7)
z = FormatNumber(arrPoint(2), 7)
FormatPoint = x & " " & y & " " & z
End Function