Current Selection doesn't report ProjectBrowser's selections

konrad
Explorer

Current Selection doesn't report ProjectBrowser's selections

konrad
Explorer
Explorer

When user selects items in the model, they typically are accessible via the API by grabbing the UIDocument selection. That also, works for some of the selections made via the Project Browser. Let's take for example an idea to override the ID_DELETE_BUTTON command. If you override the delete command, you could see what's being deleted by grabbing current selection from the document. That's not going to work for Families selected in the Project Browser. Nothing is being added to the selection for that category. To re-create this just:
- override ID_DELETE_BUTTON command
- access selection in the handler for the command override and see what's being shown as selected

- select family from the Project Browser, and hit delete. Nothing will be reported in the selection. That's not correct because Revit would normally delete all instances and types of that family and then unload that family itself. Selection here should report at least the family, so we can track down all types/instances. Otherwise overriding delete command is going to be impossible/tricky. 

Bear in mind that if you were to select + delete a view, sheet, schedule, legend, group etc. via Project Browser and deleted it, selection would report properly what is being selected/deleted. 

Thoughts? @jeremytammik 

I guess if my goal here is to override the delete command so that I can capture what is being deleted, would there be another way to get that data. Document Changed event is a no go, because by the time it fires, deleted ids are not valid anymore so I cannot get info about deleted elements. Any suggestions would be welcome here. 

0 Likes
Reply
284 Views
5 Replies
Replies (5)

RPTHOMAS108
Mentor
Mentor

I think your approach is wrong anyway since binding to the Delete command will not stop the item being deleted in the project browser via the context menu (ID_PRJBROWSER_DELETE) so you need two bindings.

 

In 2023 you can use the selection changed event to monitor what has been selected that includes items in project browser.

Private WithEvents DeleteBinding As AddInCommandBinding = Nothing
    Private WithEvents DeleteBinding0 As AddInCommandBinding = Nothing
    Private Selected As ISet(Of ElementId) = Nothing
    Private Sub Deleted(s As Object, e As Autodesk.Revit.UI.Events.BeforeExecutedEventArgs) _
                                                                Handles DeleteBinding.BeforeExecuted, DeleteBinding0.BeforeExecuted
        If Selected Is Nothing Then Exit Sub Else
        If Selected.Count = 0 Then Exit Sub Else

        Dim ECF As New ElementClassFilter(GetType(ViewSheet))
        Dim FEC As New FilteredElementCollector(e.ActiveDocument, Selected)
        Dim Els As IList(Of Element) = FEC.WherePasses(ECF).ToElements

        If Els.Count > 0 Then
            TaskDialog.Show("Sheet selected", "Can't delete this sheet, it is a sheet like no other." &
                            " A precious gem that must be preserved for future generations to gaze upon!")
            For i = 0 To Els.Count - 1
                Debug.WriteLine(Els(i).Name)
            Next
            e.Cancel = True
        End If

    End Sub
    Private Sub SelChanged(s As Object, e As Autodesk.Revit.UI.Events.SelectionChangedEventArgs)
        Selected = e.GetSelectedElements()
    End Sub

    Private Function Obj_221102a(ByVal commandData As Autodesk.Revit.UI.ExternalCommandData,
ByRef message As String, ByVal elements As Autodesk.Revit.DB.ElementSet) As Result
        Dim UIDoc As UIDocument = commandData.Application.ActiveUIDocument
        If UIDoc Is Nothing Then Return Result.Cancelled Else
        Dim IntDoc As Document = UIDoc.Document

        If DeleteBinding IsNot Nothing Then
            commandData.Application.RemoveAddInCommandBinding(RevitCommandId.LookupPostableCommandId(PostableCommand.Delete))
            commandData.Application.RemoveAddInCommandBinding(RevitCommandId.LookupCommandId("ID_PRJBROWSER_DELETE"))

            DeleteBinding = Nothing
            DeleteBinding0 = Nothing
            RemoveHandler commandData.Application.SelectionChanged, AddressOf SelChanged
        Else
            DeleteBinding = commandData.Application.CreateAddInCommandBinding(
                           RevitCommandId.LookupPostableCommandId(PostableCommand.Delete))

            DeleteBinding0 = commandData.Application.CreateAddInCommandBinding(
                           RevitCommandId.LookupCommandId("ID_PRJBROWSER_DELETE"))

            AddHandler commandData.Application.SelectionChanged, AddressOf SelChanged
        End If

        Return Result.Succeeded
    End Function

 

 

0 Likes

konrad
Explorer
Explorer

Well, there is a different command to catch context menu deletions from the project browser. ID_PRJBROWSER_DELETE so combining that with ID_BUTTON_DELETE should do the trick to catch all deletions, only if I could get these selections from the browser. 

New selection changed event in 2023 sounds like a good start. Any ideas for the older versions? 

0 Likes

RPTHOMAS108
Mentor
Mentor

I think the two bindings may be new I'm sure in the past one was sufficient.

 

I used to derive the selection changed event from the idling event, but you are saying that UIDocument.Selection.GetElementIds() doesn't include the project browser?

0 Likes

konrad
Explorer
Explorer

It does, UNLESS you select a Family and hit the delete button. Under the hood Revit finds all instances, types and the actual family element and deletes them all. The selection of these objects doesn't show up when the delete command override fires. That's my issue here. 

0 Likes

RPTHOMAS108
Mentor
Mentor

Yes that is an awkward one doesn't appear to work with idling event or the selection changed event.

 

Only thing I can suggest is reading last journal entries e.g. do they contain 'SelChange...>>Families' prior to delete.

 

 'E 02-Nov-2022 23:24:28.649;   0:< 
  Jrn.Browser "SelChange"  _
           , ">>Families>>Structural Framing>>UB-Universal Beams>>" 
  'E 02-Nov-2022 23:24:30.778;   0:< 
  Jrn.Command "ProjectBrowser"  , " , ID_PRJBROWSER_DELETE" 

 

 

 

 

0 Likes