Happy Friday! This week we are finishing our plug-in that will read an excel file and create levels from the excel file. We are taking the common Dynamo script as a guide and walking through how you would write this in C#. The goal is to show how Dynamo nodes and script structure relates to the Revit API so you can relate the two as you start coding. Last week , we read the excel file so this week we just need to set up our method to create the levels and execute our program.
I am using a simple script that many people have made versions of for creating floor levels in Revit from an Excel spreadsheet. The script is roughly based on one of the scripts from ArchSmarter's article Save Time with Revit Dynamo.
The script has 3 basic parts:
The last part of our Dynamo script is creating the levels. In order to do that in our program, we can review the methods available in the Level class in the Revit API documentation. We see that we can use the Create method. So we can write our method which will not return anything. It will need the document and our two lists with the level names and heights that we modified last week as parameters. Then we can create a for-loop with a counter to go through each item in our list. The Create method requires the height to be a double and currently our level heights are stored as strings so we will need to parse our value to a double. Please also note that the Revit API create levels with decimal feet so you would need to add a conversion if your excel file lists the height in meters. This information can be found in the API documentation. Then we can use Level.Create to create our level. Finally, we can rename the level with the name from our list.
The last step for our program does not directly relate to our Dynamo script but it is the most important step to have our application run. We need to write our execute method. First we will declare our variables. We always want to have our active document and document since we use these for almost every method in the Revit API. Then we also want to have a variable for the application for debugging. After that, we can move on to the variables specific to our program. We will need the file location for the Excel file which you will need to enter and the name of the sheet which you will need to update if you renamed it. Finally, we want two empty lists. One will store the level names, and one will store the level heights. These are the lists we have used in our methods so far to store the data from excel.
Then we can call our methods. We will want to put this in a try catch loop so our program reports the error if it fails. Additionally, When making changes to the Revit document, you need to wrap your changes in a transaction. This is in order to allow you to roll back changes if your code fails part way through. Revit won’t allow you to make changes outside of a transaction, so we put our CreateLevels method in one.
As is noted in Autodesk's My First Revit Plugin we also need a Ribbon class and an .addin file to run our program. The ribbon class puts your plug-in button on the Revit ribbon and you will need to provide an icon. You can review these pieces for this program on my GitHub page. You will need to update the Excel file path as noted above and compile it to use it.
And that’s how we can translate a Dynamo script into a basic plug-in. You can expand on your plug-in by adding a UI with WPF which would allow users to enter their own file path instead of hard-coding it. This would also allow you to add other options and features if you wanted to expand on the program. There’s lots of options once you have a foundation for your plug-in. Happy coding!