Skip to content

ui

UI functions.

Attributes

Classes

Functions:

get_mainwindow_hwnd()

Get the handle of the main window.

Returns:

Type Description
intptr

The handle of the main window.

Source code in pyrevitlib/pyrevit/revit/ui.py
def get_mainwindow_hwnd():
    """Get the handle of the main window.

    Returns:
        (intptr): The handle of the main window.
    """
    return HOST_APP.proc_window

get_mainwindow()

Get the main window of the application.

Returns:

Type Description
MainWindow

The root visual of the main window.

Source code in pyrevitlib/pyrevit/revit/ui.py
def get_mainwindow():
    """Get the main window of the application.

    Returns:
        (MainWindow): The root visual of the main window.
    """
    try:
        hwnd_source = Interop.HwndSource.FromHwnd(HOST_APP.proc_window)
        return hwnd_source.RootVisual
    except Exception:
        pass

get_statusbar_hwnd()

Retrieves the handle of the status bar control belonging to the main window.

Returns:

Type Description
IntPtr

The handle of the status bar control.

Source code in pyrevitlib/pyrevit/revit/ui.py
def get_statusbar_hwnd():
    """Retrieves the handle of the status bar control belonging to the main window.

    Returns:
        (IntPtr): The handle of the status bar control.
    """
    return Common.User32.FindWindowEx(
        get_mainwindow_hwnd(), IntPtr.Zero, "msctls_statusbar32", ""
    )

set_statusbar_text(text)

Sets the text of the status bar.

Parameters:

Name Type Description Default
text str

The text to be displayed in the status bar.

required

Returns:

Type Description
bool

True if the text was successfully set, False otherwise.

Source code in pyrevitlib/pyrevit/revit/ui.py
def set_statusbar_text(text):
    """Sets the text of the status bar.

    Parameters:
        text (str): The text to be displayed in the status bar.

    Returns:
        (bool): True if the text was successfully set, False otherwise.
    """
    status_bar_ptr = get_statusbar_hwnd()

    if status_bar_ptr != IntPtr.Zero:
        Common.User32.SetWindowText(status_bar_ptr, text)
        return True

    return False

get_window_rectangle()

Get the rectangle coordinates of the main window.

Returns:

Type Description
Tuple[int, int, int, int]

The left, top, right, and bottom coordinates of the window rectangle.

Source code in pyrevitlib/pyrevit/revit/ui.py
def get_window_rectangle():
    """Get the rectangle coordinates of the main window.

    Returns:
        (Tuple[int, int, int, int]): The left, top, right, and bottom
            coordinates of the window rectangle.
    """
    return Common.User32.GetWindowRect(get_mainwindow_hwnd())

is_infocenter_visible()

Check if the InfoCenter toolbar is visible.

Returns:

Type Description
bool

True if the InfoCenter toolbar is visible, False otherwise.

Source code in pyrevitlib/pyrevit/revit/ui.py
def is_infocenter_visible():
    """Check if the InfoCenter toolbar is visible.

    Returns:
        (bool): True if the InfoCenter toolbar is visible, False otherwise.
    """
    return (
        ad.ComponentManager.InfoCenterToolBar.Visibility == Windows.Visibility.Visible
    )

toggle_infocenter()

Toggles the visibility of the InfoCenter toolbar.

This function retrieves the current visibility state of the InfoCenter toolbar and toggles it to the opposite state. If the toolbar is currently collapsed, it will be set to visible, and if it is currently visible, it will be set to collapsed. The function then returns the visibility state of the InfoCenter toolbar after the toggle operation.

Returns:

Type Description
bool

True if the InfoCenter toolbar is visible, False otherwise.

Source code in pyrevitlib/pyrevit/revit/ui.py
def toggle_infocenter():
    """Toggles the visibility of the InfoCenter toolbar.

    This function retrieves the current visibility state of the InfoCenter
    toolbar and toggles it to the opposite state.
    If the toolbar is currently collapsed, it will be set to visible,
    and if it is currently visible, it will be set to collapsed.
    The function then returns the visibility state of the InfoCenter toolbar
    after the toggle operation.

    Returns:
        (bool): True if the InfoCenter toolbar is visible, False otherwise.
    """
    current_state = ad.ComponentManager.InfoCenterToolBar.Visibility
    is_hidden = current_state == Windows.Visibility.Collapsed
    ad.ComponentManager.InfoCenterToolBar.Visibility = (
        Windows.Visibility.Visible if is_hidden else Windows.Visibility.Collapsed
    )
    return is_infocenter_visible()

get_ribbon_roottype()

Get the type of the ribbon root.

Returns:

Type Description
type

type of the ribbon root

Source code in pyrevitlib/pyrevit/revit/ui.py
def get_ribbon_roottype():
    """Get the type of the ribbon root.

    Returns:
        (type): type of the ribbon root
    """
    ap_assm = clr.GetClrType(ap.Windows.RibbonTabList).Assembly
    for apt in ap_assm.GetTypes():
        if "PanelSetListView" in apt.Name:
            return apt

