0% found this document useful (0 votes)
38 views26 pages

Conversion of Dynamo Scripts To Add-Ins With PyRevit

The document discusses converting Dynamo scripts to Revit Add-ins using PyRevit, a rapid application development environment that supports various programming languages. It outlines the installation process, creation of custom commands, and the structure needed for developing a script that calculates lengths, areas, and volumes of wall elements in Revit. The author emphasizes the ease of use and debugging capabilities of PyRevit, making it a valuable tool for automation in BIM workflows.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views26 pages

Conversion of Dynamo Scripts To Add-Ins With PyRevit

The document discusses converting Dynamo scripts to Revit Add-ins using PyRevit, a rapid application development environment that supports various programming languages. It outlines the installation process, creation of custom commands, and the structure needed for developing a script that calculates lengths, areas, and volumes of wall elements in Revit. The author emphasizes the ease of use and debugging capabilities of PyRevit, making it a valuable tool for automation in BIM workflows.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Conversion of Dynamo Scripts to Add-ins with PyRevit

After reading the excellentpostfrom @Luis Alonso Oteroabout converting Dynamo scripts using
C#, I found it interesting to propose another alternative for people who are already developing scripts in
Dynamo, using Python Code Blocks. Since it is something that I still have pending to generate the content.
to complement our set of Automation courses with Dynamo inBIM AcademyAdditionally, me
It serves as an excuse to introduce a tool as brilliant as PyRevit, since its usage is quite low.
in Chile and Latin America in general.

What is PyRevit?
PyRevit is defined as a rapid application development (RAD) environment for Revit.
It basically allows for quick generation of Add-ins using various programming languages.
example, IronPython (same language for custom nodes in Dynamo), Cpython, c# or VB.Net. To the
allows for the rapid distribution of newly generated Add-ins within an office, which is
important. As if this were not enough, it also comes with a wide set of tools developed by
Ehsan Iran-Nejad (creator of PyRevit) has excellent documentation for the use of PyRevit.

Script to Convert
To demonstrate how to use PyRevit, I chose a rather simple Dynamo routine that provides a
solution to an annoying problem that Revit has, which is the calculation of lengths, areas, and volumes in
elements of the Walls category. For example, let’s assume that a Revit user wants to know in a straightforward manner
quickly the area or total length of a set of partitions in an apartment; the only alternative would be
create a schedule, add filters, add parameters to the walls, configure the schedule... all this for
obtain totals from a particular set of walls of interest, the same problem occurs in concrete.

The following Dynamo Script solves that problem. The user can run the script from Dynamo Player.
prior to a selection of particular walls and with this you will obtain the totals of length, area, and volume of
a quick way.

Script for quick calculation of length, area, and volume:


When running the script, the following result is obtained:
In the previous case, the volume would be redundant, but if it were concrete, it would be a data point.
very useful. The script in question uses the Spring Nodes node to obtain the current selection and a
a little bit of Python to create the dialog window showing the values. The Python code is
extremely simple and is presented below:

Introduction to PyRevit
Well, now that we have in mind the script we want to convert, let's see what we need to
first install PyRevit, and then create our tab with custom commands in which it will be
our button for quick calculation of lengths, areas, and volumes in Revit.

Installation of PyRevit
To install pyRevit it is necessary to go to the following link:https://github.com/eirannejad/pyRevit/releases For
In this case, we will use version 4.7.4, so we can go to the Assets section and download the
pyRevit_4.7.4_signed.exe file
The installation is standard. Close Revit before the installation and click next until
the installer finished doing its job. The next time Revit is opened, the software
It will ask for authorization for PyRevit, after this the following tab should appear.
with the default commands of PyRevit:

As we can see, all the commands that are available within the new tab were created using PyRevit, for
which proves the versatility of the tool for generating highly complex add-ins. I recommend you take a look at those.
commands, especially the tools for sheets, custom pattern creation, and analysis and selection tools.

Now, this allows us to use the default commands, but how do we create our own commands?

