Skip to content

settings_window

Dynamic Settings Window for pyRevit Creates a reusable WPF window for managing script configurations.

Usage

from pyrevit import script from pyrevit.forms import settings_window

settings = [ {"name": "scope", "type": "choice", "label": "Scope", "options": ["Visibility", "Active State"], "default": "Visibility"}, {"name": "set_workset", "type": "bool", "label": "Set Workset", "default": True}, {"name": "tolerance", "type": "int", "label": "Tolerance (mm)", "default": 10, "min": 0, "max": 1000}, {"name": "prefix", "type": "string", "label": "Prefix", "default": ""}, {"name": "highlight_color", "type": "color", "label": "Highlight Color", "default": "#ffff0000"}, {"name": "export_folder", "type": "folder", "label": "Export Folder", "default": ""}, {"name": "template_file", "type": "file", "label": "Template File", "default": "", "file_ext": "rvt", "files_filter": "Revit Files (.rvt)|.rvt"}, ]

if settings_window.show_settings(settings, title="My Tool Settings"): print("Settings saved!")

Classes

SettingsWindow(settings_schema, section=None, title='Settings', width=450)

Bases: WPFWindow

Dynamic settings window that generates UI from schema.

Initialize the settings window.

Parameters:

Name Type Description Default
settings_schema

List of setting definitions

required
section

Config section name

None
title

Window title

'Settings'
width

Window width in pixels

450
Source code in pyrevitlib/pyrevit/forms/settings_window.py
def __init__(
    self, settings_schema, section=None, title="Settings", width=450,
):
    """Initialize the settings window.

    Args:
        settings_schema: List of setting definitions
        section: Config section name
        title: Window title
        width: Window width in pixels
    """
    self.config = script.get_config(section)
    self.settings_schema = settings_schema
    self.window_title = title
    self.window_width = width
    self.config_section = section
    self.result = False
    self.controls = {}

    # Generate XAML
    xaml_string = self._generate_xaml()

    # Initialize WPF window with generated XAML
    forms.WPFWindow.__init__(self, xaml_string, literal_string=True)

    # Set window title
    self.Title = self.window_title

    # Populate controls with current values
    self._populate_values()

    # Wire up button events
    self.save_button.Click += self.save_clicked
    self.cancel_button.Click += self.cancel_clicked
    self.reset_button.Click += self.reset_clicked

Attributes

config = script.get_config(section) instance-attribute
settings_schema = settings_schema instance-attribute
window_title = title instance-attribute
window_width = width instance-attribute
config_section = section instance-attribute
result = False instance-attribute
controls = {} instance-attribute
Title = self.window_title instance-attribute

Functions

save_clicked(sender, args)

Handle save button click.

Source code in pyrevitlib/pyrevit/forms/settings_window.py
def save_clicked(self, sender, args):
    """Handle save button click."""
    errors = []
    validated_values = {}

    # Validate all values first
    for setting in self.settings_schema:
        name = setting.get("name")
        setting_type = setting.get("type", "string")
        control = self.controls.get(name)

        if not control:
            continue

        # Get value from control
        if setting_type == "bool":
            value = control.IsChecked
        elif setting_type == "choice":
            value = control.SelectedItem
        else:
            value = control.Text

        # Validate
        is_valid, validated_value, error_msg = self._validate_setting(
            setting, value
        )

        if not is_valid:
            errors.append(error_msg)
        else:
            validated_values[name] = validated_value

    # Show errors if any
    if errors:
        forms.alert("\n".join(errors), title="Validation Error", warn_icon=True)
        return

    # Only save if all validations passed
    for setting in self.settings_schema:
        name = setting.get("name")
        if name in validated_values:
            self.config.set_option(name, validated_values[name])

    # Save config
    script.save_config()

    self.result = True
    self.Close()
cancel_clicked(sender, args)

Handle cancel button click.

Source code in pyrevitlib/pyrevit/forms/settings_window.py
def cancel_clicked(self, sender, args):
    """Handle cancel button click."""
    self.result = False
    self.Close()
reset_clicked(sender, args)

Handle reset button click.

Source code in pyrevitlib/pyrevit/forms/settings_window.py
def reset_clicked(self, sender, args):
    """Handle reset button click."""
    # Confirm with user
    if forms.alert(
        "Are you sure you want to reset all settings, removing it from the .ini?",
        title="Reset Settings",
        yes=True,
        no=True,
    ):
        script.reset_config(self.config_section)
        self.result = False
        self.Close()

