Skip to content

pyrevit

pyRevit root level config for all pyrevit sub-modules.

Examples:

from pyrevit import DB, UI
from pyrevit import PyRevitException, PyRevitIOError

# pyrevit module has global instance of the
# _HostAppPostableCommand and _ExecutorParams classes already created
# import and use them like below
from pyrevit import HOST_APP
from pyrevit import EXEC_PARAMS

Attributes

PYREVIT_ADDON_NAME = 'pyRevit' module-attribute

PYREVIT_CLI_NAME = 'pyrevit.exe' module-attribute

VERSION_STRING = version_file.read() module-attribute

matches = re.findall('(\\d+)\\.(\\d+)\\.(\\d+)\\.?(.+)?', VERSION_STRING)[0] module-attribute

BUILD_METADATA = '' module-attribute

VERSION_MAJOR = int(VERSION_MAJOR) module-attribute

VERSION_MINOR = int(VERSION_MINOR) module-attribute

VERSION_PATCH = int(VERSION_PATCH) module-attribute

HOME_DIR = op.dirname(op.dirname(op.dirname(__file__))) module-attribute

DOTNET_RUNTIME_ID = 'netcore' if compat.NETCORE else 'netfx' module-attribute

ROOT_BIN_DIR = op.join(HOME_DIR, 'bin') module-attribute

BIN_DIR = op.join(ROOT_BIN_DIR, DOTNET_RUNTIME_ID) module-attribute

MAIN_LIB_DIR = op.join(HOME_DIR, 'pyrevitlib') module-attribute

MISC_LIB_DIR = op.join(HOME_DIR, 'site-packages') module-attribute

MODULE_DIR = op.join(MAIN_LIB_DIR, 'pyrevit') module-attribute

LOADER_DIR = op.join(MODULE_DIR, 'loader') module-attribute

RUNTIME_DIR = op.join(MODULE_DIR, 'runtime') module-attribute

ADDIN_DIR = op.join(LOADER_DIR, 'addin') module-attribute

ENGINES_DIR = op.join(BIN_DIR, 'engines', eng.EngineVersion) module-attribute

PYREVIT_CLI_PATH = op.join(HOME_DIR, 'bin', PYREVIT_CLI_NAME) module-attribute

TRACEBACK_TITLE = 'Traceback:' module-attribute

HOST_APP = _HostApplication() module-attribute

EXEC_PARAMS = _ExecutorParams() module-attribute

DOCS = _DocsGetter() module-attribute

ALLUSER_PROGRAMDATA = ' ' module-attribute

USER_ROAMING_DIR = ' ' module-attribute

USER_SYS_TEMP = ' ' module-attribute

USER_DESKTOP = ' ' module-attribute

EXTENSIONS_DEFAULT_DIR = ' ' module-attribute

THIRDPARTY_EXTENSIONS_DEFAULT_DIR = ' ' module-attribute

PYREVIT_ALLUSER_APP_DIR = ' ' module-attribute

PYREVIT_APP_DIR = ' ' module-attribute

PYREVIT_VERSION_APP_DIR = ' ' module-attribute

PYREVIT_FILE_PREFIX_UNIVERSAL = None module-attribute

PYREVIT_FILE_PREFIX = None module-attribute

PYREVIT_FILE_PREFIX_STAMPED = None module-attribute

PYREVIT_FILE_PREFIX_UNIVERSAL_USER = None module-attribute

PYREVIT_FILE_PREFIX_USER = None module-attribute

PYREVIT_FILE_PREFIX_STAMPED_USER = None module-attribute

PYREVIT_FILE_PREFIX_UNIVERSAL_REGEX = '^' + PYREVIT_ADDON_NAME + '_(?P<fname>.+)' module-attribute

PYREVIT_FILE_PREFIX_REGEX = '^' + PYREVIT_ADDON_NAME + '_(?P<version>\\d{4})_(?P<fname>.+)' module-attribute

PYREVIT_FILE_PREFIX_STAMPED_REGEX = '^' + PYREVIT_ADDON_NAME + '_(?P<version>\\d{4})_(?P<pid>\\d+)_(?P<fname>.+)' module-attribute

PYREVIT_FILE_PREFIX_UNIVERSAL_USER_REGEX = '^' + PYREVIT_ADDON_NAME + '_(?P<user>.+)_(?P<fname>.+)' module-attribute

PYREVIT_FILE_PREFIX_USER_REGEX = '^' + PYREVIT_ADDON_NAME + '_(?P<version>\\d{4})_(?P<user>.+)_(?P<fname>.+)' module-attribute

PYREVIT_FILE_PREFIX_STAMPED_USER_REGEX = '^' + PYREVIT_ADDON_NAME + '_(?P<version>\\d{4})_(?P<user>.+)_(?P<pid>\\d+)_(?P<fname>.+)' module-attribute

Classes

PyRevitException

Bases: Exception

Common base class for all pyRevit exceptions.

Parameters args and message are derived from Exception class.

Attributes

msg property

Return exception message.

PyRevitIOError

Bases: PyRevitException

Common base class for all pyRevit io-related exceptions.

Attributes

msg property

Return exception message.

PyRevitCPythonNotSupported(feature_name)