get_current_theme()

Get the current UI theme.

Returns:

Name Type Description
UITheme UITheme

The current UI theme.

Source code in pyrevitlib/pyrevit/revit/ui.py
def get_current_theme():
    """Get the current UI theme.

    Returns:
        UITheme (UITheme): The current UI theme.
    """
    return UIThemeManager.CurrentTheme

set_current_theme(theme='Dark')

Sets the current UI theme to either 'Dark' or 'Light'.

Parameters:

Name Type Description Default
theme str

The theme to set. Defaults to 'Dark'.

'Dark'
Source code in pyrevitlib/pyrevit/revit/ui.py
def set_current_theme(theme="Dark"):
    """Sets the current UI theme to either 'Dark' or 'Light'.

    Args:
        theme (str, optional): The theme to set. Defaults to 'Dark'.
    """
    if theme == "Dark":
        UIThemeManager.CurrentTheme = UITheme.Dark
    else:
        UIThemeManager.CurrentTheme = UITheme.Light

resolve_icon_file(directory, icon_name)

Resolves the icon file path based on the current UI theme.

Parameters:

Name Type Description Default
directory str

The directory where the icon file is located.

required
icon_name str

The name of the icon file.

required

Returns:

Name Type Description
full_file_path str

The full file path of the icon file.

Source code in pyrevitlib/pyrevit/revit/ui.py
def resolve_icon_file(directory, icon_name):
    """Resolves the icon file path based on the current UI theme.

    Args:
        directory (str): The directory where the icon file is located.
        icon_name (str): The name of the icon file.

    Returns:
        full_file_path (str): The full file path of the icon file.
    """
    full_file_path = op.join(directory, icon_name)

    if HOST_APP.is_newer_than(2024, True) and get_current_theme() == UITheme.Dark:
        dark_icon_name = op.splitext(icon_name)[0] + ICON_DARK_SUFFIX + ICON_FILE_FORMAT
        dark_file_path = op.join(directory, dark_icon_name)
        full_file_path = dark_file_path if op.exists(dark_file_path) else full_file_path

    return full_file_path if op.exists(full_file_path) else None

request_view_change(view, uidoc=None)

Ask Revit to make a view active once the current command or script ends.

Unlike setting revit.active_view, this works while a transaction or transaction group is open. The switch is asynchronous, so zoom the new view in a later command.

Parameters:

Name Type Description Default
view View

view to open.

required
uidoc UIDocument

defaults to the active document.

None
Source code in pyrevitlib/pyrevit/revit/ui.py
def request_view_change(view, uidoc=None):
    """Ask Revit to make a view active once the current command or script ends.

    Unlike setting ``revit.active_view``, this works while a transaction or
    transaction group is open. The switch is asynchronous, so zoom the new
    view in a later command.

    Args:
        view (DB.View): view to open.
        uidoc (UI.UIDocument, optional): defaults to the active document.
    """
    (uidoc or HOST_APP.uidoc).RequestViewChange(view)

get_active_ui_view(uidoc=None)

Return the UIView of the active view.

Raises:

Type Description
PyRevitException

when the active view has no open window.

Source code in pyrevitlib/pyrevit/revit/ui.py
def get_active_ui_view(uidoc=None):
    """Return the UIView of the active view.

    Raises:
        PyRevitException: when the active view has no open window.
    """
    uidoc = uidoc or HOST_APP.uidoc
    active_id = uidoc.ActiveView.Id
    for ui_view in uidoc.GetOpenUIViews():
        if ui_view.ViewId == active_id:
            return ui_view
    raise PyRevitException("The active view has no open window.")

zoom_to_elements(elements, padding=3.0, uidoc=None)

Zoom the active view to elements.

Parameters:

Name Type Description Default
elements list[Element]

elements to show.

required
padding float

clearance around them, in feet.

3.0
uidoc UIDocument

defaults to the active document.

None
Source code in pyrevitlib/pyrevit/revit/ui.py
def zoom_to_elements(elements, padding=3.0, uidoc=None):
    """Zoom the active view to elements.

    Args:
        elements (list[DB.Element]): elements to show.
        padding (float, optional): clearance around them, in feet.
        uidoc (UI.UIDocument, optional): defaults to the active document.
    """
    from pyrevit.revit.db import query

    box = query.get_elements_bounding_box(elements, padding=padding)
    if box is None:
        raise PyRevitException("Nothing to zoom to: no elements with a bounding box.")
    get_active_ui_view(uidoc).ZoomAndCenterRectangle(box.Min, box.Max)

zoom_fit(uidoc=None)

Zoom the active view to fit everything visible.

Source code in pyrevitlib/pyrevit/revit/ui.py
def zoom_fit(uidoc=None):
    """Zoom the active view to fit everything visible."""
    get_active_ui_view(uidoc).ZoomToFit()