Difference between Dynamo and pyRevit Code
I primarily teach Revit API using pyRevit. However, all the API concepts can also be applied in Dynamo python nodes, with very little differences. So let me show you them so you can adjust your code if needed.
Resources
Summary
What about Revit API classes?
Other Revit API classes are going to be used the same.
We will import them from the same namespaces and use it the same way. There might be a few exceptions, so let me know if you feel like you stumbled upon one.
Use Dynamo as a Learning Resource
π‘ You can get source code of additional packages if they are written in python.
If you are a Dynamo user, you will be familiar with many different packages, and you might want to copy some functionality to pyRevit. There are many great packages available that were written in python, and it's easy to access their source code.
Double click on the node that you are interested in and if it's written in python or uses dynamo nodes, it will open it and you will see how it functions. Often times you will see a python node, and you can double click and copy the code.
π Steal like an artist.
But you might need to modify how it gets variables and transaction.
Transactions
Transactions are also done differently in Dynamo comparing to pyRevit. In pyRevit we use Transaction class from Revit API. But in Dynamo we need to use TransactionManager.
π‘Also we have different options to use Transaction class in pyRevit.
π Here is an example (Left: Dynamo; Right: pyRevit)
Common Variables
This is where things get a little different. There are different approaches to get doc, uidoc and app variables for our Revit API scripts.
π Here is an example (Left: Dynamo; Right: pyRevit)
How to use .NET List?
This list is different to python list because it's Typed List. It means that we need to specify Type of elements inside of that list, and no other types can be added. You will see it's required inside many methods in Revit API.
π‘You will also see ICollection<T> as argument too, and we can use List<T> instead.
Here is an example:
.NET List
Importing .NET List will stay the same. We still need to use clr module to get access to System.dll
π Here is an example
Revit API Libraries
pyRevit takes care of a lot of stuff for us, including references to Revit API libraries. But in Dynamo we need to make these references ourselves.
For that we will use clr module and then reference RevitAPI and RevitAPIUI modules.
π Here is an example (Left: Dynamo; Right: pyRevit)
Get Dynamo Template's
You can find a link to Dynamo's Template here. I will go through it and compare the key differences that you should know about.
Modules
Obviously we won't be able to access pyRevit modules in Dynamo.
But also we won't have access to Dynamo's nodes inside of pyRevit.
Revit API in Dynamo
Using Revit API in Dynamo is not so much different to using it in pyRevit. Nearly everything will be the same, but there are a few differences and I want to show them to you.