Skip to content

transaction

Revit transactions facility.

Attributes

mlogger = get_logger(__name__) module-attribute

DEFAULT_TRANSACTION_NAME = 'pyRevit Transaction' module-attribute

Classes

Transaction(name=None, doc=None, clear_after_rollback=False, show_error_dialog=False, swallow_errors=False, log_errors=True, nested=False, failures_preprocessor=None)

Adds a context manager around Revit Transaction object.

Runs Transaction.Start() and Transaction.Commit() before and after the context. Automatically rolls back if exception is raised.

```python with Transaction('Move Wall'): wall.DoSomething()

with Transaction('Move Wall', doc, clear_after_rollback=False, show_error_dialog=False, swallow_errors=False, log_errors=True, nested=False) as action:
    wall.DoSomething()
    assert action.status == ActionStatus.Started  # True
    assert action.status == ActionStatus.Committed    # True
```
Note

failures_preprocessor takes any DB.IFailuresPreprocessor and wins over swallow_errors. Both are ignored when the transaction runs as a SubTransaction inside an already open transaction.

Source code in pyrevitlib/pyrevit/revit/db/transaction.py
def __init__(
    self,
    name=None,
    doc=None,
    clear_after_rollback=False,
    show_error_dialog=False,
    swallow_errors=False,
    log_errors=True,
    nested=False,
    failures_preprocessor=None,
):
    doc = doc or DOCS.doc
    # create nested transaction if one is already open
    if doc.IsModifiable or nested:
        self._rvtxn = DB.SubTransaction(doc)
    else:
        self._rvtxn = DB.Transaction(
            doc, name if name else DEFAULT_TRANSACTION_NAME
        )
        self._fhndlr_ops = self._rvtxn.GetFailureHandlingOptions()
        self._fhndlr_ops = self._fhndlr_ops.SetClearAfterRollback(
            clear_after_rollback
        )
        self._fhndlr_ops = self._fhndlr_ops.SetForcedModalHandling(
            show_error_dialog
        )
        if not failures_preprocessor and swallow_errors:
            failures_preprocessor = failure.FailureSwallower()
        if failures_preprocessor:
            self._fhndlr_ops = self._fhndlr_ops.SetFailuresPreprocessor(
                failures_preprocessor
            )
        self._rvtxn.SetFailureHandlingOptions(self._fhndlr_ops)
    self._logerror = log_errors

Attributes

name property writable
status property

Methods:

has_started()
Source code in pyrevitlib/pyrevit/revit/db/transaction.py
def has_started(self):
    return self._rvtxn.HasStarted()
has_ended()
Source code in pyrevitlib/pyrevit/revit/db/transaction.py
def has_ended(self):
    return self._rvtxn.HasEnded()

DryTransaction(name=None, doc=None, clear_after_rollback=False, show_error_dialog=False, swallow_errors=False, log_errors=True, nested=False, failures_preprocessor=None)

Bases: Transaction

Wrapper to a transaction that doesn't commit anything (dry-run).

Source code in pyrevitlib/pyrevit/revit/db/transaction.py
def __init__(
    self,
    name=None,
    doc=None,
    clear_after_rollback=False,
    show_error_dialog=False,
    swallow_errors=False,
    log_errors=True,
    nested=False,
    failures_preprocessor=None,
):
    doc = doc or DOCS.doc
    # create nested transaction if one is already open
    if doc.IsModifiable or nested:
        self._rvtxn = DB.SubTransaction(doc)
    else:
        self._rvtxn = DB.Transaction(
            doc, name if name else DEFAULT_TRANSACTION_NAME
        )
        self._fhndlr_ops = self._rvtxn.GetFailureHandlingOptions()
        self._fhndlr_ops = self._fhndlr_ops.SetClearAfterRollback(
            clear_after_rollback
        )
        self._fhndlr_ops = self._fhndlr_ops.SetForcedModalHandling(
            show_error_dialog
        )
        if not failures_preprocessor and swallow_errors:
            failures_preprocessor = failure.FailureSwallower()
        if failures_preprocessor:
            self._fhndlr_ops = self._fhndlr_ops.SetFailuresPreprocessor(
                failures_preprocessor
            )
        self._rvtxn.SetFailureHandlingOptions(self._fhndlr_ops)
    self._logerror = log_errors

