S1.5 - Hand - Code Vs Node - Kilkelly-2

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 30

Session 1.

Code vs Node: The Ultimate Revit Automation


Smackdown!

Michael Kilkelly AIA, ArchSmarter

Class Description
So you want to learn how to automate Revit? Great! But what method should you
use? This session will take an in-depth and entertaining look at code (Revit macros)
versus node (Dynamo) and put them through a series of automation challenges.
See who wins this ultimate smackdown!

About the Speaker:


Michael Kilkelly AIA is the founder of ArchSmarter, a website dedicated to helping
architects and designers work smarter, not harder. Michael is also a principal at
Space Command, an architecture and consulting firm in Middletown, CT.
Previously, Michael was an associate at Gehry Partners in Los Angeles. Michael
received his B.Arch from Norwich University and his MS in Design and Computation
from MIT.
Session 1.5
Code vs Node
Michael Kilkelly AIA, ArchSmarter

Ladies and Gentlemen! Welcome to Code vs Node: The Ultimate Revit


Automation Smackdown! This title match will consist of three rounds of
Revit automation challenges! Let’s get ready to rumble!!!

Code vs Node
Want to get Revit to do some of your work for you? Interested in automation but
not sure where to start? You’re in the right place!

The purpose of this session is to take a side by side look at Dynamo and Revit
macros and put them through their paces in a series of automation challenges. In
doing this, we’re going to talk about the strengths and weaknesses of both
platforms as well as why you might want to use one vs the other depending on the
task.

My goal for this session is to help you determine which automation method is best
for you based on how you learn, the task you want to automate, and how the
automation is going to be used.
Page 2 of 30
Session 1.5
Code vs Node
Michael Kilkelly AIA, ArchSmarter

Some Things to Consider


When considering automating a task in Revit, think about the following:
 Who is going to make use of the automation?
 What’s the skill level of the people using it?
 How repeatable is the problem you’re solving?
 Is the problem well-defined or is it fuzzy?
While you can essentially automate the same tasks in Dynamo as you can with
Revit macros, the circumstances of your project or your firm may make one
method more applicable than the other. As with most things, context matters. We
will come back to these factors at the end of the session.

Let’s go ringside for the tale of the tape.

What is Dynamo?
Dynamo is a visual programming environment for Revit. It is open source software
and available for free to Revit users. You can download the software at
DynamoBIM.org. A paid stand-alone version, called Dynamo Studio, is also
available. Dynamo Studio does not require Revit to run.

Page 3 of 30
Session 1.5
Code vs Node
Michael Kilkelly AIA, ArchSmarter

In Dynamo, you assemble programs graphically rather than writing code. Outputs
from one node are connected to inputs on another. A program flows from node to
node along a network of connectors. The result is a graphic representation of the
steps required to achieve the end design.

Dynamo files are called graphs and have a .dyn file extension. They are very
lightweight files. Note that Dynamo must be installed in order to run a graph. You
cannot run graphs outside of Dynamo. Also, Dynamo associates itself with whatever
is the current Revit model when it is launched. In order to run a graph on another
file, you must quit Dynamo, make the new file active in Revit, then relaunch
Dynamo.

Dynamo has an extensive library of user-created nodes. These nodes are freely
available and the node library can be searched directly from the Dynamo
interface. Nodes are downloaded and installed to your local computer. Graphs
using custom nodes require those nodes be installed on the local computer in order
to run properly.

Page 4 of 30
Session 1.5
Code vs Node
Michael Kilkelly AIA, ArchSmarter

What are Revit Macros?


A macro is a user-created command that is coded using Revit’s API. Macros are run
directly inside of Revit and are saved in the project file. Other applications, like MS
Office, provide the ability to record macros directly from your actions on the
screen. Unfortunately, Revit does not have this functionality. You must code your
Revit macros directly.

Macros are created using Sharp Develop, Revit’s built-in coding software. Macros
can be written using the C#, VB.Net, Ruby, or Python programming languages.
Macros must be compile before they can be run so they typically run much faster
than similar Dynamo graphs.

Since they are written using a programming language, macros can incorporate
elements of a user interface. Also, macros can be translated into add-ins that are
accessible from the Revit add-ins ribbon.

