Last active
August 23, 2022 18:57
-
-
Save gtalarico/0d952e9330c04e225df1d0be64835f3a to your computer and use it in GitHub Desktop.
RevitAPI::Code Snippets::Revit Transaction Decorator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' Example of a Transaction Decorator function. | |
This allows you to create functions that make changes to the revit document | |
without having to repeat the code to start/commit transactions. | |
Just add the revit_transaction decorator and a transaction will be started before | |
your function is called, and then commit after the call.''' | |
from functools import wraps | |
from Autodesk.Revit.Exceptions import InvalidOperationException | |
def revit_transaction(transaction_name): | |
def wrap(f): | |
@wraps(f) | |
def wrapped_f(*args): | |
try: | |
t = Transaction(doc, transaction_name) | |
t.Start() | |
except InvalidOperationException as errmsg: | |
print('Transaciton Error: {}'.format(errmsg)) | |
return_value = f(*args) | |
else: | |
return_value = f(*args) | |
t.Commit() | |
return return_value | |
return wrapped_f | |
return wrap | |
#Example | |
@revit_transaction('Create Text') | |
def create_text(view, text, point, align): | |
baseVec = XYZ.BasisX | |
upVec = XYZ.BasisZ | |
text_size = 10 | |
text_length = 0.5 | |
text = str(text) | |
align_options = {'left': TextAlignFlags.TEF_ALIGN_LEFT | | |
TextAlignFlags.TEF_ALIGN_MIDDLE, | |
'right': TextAlignFlags.TEF_ALIGN_RIGHT | | |
TextAlignFlags.TEF_ALIGN_MIDDLE | |
} | |
text_element = doc.Create.NewTextNote(view, point, baseVec, upVec, text_length, | |
align_options[align], text) |
@gtalarico - Where is the EXECUTE happening?
As I am relatively new to all this - the DEFs com first. So how does the @wrapper work?
Is the function called:
@revit_transaction('What shows in transaction window in Revit')
create_text(objView, stText, objPoint, 'right')
or do I just make the call and the presence of :
@revit_transaction('Create Text')
def create_text(view, text, point, align):
ahead of the def auto wrap THAT def in the transaction?
~TIA
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Need to add rollback