LearnRevitAPI

Feb 26, 2023

pyRevit Forms

It’s often necessary to get user input in our tools. But creating custom forms with WPF is not beginner friendly. It has very steep learning curve so be prepared to translate a lot of C#.

However, there is an alternative that comes shipped with pyRevit and I want to show you how simple it is to use pre-made forms with pyRevit. 


💡 It’s already well documented in pyRevit Dev Docs in Effective Inputs, but I want to shine some light on it, so you know where to look and how to use it.

👀 So let's look into forms and how easy it is to use them.

1️⃣ Alert

⚠️ Alerts are one of the most commonly used inputs in pyRevit. They are a simple yet effective way to get user input or stop execution of a tool with an alert message. 

📃 Alerts can be used to ask Yes/No questions, ask a user to select from a list of options, or simply provide a message to the user with an option to exit scrip execution.

👆 Here is doc string from forms.alert with all available arguments. 

Don't get scared! You don't need to use all of them. In most cases I only use msg, title, exitscript and leave all the rest to default values.

Below you will find a few examples on how to use them.

⚠️ Alert + Exitscript

✅ Yes/No

⏹️Custom Buttons


2️⃣ WarningBar

⚠️ Warning Bars are a great way indicate to a user that he is supposed to do something.

For example I love using them when I need user to pick some elements. By using Warning Bar you will see an obvious yellow bar on the top with instructions what to do. 


3️⃣ Pick File/Path

📁 Once in a while your scripts will need to read or write to a specific file or a folder. It’s not complicated to get it with os library in python, but this function in pyRevit is great.

👀 You will see a doc string with all available arguments if you want to dive deeper, but you can keep it simple as I did in my examples below.

📂 Pick File/File


4️⃣ Select Forms

📋pyRevit has a plenty of forms that will give you a list to choose desired elements such as:
Views, Sheets, ViewTemplates, Legends, Revisions, TitleBlocks...

✅ These are really useful and simple to use. With just one line you can create a form with a list of elements for user to choose from.

👇 There are many forms available and you can extend them even more with filterfunc if necessary. I have combined multiple examples into single code, so choose only what you need.

5️⃣ Select From List

📃 The SelectFromList input allows the user to select one or more items from a list of options. It's exactly the same form as I show above, but you have more control over what you are showing to user. 

There are different ways to use it, but I will show you 2 methods how I used it.

Select From List of Elements

🗃️ If you have a list of elements and you know that you can get names with just a property, you can provide name of that property in name_attr argument. 

📰 That will work fine for many elements, however you will get an error if you try to get a .Name of any Type. But, don't worry, I will show you how to use it with dict where we can easily control Names and Elements.


Select From Dict

❌ Once you start using SelectFromList you will notice that you can't get all attributes. It only allows attributes that can get value as a property. (e.g. level.Name)

✅ But Don't Worry! We can create custom dictionary where keys will be visible names of elements in the form, and values are going to be returned elements.


6️⃣ Text Input

TextForm is another very simple form. It allows users to enter text into a dialog box that can be used in the code. This is useful for scripts that require the user to enter a certain value or text string.


7️⃣ Conditions

🎭 Lastly, we can check conditions before running our script. It's often necessary to be in a certain ViewType or have selection in Revit for the script to run successfully. This is useful for ensuring that a script is only executed if certain conditions are met.


8️⃣ rpw - FlexForm

🎁 As a bonus I want to mention FlexForm from rpw. 
The rpw FlexForm is a powerful tool for creating custom forms in Revit and it is available in pyRevit by default!

✅ It allows users to create a custom form with multiple inputs, such as text boxes, dropdowns, checkboxes, and more. We can combine as many of them as we want and  have a form with multiple input types in the order that we need. 


I hope it helps you understand how to use built-in forms within pyRevit. It's not that complicated and you can always refer to original documentation

Enjoyed Newsletter?

Leave a Testimonial Here.

⌨️ Happy Coding!
Erik Frits

Join Newsletter

📩 You will be added to Revit API Newsletter

Join Us!

which is already read by 7400+ people!