Page 5 of 30
Session 1.5
Code vs Node
Michael Kilkelly AIA, ArchSmarter

The Revit API


Both Dynamo and Revit Macros make use of the Revit API. Anything you can do
with a macro is possible in Dynamo. . . at least in theory.

To get started with either Dynamo or Revit macros, you should install the Revit SDK.
The SDK (software development kit) contains help files and sample code that will
assist you as you learn to automate Revit. The SDK can be installed from the main
page of the Revit installer or it can be downloaded from the Autodesk Developer
Network at www.autodesk.com/developrevit.

The most useful part of the SDK is the Revit API help file. The help file is a roadmap to
understanding the API. It lists all the methods and properties that are exposed
through the API. The help file can be intimidating as first. It’s not an easy read.
However, with practice, you’ll find it an invaluable resource.

Automation Challenges
The best way to compare Dynamo with Revit macros is to put the two platforms
through a series of challenges and see how they stack up. We’re going to work
through three rounds of automation challenges to highlight the differences and
similarities between code and node.

Page 6 of 30
Session 1.5
Code vs Node
Michael Kilkelly AIA, ArchSmarter

Round 1
The first automation challenge consists of changing Revit text notes from lowercase
to uppercase. This would be extremely tedious to do manually. However, this task is
very easy to automate. The steps that we’re going to use are the same for both
code and node. They are as follows:

 Get all text elements in the current model


 Loop through each element
 Change text case to UPPER (or lower)

Let’s see how we do this in Dynamo and as a Revit macro.

Round 1 – Dynamo
First up is Dynamo. To start, we launch Revit, open the file on which we want to
change the text, then we launch Dynamo and create a new graph.

The graph we’re going to create is very straightforward. All it takes is three nodes.
Here’s what it looks like:

Page 7 of 30
Session 1.5
Code vs Node
Michael Kilkelly AIA, ArchSmarter

1. FIrst, we specify a category using the “Categories” node. We set the value of
this node to “Text Notes”.
2. Next, we connect the node to “All Elements of Category”. This selects all the
text notes in the current file.
3. We’re going to use the “TextElement.ToUpper” node to change the text case
to upper. This node is part of the Clockwork library. Click “Packages” in the
menu bar and search for the Clockwork package. Once it has downloaded,
search for “TextElement.ToUpper” in the search field on the left to find the
node. Drag the node onto the node window and connect the output from
“All Elements of Category”.
4. That’s it! Switch over to Revit to see your text in upper case.
5. If you want to change all your text to lower case or title case (capitalize the
first letter of each word), choose the appropriate node from the Clockwork
library. Make sure you disconnect any other text nodes before you connect a
new one. If you don’t, Dynamo will get caught in an infinite loop, forever
changing the text to upper, then lower, then upper. . . until you force close
the program.

That was easy. Let’s see how we’d do this in a Revit macro.

Round 1 – Revit Macro


Now it’s code’s turn. To start, open a new Revit file. Go to the “Manage” ribbon
and click the “Macro Manager” icon to open the dialog.

1. In Macro Manager, click “Module” to create a new module. Modules are


containers for macros. A single Revit file can have multiple modules each
containing multiple macros. Give your module a name. Module names
cannot contain spaces or special characters like punctuation. Next, select a
programming language for your module. You can choose C#, VB.Net,
Python, or Ruby. I’ve found that VB.Net is very easy for beginning
programmers so that’s the language we’ll use.
2. Once you create the module, Sharp Develop, Revit’s built-in code editor
opens in the background. Back in the Macro Manager, click “Macro” and

Page 8 of 30
Session 1.5
Code vs Node
Michael Kilkelly AIA, ArchSmarter

enter “TextToUpper” as the name for your new macro. Like module names,
macro names cannot contain spaces or special characters.
3. Switch over to SharpDevelop. In the code editing window, you’ll see “Sub
TextToUpper()”. This is out macro. You can delete the other two subroutines,
“Module_Startup” and “Module_Shutdown”.
4. The first thing to do with any macro is to define the current Revit document.
Macros can run on the macro file containing the macro, or on whatever is
the current file, which is more useful. The define the current document, type
the following:

