Get ViewSheet from a given View

Sometimes you might want to get a Sheet of your Active view, or any other. Check out this snippet of a function that will return ViewSheet of any given View.

Tags:  Revit API
Jul 2021

About snippet:

I assume you were looking on the internet how to get ViewSheet from a given View just like I was doing it before I decided to create my own solution and post it for you.

I feel like there is not much for me to explain since most of the work here is done with FilteredElementCollector(doc).WherePasses(filter) and filters deserve their own article and even a video explaining them with lots of examples.

At the moment, I feel like I won't be able to explain that concept very well because I go through multiple trials and errors myself for any of them to work, but each time I understand them better, and once I feel comfortable I will definitely write a detailed guide on filters.

So right now you could use the code below and adapt it in your own scripts. And hopefully, in the future, I will find enough confidence to write about this topic in depth.

PyRevit example script: get_sheet_from_view.pushbutton

# -*- coding: utf-8 -*-
__title__ = "Get sheet from View"
__author__ = "Erik Frits"

#>>>>>>>>>>>>>>>>>>>> IMPORTS
import clr, os
from Autodesk.Revit.DB import *

#>>>>>>>>>>>>>>>>>>>> VARIABLES
doc = __revit__.ActiveUIDocument.Document
uidoc = __revit__.ActiveUIDocument
app = __revit__.Application

#>>>>>>>>>>>>>>>>>>>> FUNCTIONS
def create_string_equals_filter(key_parameter, element_value, caseSensitive = True):
    """Function to create ElementParameterFilter based on FilterStringRule."""
    f_parameter         = ParameterValueProvider(ElementId(key_parameter))
    f_parameter_value   = element_value
    caseSensitive       = True
    f_rule              = FilterStringRule(f_parameter, FilterStringEquals(), f_parameter_value, caseSensitive)
    return ElementParameterFilter(f_rule)

def get_sheet_from_view(view):
    #type:(View) -> ViewPlan
    """Function to get ViewSheet associated with the given ViewPlan"""
    #>>>>>>>>>> CREATE FILTER 
    my_filter = create_string_equals_filter(key_parameter=BuiltInParameter.SHEET_NUMBER,
                                            element_value=view.get_Parameter(BuiltInParameter.VIEWER_SHEET_NUMBER).AsString() )
    #>>>>>>>>>> GET SHEET
    return FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Sheets).WhereElementIsNotElementType()\

#>>>>>>>>>>>>>>>>>>>> MAIN
if __name__ == '__main__':

    #>>>>>>>>>> ACTIVE VIEW
    active_view = doc.ActiveView
    sheet       = get_sheet_from_view(active_view)

    #>>>>>>>>>> PRINT RESULTS
    if sheet:   print('Sheet Found: {} - {}'.format(sheet.SheetNumber, sheet.Name))
    else:       print('No sheet associated with the given view: {}'.format(active_view.Name))