Attributes

name property writable
status property

Methods:

has_started()
Source code in pyrevitlib/pyrevit/revit/db/transaction.py
def has_started(self):
    return self._rvtxn.HasStarted()
has_ended()
Source code in pyrevitlib/pyrevit/revit/db/transaction.py
def has_ended(self):
    return self._rvtxn.HasEnded()

TransactionGroup(name=None, doc=None, assimilate=True, log_errors=True)

Transactions group with context manager.

Source code in pyrevitlib/pyrevit/revit/db/transaction.py
def __init__(self, name=None, doc=None, assimilate=True, log_errors=True):
    self._rvtxn_grp = DB.TransactionGroup(
        doc or DOCS.doc, name if name else DEFAULT_TRANSACTION_NAME
    )
    self.assimilate = assimilate
    self._logerror = log_errors

Attributes

assimilate = assimilate instance-attribute
name property writable
status property

Methods:

has_started()
Source code in pyrevitlib/pyrevit/revit/db/transaction.py
def has_started(self):
    return self._rvtxn_grp.HasStarted()
has_ended()
Source code in pyrevitlib/pyrevit/revit/db/transaction.py
def has_ended(self):
    return self._rvtxn_grp.HasEnded()

DryTransactionGroup(name=None, doc=None, assimilate=True, log_errors=True)

Bases: TransactionGroup

Transaction group that always rolls back, even when its body succeeds.

Transactions inside it commit normally, so DocumentChanged fires and their effects can be inspected, then the whole group is discarded.

Invariant

The document is left exactly as it was before the group started.

Source code in pyrevitlib/pyrevit/revit/db/transaction.py
def __init__(self, name=None, doc=None, assimilate=True, log_errors=True):
    self._rvtxn_grp = DB.TransactionGroup(
        doc or DOCS.doc, name if name else DEFAULT_TRANSACTION_NAME
    )
    self.assimilate = assimilate
    self._logerror = log_errors

Attributes

assimilate = assimilate instance-attribute
name property writable
status property

Methods:

has_started()
Source code in pyrevitlib/pyrevit/revit/db/transaction.py
def has_started(self):
    return self._rvtxn_grp.HasStarted()
has_ended()
Source code in pyrevitlib/pyrevit/revit/db/transaction.py
def has_ended(self):
    return self._rvtxn_grp.HasEnded()

Functions:

carryout(name, doc=None)

Transaction Decorator.

Decorate any function with @doc.carryout('Txn name') and the funciton will run within an Transaction context.

Parameters:

Name Type Description Default
name str

Name of the Transaction

required
doc Document

Revit document

None
@doc.carryout('Do Something')
def set_some_parameter(wall, value):
    wall.parameters['Comments'].value = value


set_some_parameter(wall, value)
Source code in pyrevitlib/pyrevit/revit/db/transaction.py
def carryout(name, doc=None):
    """Transaction Decorator.

    Decorate any function with ``@doc.carryout('Txn name')``
    and the funciton will run within an Transaction context.

    Args:
        name (str): Name of the Transaction
        doc (Document): Revit document

    ```python
    @doc.carryout('Do Something')
    def set_some_parameter(wall, value):
        wall.parameters['Comments'].value = value


    set_some_parameter(wall, value)
    ```
    """
    from functools import wraps

    def wrap(f):
        @wraps(f)
        def wrapped_f(*args, **kwargs):
            with Transaction(name, doc=doc):
                return_value = f(*args, **kwargs)
            return return_value

        return wrapped_f

    return wrap