Using Rhino with Excel VBA

Hello Everyone,
In the past, I used Excel VBA to do lots of calculations and automated workbooks for certain routine operations.
One of them was to create a 3D model in Microstation ( Bentley CAD Program) using Excel VBA program.

I was thinking about how i can do the same for Rhino.

Does anyone have any experience in this?

Thanks for your time

regards

CK

Hi,
The closest thing to VBA perhaps would be RhinoScript

Alternative languages for scripting in Rhino is Python or C#, which also are supported by lots of Math-libraries out there.

//Rolf

I have some experience connecting Rhino to excel worksheets with Python. It’s not completely clear what your expectations are regarding the exchange between the two.

This thread may be relevant: Connect to Rhino from Excel - #14 by eric.bunn

Hello Gijs,
In Excel VBA, typically i had an interface to define my parameters and i take these parameters and calculate the coordinates of the intended model and create the object using the applicable syntax of the specific program.
i am aiming to do the same for rhino, using excel vba.

i saw someone posted this message here and it said that Rhino script can be used in excel vba.

That s why i was asking this question

let me know

thanks

CK

well, here you got an example node on how to connect rhino with excel via vb node. Please note that screenupdate switched in between. In case your code fails, Excel won’t update the screen any more. You can comment out these lines.


here’s the file:

vba_base.gh (3.4 KB)

EDIT: this uses grasshopper, which is part of rhino, as @davidcockey pointed out correctly. I think using grasshopper allows you to easily get values from (and write to) Excel and make further operations with the values or the created geometry.
It should also be noted that it is not a good idea to connect a toggle to the node without a timer. This usually makes crash xls.

hope that helps

I see, I have no experience with Excel VBA, but since @benedict already posted an example, I guess you’re covered. If you need something in python then let me know. What I have is only reading stuff from excel though and some code to handle correct closing of opened excel files (which was far from straightforward, hence I mention)

It should be noted that this uses Grasshopper, which is part of Rhino.

Edit: Ops, I thought the code was in VBA, so the code below doesn’t work. In VB.NET you could use Try… Finally … End Try instead,

Try 
    ' All your code here
Finally
    objExcel.screenUpdating = True
End Try

VBA below only works in Excel:

@benedict , you can ensure restoring Screenupdate by wrapping your code in an exception handle, like so:

VBA:

Private Sub RunScript(ByVal _stream... )

    objExcel.screenUpdating = False
    On Error GoTo OnException ' Wrap code in exception handler
        If _stream Then
            ' ...
            ' ...
            ' ...
            objExcel.screenUpdating = True
        End If
    Exit Sub ' If no exception, the sub ends here

OnException:
    objExcel.screenUpdating = True  ' <-- Screen update restored
End Sub

//Rolf

1 Like

yeah, it’s pretty brute what I’ve posted here :slight_smile: I’m more into c# although it’s about the same thing.
thanks @RIL!!

it is VBA, all you did is correct

EDIT: ah, yeah, I always mess up with vba and vb.Net as well :slight_smile: goto should work with vba try…except was c# and vb.net

thank you in any case, I usually don’t update old nodes, but I guess remembering how much trouble it had made back then, I probably should

Thank you very much Ben
I really appreciate it

Hello Gijs,
As i was digging in Rhino, i realized that i could do the same thing that i was planning to do using excel vba by Python script within Rhino.
I an interested to know that how you can read data from excel within Python code.
Could you send me an example file that uses that way of getting data into Python code?
Thanks
CK

IronPython 2.7 used by Rhino is an implementation of Python that can run on the .NET Framework, therefore it provides support for interacting with Excel using the Microsoft Office Interop libraries.

You can use the pywin32 library to interact with Excel from IronPython. Here’s an example of how you can read data from an Excel spreadsheet using IronPython 2.7
It should be very easy and straightforward

import clr
clr.AddReference("Microsoft.Office.Interop.Excel")
from Microsoft.Office.Interop import Excel

xlApp = Excel.ApplicationClass()
xlApp.Visible = False

xlWorkbook = xlApp.Workbooks.Open(r"C:\path\to\your\excel\file.xlsx")
xlWorksheet = xlWorkbook.Sheets[1]

for row in range(1, xlWorksheet.UsedRange.Rows.Count + 1):
    for col in range(1, xlWorksheet.UsedRange.Columns.Count + 1):
        cell = xlWorksheet.Cells[row, col]
        print(cell.Value)

xlWorkbook.Close()
xlApp.Quit()
1 Like