Site Tools


Converting Data from Text Files

Developer: RhinoScript
Summary: Discusses how to convert data read from a text file into its proper data type.

Question

I have a text file that I am outputting from a C++ program and then trying to use within Rhino to change a model. We have the model made up and know how to read in the text file and parse it with VBScript. What I am having trouble doing is connecting the two. Can I read the information the text file and assign it as a variable?

Answer

Yes you can. 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 that is 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. Thus, if you are expecting something that you have read to be an integer or a floating point number, then you will need to convert the string to that data type. For example, let's say that you have a text file appears as follows:

 7
 3.14159
 Hello Rhino!

If you use VBScript to read this file, all three lines will be read in as strings. If you want line one to be read as an integer and line two to be read as a double, then you can do 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 have the limitation of you having to know what kind of data is on each line. If you think might not be possible to know what kind of data is on each line, then you might consider exporting a “data type identifier” along with the data from your C++ program. This way, when you read a line from VBScript, you will know what kind of data it is that you have.

VBScript's VarType function that will return a value indicating the type of variable. The possible values are as follows:

 vbEmpty       0 Uninitialized (default)
 vbNull        1 Contains no valid data
 vbInteger     2 Integer subtype
 vbLong        3 Long subtype
 vbSingle      4 Single subtype
 vbSingle      5 Double subtype
 vbCurrency    6 Currency subtype
 vbDate        7 Date subtype
 vbString      8 String subtype
 vbObject      9 Object
 vbError      10 Error subtype
 vbBoolean    11 Boolean subtype
 vbVariant    12 Variant (used only for arrays of variants)
 vbDataObject 13 Data access object
 vbDecimal    14 Decimal subtype
 vbByte       17 Byte subtype
 vbArray    8192 Array

Thus, above text file written to included the data's type identifier would like like the following:

 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 coersion. 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
developer/scriptsamples/parsetextfile.txt · Last modified: 2011/11/11 by dale