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

closed as unclear what you're asking by Midavalo, Andre Silva, Mapperz Apr 27 '16 at 21:30

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • By "selection on layer" do you mean select features or the actual layer in the table of contents? – Midavalo Apr 27 '16 at 9:13
  • I mean select the feature in the layer in MXD not the actual layer in the table of contents – mahmoud hageer Apr 27 '16 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 '16 at 9:21
  • "Source Layer Path", is the Path for layer in MXD, – mahmoud hageer Apr 27 '16 at 11:09
1

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)) 
  • 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 – mahmoud hageer Apr 27 '16 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 '16 at 10:09
  • When i loop through List layers i got error: AttributeError: LayerObject: Get attribute layers does not exist – mahmoud hageer Apr 27 '16 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.