This defines a variable called “curDoc” as a Document object and then sets
its value to the current Revit file which is represented in code as
me.application.activeUIdocument.document
5. With the document defined, we can now select all the text elements. To do
this, we’ll use a Filtered Element Collector. This object collects all the elements
in the current Revit file. We can then filter the collector for the specific
elements we need. To create the collector, type the following line:

6. To filter the collector, we’re going to specify the Text Notes category through
the .OfCategory line.
7. The Revit API includes objects for all the built-in categories. When you start
typing the code, SharpDevelop will pop-up an Intellisense dialog where you
can select the specific category, rather than having to type it out.
8. Let’s test our code and make sure it’s collecting the text elements. Below the
collector code, add the following:

9. Unlike Dynamo, Revit macros need to be compiled before they can be run.
Compiling the macro converts it from VB.Net into Microsoft’s runtime
lanaguage. To compile the macro, go to “Build” on the menu bar and click
“Build Solution”.

Page 9 of 30
Session 1.5
Code vs Node
Michael Kilkelly AIA, ArchSmarter

10. If your code has any errors, you’ll see them down in the Error window located
below the code window
11. If you didn’t get any errors, switch over to Revit and go to Manage > Macro
Manage. Select the “TextToUpper” macro and click the “Run” button. You
should see the following dialog box in Revit:

12. Provided there are some text notes in the current model file, you should see a
number indicating the number of text notes.
13. Switch back to SharpDevelop and delete the TaskDialog line of code. Since
we know our collector is working, we now need to loop through the text
notes and change their text to upper. To create the loop, we’re going to use
a For Each loop. Add the following code to your macro:

Page 10 of 30
Session 1.5
Code vs Node
Michael Kilkelly AIA, ArchSmarter

14. The For Each loop goes through all the elements in the collector. The current
element is assigned the “curTextNote” variable. I can refer to the current
element inside of the loop using this variable
15. We’re going to change the text to uppercase inside of the loop. Add the
following code after the For Each line:

16. Because we’re making a change to the model by modifying the text, we
need to lock the model so the change can take place. We do this using a
transaction. The transaction gets created before the code that makes the
change. After the code that makes the change, we’ll then commit the
changes to the model and dispose of the transaction. If we try to run the
macro without a transaction, we’ll get an error in Revit. Before the For Each
loop, add the following code:

17. This code creates a variable for the transaction. We have to specify the
document we’re working on as well as a name for the transaction. The
transaction name is what appears in Revit’s undo menu.
18. After the For Each loop, add the following code to commit our changes and
dispose of the transaction.
19. Go to Build > Build Solution to compile the macro. Switch over the Revit, go to
Manage > Macro Manager and run the macro. The text changes to upper
case!
20. To change the text to lower case, switch back to SharpDevelop. Highlight the
TextToUpper code and copy it to the clipboard. Below the code, paste
another copy. Change the macro name to “TextToLower”. Change the
name of the transaction to “Text to lower”. Inside of the loop, modify the
existing code to the following:

21. Build the macro, switch over to Revit and run the macro. The text now
changes to all lower case.

Page 11 of 30
Session 1.5
Code vs Node
Michael Kilkelly AIA, ArchSmarter

Here is the code in its entirety:

Round 1 - Next Steps


Both code and node handled this task with relative ease. We can easily expand
this automation to do a lot more. Here are some ideas:
 Change the text only in the current view
 Change text in multiple RVT files
 Change room names, sheet names, etc. . .

So who do you think won round 1? Which one would be easier for you to create?
Which one would be easier to have others in your office use? Which one would be
easier to modify?

Page 12 of 30
Session 1.5
Code vs Node
Michael Kilkelly AIA, ArchSmarter

Round 2
On to the next challenge! In round 2, we’re going to automate creating 20 sheets
using data from an Excel file. It’s easy to create sheets in Revit but wouldn’t it be
better if you could simply outline your sheet index in Excel then have Revit
automatically create the sheets, update the sheet names and numbers then add a
views?

