Event_sender 'Application' object has no attribute 'GetElement'

Trying to make a hook that looks at the current view’s filters to see if it is a site plan and check a parameter automatically. First thing I’m running into is that the event_sender doesn’t seem to have GetElement.

I saw this used here: Using Hooks to get the Family Name being loaded - #2 by sanzoghenzo

I know this is supper messy. I plan to clean it up later:

# Checks if placed element is in a view that has a NeedsSite filter and checks that parameter automatically.

# Import libraries
from pyrevit import DB, revit, script, forms, EXEC_PARAMS

from Autodesk.Revit.DB.Events import DocumentChangedEventArgs
from Autodesk.Revit.DB import ElementFilter, ElementId, BuiltInCategory, ElementMulticategoryFilter

# import clr
#
# clr.AddReference('System')
# from System.Collection.Generic import List

from System.Collections.Generic import List

# NOTE TO CODY : This section may not work in hooks at all. Try using sender.GetElement(Id) instead of using doc.
# https://discourse.pyrevitlabs.io/t/using-hooks-to-get-the-family-name-being-loaded/1938/2
# doc = __revit__.ActiveUIDocument.Document
# cView = doc.ActiveView
# NOTE TO CODY

# ################################################################################
# Debug toggles
globalDebug = True
disabledScript = False


def dprint(*printItems):
    if globalDebug:
        for pI in printItems:
            print(pI)

# ################################################################################

categories = List[BuiltInCategory]([BuiltInCategory.OST_DuctTerminal,
                                   BuiltInCategory.OST_CommunicationDevices,
                                   BuiltInCategory.OST_TelephoneDevices,
                                   BuiltInCategory.OST_SecurityDevices,
                                   BuiltInCategory.OST_DataDevices,
                                   BuiltInCategory.OST_Conduit,
                                   BuiltInCategory.OST_ConduitFitting,
                                   BuiltInCategory.OST_LightingDevices,
                                   BuiltInCategory.OST_LightingFixtures])

custom_filter = ElementMulticategoryFilter(categories)

#elements = DocumentChangedEventArgs.GetAddedElementIds()

args = EXEC_PARAMS.event_args
sender = EXEC_PARAMS.event_sender

test = args.GetAddedElementIds()

if test:
    dprint("You've placed something!")
    for e in test:
        element = sender.GetElement(e)
        dprint(e)
        dprint(element)
else:
    dprint("You didn't place shit!")

Error Message:

IronPython Traceback:
Traceback (most recent call last):
 File "L:\Revit\Development\PyRevit\DevTool\DevTool.extension\hooks\doc-changed.py", line 57, in <module>
AttributeError: 'Application' object has no attribute 'GetElement'

Script Executor Traceback:
System.MissingMemberException: 'Application' object has no attribute 'GetElement'
 at IronPython.Runtime.Binding.PythonGetMemberBinder.FastErrorGet`1.GetError(CallSite site, TSelfType target, CodeContext context)
 at System.Dynamic.UpdateDelegates.UpdateAndExecute2[T0,T1,TRet](CallSite site, T0 arg0, T1 arg1)
 at Microsoft.Scripting.Interpreter.FuncCallInstruction`5.Run(InterpretedFrame frame)
 at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame)
 at Microsoft.Scripting.Interpreter.LightLambda.Run2[T0,T1,TRet](T0 arg0, T1 arg1)
 at IronPython.Compiler.PythonScriptCode.RunWorker(CodeContext ctx)
 at IronPython.Compiler.PythonScriptCode.Run(Scope scope)
 at IronPython.Compiler.RuntimeScriptCode.InvokeTarget(Scope scope)
 at PyRevitLabs.PyRevit.Runtime.IronPythonEngine.Execute(ScriptRuntime& runtime)

When you don’t know what methods and props an object has try print(dir(object))

1 Like

I wrote a little function to help understand objects. Might be useful here…

def print_attributes(object, include_dunders=False, include_callables=False):
    # BUG: failes if any getattr fails
    if include_dunders:
        if include_callables:
            return '\n'.join('{}={}'.format(x, getattr(object, x)) for x in dir(object) if hasattr(object, x))
        else:
            return '\n'.join('{}={}'.format(x, getattr(object, x)) for x in dir(object) if hasattr(object, x) and not callable(getattr(object, x)))
    else:
        if include_callables:
            return '\n'.join('{}={}'.format(x, getattr(object, x)) for x in dir(object) if '__' not in x and hasattr(object, x))
        else:
            return '\n'.join('{}={}'.format(x, getattr(object, x)) for x in dir(object) if '__' not in x and hasattr(object, x) and not callable(getattr(object, x)))

I use RevitPythonShell to explore this kind of thing, but probably rpw.forms.Console() could do the same thing in a pyRevit script. I also highly recommend RevitDBExplorer.

1 Like