LearnRevitAPI

Dec 11, 2022

Transactions

Transactions are like Revit guardians—they make sure that you won't change anything unless it's intended. 

Transaction Types

There are 3 types of Transactions.
But you are going to use regular one 95-100% of the time. 

Transaction Types:
1️⃣ Transaction
2️⃣ SubTransaction       
3️⃣ TransactionGroup

Let's look at each and when to use them

Transaction

If you want to make changes to a Revit project, you'll need to use regular transactions. This means that you need to create an instance of the transaction and then make your changes between .Start() and .Commit(). There is also .Rollback() available if you want to cancel changes.

📰 RevitAPI docs

P.S. Avoid placing transactions inside of For/While loops if possible. It might ruin performance of your script too much.

💡 There is a clever way of using .Rollback to get associated elements with a Level.

If you delete a level, any element associated with that level will also be deleted. Luckily doc.Delete() method returns deleted element id set. So we can use it to our advantage.

Start a transaction, then Delete a level and capture all ElementId that got deleted as a result. Then we just RollBack Transaction to make sure we are not harming the model and we still have a list of elements.

Sub-Transaction

SubTransactions are nested transactions in an open Transaction. This allows us to create sub-steps on a large script that are necessary to commit before you move to the next step.

📰 RevitAPI docs

I personally avoid using SubTransactions. I have tried it a few times in the past, but it did not give me results that I was after... 

I prefer using regular Transactions with TransactionGroup whenever I need to commit multiple steps before going to the next one. So let's talk about TransactionGroup 👇

TransactionGroup

Transaction Groups can be used to:
.Commit()
.Assimilate() 

Commit:

A transaction group controls whether transactions committed inside the group should stay committed or should be all discarded. This could be useful when your script has to be done in 3 steps, and your last step has failed, therefore you want to RollBack all the steps within the group.

Assimilate:

By assimilating, all transactions within the group will be merged into a single transaction. This will make sure that you only have 1 action in Undo/Redo menu, instead of many. 

📰 RevitAPI docs

I mainly use Transaction Group to combine multiple transaction into single one with .Assimilate(). I have never had a need for using .Commit() so far.

Sumamry

Since you will use regular Transaction 95-100% of the time - you don't need to focus on SubTransaction and TransactionGroup a lot. 

Just know that they exist and if you encounter situation when you need to commit multiple steps, you can always read more about them.

Happy Coding!

Enjoyed Newsletter?

Leave a Testimonial Here.

⌨️ Happy Coding!
Erik Frits

Join Newsletter

📩 You will be added to Revit API Newsletter

Join Us!

which is already read by 7200+ people!

Last Chance To Claim LIFETIME ACCESS. [~30 Seats left]