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)

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') as action:
wall.DoSomething()
assert action.status == ActionStatus.Started  # True
assert action.status == ActionStatus.Committed    # True
```
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):
    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 swallow_errors:
            self._fhndlr_ops = \
                self._fhndlr_ops.SetFailuresPreprocessor(
                    failure.FailureSwallower()
                    )
        self._rvtxn.SetFailureHandlingOptions(self._fhndlr_ops)
    self._logerror = log_errors

Attributes

name property writable
status property

Functions

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)

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):
    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 swallow_errors:
            self._fhndlr_ops = \
                self._fhndlr_ops.SetFailuresPreprocessor(
                    failure.FailureSwallower()
                    )
        self._rvtxn.SetFailureHandlingOptions(self._fhndlr_ops)
    self._logerror = log_errors

Attributes

name property writable
status property

Functions

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

Functions

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