pyrevitlabs / pyRevit

Rapid Application Development (RAD) Environment for Autodesk Revit®
http://wiki.pyrevitlabs.io
GNU General Public License v3.0
1.3k stars 334 forks source link

Debugging code in Transaction using pyRevit #1923

Closed imigas434 closed 1 year ago

imigas434 commented 1 year ago

Hello, I create new scripts and custom panels/buttons using pyRevit. One of my scripts must simply update parameter of a single room. It seems there is an error, but because it is in Transaction part, I cannot see the error in the output terminal. Revit immediately shuts down pyRevit window and throws an alert that it will rollback transaction because it was not closed.

My question is: how can I properly debug my script in this situation? Can I do anything to stop Revit from throwing this alert?

Following code is my test script. (What is even funnier, if I used with revit.Transaction(sth) from pyrevit module instead of EnsureInTransaction, the script would not throw any error or alert. But I cannot use this method as importing revit from pyrevit is impossible while using CPython 3)

#! python3

import clr
clr.AddReference('RevitAPI')
import Autodesk
from Autodesk.Revit.DB import *
clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

doc = __revit__.ActiveUIDocument.Document

room_ref = doc.GetElement('bf8888de-1dca-490a-a125-17b87a35bea6-006b3d62')

def set_parameter_value(element, parameter_name, value):
    param = element.LookupParameter(parameter_name)
    if param:
        return param.Set(value)
    return False

TransactionManager.Instance.EnsureInTransaction(doc)
set_parameter_value(room_ref, '_dyn_sh_MieszkaniePowierzchnia', 5000)
TransactionManager.Instance.TransactionTaskDone()
jmcouffin commented 1 year ago

more suitable for the forum IMHO

see https://danimosite.wordpress.com/2017/05/11/intro-to-transactions/


 TransactionManager.Instance.EnsureInTransaction(doc)
 try:            
     #Make some changes to the Revit Document...
 except Exception as e:
    print(e)
     #Maybe Throw an Error Message...
 TransactionManager.Instance.TransactionTaskDone()