Skip to content

failure

Revit failures handler.

Attributes

mlogger = get_logger(__name__) module-attribute

RESOLUTION_TYPES = [DB.FailureResolutionType.MoveElements, DB.FailureResolutionType.CreateElements, DB.FailureResolutionType.DetachElements, DB.FailureResolutionType.FixElements, DB.FailureResolutionType.UnlockConstraints, DB.FailureResolutionType.SkipElements, DB.FailureResolutionType.DeleteElements, DB.FailureResolutionType.QuitEditMode, DB.FailureResolutionType.SetValue, DB.FailureResolutionType.SaveDocument] module-attribute

Classes

FailureSwallower(log_errors=True)

Bases: IFailuresPreprocessor

Swallows all failures.

Source code in pyrevitlib/pyrevit/revit/db/failure.py
def __init__(self, log_errors=True):
    self._logerror = log_errors
    self._failures_swallowed = []

Functions

get_swallowed_failures()
Source code in pyrevitlib/pyrevit/revit/db/failure.py
def get_swallowed_failures(self):
    failures = set()
    failure_reg = HOST_APP.app.GetFailureDefinitionRegistry()
    if failure_reg:
        for failure_id in self._failures_swallowed:
            failure_obj = failure_reg.FindFailureDefinition(failure_id)
            if failure_obj:
                failures.add(failure_obj)
            else:
                mlogger.debug(
                    'can not find failure definition for: %s', failure_id
                    )
    return failures
reset()

Reset swallowed errors.

Source code in pyrevitlib/pyrevit/revit/db/failure.py
def reset(self):
    """Reset swallowed errors."""
    self._failures_swallowed = []
preprocess_failures(failure_accessor)

Pythonic wrapper for PreprocessFailures interface method.

Source code in pyrevitlib/pyrevit/revit/db/failure.py
def preprocess_failures(self, failure_accessor):
    """Pythonic wrapper for `PreprocessFailures` interface method."""
    return self.PreprocessFailures(failure_accessor)
PreprocessFailures(failuresAccessor)

Required IFailuresPreprocessor interface method.

Source code in pyrevitlib/pyrevit/revit/db/failure.py
def PreprocessFailures(self, failuresAccessor):
    """Required IFailuresPreprocessor interface method."""
    severity = failuresAccessor.GetSeverity()
    # log some info
    mlogger.debug('processing failure with severity: %s', severity)

    if severity == coreutils.get_enum_none(DB.FailureSeverity):
        mlogger.debug('clean document. returning with'
                      'FailureProcessingResult.Continue')
        return DB.FailureProcessingResult.Continue

    # log the failure messages
    failures = failuresAccessor.GetFailureMessages()
    mlogger.debug('processing %s failure messages.', len(failures))

    # go through failures and attempt resolution
    action_taken = False
    get_elementid_value = get_elementid_value_func()
    for failure in failures:

        failure_id = failure.GetFailureDefinitionId()
        failure_guid = getattr(failure_id, 'Guid', '')
        failure_severity = failure.GetSeverity()
        failure_desc = failure.GetDescriptionText()
        failure_has_res = failure.HasResolutions()

        # log failure info
        mlogger.debug('processing failure msg: %s', failure_guid)
        mlogger.debug('\tseverity: %s', failure_severity)
        mlogger.debug('\tdescription: %s', failure_desc)
        mlogger.debug('\telements: %s',
                      [get_elementid_value(x)
                       for x in failure.GetFailingElementIds()])
        mlogger.debug('\thas resolutions: %s', failure_has_res)

        # attempt resolution
        mlogger.debug('attempt resolving failure: %s', failure_guid)

        # if it's a warning and does not have any resolution
        # delete it! it might have a popup window
        if not failure_has_res \
                and failure_severity == DB.FailureSeverity.Warning:
            failuresAccessor.DeleteWarning(failure)
            mlogger.debug(
                'deleted warning with no acceptable resolution: %s',
                failure_guid
                )
            continue

        # find failure definition id
        # at this point the failure_has_res is True
        failure_def_accessor = get_failure_by_id(failure_id)
        default_res = failure_def_accessor.GetDefaultResolutionType()

        # iterate through resolution options, pick one and resolve
        for res_type in RESOLUTION_TYPES:
            if default_res == res_type:
                mlogger.debug(
                    'using default failure resolution: %s', res_type)
                self._set_and_resolve(failuresAccessor, failure, res_type)
                action_taken = True
                break
            elif failure.HasResolutionOfType(res_type):
                mlogger.debug('setting failure resolution to: %s', res_type)
                self._set_and_resolve(failuresAccessor, failure, res_type)
                # marked as action taken
                action_taken = True
                break
            else:
                mlogger.debug('invalid failure resolution: %s', res_type)

    # report back
    if action_taken:
        mlogger.debug('resolving failures with '
                      'FailureProcessingResult.ProceedWithCommit')
        return DB.FailureProcessingResult.ProceedWithCommit
    else:
        mlogger.debug('resolving failures with '
                      'FailureProcessingResult.Continue')
        return DB.FailureProcessingResult.Continue

Functions

get_failure_by_guid(failure_guid)

Source code in pyrevitlib/pyrevit/revit/db/failure.py
def get_failure_by_guid(failure_guid):
    fdr = HOST_APP.app.GetFailureDefinitionRegistry()
    fgid = framework.Guid(failure_guid)
    fid = DB.FailureDefinitionId(fgid)
    return fdr.FindFailureDefinition(fid)

get_failure_by_id(failure_id)

Source code in pyrevitlib/pyrevit/revit/db/failure.py
def get_failure_by_id(failure_id):
    fdr = HOST_APP.app.GetFailureDefinitionRegistry()
    return fdr.FindFailureDefinition(failure_id)