Similar to below:
Private Class SelectionFilterByViewType
Implements Autodesk.Revit.UI.Selection.ISelectionFilter
Private IntVT As ViewType
Private IntResult As ElementId = ElementId.InvalidElementId
Public ReadOnly Property Result As ElementId
Get
Return IntResult
End Get
End Property
Public Sub New(VT As ViewType)
IntVT = VT
End Sub
Public Function AllowElement(elem As Global.Autodesk.Revit.DB.Element) As Boolean Implements Selection.ISelectionFilter.AllowElement
IntResult = ElementId.InvalidElementId
Dim VP As Viewport = TryCast(elem, Viewport)
If VP Is Nothing Then Return False Else
Dim V As View = TryCast(elem.Document.GetElement(VP.ViewId), View)
If V Is Nothing Then Return False Else
If IntVT <> V.ViewType Then Return False Else
IntResult = V.Id
Return True
End Function
Public Function AllowReference(reference As Reference, position As XYZ) As Boolean Implements Selection.ISelectionFilter.AllowReference
Return False
End Function
End Class
Private Function TObj15(ByVal commandData As Autodesk.Revit.UI.ExternalCommandData, _
ByRef message As String, ByVal elements As Autodesk.Revit.DB.ElementSet) As Result
Dim IntApp As UIApplication = commandData.Application
Dim UIDoc As UIDocument = IntApp.ActiveUIDocument
If UIDoc Is Nothing Then Return Result.Cancelled Else
Dim IntDoc As Document = UIDoc.Document
Dim SelRes As New SelectionFilterByViewType(ViewType.Legend)
Dim R As Reference = Nothing
Try
R = UIDoc.Selection.PickObject(Selection.ObjectType.Element, SelRes, "Pick viewport on sheet containing legend")
Catch ex As Exception
End Try
If R Is Nothing Then Return Result.Cancelled Else
Dim FEC As New FilteredElementCollector(IntDoc)
Dim ECF As New ElementClassFilter(GetType(ViewSheet))
Dim IDS As ICollection(Of ElementId) = FEC.WherePasses(ECF).WhereElementIsNotElementType.ToElementIds
Dim VS_Fnd As IEnumerable(Of ViewSheet) = IDS.Select(Function(x) IntDoc.GetElement(x)) _
.Cast(Of ViewSheet) _
.Where(Function(vs) vs.IsTemplate = False) _
.Where(Function(vs) vs.IsPlaceholder = False) _
.Where(Function(vs) vs.GetAllPlacedViews.Contains(SelRes.Result)) _
.OrderBy(Function(vs) vs.Name)
Dim SB As New Text.StringBuilder
For Each item As ViewSheet In VS_Fnd
SB.AppendLine(item.Name)
Next
Dim TD As New TaskDialog("Found sheets") With {.ExpandedContent = SB.ToString, .MainContent = "Noted below"}
TD.Show()
Return Result.Succeeded
End Function