Revit + Python

Revit API – TRANSACTION

R

Most commands in Revit require some kind of modification to the file you are working in.  If that’s the case, chances are good that you’re going to need to access the Transaction Class within the Revit API.  This is a relatively straight-forward class to use, but it’s worth diving into a little bit to head off any potential problems in using it.

Transaction Constructors:  There are two constructors for Transaction, but, for some reason, only one of them seems to work consistently for me.  The two options are:

  • Transaction (Document):  This constructor only requires a document to be fed into it.  I’m not sure why, but most times I try to use this one I am asked to also add a second argument.  It’s probably safer to use the other constructor.

  • Transaction (Document, String):  This constructor also requires a document as an argument, but, in addition, it needs a string as well.  This string is what will show in the ‘Undo’ menu within Revit. I find that it doesn’t matter too much what this is, as long as it’s something.

Transaction Methods:  There are a few methods available to use with Transaction, but there are only 3 of them that will probably end up being used. 

  • .Start():  This will start the transaction.  Essentially, this line of code will need to come before the code that actually modifies the model.  It will need to be closed out with one of the other two methods I mention here.

  • .Commit():  This method will close the transaction and commit the changes to the model. 

  • .RollBack():  This method will close the transaction, but discard any changes that were made during the Transaction and revert the model back to its status right before the Transaction started.  This is useful in cases where you want to get some data from a potential change but don’t want to actually do the change.

Transaction Properties:  The only property available for Transactions is IsValidObject.  I don’t think I’ve ever used this property within any class, so it can probably be ignored, generally.


In its basic sense, here’s what a Transaction would look like in Python code:

Transaction(doc, ‘do stuff’).Start()
#your code here#
Transaction(doc, ‘do stuff’).Commit()

or

Transaction(doc, ‘do stuff’).Start()
#your code here#
Transaction(doc, ‘do stuff’).RollBack()


One other thing to note is that you cannot have a Transaction begin while still inside another open transaction. The first Transaction needs to be committed or rolled back first.

It’s not glamorous, but the Transaction Class will definitely be one that shows up in a lot of your code.

About the author

Andrew Urban

With almost 15 years as a registered architect, and nearly as much experience using Revit, I have recently taught myself Python and have begun to create productivity increasing addons for Revit. I'm am always interested in learning new and innovative ways to use Revit and all other tools of our profession.

By Andrew Urban
Revit + Python

Andrew Urban

With almost 15 years as a registered architect, and nearly as much experience using Revit, I have recently taught myself Python and have begun to create productivity increasing addons for Revit. I'm am always interested in learning new and innovative ways to use Revit and all other tools of our profession.