forked from mcneel/MOVED-rhinoscript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExportLabels.rvb
92 lines (75 loc) · 2.54 KB
/
ExportLabels.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
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' ExportLabels.rvb -- February 2012
' 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
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Description
' Exports a dimension's value and it's user text (labels)
' to a commma-delimited (.CSV) file.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub ExportLabels
' Local constants
Const RhAnnotations = 512
' Local variables
Dim arrDims, strDim, strValue, strUserText
Dim strFilter, strFileName, strLine
Dim objFSO, objStream
' Select some dimensions
arrDims = Rhino.GetObjects("Select dimensions with user-defined labels for export", RhAnnotations, True, True)
If IsNull(arrDims) Then Exit Sub
' Define a file filter
strFilter = "Comma-Delimited File (*.csv)|*.csv||"
' Get the name of the file to create
strFileName = Rhino.SaveFileName("Export", strFilter)
If IsNull(strFileName) Then Exit Sub
' Create a file system object
Set objFSO = CreateObject("Scripting.FileSystemObject")
' Try creating the file, overwrite if needed
On Error Resume Next
Set objStream = objFSO.CreateTextFile(strFileName, True, True)
If Err Then
MsgBox Err.Description
Exit Sub
End If
' Process each selected dimension
For Each strDim In arrDims
If Rhino.IsDimension(strDim) Then
' Build a comma-delimited string to write
strUserText = Rhino.DimensionUserText(strDim)
strUserText = ParseDimensionUserText(strUserText)
strValue = CStr(Rhino.DimensionValue(strDim))
strLine = strUserText & "," & strValue
' Write to file
objStream.WriteLine(strLine)
End If
Next
' Close the file
objStream.Close
End Sub
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Description
' Utility function to remove "<>" prefix from string.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function ParseDimensionUserText(strDim)
' Local variables
Dim nPos, nLen, objRegExp
' Validate string
If IsNull(strDim) Then
strDim = "<Empty>"
Else
' Remove "<>" block
Set objRegExp = New RegExp
objRegExp.Pattern = "<[^>]*>"
objRegExp.Global = True
strDim = objRegExp.Replace(strDim, "")
strDim = Trim(strDim)
End If
' One final validation
If Len(strDim) = 0 Then
strDim = "<Empty>"
End If
ParseDimensionUserText = strDim
End Function