Revit Task Dialog in Python

Revit Task Dialogs are invaluable for displaying messages and collecting finite user input. To make them easier to use in Python code, I have added a TaskDialog wrapper class to RevitPythonWrapper.

The first step I went through, was to read through code samples that use the class – Spiderinnet had great comprehensive examples: TaskDialog of Revit API – 1: A Comprehensive TaskDialog Example. Although they were all all in C#, once one gets used to reading C# it’s very easy to convert it to Python.

Next, I wrote code a some code from scratch to try to get a detailed understanding of how the TaskDialog and related API classes work (TaskDialog, TaskDialogResult,   TaskDialogCommandLinkId, etc). The result was the snippet below – a minimal yet comprehensive code sample that demonstrates all features of the TaskDialog Class.  [The code below has also been added to revitapidocs.com/code/.]

And lastly, I refactored the snippet to convert it into a reusable Class, and to take advantage of rpw’s abstractions. The result is a python-friendly interface to the C# classes. The final implementation is available here.

Revit Task Dialog

 

 

 

 

 

 

 

 

 

 

"""
Creates a TaskDialog

TESTED REVIT API: -

Author: Gui Talarico | github.com/gtalarico

This file is shared on www.revitapidocs.com
For more information visit http://github.com/gtalarico/revitapidocs
License: http://github.com/gtalarico/revitapidocs/blob/master/LICENSE.md
"""

from Autodesk.Revit.UI import (TaskDialog, TaskDialogCommonButtons,
                               TaskDialogCommandLinkId, TaskDialogResult)

title = 'Task Dialog Title'
dialog = TaskDialog(title)

# Properties
dialog.MainInstruction = 'Text Header'
dialog.MainContent = 'Text Content'
dialog.FooterText = 'Footer Text'
dialog.VerificationText = 'Verification Text'
# dialog.ExpandedContent = expanded_content

# Settings
dialog.TitleAutoPrefix = False
dialog.AllowCancellation = True

# Add Button
dialog.CommonButtons = TaskDialogCommonButtons.Ok | TaskDialogCommonButtons.Yes

# Set Default Button
dialog.DefaultButton = TaskDialogResult.None

# Add Command Link
dialog.AddCommandLink(TaskDialogCommandLinkId.CommandLink1,
                      'Command Button Text',
                      'Command Button Sub Text')
dialog.AddCommandLink(TaskDialogCommandLinkId.CommandLink2,
                      'Command Button Text 2',
                      'Command Button Sub Text 2')

result = dialog.Show()

if result == TaskDialogResult.Ok:
    print('Dialog was OK')
if result == TaskDialogResult.Yes:
    print('Dialog was Yes')
if result == TaskDialogResult.Cancel:
    print('Dialog was Cancelled')
if result == TaskDialogResult.CommandLink1:
    print('Button Was Pressed')
if result == TaskDialogResult.CommandLink2:
    print('Button 2 Was Pressed')
if dialog.WasVerificationChecked():
    print('Verification was Checked')

RevitPythonWrapper Task Dialog Wrapper

TaskDialog Example

Leave a Reply