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.

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.

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.

import clr

clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

doc = DocumentManager.Instance.CurrentDBDocument

#Do some action in a Transaction
TransactionManager.Instance.EnsureInTransaction(doc)
#changes here
TransactionManager.Instance.TransactionTaskDone()
doc   = __revit__.ActiveUIDocument.Document

# Regular Style
t = Transaction(doc, "Change")

t.Start()
#changes here
t.Commit()

# Context Manager Style
with Transaction(doc, 'Change') as t:
    t.Start()
    # Changes Here
    t.Commit()
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)

import clr

clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager

doc   = DocumentManager.Instance.CurrentDBDocument
uidoc = DocumentManager.Instance.CurrentUIApplication.ActiveUIDocument
app   = doc.Application
#1️⃣ Variables
uidoc = __revit__.ActiveUIDocument
doc   = __revit__.ActiveUIDocument.Document
app   = __revit__.Application

#2️⃣ Alternative with pyrevit module
from pyrevit import revit

doc   = revit.doc
uidoc = revit.uidoc
app   = revit.app
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)

#1️⃣ Create Empty List and Add Items
empty_list = List[ElementId]()
empty_list.Add(element_A.Id)
empty_list.Add(element_B.Id)

#2️⃣ Convert python list in .NET List
python_list_ids = [element_A.Id, element_B.Id]
List_el_ids     = List[ElementId](python_list_ids)
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:

import clr
clr.AddReference('System')
from System.Collections.Generic import List # List[ElementId]()
.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

import clr

clr.AddReference('RevitAPI')
from Autodesk.Revit.DB import *
from Autodesk.Revit.DB.Structure import *

clr.AddReference('RevitAPIUI')
from Autodesk.Revit.UI import *
from Autodesk.Revit.DB import *
from Autodesk.Revit.DB.Structure import *
from Autodesk.Revit.UI import *
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.

HomeWork

⌨️ Happy Coding!

Questions:

Q1

Q1

Discuss the lesson:

P.S. Sometimes this chat might experience connection issues.
Please be patient or join via Discord app so you can get the most out of this community and get access to even more chats.

P.S. Sometimes this chat might experience connection issues.
Please be patient or join via Discord app so you can get the most out of this community and get access to even more chats.

© 2023-2024 EF Learn Revit API

© 2023-2024 EF Learn Revit API

© 2023-2024 EF Learn Revit API