revit-apirevitrevitpythonshellpyrevit

Revit API - How to check for open Transactions, Sub-transactions, or Group Transactions


I'm working on a button where I have a project document open and a family document open. I'm trying to close the family document however I'm getting an error saying:

Autodesk.Revit.Exceptions.InvalidOperationException: 'Close is not allowed when there is any open sub-transaction, transaction, or transaction group.'

I've checked all of my transactions and they are all started and committed using transactionName.Start(document) and transactionName.Commit()

does anyone know of a way to check for any ongoing active transactions?

I have also tried using

'RevitCommandId closeDoc = RevitCommandId.LookupPostableCommandId(PostableCommand.Close); uiapp.PostCommand(closeDoc);'

however that tends to only want to close my project document.

///////////////////////////////////UPDATE///////////////////////////// soooo I just found out I didn't need to use uiapp.OpenAndActivateDocument(). I didn't know you could edit a family without opening the document. That solves my problem. I'm still curious if there's a way to check for open transactions though.


Solution

  • Yes there is a way to check for open Transactions and its quite helpful in making flexible helper-functions. The Document object has an IsModifiable property - essentially if a Transaction is open, then this will be True.

    I use it like this:

    autoTransaction = False
    if not document.IsModifiable:
         t = Transaction(document, 'New Transaction cause no transaction was open')
         t.Start()
         autoTransaction = True
    
    # go ahead and modify the database
    
    if autoTransaction:
         t.Commit()
    

    Its served me well so far, hope this helps!