Resources
Summary
Brainstorm The Tool Idea
Let's create a tool to rename selected views with Prefix, Suffix, Find, Replace logic.
We will have to do the following steps:
Get Views
Get User Input
Prepare New Unique View names
Rename Views
Let's Begin.
Get Views
We will combine 2 ways of getting our views.
FIrst of all we will check if user has any views manually selected in the Project Browser, because uidoc.Selection.GetElementIds() will return them as well.
And if user hasn't selected any views in Project Browser, then we will prompt a menu asking them to select views from the list. And we will use pyrevit.forms.select_views for that.
There is a prewritten function which will show a menu to select views with a single line of code. We can see many code examples in pyRevit Dev Docs: Effective Inputs.
👇Here is an example.
Lastly, if user still hasn't selected any views after the form, then it's a good idea to notify user about it and stop execution.
Since we already started using pyrevit.forms, we can also use alert function that will show a dialog box with a message, and it has an option to exitscript.
👇 Here is the code Snippet for selecting views in our tool
💡We will cover all pyrevit.forms in a seperate module in the future as well.
Get User Input (temp for Dev phase)
Next, we want to get user input.
But since we are still in development phase, it's good to keep it simple until everything works. So I will just hardcode these values by creating 4 variables like below.
💡We will create an actual UI form when everything works!
Create New View Names
Now we can start iterating through selected views and prepare new View Names.
It's very simple, we will use python's built-in str method .replace() with the current view.Name and then add prefix and suffix on both ends.
Rename Views with Unique Name
Now we can actually begin renaming. We will override .Name property since it can be overridden.
Also as you're aware, View Names in Revit must be unique, so we need to make sure that our new names also unique when we rename them.
We could get a list of all view names and then check our name, but I prefer to attempt to rename them and If Revit returns an error - it means that View Name already exists, and we need to append something to make it unique. So that's why we have Try/Except statements to check it.
Also I create a for loop to try it for 20 times, and keep adding symbol in the end. We could use a while loop, but I hate when I forget to add a break statement, and then my Revit is crashing because of an infinite loop. So I stick with for loops when I can.
👇 And this will be enough to rename your views with hardcoded values.
Transaction
Also you can notice that I use Transaction class.
💡Transaction are used to make any changes in Revit projects. This protects our project from unintentional changes.
💡 It's also great for beginners to know, we can't mess up anything unless we use them, so get comfortable using Revit API!
In a nutshell: We need to
1) Define a Transaction
2) .Start() transaction
3) .Commit() transaction.
and all the changes have to be between Start and Commit statements.
Here are 2 syntax examples of how to use them:
Get User Input with rpw.FlexForm
Now all the main functionality works!
So we can explore how to improve getting find, replace, prefix and suffix variables.
Creating custom UI forms is not an easy task, but luckily for us, there are plenty of pre-written forms in pyRevit and rpw modules.
rpw is a module written by Gui Tallarico and it's available in pyRevit by default! He is also a creator of RevitAPIdocs.com website (Yes, this website is not from Autodesk…).
There are different modules available, but I want to focus your attention on FlexForm.
It allows you to quickly create simple forms with selected elements.
In our case we will need Label, TextBox, Separator and Button components, and it will be a very useful form for our tool.
Then we can get user inputs by using form.values, it will return a dictionary that contains user input.
🎦 Check the video to understand it better.
Final Code
So now you have all the parts of the script!
Let's put it all together in a single code!
Bonus Snippet for improved selection
👀 Here is the improved get_selected_elements function that I mentioned during the video. Check it out if you are curious enough!
HomeWork
Try to create similar tool for Sheets.
You will need to:
1️⃣ Get ViewSheets
2️⃣ Get User Input
3️⃣ Change ViewSheet.SheetNumber / ViewSheet.Name
4️⃣ Close and Show ProjectBrowser to see updated SheetNumbers
Help resources:
👉 pyrevit.forms.select_sheets
👉 ViewSheet.SheetNumber
👉 ViewSheet.Name
👇 Snippet To Close and Show Project Browser
⌨️ Happy Coding!