Summary
Reuse SelectFromDict Form
In this lesson, we will practice once again on reusing our WPF form. But this time, not only we will make SelectFromDict reusable, we will also prepare many classes so we can simplify selection of various elements.
Let's dive in.
Recap - Reusing Code
Firstly, let's quickly recap what we did in Lesson 3.2.
As you remember we created ef_forms
folder inside our lib, and we placed there Alert.py
and Alert.xaml
files. Then we imported the relevant classes inside of __init__
file so we can easily import it directly from ef_forms
Now, we are going to do the same steps to make SelectFromDict
reusable.
Copy SelectFromDict to lib
Now, let's do the same for SelecFromDict.
Firstly, go to the previous lesson and copy python and xaml code to lib folder. I will rename them to:
SelectFromDict.py
SelectFromDict.xaml
💡Be careful with Refactor function in pyCharm. If you don't pay attention you can rename matching keyword in other places too.
Once you copy files, we can clean up Python file:
Remove pyRevit metatags (title, doc…)
Remove execution code on the bottom
Add doc strings
Let's also add another feature to this class, so it can also take lists as arguments. It's very simple, we just need to check if input is a list
and convert it to a dict
by making key:value
as the same values.
Create a function
Next, let's create a function as a placeholder base, so we can make a few copies in a moment and add more logic. Also we will change the name to ef_select_from_dict
to keep naming logic similar for all our custom forms.
Don't forget to import this new function inside of ef_forms/__init__.py
file
Now you can also test it inside any of your pushbuttons by using the following code:
Now let's create more reusable functions based on SelectFromDict
class.
ef_select_views/view_templates()
Let's create a function so we can easily select Views or ViewTemplates.
Copy the function that we made and get views from the project so you don't have to do that every single time.
We just want to call the function without arguments from now on to select views.
Here is the code:
Don't forget to update ef_forms/__init__.py
And now we can test these functions in any pushbutton:
And here are results:
select_wall_types()
Let's practice a bit more. Let's create a function to choose WallTypes.
💡Don't forget to update __init__.py
file.
And here is how to use it
select_types_by_cat()
Also, while you are creating your functions, think of how to make them reusable too. For example we just create a function to select form WallTypes
.
But we could make it better and allow user to provide any BuiltInCategory
to make it more reusable.
Here is the code:
💡Update __init__ imports
And then you can test it
And here is your final form, which is really versatile and can save you a lot of time in the long-run.
Final Code
Now, let me share all my final code snippets so we avoid any confusion.
And here is ef_forms/__init__.py
code:
Conclusion
And that's how you can reuse your WPF form and also prepare more functions by using the same form to make selection of different elements much more efficient in your scripts.
Homework
Now it's time to practice.
I want you to create more reusable functions that will be more useful to you. Maybe you work with MEP or Structure or other Architectural elements. Just create a few more functions, test them and of course share with the community.
By sharing you will all see more examples and also get to share your hard work with others. Sharing is Caring after all!
And once you share a few of your own custom functions, you are ready for the next lesson where we will create a FlexForm and cover Behind-Code in process.
Happy Coding!