Functions

show_settings(settings_schema, section=None, title='Settings', width=450)

Show settings window and return True if saved.

Parameters:

Name Type Description Default
settings_schema

List of setting definitions. Each setting is a dict with: - name (str): Setting key name for config - type (str): "bool", "choice", "int", "float", "string", "color", "folder", or "file" - label (str): Display label for the setting - default: Default value if not in config - options (list): For "choice" type, list of options - min (int/float): For "int"/"float" type, minimum value - max (int/float): For "int"/"float" type, maximum value - required (bool): For "string" type, whether field is required - file_ext (str): For "file" type, file extension filter (e.g., "rvt") - files_filter (str): For "file" type, files filter (e.g., "Revit Files (.rvt)|.rvt") - init_dir (str): For "file" type, initial directory - multi_file (bool): For "file" type, allow multiple file selection

required
section str

Config section name (default: None)

None
title str

Window title (default: "Settings")

'Settings'
width int

Window width in pixels (default: 450)

450

Returns:

Name Type Description
bool

True if settings were saved, False if canceled

Example
settings_schema = [
    {"name": "scope", "type": "choice", "label": "Scope",
     "options": ["Visibility", "Active State"], "default": "Visibility"},
    {"name": "set_workset", "type": "bool", "label": "Set Workset", "default": True},
    {"name": "tolerance", "type": "int", "label": "Tolerance (mm)",
     "default": 10, "min": 0, "max": 1000},
    {"name": "highlight_color", "type": "color", "label": "Highlight Color",
     "default": "#ffff0000"},
    {"name": "export_folder", "type": "folder", "label": "Export Folder", "default": ""},
    {"name": "template_file", "type": "file", "label": "Template File",
     "default": "", "file_ext": "rvt", "files_filter": "Revit Files (*.rvt)|*.rvt"},
]

if show_settings(settings_schema, section="MyToolSection", title="My Tool Settings"):
    print("Settings saved!")
Source code in pyrevitlib/pyrevit/forms/settings_window.py
def show_settings(settings_schema, section=None, title="Settings", width=450):
    """Show settings window and return True if saved.

    Args:
        settings_schema: List of setting definitions. Each setting is a dict with:
            - name (str): Setting key name for config
            - type (str): "bool", "choice", "int", "float", "string", "color", "folder", or "file"
            - label (str): Display label for the setting
            - default: Default value if not in config
            - options (list): For "choice" type, list of options
            - min (int/float): For "int"/"float" type, minimum value
            - max (int/float): For "int"/"float" type, maximum value
            - required (bool): For "string" type, whether field is required
            - file_ext (str): For "file" type, file extension filter (e.g., "rvt")
            - files_filter (str): For "file" type, files filter (e.g., "Revit Files (*.rvt)|*.rvt")
            - init_dir (str): For "file" type, initial directory
            - multi_file (bool): For "file" type, allow multiple file selection
        section (str): Config section name (default: None)
        title (str): Window title (default: "Settings")
        width (int): Window width in pixels (default: 450)

    Returns:
        bool: True if settings were saved, False if canceled

    Example:
        ```python
        settings_schema = [
            {"name": "scope", "type": "choice", "label": "Scope",
             "options": ["Visibility", "Active State"], "default": "Visibility"},
            {"name": "set_workset", "type": "bool", "label": "Set Workset", "default": True},
            {"name": "tolerance", "type": "int", "label": "Tolerance (mm)",
             "default": 10, "min": 0, "max": 1000},
            {"name": "highlight_color", "type": "color", "label": "Highlight Color",
             "default": "#ffff0000"},
            {"name": "export_folder", "type": "folder", "label": "Export Folder", "default": ""},
            {"name": "template_file", "type": "file", "label": "Template File",
             "default": "", "file_ext": "rvt", "files_filter": "Revit Files (*.rvt)|*.rvt"},
        ]

        if show_settings(settings_schema, section="MyToolSection", title="My Tool Settings"):
            print("Settings saved!")
        ```
    """
    window = SettingsWindow(settings_schema, section, title, width)
    window.ShowDialog()
    return window.result