LearnRevitAPI

Why Beginners Shouldn't Be Afraid to Explore Revit API

Dec 6, 2024

Changes in Revit API

If you are a beginner Revit API hacker, you shouldn't be afraid to explore Revit API. You won't change anything by accident.

Whenever you make changes with Revit API you need to use 🔓Transaction.

If you don't, you will encounter an error message saying 'Attempt to modify project without Transaction'.

And this is great news for beginners. It means you don't have to be afraid to mess something up when you explore Revit API.

Let me explain.

Why we need Transactions?

Transaction Class in Revit API is like a guardian of your projects. It makes sure that you can only make changes that are on purpose, and nothing is changed by accident.

This is great news for beginners as you don't have to be afraid to explore Revit API.

Many are afraid to try anything new with Revit API on the live projects, and it's good protective measure for sure, but also know you can't mess it up by accident.

How Transactions Work?

Transactions are very simple to use. You have to create a transaction by providing:

  • doc - The project where you make changes

  • change-name - Text shown in the Undo menu of Revit

And here is how it looks


You can write anything before and after the Transaction, just make sure that code that makes any changes is between Start and Commit statements.

What if I get an Error?

Often time you might encounter errors with your code during Transaction that was started. And it's also nothing to worry about.

If you Start Transaction and then you get an error, it will be RolledBack by default. It means it will not apply any changes and keep the same state as before transaction was started.

But it's still recommended that you use Try/Except statements, to avoid showing huge red wall of error text to users.

However, make sure you can see error message during development, so you can solve the issue.

Here is example with Try/Except


Transaction with Loops

Another important advice I want to share is to keep transactions out of loops.

Sometimes it makes sense to make it inside the loop, but keep in mind that it will drastically affect your execution time, as each change would have to update the database, and it can take a lot of time on large projects...

So as a rule of thumb - keep Transactions out of loops if possible.


Transaction Context-Manager

Lastly, I want to let you know that you can use Transactions as a context-manager. If you new to python - context-manager is when you use with keyword.

It's pretty much the same thing, but some people might prefer to use it for readability purposes.

Here is example:


💡 You can even create custom context-manager and include Start, Commit, Try, Except all included.

Have Questions?

Let me know if you have any questions about Transactions and how to make changes in Revit API.

Hit 'Reply' and ask a question.

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 7500+ people!