Revit API - TaskDialog

There is a class in Revit API that can create a simple TaskDialog to display various information to the user, and even collect very simple input such as buttons, and checkbox. Let's look at how to use it.

Revit API - TaskDialog

There is a class in Revit API that can create a simple TaskDialog to display various information to the user, and even collect very simple input such as buttons, and checkbox. Let's look at how to use it.

Summary

Task Dialog in Revit API

Revit API already has a simple form that we can use with TaskDialog Class.

It can be used to display different messages to the user and get a very simple user input, by using Common Buttons, Command Buttons or even Verification Checkbox.

Let's have a look at how to create TaskDialog and override its properties.

Task Dialog Constructor

First of all we need to create TaskDialog instance using the constructor. It takes a single argument - title.

from Autodesk.Revit.UI import TaskDialog

#🔷 Create TaskDialog
task_dialog = TaskDialog('Hello BIM World!')

Once you've created an instance of a TaskDialog, make sure you use Show() method to actually display it to a user.

There are 4 different Show Variations, and we will start using the regular one without any arguments, because we will override different properties manually on the TaskDialog instance.

#🔸Show TaskDialog
task_dialog.Show()

💡 Other Show methods give you an option to provide different arguments in the Show method, so you can save some space in the code. I will show you an example in the end, but first let's break down the most commonly used properties.

Heading and Message

First of all let's add some text. We can use MainInstruction and MainContent for displaying the Heading message and the content itself.

from Autodesk.Revit.UI import TaskDialog

#🔷 Create TaskDialog
task_dialog = TaskDialog('Hello BIM World!')

#🔹Add Heading amd Message
task_dialog.MainInstruction = "This is a taskDialog from Revit API"
task_dialog.MainContent     = ("This shows you what is possible to create with Revit API\n"
                               "You can even create buttons here!")

#🔸Show TaskDialog
task_dialog.Show()

💡 Make sure you use Show method after you've overridden all the properties.

Common Buttons

Now Let's add a few buttons to the form. We need to use CommonButtons property and use TaskDialogCommonButtons Enumeration for defining the buttons.

You will also notice that we don't create a list, but use a | ('pipe') symbol between them. This symbol represents OR/Union actions. To make it you have to click on SHIFT + \ (see image below of keyboard key)

We can also define which of these buttons should be highlighted by using DefaultButton property and providing TaskDialogResult Enumeration.

from Autodesk.Revit.UI import TaskDialog, TaskDialogResult, TaskDialogCommonButtons

task_dialog = TaskDialog('Hello BIM World!')

#🔹Add Heading amd Message
task_dialog.MainInstruction = "This is a taskDialog from Revit API"
task_dialog.MainContent     = ("This shows you what is possible to create with Revit API\n"
                               "You can even create buttons here!")


#🔹Buttons
task_dialog.CommonButtons = TaskDialogCommonButtons.Yes | TaskDialogCommonButtons.No | TaskDialogCommonButtons.Cancel |  TaskDialogCommonButtons.Retry | TaskDialogCommonButtons.Close | TaskDialogCommonButtons.Ok
task_dialog.DefaultButton = TaskDialogResult.Cancel

#🔸Show TaskDialog
result = task_dialog.Show()

💡 Also notice that we've created a result variable to store the result of a task dialog after we call .Show method.

What Button was selected?

We can make conditional statement to find out what button user has selected in the TaskDialog. For that we need to use result variable that we defined, and compare it to TaskDialogResult Enumertaion like this:

#✅ Check Selected Common Buttons
if result == TaskDialogResult.Ok:
    print('You selected Ok')

if result == TaskDialogResult.Cancel:
    print('You selected Cancel')
Command Buttons

TaskDialog also has an option to create up to 4 Command Buttons.

We need to use AddCommandLink method and provide TaskDialogCommandLinkId Enumeration and the displayed text.

from Autodesk.Revit.UI import TaskDialog, TaskDialogCommandLinkId, TaskDialogResult

task_dialog = TaskDialog('Hello BIM World!')
#🔸Show TaskDialog

#🔹Add Heading amd Message
task_dialog.MainInstruction = "This is a taskDialog from Revit API"
task_dialog.MainContent     = ("This shows you what is possible to create with Revit API\n"
                               "You can even create buttons here!")

# #🔹Command Buttons
task_dialog.AddCommandLink(TaskDialogCommandLinkId.CommandLink1, 'Option A')
task_dialog.AddCommandLink(TaskDialogCommandLinkId.CommandLink2, 'Option B')
task_dialog.AddCommandLink(TaskDialogCommandLinkId.CommandLink3, 'Option C')
task_dialog.AddCommandLink(TaskDialogCommandLinkId.CommandLink4, 'Option D')


result = task_dialog.Show()

To read what button was selected, we can use the same logic as for CommonButtons. Just make sure you look at TaskDialogResult.CommandLink1, 2,3,4

#✅ Check Selected Command Buttons
if result == TaskDialogResult.CommandLink1:
    print('You selected Option A')

if result == TaskDialogResult.CommandLink2:
    print('You selected Option B')

Footer

TaskDialog also has an option to create a footer.

We can also provide an HTML <a> tag, so it's actually a link.

#🔹Footer
task_dialog.FooterText = "<a href=\"https://learnrevitapi.com\"> Visit LearnRevitAPI </a>"
CheckBoxes

In case you would like to add a checkbox in your TaskDialog you have 2 options:

💡 Make sure you don't use both at the same time, as it will trigger an error!

