How to save a document and close all opened transaction

Hi all,
I created a custom node that handle some operation to a Revit file, like modify parameters values.
I want to use it in the main script using the LoopWhile node. Since here the custom node and the loop while work as I want.

The issue is that I want to save the Revit document after each cycle.
I tried to place a Python node within the custom node at its end with the following code:

import clr
clr.AddReference('RevitAPI')
from Autodesk.Revit.DB import *
from System.Collections.Generic import *
clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager

doc = DocumentManager.Instance.CurrentDBDocument
elem = IN[0]
doc.Save()
OUT=elem

The problem is that there are some opened transaction and Dynamo isn’t able to save the document due to them.

So the question is: is there a way to close all the opened transaction before this Python node to save the document?

Thank you all!

@ygor.fasanella

It works well, i save local. Can you share the whole script? where are your transactions located?

import clr
clr.AddReference("RevitAPI")
from Autodesk.Revit.DB import *
from System.Collections.Generic import *

clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

doc = DocumentManager.Instance.CurrentDBDocument

t = Transaction(doc,"Do Something")
t.Start()
# do something
t.Commit()

doc.Save()

OUT = "I saved"

Hi @Draxl_Andreas,
thanks for your quick answer.
Yes the script works well if there aren’t opened transactions before the python node with the save function, the problem comes when there are some of them.
I tried also the version with Start and Commit methods.
I think isn’t useful to share all the custom node, it is a bit too complex.
However my question is if there is a way to close all the opened transaction before this Python node to save the document, without knowing exactly which and where they are. So all my problems will be resolved.
I can help you saying that some nodes used inside the custom node return null or empty values sometimes causing errors in following nodes, but for the correct execution of the custom node this is correct, so these errors don’t influence the correct execution of the custom node, but I think that could influence the opened transactions.

@ygor.fasanella

the save has to happen at the the end. Outside of the transaction.

KR

Andreas

@Draxl_Andreas
Yes, of course, I put the node at the end.

This is the script. At the very end there is the Python node with the code to save the document.

And this is the error that comes to the save node (the code is exactly the same you shared in the previous answer).

@ygor.fasanella ,

you can create a passThrough Node…

so when your transactions done finaly save…

topics like this

KR
Andreas

Hi,

The Revit API explains :
This method may not be called unless all transactions, sub-transactions, and transaction groups that were opened by the API code were closed.

So add the TransactionManager.Instance.ForceCloseTransaction() to close all transactions before saving your document.

import clr
clr.AddReference("RevitAPI")
from Autodesk.Revit.DB import *
from System.Collections.Generic import *

clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

doc = DocumentManager.Instance.CurrentDBDocument
TransactionManager.Instance.ForceCloseTransaction()

result=doc.Save()

OUT = result
3 Likes

@Alban_de_Chasteigner @Draxl_Andreas
Thanks to both of you. Thanks to your answer I tried doing in this way and it works:


I collapsed in a list all the nodes which their output aren’t linked to a following node, then I linked the list to the node “Transaction.End” and then I saved the file using the this code:

import clr
clr.AddReference('RevitAPI')
from Autodesk.Revit.DB import *
from System.Collections.Generic import *
clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
from Autodesk.Revit.DB import Transaction

doc = DocumentManager.Instance.CurrentDBDocument
t = Transaction(doc,"Do Something")
t.Start()
elem=IN[1]
t.Commit()
doc.Save()
OUT=elem

This is the custom node .dyf.
The OUT is the counter that counts the iteration for the LoopWhile node in the main .dyn script where I used the custom node.
In this way the main script save the Revit file at each iteration.

1 Like