RevitAPI tool: View name to SheetNumber

Here is a snippet of how to rename SheetNumber to be the same as placed view name.

Dec 2020

Introduction

This is not very often that you would want your SheetNumbers to be the same as view names. However, if you want to do so, and you happen to find this blog post, here you go:

code
# -*- coding: utf-8 -*-
__title__ = "View name \nto SheetNumber"
__author__ = "Erik Frits"
__helpurl__ = "https://bim-buddy.com/blog/view-name-to-sheetnumber/"
__doc__ = """Version = 1.0
Date    = 23.12.2020
_____________________________________________________________________
Description:

This tool will set SheetNumbers of the selected sheets to be the same 
as View Name of the placed on sheet view.

NB! It will not change a SheetNumber if the sheet has more than 1
viewport placed. 
_____________________________________________________________________
How-to:

- Select Sheets that you want to rename in Project Browser.
- Click on the button.
_____________________________________________________________________
Prerequisite:

This tool will work on sheets that have single view placed on them,
otherwise script will change SheetNumber.
Extra logic can be introduced to deal with multiple views on sheet.
_______________________________________________________________
To-do:

- add logic in case sheet has multiple viewports placed.
_____________________________________________________________________
"""


# Imports
from Autodesk.Revit.DB import *
from pyrevit import forms
from Autodesk.Revit.UI import DockablePanes, DockablePane
from Autodesk.Revit.Exceptions import ArgumentException
uidoc   = __revit__.ActiveUIDocument
doc     = uidoc.Document

# Main
if __name__ == '__main__':
    count = 0
    selected_sheets = [doc.GetElement(sheet_id) for sheet_id in uidoc.Selection.GetElementIds() if type(doc.GetElement(sheet_id)) == ViewSheet]
    if not selected_sheets:
        forms.alert(title="{} - was cancelled.".format(__title__),
                    msg="No ViewSheet were selected.\nPlease, try again.",
                    exitscript=True,
                    )

    t = Transaction(doc, "Py: {}".format(__title__))
    t.Start()

    # Project browser does not refresh itself after changing SheetNumber
    # Therefore it is hidden and revealed in the code to update it.
    project_browser_id  = DockablePanes.BuiltInDockablePanes.ProjectBrowser
    project_browser     = DockablePane(project_browser_id)
    project_browser.Hide()


    for sheet in selected_sheets:
        placed_views = sheet.GetAllPlacedViews()
        if placed_views.Count == 1 :
            first_view_id = list(placed_views)[0]
            first_view = doc.GetElement(first_view_id)

            new_sheet_number = first_view.Name
            if sheet.SheetNumber == new_sheet_number:
                continue

            # While loop is made in case SheetNumber already exists.
            # It will keep adding '*' until SheetNumber is unique.
            while True:
                try:
                    sheet.SheetNumber = new_sheet_number
                    count += 1
                    break
                except ArgumentException:
                    print("SheetNumber [{}] already exists. '*' will be added".format(new_sheet_number))
                    new_sheet_number += "*"
        else:
            print("More than 1 view have been found on the sheet.[{}]".format(sheet.SheetNumber))
            # Add logic for multiple views on sheet here if needed.

    project_browser.Show()
    t.Commit()


    if count == 0:
        print("Script has finished. There were no sheets renamed.")
    if count == 1:
        print("Script has finished. Totally there was 1 sheet renamed.")
    else:
        print("Script has finished. Totally there were {} sheets renamed.".format(count))