as the others before said, you can use the same code you used for a shapefile:
# describe the feature layer to access the the selected set
desc = arcpy.Describe(lyr)
# FIDSet will contain the selected features
selectedFids = desc.FIDSet
But you will only get a list of values if you have selected some features before running the code.
Dear RafaelYou and other friends are right FIDSet still works in my script. My problem is in another part of the script. I first write the whole codes to illustrate what I am doing and then I show you the problem . These are the codes:
import sys,os,math,string,arcpy
from arcpy import env
# Read from current map
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd, "Data Themes")[0]
layer = arcpy.mapping.ListLayers(mxd, "*", df )
#layer_list = [layer for layer in arcpy.mapping.ListLayers(mxd) if 'FID' in [field.name for field in arcpy.ListFields(layer, 'FID')]]
for lyr in layer:
if lyr.isGroupLayer == False:
# describe the feature layer to access the the selected set
desc = arcpy.Describe(lyr)
# FIDSet will contain the selected features
selectedFids = desc.FIDSet
# If there are selectedFids (a selection set), write them to a new feature
# class in the current workspace.
if len(selectedFids) > 0:
queryList = selectedFids.replace(';', ',')
newName = arcpy.AddFieldDelimiters(lyr, 'OBJECTID')
lyr.definitionQuery = '{0} in ({1})' .format(newName, queryList)
arcpy.RefreshActiveView()
This script must exclude unselected features in the feature class. My problem is in the last three lines here
queryList = selectedFids.replace(';', ',')
newName = arcpy.AddFieldDelimiters(lyr, "OBJECTID")
# This query will exclude unselected fatures
lyr.definitionQuery = '{0} in ({1})' .format(newName, queryList)
For query it must be something like [OBJECTID] in ...... But if I don't put quotes in newName line it will be wrong. How should I write it?Thank you for your help.Babak