Dynamo Python Primer
  • Take Dynamo Further 🚀
  • 1 Hello Python 🐍
    • 1.1 Why Should I Learn to Code?
    • 1.2 Python Introduction
    • 1.3 What is IronPython?
  • 2 Seeing The Bigger Picture 🔭
    • 2.1 Context Matters
    • 2.2 What is an API?
    • 2.3 The .NET Framework
    • 2.4 Object-Oriented Programming
  • 3 Getting Started 🛴
    • 3.1 Dynamo's Python Node
    • 3.2 Boilerplate Setup Code
    • 3.3 Basics: Input and Output
    • 3.4 Common Errors
  • 4 Revit-Specific Topics 🏡
    • 4.1 Introduction to Revit's API
    • 4.2 How to Read Revit's API Documentation
    • 4.3 Doc, UIDoc, App, UIApp
    • 4.4 Unwrapping Revit Elements
    • 4.5 The FilteredElementCollector
    • 4.6 Geometry Conversion Methods
    • 4.7 Working With Parameters
      • Family Parameters
      • Global Parameters
      • Project Information Parameters
      • Group Parameters
      • Built-In Parameters
    • 4.8 Working With Transactions
    • 4.9 Opening & Closing External Files
    • 4.10 Prompting UI Selection
    • 4.11 Working With Units
    • 4.12 Built-In Categories
    • 4.13 Family Acrobatics
    • 4.14 Feedback: TaskDialogs
  • 5 Glossary 📚
  • About This Primer 👋
Powered by GitBook
On this page

Was this helpful?

  1. 4 Revit-Specific Topics 🏡

4.8 Working With Transactions

Using Transactions is easy and you won't get very far without them!

PreviousBuilt-In ParametersNext4.9 Opening & Closing External Files

Last updated 5 years ago

Was this helpful?

Revit is a Database

In the , we covered how Revit is best thought of as a giant database program. Changes to this database need to be carefully ordered, the process needs to be managed and checked (for validity against schemas and for legality against the design of Revit's API). All of this is handled by Revit's native API.

Any code that is intended to change the Revit document's database needs to be 'wrapped in a transaction'.

Dynamo's TransactionManager

When coding directly for the Revit API (either in a Macro or a C# add-in) we can simply start a new transaction by instantiating a new object of the Transaction class using code and calling its Start() method. However, in the Dynamo context, we'll need to handle this differently.

The TransactionManager is a part of Dynamo's API that handles changes made to the Revit document's database from within the Dynamo application. We can start a transaction like so:

#Boilerplate Code

#To open a new transaction, we need to use the TransactionManager class
#Which takes the current document's handle as an argument
TransactionManager.Instance.EnsureInTransaction(doc)

#The following method will close the transaction and confirm any changes
#made to the Revit document's database.
TransactionManager.Instance.TransactionTaskDone()

Between these two method calls, you'll need to put any code that's affecting the Revit document's database. This could be:

  • Creating a new sheet

  • Updating a parameter value

  • Modifying an element's colour overrides

In short, editing the Revit document in (nearly) anyway requires an open transaction.

Structuring Transactions

It's worth knowing that it's possible to enable more advanced workflows by structuring edits to the database using Transactions, TransactionGroups and SubTransactions.

For more information, TheBuildingCoder blog has an excellent in-depth section on carrying out fancy transaction behaviours..

Read more here
Transaction
introduction