Python - Closing a transaction

I’ve made a python script to create a view set from a selected revision.
I get this error to close the transaction. Any tips on how to do that?

image

import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *

clr.AddReference("RevitNodes")
import Revit
clr.ImportExtensions(Revit.Elements)

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

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

clr.AddReference("System.Drawing")
clr.AddReference("System.Windows.Forms")
import System.Drawing
import System.Windows.Forms
from System.Drawing import *
from System.Windows.Forms import *

class RevisionSelectionForm(Form):
    def __init__(self, RevNames):
        self.revname = RevNames
     
        self.Text = 'Revision Print Set'
        self.label = System.Windows.Forms.Label()
        self.label.Text = "Select Revision"
        self.label.Location = Point(1, 50)
        self.label.Height = 30
        self.label.Width = 200
        self.count = 0
	
	
        self.cb = System.Windows.Forms.ComboBox()
        self.cb.Width = 200
        self.cb.Height = 20
        self.cb.Location = Point(15,20)
        self.cb.DataSource = self.revname
    
        self.button = System.Windows.Forms.Button()
        self.button.Text = 'Select Revision'
        self.button.Location = Point(15,60)
        self.button.Width = self.cb.Width
        self.button.Height = 25
        self.button.Click += self.button_click

        self.Width = self.cb.Width + 50
        self.Height = 100
	
        self.Controls.Add(self.cb)
        self.Controls.Add(self.button)

    def button_click(self, sender, event):
        self.selected = self.cb.SelectedValue
        self.Close()

def filter_list(revisionlist, search_str):
	passed = None
	for element in revisionlist:
		name = element.Name
		if search_str == name:
			passed = element
	return passed        

sheets = FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Sheets).WhereElementIsNotElementType().ToElements()
revisionlist = FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Revisions).WhereElementIsNotElementType().ToElements()
RevNames = [x.Name for x in revisionlist]       
    
form = RevisionSelectionForm(RevNames)
form.ShowDialog()
SelectedRev = form.selected

SelRevEle = filter_list(revisionlist, SelectedRev)

filterview = FilteredElementCollector(doc).OfClass(ViewSheetSet).GetElementIterator()
filterview.Reset()
viewexists = None

sheetToPrint = []
for i in sheets: 
	revIds = i.GetAllRevisionIds()
	if SelRevEle.Id in revIds:
		sheetToPrint.append(i)

# remove viewset if already exists
for f in filterview:
	if f.Name == SelectedRev:
		viewexists = f
		break
if viewexists != None:
	TransactionManager.Instance.EnsureInTransaction(doc)
	doc.Delete(viewexists.Id)
	TransactionManager.Instance.TransactionTaskDone()
	viewexists = None
	
# create viewset
if viewexists == None:
	viewSet = ViewSet()
	for v in sheetToPrint: (viewSet.Insert(v))
	printManager = doc.PrintManager
	printManager.PrintRange = PrintRange.Select
	viewSS = printManager.ViewSheetSetting
	try:
		TransactionManager.Instance.EnsureInTransaction(doc)
		viewSS.CurrentViewSheetSet.Views = viewSet
		viewSS.SaveAs(SelectedRev)
		TransactionManager.Instance.TransactionTaskDone()
		msg = "Sheet set '" + SelectedRev + "' created."
	except:
		msg = "The ViewSet could not be created"


#Assign your output to the OUT variable.
OUT = msg

I got it to work. see code below.

import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *

clr.AddReference("RevitNodes")
import Revit
clr.ImportExtensions(Revit.Elements)

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


clr.AddReference("RevitAPI")
import Autodesk
from Autodesk.Revit.DB import *
clr.AddReference('RevitAPIUI')
clr.AddReference("System.Drawing")
clr.AddReference("System.Windows.Forms")
import System.Drawing
import System.Windows.Forms
from System.Drawing import *
from System.Windows.Forms import *

app = __revit__.Application
doc = __revit__.ActiveUIDocument.Document

class RevisionSelectionForm(Form):
    def __init__(self, RevNames):
        self.revname = RevNames
     
        self.Text = 'Revision Print Set'
        self.label = System.Windows.Forms.Label()
        self.label.Text = "Select Revision"
        self.label.Location = Point(1, 50)
        self.label.Height = 30
        self.label.Width = 200
        self.count = 0
	
	
        self.cb = System.Windows.Forms.ComboBox()
        self.cb.Width = 200
        self.cb.Height = 20
        self.cb.Location = Point(15,20)
        self.cb.DataSource = self.revname
    
        self.button = System.Windows.Forms.Button()
        self.button.Text = 'Select Revision'
        self.button.Location = Point(15,60)
        self.button.Width = self.cb.Width
        self.button.Height = 25
        self.button.Click += self.button_click

        self.Width = self.cb.Width + 50
        self.Height = 150
	
        self.Controls.Add(self.cb)
        self.Controls.Add(self.button)

    def button_click(self, sender, event):
        self.selected = self.cb.SelectedValue
        self.Close()

def filter_list(revisionlist, search_str):
	passed = None
	for element in revisionlist:
		name = element.Name
		if search_str == name:
			passed = element
	return passed        

sheets = FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Sheets).WhereElementIsNotElementType().ToElements()
revisionlist = FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Revisions).WhereElementIsNotElementType().ToElements()
RevNames = [x.Name for x in revisionlist]       
    
form = RevisionSelectionForm(RevNames)
form.ShowDialog()
SelectedRev = form.selected

SelRevEle = filter_list(revisionlist, SelectedRev)

filterview = FilteredElementCollector(doc).OfClass(ViewSheetSet).GetElementIterator()
filterview.Reset()
viewexists = None

sheetToPrint = []
for i in sheets: 
	revIds = i.GetAllRevisionIds()
	if SelRevEle.Id in revIds:
		sheetToPrint.append(i)

# remove viewset if already exists
for f in filterview:
	if f.Name == SelectedRev:
		viewexists = f
		break
if viewexists != None:
	tcheck = Transaction(doc, 'viewcheck')
	tcheck.Start()
	doc.Delete(viewexists.Id)
	tcheck.Commit()
	viewexists = None
	
# create viewset
if viewexists == None:
	viewSet = ViewSet()
	for v in sheetToPrint: (viewSet.Insert(v))
	printManager = doc.PrintManager
	printManager.PrintRange = PrintRange.Select
	viewSS = printManager.ViewSheetSetting
	try:
		trev = Transaction(doc, 'create viewset')
		trev.Start()
		viewSS.CurrentViewSheetSet.Views = viewSet
		viewSS.SaveAs(SelectedRev)
		trev.Commit()
		msg = "Sheet set '" + SelectedRev + "' created."
	except:
		msg = "The ViewSet could not be created"


#Assign your output to the OUT variable.
OUT = msg
1 Like