# describe the feature layer to access the the selected set desc = arcpy.Describe(lyr) # FIDSet will contain the selected features selectedFids = desc.FIDSet
not sure if i completely understand, but your script looks like you´re working with layers inside arcmap in the current mxd.If you want to copy selected features of a layer to a new feature class, CopyFeatures will respect the selection:http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//001700000035000000
SummaryCopies features from the input feature class or layer to a new feature class. If the input is a layer which has a selection, only the selected features will be copied. If the input is a geodatabase feature class or shapefile, all features will be copied.
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.FIDSetBut you will only get a list of values if you have selected some features before running the code.
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()
queryList = selectedFids.replace(';', ',') newName = arcpy.AddFieldDelimiters(lyr, "OBJECTID") # This query will exclude unselected fatures lyr.definitionQuery = '{0} in ({1})' .format(newName, queryList)
You can use describe to figure out what the 'oidFieldName' is (whether it be FID, OID, OBJECTID, or something else).oidFieldName = arcpy.Describe(myLayer).oidFieldNameWorth mentioning - If you have v10.1, the data access cursors allow a MUCH faster and better way of getting a list of the selected OID values than the .fidSet describe property:oidValueList = [r[0] for r in arcpy.da.SearchCursor(myLayer, ["OID@"])]
oidFieldName = arcpy.Describe(myLayer).oidFieldName
oidValueList = [r[0] for r in arcpy.da.SearchCursor(myLayer, ["OID@"])]
desc = arcpy.Describe(lyr) if desc.hasOID: selectedOID = desc.OIDFieldName
oidValueList = [r[0] for r in arcpy.da.SearchCursor(myLayer, ["OID@"])].
Signed in members can post, follow updates, and more. New here? Register a free account.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.