Final Code
Here is the final code to avoid any confusion.
# -*- coding: utf-8 -*-
__title__ = "Final Tool: Rename Views"
__doc__ = """Version = 1.0
Date = 03.01.2025
_____________________________________________________________________
Description:
This tool will rename views with Find/Replace logic.
_____________________________________________________________________
How-to:
-> Click on the button
-> ...
_____________________________________________________________________
Last update:
- [03.01.2025] - 1.0 RELEASE
_____________________________________________________________________
Author: Erik Frits"""
# ╦╔╦╗╔═╗╔═╗╦═╗╔╦╗╔═╗
# ║║║║╠═╝║ ║╠╦╝ ║ ╚═╗
# ╩╩ ╩╩ ╚═╝╩╚═ ╩ ╚═╝ IMPORTS
#==================================================
from Autodesk.Revit.DB import *
from pyrevit import forms
# ╦ ╦╔═╗╦═╗╦╔═╗╔╗ ╦ ╔═╗╔═╗
# ╚╗╔╝╠═╣╠╦╝║╠═╣╠╩╗║ ║╣ ╚═╗
# ╚╝ ╩ ╩╩╚═╩╩ ╩╚═╝╩═╝╚═╝╚═╝ VARIABLES
#==================================================
doc = __revit__.ActiveUIDocument.Document
uidoc = __revit__.ActiveUIDocument
# ╔═╗╦ ╦╔╗╔╔═╗╔╦╗╦╔═╗╔╗╔╔═╗
# ╠╣ ║ ║║║║║ ║ ║║ ║║║║╚═╗
# ╚ ╚═╝╝╚╝╚═╝ ╩ ╩╚═╝╝╚╝╚═╝
#==================================================
def get_sel_views():
# Get Currently Selected Views in ProjectBrowser
sel_el_ids = uidoc.Selection.GetElementIds()
sel_el = [doc.GetElement(el_id) for el_id in sel_el_ids]
sel_views = [el for el in sel_el if issubclass(type(el), View)]
# print(sel_views)
# If nothing selected: Prompt User To Select Views from List
if not sel_views:
# selected_views = forms.select_views()
# print(selected_views)
# Get All Views in Project
all_views_vt = FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Views).WhereElementIsNotElementType().ToElements()
all_views = [view for view in all_views_vt if not view.IsTemplate] # Filter ViewTemplates out
# dict_views = {view.Name:view for view in all_views}
# Create Dict of Views with view.Name and view.ViewType
dict_views = {}
for view in all_views:
view_name = "{} ({})".format(view.Name, view.ViewType)
dict_views[view_name] = view
sel_view_names = forms.SelectFromList.show(sorted(dict_views.keys()), button_name='Select Item', multiselect=True)
if sel_view_names:
sel_views = [dict_views[sel_view_name] for sel_view_name in sel_view_names]
# print(sel_views)
# Ensure that views selected
if not sel_views:
forms.alert('No Views Selected. PLease Try Again', exitscript=True)
return sel_views
# ╔╦╗╔═╗╦╔╗╔
# ║║║╠═╣║║║║
# ╩ ╩╩ ╩╩╝╚╝ MAIN
#==================================================
# 1️⃣ Select Views
sel_views = get_sel_views()
#2️⃣🅰️ Define Renaming Rules
# prefix = 'EF_'
# find = 'L'
# replace = 'Level-'
# suffix = ''
#2️⃣🅱️ Ask for Renaming Rules
from rpw.ui.forms import *
# Create UI Form
components = [Label('Prefix:'), TextBox('UI_prefix'),
Label('Find:'), TextBox('UI_find'),
Label('Replace:'), TextBox('UI_replace'),
Label('Suffix:'), TextBox('UI_suffix'),
Separator(), Button('Rename Views')]
form = FlexForm('Rename Views', components)
form.show()
# Get User Input from the UI Form
UI_values = form.values
prefix = UI_values['UI_prefix']
find = UI_values['UI_find']
replace = UI_values['UI_replace']
suffix = UI_values['UI_suffix']
#🔓 Start Transaction to make Changes
t = Transaction(doc, 'py_Rename Views')
t.Start() # 🔓
for view in sel_views:
#3️⃣ Create new View Name
old_name = view.Name
new_name = prefix + view.Name.replace(find, replace) + suffix
#4️⃣ Rename Views (Ensure unique view name)
for i in range(50):
try:
view.Name = new_name
print('{} -> {}'.format(old_name, new_name))
break
except:
new_name += "*"
t.Commit() #🔒
HomeWork
Recreate the tool that I've showed you during this lesson.
And if you really want to learn something new, try adjusting the code for other categories or classes and see if you can rename them.
Remember: the best way to learn programming is by doing.
⌨️ Happy Coding!
Discuss the lesson :
P.S. Sometimes this chat might experience connection issues.
Use Discord App for best experience.
Discuss the lesson :
P.S. Sometimes this chat might experience connection issues.
Use Discord App for best experience.
Discuss the lesson :
P.S. Sometimes this chat might experience connection issues.
Use Discord App for best experience.

Unlock Community

The pyRevit Hackers Community is only available with pyRevit Hackers Bundle.
Upgrade Here to Get Access to the community and all pyRevit Courses.
Use coupon code "upgrade" to get 150EUR Discount as a member.
⌨️ Happy Coding!

Unlock Community

The pyRevit Hackers Community is only available with pyRevit Hackers Bundle.
Upgrade Here to Get Access to the community and all pyRevit Courses.
Use coupon code "upgrade" to get 150EUR Discount as a member.
⌨️ Happy Coding!

Unlock Community

The pyRevit Hackers Community is only available with pyRevit Hackers Bundle.
Upgrade Here to Get Access to the community and all pyRevit Courses.
Use coupon code "upgrade" to get 150EUR Discount as a member.
⌨️ Happy Coding!
© 2023-2024 EF Learn Revit API
© 2023-2024 EF Learn Revit API
© 2023-2024 EF Learn Revit API