Pyrevit Forms -
Comprehensive Report: PyRevit Forms 1. Executive Summary pyRevit is an open-source scripting environment for Autodesk Revit, dramatically extending its functionality through Python. A core feature of pyRevit is its Forms module – a toolkit for creating custom graphical user interfaces (GUIs) directly within Revit. This report details the architecture, components, implementation, and strategic use of pyRevit Forms, enabling developers to build interactive, user-friendly tools without external libraries like Windows Forms or WPF . 2. Introduction Revit's native API offers limited UI capabilities. pyRevit Forms bridge this gap, allowing scripters and developers to:
Collect user inputs (text, numbers, selections). Display data (lists, tables, status updates). Guide users through multi-step processes. Validate inputs before execution.
Built on Windows Forms (.NET) internally, pyRevit wraps complex code into simple, Revit-native-looking interfaces. 3. Core Architecture pyRevit Forms leverage the .NET Framework (specifically System.Windows.Forms ) via Python's clr (Common Language Runtime) integration. Key characteristics:
Modal Dialogs : Most forms block user interaction with Revit until closed. Revit Owner Window : Forms are parented to Revit's main window, preventing focus issues. Thread Safety : UI operations must occur on Revit's main thread (handled automatically by pyRevit). Simplified Syntax : pyRevit abstracts verbose .NET code into compact, readable functions. pyrevit forms
4. Main Form Components 4.1. Basic Input Controls | Control | pyRevit Function | Purpose | |---------|----------------|---------| | TextBox | forms.TextBox | Single-line text/number input | | ComboBox | forms.Combobox | Dropdown selection | | ListBox | forms.ListBox | Multi-item selectable list | | CheckBox | forms.CheckBox | Boolean option | | RadioButton | forms.RadioButton | Mutually exclusive options | | Button | forms.Button | Action trigger | 4.2. Specialized Forms | Form Type | Function | Typical Use | |-----------|----------|--------------| | Alert | forms.alert | Warning/error messages | | Confirm | forms.alert (with yes_no=True ) | Yes/No decisions | | Progress Bar | forms.ProgressBar | Long operations feedback | | TaskDialog | forms.taskdialog | Rich, command-link dialogs | | SelectFromList | forms.SelectFromList | Choose from collection | | FlexForm | forms.FlexForm | Custom multi-control layouts | 5. Implementation Patterns 5.1. Simple Alert from pyrevit import forms forms.alert("Operation completed successfully.", title="Info")
5.2. Yes/No Confirmation if forms.alert("Delete selected walls?", yes_no=True, cancel=True): # User clicked Yes pass else: # User clicked No or Cancel pass
5.3. Text Input user_value = forms.TextBox.show( prompt="Enter wall height (mm):", title="Input Required", default="2500" ) if user_value: height = float(user_value) Comprehensive Report: PyRevit Forms 1
5.4. Selection from List levels = ["Level 1", "Level 2", "Level 3"] selected = forms.SelectFromList.show( levels, title="Select Target Level", multiselect=False, button_name="Select" )
5.5. Progress Bar with forms.ProgressBar(title="Processing", cancellable=True) as pb: for i, element in enumerate(elements): if pb.cancelled: break pb.update_progress(i, len(elements)) # Process element
5.6. FlexForm (Custom Layout) components = [ forms.flexform.Label("Wall Parameters"), forms.flexform.TextBox("height", Text="3000"), forms.flexform.CheckBox("structural", Text="Structural?"), forms.flexform.ComboBox("type", Items=["Basic", "Curtain", "Stacked"]) ] result = forms.FlexForm.show(components, title="Create Wall") if result: height = result.height is_structural = result.structural wall_type = result.type pyRevit Forms bridge this gap, allowing scripters and
6. Advanced Features 6.1. Data Validation Integrate validation callbacks: def validate_positive(value): try: return float(value) > 0 except: return False height = forms.TextBox.show( prompt="Enter positive number:", validator=validate_positive, validation_msg="Must be >0" )
6.2. Dynamic UI Updates Use FlexForm with refresh callback to show/hide controls based on selections. 6.3. Context Help Add tooltips or help buttons: forms.alert( "Click OK to continue.", title="Help", footer="For more info, see documentation." )