Bases: PyRevitException

Exception for features not supported under CPython.

Source code in pyrevitlib/pyrevit/__init__.py
def __init__(self, feature_name):
    super(PyRevitCPythonNotSupported, self).__init__()
    self.feature_name = feature_name

Attributes

feature_name = feature_name instance-attribute
msg property

Return exception message.

_HostApplication()

Bases: object

Private Wrapper for Current Instance of Revit.

Provides version info and comparison functionality, alongside providing info on the active screen, active document and ui-document, available postable commands, and other functionality.

Examples:

hostapp = _HostApplication()
hostapp.is_newer_than(2017)
Source code in pyrevitlib/pyrevit/__init__.py
def __init__(self):
    self._postable_cmds = []

Attributes

uiapp property

Return UIApplication provided to the running command.

app property

Return Application provided to the running command.

addin_id property

Return active addin id.

has_api_context property

Determine if host application is in API context.

uidoc property

Return active UIDocument.

doc property

Return active Document.

active_view property writable

Return view that is active (UIDocument.ActiveView).

docs property

Return :obj:list of open :obj:Document objects.

available_servers property

Return :obj:list of available Revit server names.

version property

str: Return version number (e.g. '2018').

subversion property

str: Return subversion number (e.g. '2018.3').

version_name property

str: Return version name (e.g. 'Autodesk Revit 2018').

build property

str: Return build number (e.g. '20170927_1515(x64)').

serial_no property

str: Return serial number number (e.g. '569-09704828').

pretty_name property

Returns the pretty name of the host.

Examples:

Autodesk Revit 2019.2 build: 20190808_0900(x64)

Returns:

Type Description
str

Pretty name of the host

is_demo property

bool: Determine if product is using demo license.

language property

str: Return language type (e.g. 'LanguageType.English_USA').

username property

str: Return the username from Revit API (Application.Username).

proc property

System.Diagnostics.Process: Return current process object.

proc_id property

int: Return current process id.

proc_name property

str: Return current process name.

proc_path property

str: Return file path for the current process main module.

proc_window property

intptr: Return handle to current process window.

proc_screen property

intptr: Return handle to screen hosting current process.

proc_screen_workarea property

System.Drawing.Rectangle: Return screen working area.

proc_screen_scalefactor property

float: Return scaling for screen hosting current process.

Functions

is_newer_than(version, or_equal=False)

bool: Return True if host app is newer than provided version.

Parameters:

Name Type Description Default
version str or int

version to check against.

required
or_equal bool

Whether to include version in the comparison

False
Source code in pyrevitlib/pyrevit/__init__.py
def is_newer_than(self, version, or_equal=False):
    """bool: Return True if host app is newer than provided version.

    Args:
        version (str or int): version to check against.
        or_equal (bool): Whether to include `version` in the comparison
    """
    if or_equal:
        return int(self.version) >= int(version)
    else:
        return int(self.version) > int(version)
is_older_than(version)

bool: Return True if host app is older than provided version.

Parameters:

Name Type Description Default
version str or int

version to check against.

required
Source code in pyrevitlib/pyrevit/__init__.py
def is_older_than(self, version):
    """bool: Return True if host app is older than provided version.

    Args:
        version (str or int): version to check against.
    """
    return int(self.version) < int(version)
is_exactly(version)

bool: Return True if host app is equal to provided version.

Parameters:

Name Type Description Default
version str or int

version to check against.

required
Source code in pyrevitlib/pyrevit/__init__.py
def is_exactly(self, version):
    """bool: Return True if host app is equal to provided version.

    Args:
        version (str or int): version to check against.
    """
    return int(self.version) == int(version)
get_postable_commands()

Return list of postable commands.

Returns:

Type Description
list[_HostAppPostableCommand]

postable commands.

Source code in pyrevitlib/pyrevit/__init__.py
def get_postable_commands(self):
    """Return list of postable commands.

    Returns:
        (list[_HostAppPostableCommand]): postable commands.
    """
    # if list of postable commands is _not_ already created
    # make the list and store in instance parameter
    if not self._postable_cmds:
        for pc in UI.PostableCommand.GetValues(UI.PostableCommand):
            try:
                rcid = UI.RevitCommandId.LookupPostableCommandId(pc)
                self._postable_cmds.append(
                    # wrap postable command info in custom namedtuple
                    _HostAppPostableCommand(name=safe_strtype(pc),
                                            key=rcid.Name,
                                            id=rcid.Id,
                                            rvtobj=rcid)
                    )
            except Exception:
                # if any error occured when querying postable command
                # or its info, pass silently
                pass

    return self._postable_cmds
post_command(command_id)

Request Revit to run a command.

Parameters:

Name Type Description Default
command_id str

command identifier e.g. ID_REVIT_SAVE_AS_TEMPLATE

required
Source code in pyrevitlib/pyrevit/__init__.py
def post_command(self, command_id):
    """Request Revit to run a command.

    Args:
        command_id (str): command identifier e.g. ID_REVIT_SAVE_AS_TEMPLATE
    """
    command_id = UI.RevitCommandId.LookupCommandId(command_id)
    self.uiapp.PostCommand(command_id)