0

Using arcgis 10.3, I trying to write code that when i do selection on layer manualy on MXD (not in the script) and run the script, the script need to run just on the selected features. I have tried -

layer = "Source Layer Path"
desc = arcpy.Describe(layer)
selected = desc.name
print (selected)

but it returns an empty list, is there a way to this in python?

I have tried this :

dataFrame = arcpy.mapping.ListDataFrames(Mxd)[0]
Layers=arcpy.mapping.ListLayers(Mxd, "", dataFrame)[0]
desc = arcpy.Describe(Layers)
selected = desc.Fidset
print ("selected = "+ str(selected))

Still same result

4
  • By "selection on layer" do you mean select features or the actual layer in the table of contents?
    – Midavalo
    Apr 27, 2016 at 9:13
  • I mean select the feature in the layer in MXD not the actual layer in the table of contents Apr 27, 2016 at 9:18
  • Do you have a layer in your MXD called "Source Layer Path"? or is this referring to a path to a feature class?
    – Midavalo
    Apr 27, 2016 at 9:21
  • "Source Layer Path", is the Path for layer in MXD, Apr 27, 2016 at 11:09

2 Answers 2

2

Your layer needs to refer to the layer in your MXD, not the path to the feature class.

layer = "Layer In MXD"
desc = arcpy.Describe(layer)
selected = desc.FIDSet
print (selected)

desc.Fidset will return the ObjectIDs of the selected features


mxd = arcpy.mapping.MapDocument("CURRENT")
dataFrame = arcpy.mapping.ListDataFrames(mxd)[0]

layers = arcpy.mapping.ListLayers(mxd, "", dataFrame)[0]

for layer in layers:
    desc = arcpy.Describe(layer) 
    selected = desc.FIDSet 
    print ("selected = "+ str(selected)) 
3
  • I have tried this : dataFrame = arcpy.mapping.ListDataFrames(Mxd)[0] Layers=arcpy.mapping.ListLayers(Mxd, "", dataFrame)[0] desc = arcpy.Describe(Layers) selected = desc.Fidset print ("selected = "+ str(selected)) Still same result Apr 27, 2016 at 10:04
  • that's because Layers is a list of layers. You need to loop through your layers. I'll update my answer
    – Midavalo
    Apr 27, 2016 at 10:09
  • When i loop through List layers i got error: AttributeError: LayerObject: Get attribute layers does not exist Apr 27, 2016 at 11:11
-1

I find the answer : Need to do MakeFeatureLayer_management

dataFrame = arcpy.mapping.ListDataFrames(Mxd)[0]
Layers=arcpy.mapping.ListLayers(Mxd, "", dataFrame)[0]
for layer in dataFrame:
        Layers=arcpy.mapping.ListLayers(Mxd, "", dataFrame)[0]
        if Layers.name=="Signs":
            arcpy.MakeFeatureLayer_management(layer,"Signs_Shape_layer")
            desc = arcpy.Describe("Signs_Shape_layer")
            FID_selected = desc.FIDSet
            selected = FID_selected.split("; ")
            print (selected)
            break

Not the answer you're looking for? Browse other questions tagged or ask your own question.