AddSrfSectionCrvs works within RhinoScript, but not within Excel VBA


I am trying to use AddSrfSectionCrvs by calling it thru Excel VBA. Below are two identical codes for making a section from a cube (One within RhinoScript and another accessing RhinoScript thru Excel VBA).

When running the script within RhinoScript I can get section of the cube without a problem. But when running it thru Excel VBA everything works besides the last command, i.e. AddSrfSectionCrvs.

I’ve run the code in debug mode, both in RhinoScript and Excel VBA. In the former I get the curve and the string for the newly created section curve, but in Excel VBA no string and no curve is returned.

Picture 1. Sections made x=450 and x=400

Calling AddSrfSectionCrvs within RhinoScript:

Const rhObjectSurface = 8
Const rhObjectPolysurface = 16

Dim strObject, arrPlane

strObject = Rhino.GetObject("Select object", rhObjectSurface + rhObjectPolysurface)
arrCPlane = Rhino.ViewCPlane

Dim arrOrigin
Dim arrNormal
Dim strCurves

arrOrigin = array(300, 0, 0)
arrNormal = array(1, 0, 0)

arrPlane = Rhino.PlaneFromNormal(arrOrigin, arrNormal)
strCurves = Rhino.AddSrfSectionCrvs(strObject, arrPlane)

Calling AddSrfSectionCrvs within Excel VBA:

* Initializing connection with Rhino *

Dim version As Integer

version = Range("C4")

' Create Interface object. Connect to an allready running instance of Rhino 5.

Dim RhinoApp As Object

Select Case version
    Case 1
        On Error Resume Next
        Set RhinoApp = CreateObject("Rhino5.Interface")

        If (Err.Number <> 0) Then
            MsgBox ("Failed to create Rhino5 x86 object")
            Exit Sub
        End If
    Case 2
        On Error Resume Next
        Set RhinoApp = CreateObject("Rhino5x64.Interface")
        If (Err.Number <> 0) Then
            MsgBox ("Failed to create Rhino5 x64 object")
            Exit Sub
        End If
End Select

' Make attempts to get RhinoScript, sleep between each attempt.

Dim RhinoScript As Object
Dim nCount As Integer
nCount = 0

Do While (nCount < 10)
  On Error Resume Next
  Set RhinoScript = RhinoApp.GetScriptObject()

  If Err.Number <> 0 Then
    Sleep 500 'waits for 500 ms
    nCount = nCount + 1
    Exit Do
  End If

' Display an error if needed.

If (RhinoScript Is Nothing) Then
  MsgBox ("Failed to get RhinoScript")
End If

* Same part of code as in RhinoScript for calling AddSrfSectionCrvs *

    Const rhObjectSurface = 8
    Const rhObjectPolysurface = 16

    Dim strObject As String
    Dim  arrPlane()

    strObject = RhinoScript.GetObject("Select object", rhObjectSurface + rhObjectPolysurface
    Dim arrOrigin()
    Dim arrNormal()
    Dim strCurve As String

    arrOrigin = Array(200, 0, 0)
    arrNormal = Array(1, 0, 0)

    arrPlane = RhinoScript.PlaneFromNormal(arrOrigin, arrNormal)

    strCurve = RhinoScript.AddSrfSectionCrvs(strObject, arrPlane)


I read this topic before posting:

Helvetosaur mentions here that AddSrfSectionCrvs is only in RhinoScript and not accessible thru Python.

My understanding is that calling RhinoScript methods thru Excel VBA is basically the same as calling it within RhinoScript itself. Thus, there should not be any problem calling the method from Excel VBA?

I found the culprit of the problem. In the Excel VBA code you need to declare the “strObject” and “strCurve” as Variant not String. Then the data type will be Variant/String instead of just String and the script works fine.

I found that some methods I had in other bits of the full code could take variables declared as String, but others couldn’t, e.g.

Excel VBA code

'''''''This works'''''''
Dim strNewLayer as String
strNewLayer = "some layer"
RhinoScript.CurrentLayer strNewLayer

''''This does not work'''''
Dim strNewLayer as String
strNewLayer = "some layer"
RhinoScript.LayerVisible strNewLayer, False

'''''However, this works'''''
Dim strNewLayer as Variant
strNewLayer = "some layer"
RhinoScript.LayerVisible strNewLayer, False

Thus, it seems better to declare any variable containing a string as Variant. I do not understand why this is, would appriciate if anyone can shed light on this.


Maybe - Rhinoscript is based on VBscript - which does not have variable type declaration as VBA, all variables in VBscript are type “variant” (until you actually assign something to them)…

1 Like

Thank you! I read thru that part of the Fundamentals, but did not but it together a few days ago.

Makes more sense now. However, it is interesting that some methods do not dislike the variable being declared “string” and others do.