VBScript Runtime error - Type mismatch

Hello everyone,

I’m using Rhino 6 at work, and I use a script made by a former colleague to import XCAT phantoms (as Nurbs (.nrb) files) into Rhino. Somehow, the script worked on his workstation, but not on mine, and I can’t see why.

Here is the script :

Sub importPhantomCT
Dim arrPoints, arrCount(1), arrKnots(1), arrDegree(1), arrWeights(1), arrVector
Dim line, srfInfo
Dim iFilesys, iFiletxt 
Dim strFilename, strSurface, layerName, material, comp
Dim num_surfs

strFilename = Rhino.OpenFileName("Open...", "NRB Files (*.nrb)|*.nrb||")

If IsNull(strFileName) Then Exit Sub

Rhino.print "Filename = " & strFilename

If Not IsNull(strFilename) Then
	Set iFilesys = CreateObject("Scripting.FileSystemObject")
	Set iFiletxt = iFilesys.OpenTextFile(strFilename, 1) 
 
	num_surfs = 9999
	arrDegree(0) = 3
	arrDegree(1) = 3

	For n = 0 To num_surfs - 1
		layerName = iFiletxt.ReadLine  'blank space
		layerName = iFiletxt.ReadLine  'title
		
		material = iFiletxt.ReadLine  'material ID
		comp = iFiletxt.ReadLine  'material ID
		'      Rhino.Print "Material = " & CStr(material)
         
		line = iFiletxt.ReadLine
		srfInfo = Split(line, " ")
		arrCount(0) = CInt(srfInfo(0))

		line = iFiletxt.ReadLine
		srfInfo = Split(line, " ")
		arrCount(1) = CInt(srfInfo(0))
  
		line = iFiletxt.ReadLine    
		line = iFiletxt.ReadLine  'Ignore 1st zero
		arrVector = arrKnots(1)
		ReDim arrVector(arrCount(1)+1)  'ignore 1st zero and last 1
		For v = 0 To UBound(arrVector)
			arrVector(v) = CDbl(iFiletxt.ReadLine) 
		Next
		arrKnots(1) = arrVector
		line = iFiletxt.ReadLine  'Ignore last 1
 
		line = iFiletxt.ReadLine  
		line = iFiletxt.ReadLine  'Ignore 1st zero
		arrVector = arrKnots(0)
		ReDim arrVector(arrCount(0)+1)
		For v = 0 To UBound(arrVector)
			arrVector(v) = CDbl(iFiletxt.ReadLine)     
		Next   
		arrKnots(0) = arrVector
		line = iFiletxt.ReadLine  'Ignore last 1    
    
		line = iFiletxt.ReadLine   
		ReDim arrPoints(arrCount(0)*arrCount(1)-1)
		For v = 0 To UBound(arrPoints)
			line = iFiletxt.ReadLine
			srfInfo = Split(line, " ")
  
			arrVector = arrPoints(v)
			ReDim arrVector(2)
			arrVector(0) = CDbl(srfInfo(0))
			arrVector(1) = CDbl(srfInfo(1))
			arrVector(2) = CDbl(srfInfo(2))
    
			'        arrVector = Rhino.Str2Pt(line)
			arrPoints(v) = arrVector
		Next  
                   
		strSurface = Rhino.AddNurbsSurface(arrCount, arrPoints, arrKnots(0), arrKnots(1), arrDegree)

		If Rhino.IsLayer(layerName) Then
			Rhino.ObjectLayer strSurface, layerName
		Else
			Rhino.AddLayer(layerName) '<<<<REMOVED	
			Rhino.ObjectLayer strSurface, layerName
		End If	
		
		Rhino.ObjectName strSurface, material & ";" & comp
	Next                                            
End If     
iFiletxt.Close  
End Sub

Rhino.AddStartupScript Rhino.LastLoadedScriptFile
Rhino.AddAlias "importPhantomCT", "_noEcho _-Runscript importPhantomCT"

And the first lines of the .nrb file I try to import :

chest_surface
5
1.0
50 :M
53 :N
U Knot Vector
0.000000
0.000000
0.000000
0.000000
0.020000
0.040000

The thing is, when I launch the script and I select this file, I get nearly immediatly an error message telling me :

Microsoft VBScript Runtime Error
Type Mismatch : 'CDbl'
Line : 43
Car : 4
Code : 

This refers to the line " arrVector(v) = CDbl(iFiletxt.ReadLine)", but I can’t see why is there a mismatch.

And obviously, yes, the script worked for this file and many others on my colleague’s workstation (which is unavailable for now on, as my colleague).

Thanks, Vince

Hi Vince - hard to say without the input file - if you put a break point at that line in the script, what value does arrVector(v) have? If there are many repetitions in the loop before it breaks and it’s too tedious, one thing I do is something like

	For v = 0 To UBound(arrVector)
		Rhino.Print v
		arrVector(v) = CDbl(iFiletxt.ReadLine) 
	Next

Let it break and note the last print of v

then go back and put

	For v = 0 To UBound(arrVector)
            if v = (that number) then
               Rhino.Print "OK"   '<<<< break point here so you can see what  arrVector(v) is
            End if

		arrVector(v) = CDbl(iFiletxt.ReadLine) 
	Next

And put your break point at the Rhino.print “OK” statement.

-Pascal

Hi Pascal, thanks for the advice !

I tried what you suggested, and the thing is that it breaks when v = 0 for the first thing, and when I run the breakpoint, arrVector(v) is empty when the the breakpoint is triggered (which is pretty logic), and it breaks when I execute the next line.

-Vince

Hi Vince - it it breaks on the first run then there is no need for all the fancy stuff I described (and yes, I was mixing in python, thanks for pointing that out, fixed above) .

So, I’d set a variable to iFiletxt.ReadLine and see what that actually is.

-Pascal

Well, it just reads the first line of decimals, so it is just 0.000000. I tried decomposing everything in that step, by declaring a “res” variable and doing this :

For v = 0 To UBound(arrVector)
        res = iFiletxt.ReadLine
        Rhino.Print res
        res = CDbl(res) 
        arrVector(v) = res
Next

But it doesn’t have any effects, the problem still lies with the CDbl function…

-Vince

Post the file causing the problem.

// Rolf

I can neither upload it as it is too large (83,8MB) nor retranscript it here as it contains over 2M lines, but I don’t think it comes from the file, as my colleague used to be able to work it out with this same file.

-Vince

Do your collegue and you have the same locale settings? Specifically decimal separator…

I just learned about this issue a few minutes ago, I’m looking into it.

-Vince

Well, that was that… On my computer, the decimal separator is the comma, when on my colleague it seems that it was the point… Thank you all for your help !