Both the Dynamo graph and Revit macro are going to use the following steps:
 Read Excel data and get sheet numbers, sheet names, and views
 Remove the Excel header data
 Create a new sheet
 Update the sheet’s parameters using Excel data
 Add view to sheet

Here’s the Excel data we’re going to use:

Let’s see how we do this in Dynamo and as a Revit macro

Page 13 of 30
Session 1.5
Code vs Node
Michael Kilkelly AIA, ArchSmarter

Round 2 – Dynamo
Dynamo makes working with Excel very easy. There are nodes that easily read and
write data to Excel. Here is an image of the finished graph:

To make the graph easier to understand, I’ve grouped the nodes based on their
specific task.

Let’s look at each group in detail:

Page 14 of 30
Session 1.5
Code vs Node
Michael Kilkelly AIA, ArchSmarter

In the first group of nodes, we specify the Excel file and connect it to the
Excel.ReadFromFile node.

Now that we have the data, we need to format and separate out the individual
columns into separate lists. We also remove the header row, from the list.

Page 15 of 30
Session 1.5
Code vs Node
Michael Kilkelly AIA, ArchSmarter

We have a list of views from the Excel file. This is just a list of view names. We need to
get the actual view objects from the Revit model. To do this, we’re going to get all
the ViewPlans from the model then compare this list with our list from Excel. We’re
going to use the results of this comparison to filter the ViewPlan list.

Now that we have the sheet numbers, names, and view in their own lists, we’re
going to create the sheets using the Sheets.ByNameNumberTitleblockAndView.
We’re going to use the FamilyTYpes node to select the titleblock family. Connect all
these nodes as shown below to create the sheets.

Page 16 of 30
Session 1.5
Code vs Node
Michael Kilkelly AIA, ArchSmarter

Switch over to Revit to see the new sheets.

Round 2 - Revit Macro


The round 2 macro follows similar steps as the Dynamo graph. The only difference is
that instead of reading an Excel file, we’re going to read a CSV version of the Excel
file. Macros can read Excel but it requires a lot more code than just reading a CSV
file.

Here is the code for creating sheets:

Page 17 of 30
Session 1.5
Code vs Node
Michael Kilkelly AIA, ArchSmarter

Page 18 of 30
Session 1.5
Code vs Node
Michael Kilkelly AIA, ArchSmarter

Round 2 - Next Steps


As you can see, this is a really useful tool. You’re going to create sheets for nearly
every project. Why not have Revit do all the work for you? Some ideas to expand
this automation are:
 Add multiple views to sheets
 Specify location of view on sheet
 Use different title block for some sheet
 Update additional sheet instance parameters

Round 3
The final round is a tough one. In this challenge, we’re going to design and model a
house automatically. Yes, a house. Here are the steps we’re going to use to
automate this process:

 Define corner points


 Create floor, walls, and roof
 Change roof slope
 Add windows and doors

Let’s see how code and node stack up on this task.

Round 3 – Dynamo
Dynamo ran into some trouble with this challenge. Creating the walls and floor was
easy but Dynamo could not create the roof or add the doors and window - at least
not without a significant amount of custom code. Even then, there was no
guarantee that the code would work.

There is a node available that adds doors and window. However, it did not work in
Dynamo version 1.0. It’s possible a custom node could be created to do the task
but this wasn’t tested as part of the challenge.

There are currently no nodes available that create footprint roofs. Likewise, using
custom Python code could not create a roof. Hopefully this will get resolved in a
future release.
Page 19 of 30
Session 1.5
Code vs Node
Michael Kilkelly AIA, ArchSmarter

Here is the graph in its half-finished state:

To make the graph easier to understand, I’ve grouped the nodes based on their
specific task.

Let’s look at each group in detail:

Page 20 of 30
Session 1.5
Code vs Node
Michael Kilkelly AIA, ArchSmarter

The first group defines the house’s base geometry. Four points are created then
these points are used to create lines.

Once the lines have been defined, the walls can be created. The
Wall.ByCurveAndHeight node requires a curve (line) as well as the wall height, a
level, and a wall type. The Levels and Wall Types are used to define their respective
indputs. A code block is used to define the wall height.

