Help with SelectionChanged Event Handler

Hi!
Im working on creating a simple dockable pane that shows some simple information about selected objects. I’ve seen and read this post: WPF Register SelectionChanged Event which did help me quite alot. I’ve also studied the pyrevit startup script over at github.

The pane gets registered fine, and i have a pushbutton to display it. At the moment its just a pane with some plain text.

My next goal is to get the event handler for SelectionChanged to work. The issue im having is that it only fires once. I.e the first time i select an element in my model, it executes as intended. Then the consecutive times i select something, nothing happens.

To clarify, not an issue with the dockable pane atm, but rather making the event handler trigger every time i select something.

See the current state of my code below. (If you have any tips or pointer on how to improve the code in general I’d be happy to recieve some feedback, since im quite new to this whole python in revit thing. )

Code:

# BASIC IMPORTS:
import os
import sys
import time
import os.path as op
import clr
clr.AddReference("PresentationFramework")
clr.AddReference("PresentationCore")
clr.AddReference("WindowsBase")
clr.AddReference("RevitAPI")
clr.AddReference("RevitAPIUI")

# PYREVIT IMPORTS:
from pyrevit import script
from pyrevit import HOST_APP, framework
from pyrevit import revit, DB, UI
from pyrevit import forms
from pyrevit import routes

from pyrevit.framework import Windows, wpf
from pyrevit.coreutils import Guid
from pyrevit.framework import EventHandler
#----------------------------------------------------------------------------------------------


# VARIABLES:
#----------------------------------------------------------------------------------------------
MY_PANE_GUID = Guid("064b18cc-f79a-4dca-b256-5c12ac1f7ed7")

script_dir = op.dirname(__file__)

xaml_path = op.join(script_dir,"interface.xaml")
#----------------------------------------------------------------------------------------------


# Classes
#----------------------------------------------------------------------------------------------
class DockableExample(Windows.Controls.Page):
    def __init__(self,xaml_path):
        wpf.LoadComponent(self, xaml_path)


    def OnSelection(self,selection):
        print("OnSelection print statement triggered.")
        #forms.alert("OnSelection triggered.")
        doc = HOST_APP.doc
        if len(selection) == 1:
            element = doc.GetElement(selection[0])
            elementId = element.Id
            elementTypeName = element.Name

            print("Element Type: {}".format(elementTypeName))
            print("Element ID: {}".format(elementId))
        elif len(selection) > 1:
            print("Multiple elements selected.")
        else:
            print("No elements selected.")


class DockableExamplePanelProvider(UI.IDockablePaneProvider):
    def SetupDockablePane(self, data):
        data.FrameworkElement = DockableExample(xaml_path)
        data.VisibleByDefault = True
#----------------------------------------------------------------------------------------------


# Registering pane on startup:
#------------------------------------------------------------------------------------------------
DOCKABLE_PANE_ID = UI.DockablePaneId(MY_PANE_GUID)

if not UI.DockablePane.PaneIsRegistered(DOCKABLE_PANE_ID): # Reigstering the pane during startup
    HOST_APP.uiapp.RegisterDockablePane(
        DOCKABLE_PANE_ID, 
        "My First Pane",
        DockableExamplePanelProvider()
    )


# Eventhandler (or supposed to be at least):
#------------------------------------------------------------------------------------------------
dockableExampleInstance = DockableExample(xaml_path) # Create an instance of the DockableExample

# Defining a handler to match event signature
def SelectionChangedHandler(sender,args):
    # Retrieve the updated selection from the Revit UI
    selection = HOST_APP.uidoc.Selection.GetElementIds() # Argument to be passed for OnSelection method.
    dockableExampleInstance.OnSelection(selection)


HOST_APP.uiapp.SelectionChanged += \
    framework.EventHandler[UI.Events.SelectionChangedEventArgs](SelectionChangedHandler)

#------------------------------------------------------------------------------------------------

1 Like