Multiple Transactions in different FamilyDocuments

Hi everyone :slight_smile:
I´ve got a problem with the use of a python node to make changes in multiple family objects. Currently I do the following:

  1. Open Family Documents
  2. GetViews with the Intention of changing the visibility of ReferencePlan. I modified the Node “View.SetCategoryVisibility” from MEPover-Package.

This is the code:
import clr

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

import sys
pyt_path=r'C:\Program Files (x86)\IronPython 2.7\Lib'
sys.path.append(pyt_path)


uiapp = DocumentManager.Instance.CurrentUIApplication
app = uiapp.Application

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

clr.AddReference("RevitNodes")
import Revit
clr.ImportExtensions(Revit.Elements)
clr.ImportExtensions(Revit.GeometryConversion)
#The inputs to this node will be stored as a list in the IN variables.
if isinstance(IN[0],list):
	category = UnwrapElement(IN[0])
else:
	category = [UnwrapElement(IN[0])]
if isinstance(IN[1], list):	
	views = UnwrapElement(IN[1])
else:
	views = [UnwrapElement(IN[1])]
if isinstance(IN[2],list):
	bools = IN[2]
else:
	bools = [IN[2]]

docs = IN[3]

report = []
outDocuments = []

for view,famDoc in zip(views,docs):
	TransactionManager.Instance.EnsureInTransaction(famDoc)

	for cat,bool in zip(category,bools):
		try:
			view.SetCategoryHidden(cat.Id,bool)
			outDocuments.append(famDoc)
			rep = famDoc
		except:
			import traceback
			rep = traceback.format_exc()
			outDocuments.append(famDoc)
	TransactionManager.Instance.TransactionTaskDone()
	report.append(rep)

OUT = outDocuments,report

The first transaction works fine, but the next one gets the error that the transaction is outside of a transaction. In my opinion, I close the transaction in every loop. Isn´t it possible to start and end transactions this way?

I also attached the Dynamo File. Im my folder there are three random families.
_multiple_transactions.dyn (30.7 KB)

At a quick glance the “report.append(rep)” is probably in the wrong spot. It’s trying to work in the outer loop not the inner loop where you probably want it. Try this at the end and let me know if it works.

  except:
  	import traceback
  	rep = traceback.format_exc()
  	outDocuments.append(famDoc)
 report.append(rep)

TransactionManager.Instance.TransactionTaskDone()

OUT = outDocuments,report

1 Like

You may have better luck creating your own transaction rather than using the TransactionManager. For example:

t = Transaction(famDoc, 'Set Category Hidden')
t.Start()
# for cat, bool etc.
t.Commit()

Thanks a lot :slight_smile: That was fast… I was struggling with this one for a while. Didn´t know that the TransactionManager does not work properly…

@felixowns
Thanks for the hint :slight_smile:, makes sense.

So this is my new code:
import clr

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

import sys
pyt_path=r'C:\Program Files (x86)\IronPython 2.7\Lib'
sys.path.append(pyt_path)


uiapp = DocumentManager.Instance.CurrentUIApplication
app = uiapp.Application

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

clr.AddReference("RevitNodes")
import Revit
clr.ImportExtensions(Revit.Elements)
clr.ImportExtensions(Revit.GeometryConversion)
#The inputs to this node will be stored as a list in the IN variables.
if isinstance(IN[0],list):
	category = UnwrapElement(IN[0])
else:
	category = [UnwrapElement(IN[0])]
if isinstance(IN[1], list):	
	views = UnwrapElement(IN[1])
else:
	views = [UnwrapElement(IN[1])]
if isinstance(IN[2],list):
	bools = IN[2]
else:
	bools = [IN[2]]

docs = IN[3]

report = []
outDocuments = []

for view,famDoc in zip(views,docs):
	t = Transaction(famDoc, 'Set Category Hidden')
	t.Start()
	for cat,bool in zip(category,bools):
		try:
			view.SetCategoryHidden(cat.Id,bool)
			outDocuments.append(famDoc)
			rep = "Success"
		except:
			import traceback
			rep = traceback.format_exc()
			outDocuments.append(famDoc)
		report.append(rep)
	t.Commit()


OUT = outDocuments,report