Page 21 of 30
Session 1.5
Code vs Node
Michael Kilkelly AIA, ArchSmarter

The Floor.ByOutlineTypeAndLevel node is used to create the floor. This node requires
an outline as an input. The create the outline, the lines created in step 2 are joined
using a series of Curve.Join nodes.

An attempt was made to insert doors and windows using the


Place.Windows.Doors.InWalls node. This node did not give any errors but also did
not insert any doors or windows.

Page 22 of 30
Session 1.5
Code vs Node
Michael Kilkelly AIA, ArchSmarter

It was not possible to create a footprint roof. There are not nodes available.
Attempts made at writing a custom node in Python did not get very far.

Here is a screenshot of the resulting house:

Not a very nice house, is it?

Page 23 of 30
Session 1.5
Code vs Node
Michael Kilkelly AIA, ArchSmarter

Round 3 - Revit Macros


Completing this challenge using code was much more successful. All of the steps
were achieved. The macro requires a lot of code but it is logically structured. A
function to get a level object by name is used to make the macro more modular.
Here is the code:

Continued on next page.


Page 24 of 30
Session 1.5
Code vs Node
Michael Kilkelly AIA, ArchSmarter

Continued on next page.

Page 25 of 30
Session 1.5
Code vs Node
Michael Kilkelly AIA, ArchSmarter

Continued on next page.

Page 26 of 30
Session 1.5
Code vs Node
Michael Kilkelly AIA, ArchSmarter

Below is a screenshot of the resulting house:

Page 27 of 30
Session 1.5
Code vs Node
Michael Kilkelly AIA, ArchSmarter

Adjusting the point coordinates or roof slope values can result in some interesting
designs, like the one shown in the image below:

Round 3 - Next Steps


While this example isn’t particularly realistic, it does illustrate some of the limitations
of Dynamo, particularly when it comes to the availability and reliability of custom
nodes. This challenge could easily be modified to do some of the following:

 Prompt the user to select the corner points.


 Incorporate multiple “designs” and let user decide which to use.
 Create a furniture, restroom, or apartment layout (rather than an ugly house)

Conclusion
Choosing the appropriate automation method depends on many factors. What is
the nature of the task? Is it a well-defined problem with logical steps or is it fuzzy?
Who’s going to use the resulting tool? Just you or a few people on your project
team? Or is it for your whole office? Likewise, what’s the skill level of the people
using the tool? Will they be able to open Dynamo, make some changes to the
nodes and run the graph? Lastly, what are your own interests and abilities? Does
the idea of learning to code scare you or excite you?
Page 28 of 30
Session 1.5
Code vs Node
Michael Kilkelly AIA, ArchSmarter

To help you decide, here’s a handy chart that highlights some of the strengths and
weaknesses of both Dynamo and Revit macros.

So what do I recommend? Learn both! If you’re just starting out with Revit
automation, here’s the learning path I recommend:

1. Download the Revit API help file


2. Start playing with Dynamo
3. Create graphs to automate your tasks
4. Learn how to create your own nodes
5. Translate a Dynamo graph into a macro
6. Create a user interface for your macro

So who won? Code or node? Go to www.archsmarter.com/code-vs-node to see


the results of the RTC Smackdown.

Page 29 of 30
Session 1.5
Code vs Node
Michael Kilkelly AIA, ArchSmarter

Resources
Want to learn more about Dynamo or Revit macros? Check out these resources for
more information.

• Blogs
– ArchSmarter – http://archsmarter.com
– Boost Your Bim ‐ http://boostyourbim.wordpress.com
– The Dynamo Primer ‐  http://dynamoprimer.com
– Simply Complex ‐ http://therevitcomplex.blogspot.com

• Online Forums
– AUGI ‐ http://forums.augi.com/forumdisplay.php?218‐Revit‐API
– DynamoBIM ‐ https://forum.dynamobim.com

• Books
– Autodesk Revit 2013 Customization with .Net How‐to 
by Don Rudder 

• Online Courses
– Revit Macros ‐ http://www.archsmarter.com/go/revit‐macros
– Learning Dynamo ‐ http://www.archsmarter.com/go/learning‐dynamo

Page 30 of 30

You might also like