I have been trying to get into Revit API. When I try to create a custom Python component in Grasshopper, I get the following error.
Solution exception:Starting a new transaction is not permitted. It could be because another transaction already started and has not been completed yet, or the document is in a state in which it cannot start a new transaction (e.g. during failure handling or a read-only mode, which could be either permanent or temporary).
I have tried it in a Python component that is shared here. And got the above error.
Code here:
import clr
clr.AddReference('System.Core')
clr.AddReference('RhinoInside.Revit')
clr.AddReference('RevitAPI')
clr.AddReference('RevitAPIUI')
import rhinoscriptsyntax as rs
import Rhino
import RhinoInside
import Grasshopper
import Autodesk
doc = RhinoInside.Revit.Revit.ActiveDBDocument
sheets = []
if RUN:
#This is required to start a transaction in the Revit Document
t = Autodesk.Revit.DB.Transaction(doc, 'Add Sheets')
t.Start()
for i in range(len(sheetNumbers)):
newSheet = Autodesk.Revit.DB.ViewSheet.Create(doc, titleblock.Id)
newSheet.SheetNumber = sheetNumbers[i]
newSheet.Name = sheetNames[i]
sheets.append(newSheet)
t.Commit()
a = sheets
Your code is fine, but if occur any exception during the transaction, the transaction is never Commited or RolledBack.
To solve this I use a with statement, that ensures the Transaction t is disposed in case of exception.
import clr
clr.AddReference('System.Core')
clr.AddReference('RhinoInside.Revit')
clr.AddReference('RevitAPI')
clr.AddReference('RevitAPIUI')
import rhinoscriptsyntax as rs
import Rhino
import RhinoInside
import Grasshopper
import Autodesk
doc = RhinoInside.Revit.Revit.ActiveDBDocument
sheets = []
if RUN:
#This is required to start a transaction in the Revit Document
with Autodesk.Revit.DB.Transaction(doc, 'Add Sheets') as t :
t.Start()
for i in range(len(sheetNumbers)):
newSheet = Autodesk.Revit.DB.ViewSheet.Create(doc, titleblock.Id)
newSheet.SheetNumber = sheetNumbers[i]
newSheet.Name = sheetNames[i]
sheets.append(newSheet)
t.Commit()
a = sheets
This is because the Sheet is created on the previous iteration.
You need to make the component a bit smarter and using a FilteredElementCollector check a sheet with that number do not exist before creating a new one.
If it already exist you may want to add it to the resulting list of sheets.
. Using with statement actually solves the problem. I just needed to check parameter type
When I changed it from Item access to List access, it worked just fine.