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.
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.
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.
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.
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.
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)
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
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()