Import data from text file - Problem with CDbl

Hi everyone,

I have been using Rhino(script) for a while and I am now starting to use Rhino as a user interface for drawing civil engineering structures. In this example, I am trying to draw a serie of anchorage structures from a csv file containing all the geometry parameters needed.

My problem is very simple : I am importing data from a csv file (using rhino.readTextFile and then Split functions, that work just fine) but when I am trying to convert Strings to Doubles using CDbl function, but it does not work at all.PlatinesForRhino.txt (393 Bytes) Script_Txt_To_Rhino.rvb (5.2 KB)

Any Suggestion ? Thanks for your help.
Here attached is my VBscript code and the .txt file to be chosen when prompted at execution of the code.

Hi @tanguy.prevost,

Here are a couple of useful utility functions for converting string to numbers:

' Trys to convert a string to a long integer
Function TryGetLong(ByVal strInput, ByRef intOutput)
	Dim intInput
	TryGetLong = False
	intOutput = 0
	If IsNumeric(strInput) Then
		intInput = CLng(strInput)
		If CStr(intInput) = CStr(strInput) Then 
			intOutput = intInput
			TryGetLong = True
		End If
	End If
End Function

' Trys to convert a string to a double-precision floating-point number
Function TryGetDouble(ByVal strInput, ByRef dblOutput)
	Dim dblInput
	TryGetDouble = False
	dblOutput = 0.0
	If IsNumeric(strInput) Then
		dblInput = CDbl(strInput)
		If CStr(dblInput) = CStr(strInput) Then 
			dblOutput = dblInput
			TryGetDouble = True
		End If
	End If
End Function

Here is an example of using the above with the test file you posted:

Sub TestTanguy
	Dim strFile, arrText, strText
	Dim arrTokens, strToken
	Dim varToken, intToken, dblToken
	strFile = Rhino.OpenFileName("Open", "Text Files (*.txt)|*.txt||")
	If IsNull(strFile) Then Exit Sub
	arrText = Rhino.ReadTextFile(strFile)
	If IsNull(arrText) Then Exit Sub
	For Each strText In arrText
		arrTokens = Split(strText, ";")
		If IsArray(arrTokens) Then
			For Each strToken In arrTokens
				strToken = Trim(strToken)
				If TryGetLong(strToken, intToken) Then
					varToken = intToken
				ElseIf TryGetDouble(strToken, dblToken) Then
					varToken = dblToken
					varToken = strToken
				End If
				Rhino.Print TypeName(varToken) & " = " & CStr(varToken)
		End If
End Sub

– Dale

Hi Dale,
Thank you for thèses examples. Very helpfull. This should do the job just fine.