Get Element ID of Selected Element with Python Script

Howdy all,

I’m relatively new to Dynamo and especially Python. I am trying to get a grasp on how to create a python script block that will take the currently selected element in the model and get the element ID, so I can put it through other functions (e.g., get/set parameter of element(s)). I have done some searches, and found some solutions that I have tried to replicate. However, I run into an error with the GetElementIds method, which tells me that it needs an input. Everything that I have found online, showed that there was no input necessary. Does anyone know where I might be going wrong?

import clr
import System
clr.AddReference(‘ProtoGeometry’)
from Autodesk.DesignScript.Geometry import *

clr.AddReference(‘RevitAPI’)
import Autodesk
from Autodesk.Revit.DB import *

clr.AddReference(‘RevitAPIUI’)
from Autodesk.Revit.UI import *

clr.AddReference(‘RevitNodes’)
import Revit
clr.ImportExtensions(Revit.GeometryConversion)
clr.ImportExtensions(Revit.Elements)

clr.AddReference(‘RevitServices’)
import RevitServices
from RevitServices.Persistence import DocumentManager

import sys
pyt_path = r’C:\Program Files (x86)\IronPython 2.7\Lib’
sys.path.append(pyt_path)

doc = DocumentManager.Instance.CurrentDBDocument
uiapp = DocumentManager.Instance.CurrentUIApplication
app = uiapp.Application
adoc = doc.ActiveView
uidoc = uiapp.ActiveUIDocument

sel = Selection.Selection.GetElementIds()

Any help/guidance/advice would be greatly appreciated!

Thanks!

Hi @wyatth_tgce,

Here’s an example:

Python:

import clr

#Import the Revit API
clr.AddReference('RevitAPI')
clr.AddReference('RevitAPIUI')
import Autodesk
from Autodesk.Revit.DB import *
from Autodesk.Revit.UI import *

#Import DocumentManager and TransactionManager
clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager

#Reference the active Document and application
doc = DocumentManager.Instance.CurrentDBDocument
uiapp = DocumentManager.Instance.CurrentUIApplication
uidoc = uiapp.ActiveUIDocument

bool = IN[0]

if bool:
	sel = uidoc.Selection.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element)
	td = TaskDialog(str(sel.ElementId)) 
	OUT = td.Show(), sel.ElementId

else:
	OUT = 'Set bool to true'
3 Likes