Select to view content in your preferred language

Get Selected Layer

593
2
04-24-2013 05:03 AM
TedChapin
Frequent Contributor
I'm looking for a way to get the currently selected layer(s) from the TOC.  arcpy.mapping.ListLayers() returns all layers in the data frame.  I only want the ones the user has selected.  Does anyone know how to do this?

Ted Chapin
SGC Engineering
Tags (2)
0 Kudos
2 Replies
DuncanHornby
MVP Notable Contributor
As of 10.1 you can return a handle on a single selected layer, have a look at this thread.
0 Kudos
ChrisSnyder
Honored Contributor
How about:

#prints a list of the selected layers in the TOC along with the number of selected records
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]
for lyr in arcpy.mapping.ListLayers(df):
   selectedCount = len([int(fid) for fid in arcpy.Describe(lyr).fidset.split(";") if fid != ''])
   if selectedCount > 0:
      print "The " + str(lyr) + " layer has " + str(selectedCount) + " records selected..."        
0 Kudos