forked from mcneel/MOVED-rhinoscript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExportCurveDefinition.rvb
134 lines (107 loc) · 4.15 KB
/
ExportCurveDefinition.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' ExportCurveDefinition.rvb -- July 2011
' 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 main script procedure
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub ExportCurveDefinition
' Declare local variables
Dim strCurve, strFileName, objFSO, objStream
Dim strName, nCount, nDegree, bRational, arrPoints, arrWeights, arrKnots
Dim strTemp, i
' Pick the curve to export
strCurve = Rhino.GetObject("Select curve for definition export", 4, True)
If IsNull(strCurve) Then Exit Sub
' Get the file name to create
strFileName = Rhino.SaveFileName("Export", "Curve Definition (*.dat)|*.dat|All Files (*.*)|*.*||")
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
' Here we go...
' Write an empty line
Call objStream.WriteLine("")
' Write start CURVE_DEFINITION
Call objStream.WriteLine("@CURVE_DEFINITION {")
' Write start CURVE_NAME
strName = Rhino.ObjectName(strCurve)
If IsNull(strName) Then strName = strCurve
Call objStream.WriteLine(" @CURVE_NAME {" & strName & "} {")
' Write IS_DEFINED_IN_FRAME (not sure what this means...)
Call objStream.WriteLine(" @IS_DEFINED_IN_FRAME {INERTIAL}")
' Write start POINT_DEFINITION
Call objStream.WriteLine(" @POINT_DEFINITION {")
' Write NUMBER_OF_CONTROL_POINTS
nCount = Rhino.CurvePointCount(strCurve)
Call objStream.WriteLine(" @NUMBER_OF_CONTROL_POINTS {" & CStr(nCount) & "}")
' Write DEGREE_OF_CURVE
nDegree = Rhino.CurveDegree(strCurve)
Call objStream.WriteLine(" @DEGREE_OF_CURVE {" & CStr(nDegree) & "}")
' Write RATIONAL_CURVE_FLAG
bRational = Rhino.IsCurveRational(strCurve)
If (bRational = True) Then
strTemp = "YES"
Else
strTemp = "NO"
End If
Call objStream.WriteLine(" @RATIONAL_CURVE_FLAG {" & strTemp & "}")
' Write COORDINATES
arrPoints = Rhino.CurvePoints(strCurve)
arrWeights = Rhino.CurveWeights(strCurve)
For i = 0 To UBound(arrPoints)
strTemp = FormatControlPoint(arrPoints(i), arrWeights(i))
Call objStream.WriteLine(" @COORDINATES {" & strTemp & "}")
Next
' Write end POINT_DEFINITION
Call objStream.WriteLine(" }")
' Write start KNOT_SEQUENCE_DEFINITION
Call objStream.WriteLine(" @KNOT_SEQUENCE_DEFINITION {")
' Write KNOT_SEQUENCE
Call objStream.WriteLine(" @KNOT_SEQUENCE {")
arrKnots = Rhino.CurveKnots(strCurve)
strTemp = ""
For i = 0 To UBound(arrKnots)
strTemp = strTemp & FormatNumber(arrKnots(i), 16)
If i = UBound(arrKnots) Then
strTemp = strTemp & "}"
Call objStream.WriteLine(" " & strTemp)
Else
strTemp = strTemp & ", "
If (i + 1) Mod 6 = 0 Then
Call objStream.WriteLine(" " & strTemp)
strTemp = ""
End If
End If
Next
' Write end KNOT_SEQUENCE_DEFINITION
Call objStream.WriteLine(" }")
' Write end CURVE_NAME
Call objStream.WriteLine(" }")
' Write end CURVE_DEFINITION
Call objStream.WriteLine("}")
' Close the file
objStream.Close
' Done
Call Rhino.Print(strFileName & " written successfully.")
End Sub
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Utility function to format a control point string
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function FormatControlPoint(arrPoint, dblWeight)
Dim strPoint
strPoint = FormatNumber(arrPoint(0), 16) & ", "
strPoint = strPoint & FormatNumber(arrPoint(1), 16) & ", "
strPoint = strPoint & FormatNumber(arrPoint(2), 16) & ", "
strPoint = strPoint & FormatNumber(dblWeight, 16)
FormatControlPoint = strPoint
End Function