Delete Grouped Elements Transaction.Rollback()

Hello,

I’m trying to delete a list of grouped elements (any wall hosted families) so I can get the solids from walls excluding any openings. The problem I have is that the deleted elements stay deleted from the groups in Revit after the transaction.

(Apologies for the direct unsolicited @ but I think it’s similar to what @Andreas_Dieckmann suggested here and I didn’t want to resurrect an old thread: TransactionManager vs Transaction)

Perhaps I’m trying to get too much out of a sub-transaction but I thought the quickest way about this would be to start the sub-transaction, delete grouped elements, extract solids from remaining elements and append to a list, rollback sub to undo any deletions. That’s the logic anyway but it doesn’t work… My attempt is below and any advice would be much appreciated. Thanks!

The inputs are; 1. Elements to obtain solid geometry from, 2. Elements to be deleted and 3. the affected Groups.

The outputs are; 1. Extracted solids, 2. Problematic elements without solids, 3. Groups containing problematic elements, 4. Element Ids of Problematic Elements

import clr
clr.AddReference("RevitAPI");
clr.AddReference("RevitNodes")

import Autodesk, Revit;
clr.ImportExtensions(Revit.Elements);
clr.ImportExtensions(Revit.GeometryConversion);
from Autodesk.Revit.DB import *

clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
doc = DocumentManager.Instance.CurrentDBDocument

elements = UnwrapElement(IN[0]) #sublists per model group
deleteList = UnwrapElement(IN[1]) #elements to be deleted
groupsIn = UnwrapElement(IN[2]) #model groups containing all elements

outlist = []
warnings = []
groups = []
groupIDs = []
ids = []

options = Options()

TransactionManager.Instance.EnsureInTransaction(doc)
trans = SubTransaction(doc)
trans.Start()

for d in deleteList:
	doc.Delete(d.Id)

doc.Regenerate()

for en, el in enumerate(elements):
	elList = []
	for e in el:
		geo = e.get_Geometry(options)
		for obj in geo:
			if isinstance(obj,Solid):
				try:
					geom = obj.ToProtoType()
					if str(geom) == 'Solid':
						elList.append(geom)
					else:
						ids.append(e.Id)
						groups.append(groupsIn[en])
				except:
					ids.append(e.Id)
					groups.append(groupsIn[en])
			else:
				ids.append(e.Id)
				groups.append(groupsIn[en])
	outlist.append(elList)

trans.RollBack()

TransactionManager.Instance.TransactionTaskDone()

warnings = [doc.GetElement(x) for x in ids]

OUT = outlist, warnings, groups, ids

A simple restart of Revit fixed this and the code works fine thankfully… I’ll leave the post up in case it helps others in future.

(code also edited to include doc.Regenerate() mid transaction)

1 Like