Parsing Text Files
This guide discusses how to convert data read from a text file into its proper data type in RhinoScript.
A frequent workflow is using a text file - generated outside Rhino - to change a Rhino model. You may know how to read in a text file and parse it with VBScript, but what about parsing the text file and assigning the values as a variable?
Consider the following VBScript subroutine that reads a text file:
Sub ReadTextFile Dim objFSO, objFile, strFileName, strLine Const ForReading = 1 strFileName = Rhino.OpenFileName("Open", "Text Files (*.txt)|*.txt|") If IsNull(strFileName) Then Exit Sub Set objFSO = CreateObject("Scripting.FileSystemObject") Set objFile = objFSO.OpenTextFile(strFileName, ForReading) While Not objFile.AtEndOfStream strLine = objFile.ReadLine Rhino.Print strLine Wend objFile.Close Set objFSO = Nothing End Sub
Note how every line read from the text file is assigned to the
strLine variable before it is printed.
Everything read from a text file using VBScript comes in as a string data type. If you want something that you have read to be an integer or a floating point number, then you need to convert the string to that data type. For example, if you have a text file:
7 3.14159 Hello Rhino!
If you use VBScript to read this file, all three lines are read in as strings. If you want line one read as an integer and line read as a double, then you can use some of VBScript’s data conversion functions to convert the string to the proper data type. For example:
Dim nFirst, dblSecond, strThird nFirst = CInt(objFile.ReadLine) dblSecond = CDbl(objFile.ReadLine) strThird = objFile.ReadLine
NOTE: the above example does have the limitation that you have to know what kind of data is on each line. If you don’t know what kind of data is on each line, then consider exporting a data type identifier along with the data from the program that generated the file. This way, when you read a line from VBScript, you will know what kind of data you have.
VarType function will return a value indicating the type of variable. The possible values are:
||1||Contains no valid data|
||12||Variant (used only for arrays of variants)|
||13||Data access object|
The text file above written to include the data’s type identifier would look like this:
2;7 5;3.14159 8;Hello Rhino!
Now each line in the text file contains both the data type and the data. Using VBScript’s
Split function, we can separate the data type from the data. Then, it is just a matter of testing for the types of data that our script supports and then performing the proper data conversion. For example:
Dim strLine, arrLine, nType, vaValue strLine = objFile.ReadLine arrLine = Split(strLine, ";") nType = CInt(arrLine(0)) Select Case nType Case 2 vaValue = CInt(arrLine(1)) Case 3 vaValue = CLng(arrLine(1)) Case 4 vaValue = CSng(arrLine(1)) Case 5 vaValue = CDbl(arrLine(1)) Case 8 vaValue = arrLine(1) Case 11 vaValue = CBool(arrLine(1)) Case Else Rhino.Print "Unsupported data type." End Select