Add-in creation process in PyRevit


This is where, in my opinion, PyRevit shines. The process of creating Add-ins is extremely simple;
easy to understand for someone who does not have much experience with programming environments and editors
code. The first thing we need to do is install VSCode. This is a simple programming environment and
flexiblehttps://code.visualstudio.com/ Once it is installed, we must add the Python extension.
as shown in the following image:
VSCode is the tool I personally use to develop custom nodes in Dynamo, so it fits very well in
my workflow for PyRevit.
Now, going back to PyRevit. The first thing we need to do is a very particular folder structure. The structure will be
next:

Let's assume that we will create a folder in My Documents that will contain all the information about our Add-in. I will call that
Dynamo-Py folder; inside that folder we will create the following structure and for convenience I will use the following nomenclature Name of
Folder: Description. It is not very common for us to create a folder and use nomenclature like MyCommand.pushbutton, but the
the last part after the period is the way PyRevit hierarchizes and distinguishes what a tab is, what a panel is, and what a
button.
The filescirpt.pywill not be generated at this point yet. We will do that in the next step. The icon
it can be any png image and this will be associated with the button in Revit. For the icons can
usehtps://www.freepik.com/popular-icons to find an icon that is of your interest.
Creation of script in VSCode
Now inside VSCode we will open the folder that ends with .pushbutton. This will allow us to create a new
file which we will callscript.pyNote that it is important to finish the name with .py, as this
Tell VSCode that the file in question is a Python script.

The script we will develop will consist of 5 main parts. I will explain each of them in
continuation
Part 1:

This is nothing more than the comment or tooltip that will appear in Revit when the user hovers the cursor over the new button created.
Note that 3 """ are used at the beginning and end to terminate the comment.

Part 2:

The first step, similar to an IronPython script in Dynamo, is to import the assemblies that will be used. In our case we need
access the properties of the walls, for which we need to access the Revit database; therefore we import the DB. Then,
We use one of the core modules of PyRevit to extract the active document.
Part 3:

The following lines of code allow us 2 things: first, to get the selection that the user has in the Revit interface and then
generate a list of the selected items.

Part 4:

The following portion of code is the logic that allows us to extract the values of the properties that we are interested in. Note that we are
using BuiltInParameters, that is, the name of the parameters in the API, which is not the same as the name the user sees.
For example, the length parameter in the wall properties is equivalent to CURVE_ELEM_LENGTH. For more information about
For these parameters, consult the Revit API documents at the following link:https://www.revitapidocs.com/2017/fb011c91-
be7e-f737-28c7-3f1e1917a0e0.htm

Another important note is that all internal measurements of the Revit API are in imperial units, so it is necessary to make
the conversion to meters.
Part 5:

The last thing we need is to show the result of our operation. For this we could create a hook to display the window of
Taskdialog similar to what we did within the Dynamo script. However, I prefer to use the presentation window included in
PyRevit, to access this we just need to give Print.

Adding tab in Revit


Perfect, up to this point we already have the script. The icon and the folder settings we need for
create our tool. Now we need to tell PyRevit within Revit to add this structure
within the software. To do this, the first step is to go to Settings within the PyRevit tab:
Within Settings we add a folder and look for the first folder in our hierarchy, to
our example would be the Dynamo-Py folder.
Then we execute the Save Settings and Reload button. After that, a should appear.
new tab in Revit with our button as shown in the following image:

And that is all, there is no need to do anything else but test our script.
As we can see, we obtain the same values as with the Dynamo script, but with
several advantages. Among them, that we can now anchor this new button to a keyboard shortcut,
With which we can obtain the desired calculations very quickly.
Final notes
Debug and changes to script

To close, I want to mention how easy the debugging process of the created script is. Something that
it usually takes a lot of time using other methods. For the case of PyRevit,
let's assume we want to change the script to display another parameter, or simply
change the names or units in which the information is presented. It's as simple as doing
the change, save the script and run the command again:

You might also like