我確實知道使用arcpy獲取選定項目的一種方法。
#get an map object
mxd = arcpy.mapping.MapDocument("CURRENT")
#get an dataframe object, here the first one is taken
df = arcpy.mapping.ListDataFrames(mxd)[0]
#get a layer object
lyr = arcpy.mapping.ListLayers(mxd,"NameOfRequiredLayer",df)
#now get the FIDs of the selected Elements of your layer
selection = arcpy.Describe(lyr).FIDSet
「選擇」,則包含所選元件的的FID。有了這個,你可以攜帶任何你必須做的事情。例如,您可以設置圖層定義查詢:
#shapes use FID, feature classes use OBJECTID, so you better check
IDname = "\"OBJECTID\""
if lyr.dataSource.endswith("shp"):
IDname = "\"FID\""
querystring = IDname + " = " + str(selection[0])
for count in range(1,len(selection)):
querystring = querystring + " OR " + IDname + " = " + str(selection[count])
if lyr.supports("DEFINITIONQUERY"):
lyr.definitionQuery = querystring