by Demetris Kekatos: email@example.com
This is a tutorial for amateur users of the VSTA Macro tools in Revit. After this tutorial you will be able to draw structural linear elements in Revit (beams, columns and braces) retrieving the geometry data from an open Excel spreadsheet.
When transferring geometry from one software to another, Excel can become the mid step between the two software. For instance you can retrieve the start and end point of all the elements from a Grasshopper building model, print this data to an Excel Spreadsheet and then make the VSTA macro read this data and draw the elements in Revit.
Even if you don’t have in mind to transfer elements from whatever software to Revit, this tutorial shows the way to draw beams, column and braces in general. So then you can use these tools to create a macro that draws towers automatically for instance.
So we are going to start from the very beginning :
Open a new Revit 2011 project and Save you project as importingGeometry
- In the Revit environment:
Go to the “Manage” tab, click on the Macros icon and choose Macro Manager
- The Macro Manager window pops up and here you can see two tabs . Application and a Tab with the name of the file. We will create a macro in the importingGeometry Tab, that means that the macro can only be used from this specific file.
Click on Create > Module
Note: If we want to create a macro and it in different projects you have to create the same macro into the Application tab.
- Enter a name for the Module (continuous name with no gaps). Select VB.NET as the programming language and before clicking OK you may add a description of what the macro does.
This will later help you when you have a list of different macros in Revit.
Now you are able to see your empty macro.
- Next step click on > Edit, in order to get into the macro and write our code.
- So our subroutine “create Beams” will be placed between the default subroutines that Revit places in this Class (called: ThisDocument)
Here we just imported a message box
- In order to make this simple macro work, we have to compile this first.
Go to > Build > Build import_geometry
- After compiling the macro, we are able to use and Run it from the Macro Manager window.
- Click on Run and a message will pop out:
Now we are sure that everything went through..
back to the drawBeams tutorial, next steps:
Get access to Microsoft Excel from the VSTA Macro environment :
- The first step is to add the proper Reference file, that Revit will use in order to “talk” with Excel.
Go to > Project > Add Reference…
On the COM tab choose the Microsoft Excel Reference as shown to the image below
- Next we have to import the namespace of Excel. Attention the best place to import namespaces in Revit VSTA is above all the default code that Revit imports in each macro
Before presenting the needed code I just want to refer what Revit needs to draw an element like a beam:
- Start and End point of the element
- The section of the element and
- The level placed to the project
When coding it’s really important to know exactly what you need in order to achieve your goal… Now we are ready to write some code
Getting access to Revit model and the open Excel Spreadsheet:
'' method for creating Beams in Revit Public Sub createBeams() MsgBox("Retrieve Beam Data") ''the first thing to do is get access to the Revit project Dim myModel As Autodesk.Revit.DB.Document myModel = Me.Document ''Get access to the active Excel Document Dim myExcel As Microsoft.Office.Interop.Excel.Application Dim myWorkBook As Microsoft.Office.Interop.Excel.Workbook myExcel = CType(GetObject(, "Excel.Application"), Application) myWorkBook = myExcel.ActiveWorkbook ''get access to the right spreadsheet Dim beam_sheet As Microsoft.Office.Interop.Excel.Worksheet beam_sheet = myWorkBook.Worksheets(1) 'the number of the parenthesis sets the sheet we want to reach
Retrieve Element data from Excel:
''InputBox where the user is called to type the number of beams Dim num_beams As Integer num_beams = InputBox("Please enter the number of Beams...") '' a loop to read data for each element from excel and draw it in Revit For i As Integer = 2 To num_beams + 1 '' retrieve Start Point x,y,z Dim my_x1 As Double Dim my_y1 As Double Dim my_z1 As Double my_x1 = beam_sheet.Cells(i, 2).value my_y1 = beam_sheet.Cells(i, 3).value my_z1 = beam_sheet.Cells(i, 4).value '' retrieve End Point x,y,z Dim my_x2 As Double Dim my_y2 As Double Dim my_z2 As Double my_x2 = beam_sheet.Cells(i, 5).value my_y2 = beam_sheet.Cells(i, 6).value my_z2 = beam_sheet.Cells(i, 7).value '' retrieve beam section Dim section As String section = beam_sheet.Cells(i, 8).text
Finally draw elements into the Revit project:
''Transaction(in order to change the existing Revit model we always have to start a transaction) Dim structure_builder As Autodesk.Revit.DB.Transaction structure_builder = New Autodesk.Revit.DB.Transaction(myModel, "create Beams") '' lets create some beams If (structure_builder.Start() = Autodesk.Revit.DB.TransactionStatus.Started) Then ''set start point and end point of the element Dim start_point As Autodesk.Revit.DB.XYZ = Nothing Dim end_point As Autodesk.Revit.DB.XYZ = Nothing start_point = New Autodesk.Revit.DB.XYZ(my_x1, my_y1, my_z1) end_point = New Autodesk.Revit.DB.XYZ(my_x2, my_y2, my_z2) 'define the element line Dim myBeamLine As Autodesk.Revit.DB.Line = Nothing myBeamLine = Autodesk.Revit.DB.Line.Bound(start_point, end_point) 'load the Section of the element from Revit Structural Library Dim beamLibrary As String beamLibrary = "C:\ProgramData\Autodesk\RST 2011\Imperial Library\Structural\Framing\I_W_Wide Flange.rfa" Dim beam_sym As Autodesk.Revit.DB.FamilySymbol = Nothing Dim loaded As Boolean loaded = myModel.LoadFamilySymbol(beamLibrary, section, beam_sym) 'define Levels Dim my_level As Autodesk.Revit.DB.Level 'finally draw the beam Dim myBeam As Autodesk.Revit.DB.FamilyInstance = Nothing myBeam = myModel.Create.NewFamilyInstance(myBeamLine, beam_sym, my_level, Autodesk.Revit.DB.Structure.StructuralType.Beam) ''finalize the transaction and update model structure_builder.Commit() End If Next MsgBox("Macro Completed") End Sub
a. In order to draw the beam we have to load the right family of sections that Revit has in its database. C:\ProgramData\Autodesk\RST 2011\Imperial Library\Structural\Framing\I_W_Wide Flange.rfa… So you have to find where Revit keeps the structural families in your system and paste the right path to this code.
b. About levels. We can realise that the levels are used in the code without defining a value. Giving the start and end point of the element Revit doesn’t need a level to place the element to the correct elevation
After importing the code we have to “Build” the macro and then run it from the Macro Manger Window
Don’t forget to create an excel spreadsheet with the proper info, because the macro will fail if it doesn’t find the source to read data
This is what you should get in Revit after running this macro…
To Conclude: This is the main body on how to create elements In Revit. For instance if you want to create braces or columns the whole logic is the same the only thing that changes is the last command:
''draw columns Dim myColumn As Autodesk.Revit.DB.FamilyInstance = Nothing myColumn = myModel.Create.NewFamilyInstance(myColumnLine, column_sym, my_level, Autodesk.Revit.DB.Structure.StructuralType.Column)
The part of levels is a bit tricky when creating columns.. This will probably be a subject in a following tutorial…
There are numerous ways to define the start and end point of an element and draw elements in Revit through VSTA macros. Hope this tutorial was informative…
Welcome to Revit Macros…