Import Rhino v5 data into Excel with Rhinoscript/VBA

rhinoscript
rhino

#1

I’m trying to write an Excel VBA script that basically accesses a running Rhino v5 session and pulls in some of the data into a worksheet tab. Unfortunately, I don’t seem to be able to talk to the running session, at least not using the documented steps:

Set Rhino = CreateObject("Rhino5x64.Interface")
If (Err.Number <> 0) Then
    'MsgBox ("Failed to create Rhino4 object")
    Set Rhino = CreateObject("Rhino5x64.Application")
    If (Err.Number <> 0) Then
        MsgBox "Failed to create Rhino5 object: " & Err.Number
        Exit Sub
    End If
End If

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

Do While (nCount < 10)
  Set RhinoScript = Rhino.GetScriptObject()
  If Err.Number <> 0 Then
    Err.Clear
    Sleep (1000)
    nCount = nCount + 1
  Else
    Exit Do
  End If
Loop
' Display an error if needed.
If (RhinoScript Is Nothing) Then
    MsgBox ("Failed to get RhinoScript")
Else

 strPath = Chr(34) & Rhino.DocumentPath & Rhino.DocumentName & Chr(34)
    MsgBox "Current File is: " & strPath
 Set arrObjects = Rhino.GetObjects(, 0)
    If IsArray(arrObjects) Then
        For Each strObject In arrObjects
        MsgBox "Object identifier: " & strObject
        Next
    Else
        MsgBox "There were no objects"
    End If
End If

The script always kicks back that there’s no file load and has no objects, regardless of what’s loaded into Rhino v5. I’ve tried an alternate method where I try to force Rhino to bring up an open file dialogue box using:

strFile = Rhino.OpenFileName("Open", "Rhino 3D Models (*.3dm)|*.3dm|")

But I’ll get an unsupported method error from Excel. Can anyone explain how to access model data in a Rhino V5 session from within an Excel VBA module?

Thanks,

Matt


(Dale Fugier) #2

Hi Matt,

This seems to work here:

' Main subroutine
Sub Main()
  
  Dim Rhino As Object
  Dim RhinoScript As Object
  Dim strFile As Variant
  
  Set Rhino = ConnectToRunningRhino()
  If (Rhino Is Nothing) Then
    MsgBox "Failed to Rhino object"
    Exit Sub
  End If
  
  Set RhinoScript = Rhino.GetScriptObject()
  If (RhinoScript Is Nothing) Then
    MsgBox "Failed to get RhinoScript object"
    Exit Sub
  End If

  strFile = RhinoScript.OpenFileName("Open", "Rhino 3D Models (*.3dm)|*.3dm|")
  If Not IsNull(strFile) Then
    RhinoScript.PrintEx strFile
  End If
  
End Sub

' Helper function
Function ConnectToRunningRhino()

  Dim Rhino As Object
  
  On Error Resume Next
  Set Rhino = CreateObject("Rhino5x64.Interface")
  If (Err.Number <> 0) Then
    'MsgBox "Unable to create Rhino5x64.Interface object"
    Exit Function
  End If
  
  Set ConnectToRunningRhino = Rhino

End Function

Does this help?

– Dale


#3

Hi, Dale,

Thanks for replying. I managed to bash out something that works by then, but yours looks similar to mine. The sticking point was Rhino.OpenFileName when apparently I should have been using RhinoScript.OpenFileName like you were. Alas, there seems to be a far amount of ambiguity in the manuals about when I should use the “Rhino” vs “Rhinoscript” in VBA. Looks like “Rhinoscript” is the way to go in nearly all cases. The section on “external access” could really do with some spiffing up on that.

Matt


(Dale Fugier) #4

Hi Matt,

“Rhino” and “RhinoScript” are just variable names. So if you want to be consistent with the help file, you can do this:

' Main subroutine
Sub Main()
  
  Dim RhinoApp As Object
  Dim Rhino As Object ' e.g. RhinoScript
  Dim strFile As Variant
  
  Set RhinoApp = ConnectToRunningRhino()
  If (RhinoApp Is Nothing) Then
    MsgBox "Failed to get Rhino interface object"
    Exit Sub
  End If
  
  Set Rhino = RhinoApp.GetScriptObject()
  If (Rhino Is Nothing) Then
    MsgBox "Failed to get RhinoScript object"
    Exit Sub
  End If

  strFile = Rhino.OpenFileName("Open", "RhinoApp 3D Models (*.3dm)|*.3dm|")
  If Not IsNull(strFile) Then
    Rhino.PrintEx strFile
  End If
  
End Sub

' Helper function
Function ConnectToRunningRhino()

  Dim RhinoApp As Object
  
  On Error Resume Next
  Set RhinoApp = CreateObject("Rhino5x64.Interface")
  If (Err.Number <> 0) Then
    'MsgBox "Unable to create Rhino5x64.Interface object"
    Exit Function
  End If
  
  Set ConnectToRunningRhino = RhinoApp

End Function