# #🔹 Checkboxes
# # task_dialog.ExtraCheckBoxText = 'Check Me'
task_dialog.VerificationText  = 'Verify Me'

To read the result of a checkbox we need to use appropriate method such as:

# print(task_dialog.WasExtraCheckBoxChecked())
print(task_dialog.WasVerificationChecked())
Final Code

Lastly, let's put all of it together into a single code snippet:

# -*- coding: utf-8 -*-
__title__   = "8.05 - RevitAPI Forms"
__doc__ = """Date    = 04.04.2024
_____________________________________________________________________
Description:
Overview of RPW Forms
https://revitpythonwrapper.readthedocs.io/en/latest/ui/forms.html
_____________________________________________________________________
Author: Erik Frits"""

# ╦╔╦╗╔═╗╔═╗╦═╗╔╦╗╔═╗
# ║║║║╠═╝║ ║╠╦╝ ║ ╚═╗
# ╩╩ ╩╩  ╚═╝╩╚═ ╩ ╚═╝ IMPORTS
#==================================================
from Autodesk.Revit.DB import *

# ╦  ╦╔═╗╦═╗╦╔═╗╔╗ ╦  ╔═╗╔═╗
# ╚╗╔╝╠═╣╠╦╝║╠═╣╠╩╗║  ║╣ ╚═╗
#  ╚╝ ╩ ╩╩╚═╩╩ ╩╚═╝╩═╝╚═╝╚═╝ VARIABLES
#==================================================
uidoc  = __revit__.ActiveUIDocument
doc    = __revit__.ActiveUIDocument.Document #type:Document



# ╔╦╗╔═╗╔═╗╦╔═  ╔╦╗╦╔═╗╦  ╔═╗╔═╗
#  ║ ╠═╣╚═╗╠╩╗   ║║║╠═╣║  ║ ║║ ╦
#  ╩ ╩ ╩╚═╝╩ ╩  ═╩╝╩╩ ╩╩═╝╚═╝╚═╝

#🔹 Create TaskDialog
# from Autodesk.Revit.UI import *
from Autodesk.Revit.UI import TaskDialog, TaskDialogCommonButtons,TaskDialogResult,TaskDialogCommandLinkId

task_dialog = TaskDialog('Hello BIM World!')

#🔹Add Heading amd Message
task_dialog.MainInstruction = "This is a taskDialog from Revit API"
task_dialog.MainContent     = ("This shows you what is possible to create with Revit API\n"
                               "You can even create buttons here!")

#🔹Footer
task_dialog.FooterText = "<a href=\"https://learnrevitapi.com\"> Visit LearnRevitAPI </a>"

#🔹Buttons
task_dialog.CommonButtons = TaskDialogCommonButtons.Yes | TaskDialogCommonButtons.No | TaskDialogCommonButtons.Cancel |  TaskDialogCommonButtons.Retry | TaskDialogCommonButtons.Close | TaskDialogCommonButtons.Ok
task_dialog.DefaultButton = TaskDialogResult.Cancel

# #🔹Command Buttons
task_dialog.AddCommandLink(TaskDialogCommandLinkId.CommandLink1, 'Option A')
task_dialog.AddCommandLink(TaskDialogCommandLinkId.CommandLink2, 'Option B')
task_dialog.AddCommandLink(TaskDialogCommandLinkId.CommandLink3, 'Option C')
task_dialog.AddCommandLink(TaskDialogCommandLinkId.CommandLink4, 'Option D')

# #🔹 Checkboxes
# # task_dialog.ExtraCheckBoxText = 'Check Me'
task_dialog.VerificationText  = 'Verify Me'

#🔸Show TaskDialog
result = task_dialog.Show()


#✅ Check Selected Common Buttons
# if result == TaskDialogResult.Ok:
#     print('You selected Ok')
#
# if result == TaskDialogResult.Cancel:
#     print('You selected Cancel')

#✅ Check Selected Command Buttons
# if result == TaskDialogResult.CommandLink1:
#     print('You selected Option A')
#
# if result == TaskDialogResult.CommandLink2:
#     print('You selected Option B')

# print(task_dialog.WasExtraCheckBoxChecked())
print(task_dialog.WasVerificationChecked())

What about other constructors?

Well, let's look at them.
Overall allow you to provide different arguments instead of overriding properties.

There are 3 more constructors, and the difference is that, they all take 1 more parameter than the previous one. So we will only look at the last one with the most parameters, but you can obviously provide less arguments if you want to.

Let's break it down. We can provide the following 4 arguments:

  • title

  • maininstruction

  • buttons

  • defaultButton

⌨️ And here is a code example:

from Autodesk.Revit.UI import *

title = "Hello BIM World"
main_instruction = "This is the main instruction of the dialog"
buttons = TaskDialogCommonButtons.Ok | TaskDialogCommonButtons.Cancel

# Create and show the task dialog
dialog_result = TaskDialog.Show(title, main_instruction, buttons)

That would be similar to overriding the properties, but instead you set these values by using Show method.

Discuss the lesson:

P.S. Sometimes this chat might experience connection issues.
Please be patient or join via Discord app so you can get the most out of this community and get access to even more chats.

P.S. Sometimes this chat might experience connection issues.
Please be patient or join via Discord app so you can get the most out of this community and get access to even more chats.

© 2023-2024 EF Learn Revit API

© 2023-2024 EF Learn Revit API

© 2023-2024 EF Learn Revit API