mxd = arcpy.mapping.MapDocument("Current") df = arcpy.mapping.ListDataFrames(mxd)[0] for lyr in arcpy.mapping.ListLayers(mxd): ... arcpy.SelectLayerByAttribute_management(lyr, "CLEAR_SELECTION") ... Runtime error Traceback (most recent call last): File "<string>", line 2, in <module> File "C:\Program Files (x86)\ArcGIS\Desktop10.1\arcpy\arcpy\management.py", line 6461, in SelectLayerByAttribute raise e ExecuteError: Failed to execute. Parameters are not valid. ERROR 000840: The value is not a Table View. ERROR 000840: The value is not a Raster Layer. ERROR 000840: The value is not a Mosaic Layer. Failed to execute (SelectLayerByAttribute).
for lyr in arcpy.mapping.ListLayers(df):
for lyr in arcpy.mapping.ListLayers(mxd):
mxd = arcpy.mapping.MapDocument("CURRENT") df = arcpy.mapping.ListDataFrames(mxd)[0] lyrList = arcpy.mapping.ListLayers(df) lyrDict = {} for lyr in lyrList: lyrDict[lyr] = [lyr.visible, False] if len(arcpy.Describe(lyr).fidSet) > 0: lyrDist[lyr][1] = True for lyr in lyrDict: #turn any visible layers off if lyrDict[lyr][0] == True: lyr.visible = False arcpy.RefreshActiveView() for lyr in lyrDict: #clear selections if lyrDict[lyr][1] == True: arcpy.SelectLayerByAttribute_management(lyr, "CLEAR_SELECTION") for lyr in lyrDict: #turn any previously visible layers back on if lyrDict[lyr][0] == True: lyr.visible = True arcpy.RefreshActiveView()
mxd = arcpy.mapping.MapDocument("CURRENT") df = arcpy.mapping.ListDataFrames(mxd)[0] lyrDict = {} for lyr in arcpy.mapping.ListLayers(df): lyrDict[lyr] = [lyr.visible, False] if len(arcpy.Describe(lyr).fidSet) > 0: lyrDist[lyr][1] = True for lyr in [lyr for lyr in lyrDict if lyrDict[lyr][0] == True]: #turn any visible layers off lyr.visible = False arcpy.RefreshActiveView() for lyr in [lyr for lyr in lyrDict if lyrDict[lyr][1] == True]: #clear selections arcpy.SelectLayerByAttribute_management(lyr, "CLEAR_SELECTION") for lyr in [lyr for lyr in lyrDict if lyrDict[lyr][0] == True]: #turns layers back on lyr.visible = True arcpy.RefreshActiveView()
for lyr in arcpy.mapping.ListLayers(df): if len(lyr.fidSet) > 0: #BTW: Wish the .fidSet property returned a native Python list! blah blah
for lyr in arcpy.mapping.ListLayers(df): if lyr.hasSelection == True: blah blah
Dim pMxDocument As IMxDocument Dim pMap As IMap Dim pActiveView As IActiveView Set pMxDocument = ThisDocument Set pMap = pMxDocument.FocusMap Set pActiveView = pMap pActiveView.PartialRefresh esriViewGeoSelection, Nothing, Nothing pMap.ClearSelection
import Snippets import comtypes.gen.esriArcMapUI as esriArcMapUI import comtypes.gen.esriCarto as esriCarto pApp = Snippets.GetApp() pDoc = pApp.Document pMxDoc = Snippets.CType(pDoc, esriArcMapUI.IMxDocument) pMap = pMxDoc.FocusMap pAV = Snippets.CType(pMap, esriCarto.IActiveView) pAV.PartialRefresh(esriCarto.esriViewGeoSelection, None, None) pMap.ClearSelection()
>>> import _winreg >>> keyESRI = _winreg.OpenKey(_winreg.HKEY_CLASSES_ROOT, \ ... r"TypeLib\{866AE5D3-530C-11D2-A2BD-0000F8774FB5}\a.0\HELPDIR") Traceback (most recent call last): File "<stdin>", line 2, in <module> WindowsError: [Error 2] The system cannot find the file specified >>>
>>> from comtypes.client import GetModule >>> GetModule(r'C:\Program Files (x86)\ArcGIS\Desktop10.1\com\esriArcMapUI.olb') # Generating comtypes.gen._40499F24_596F_45D2_ACE1_A251E2990017_0_10_1 # Generating comtypes.gen._00020430_0000_0000_C000_000000000046_0_2_0 # Generating comtypes.gen.stdole # Generating comtypes.gen._866AE5D3_530C_11D2_A2BD_0000F8774FB5_0_10_1 # Generating comtypes.gen._59FCCD31_434C_4017_BDEF_DB4B7EDC9CE0_0_10_1 # Generating comtypes.gen._C4B094C2_FF32_4FA1_ABCB_7820F8D6FB68_0_10_1 # Generating comtypes.gen._5E1F7BC3_67C5_4AEE_8EC6_C4B73AAC42ED_0_10_1 # Generating comtypes.gen.esriSystem # Generating comtypes.gen.esriGeometry # Generating comtypes.gen._4ECCA6E2_B16B_4ACA_BD17_E74CAE4C150A_0_10_1 # Generating comtypes.gen.esriSystemUI # Generating comtypes.gen.esriDisplay # Generating comtypes.gen.esriFramework # Generating comtypes.gen._4A9C9ED7_F7DB_4614_B480_A5D265C961FC_0_10_1 # Generating comtypes.gen._0475BDB1_E5B2_4CA2_9127_B4B1683E70C2_0_10_1 # Generating comtypes.gen._18F2FC71_6B30_45B9_B101_037A8B868B66_0_10_1 ... ... ... (etc.)
import comtypes.gen.esriArcMapUI as esriArcMapUI
>>> pAppROT = comtypes.client.CreateObject(esriFramework.AppROT, interface=esriFramework.IAppROT) >>> pAppROT.count 1 >>> pApp = pAppROT.Item(0)
I find this confusion over what seem to be various flavors of 'Layer' objects. Once you get arcpy to hand you a layer object from ListLayers and you loop through them, try using the name property of the lyr instead of the layer object itself. E.g.:
this seems to work for me:
arcpy.SelectLayerByAttribute_management(lyr.name, "CLEAR_SELECTION")
And, as many others note, this is really slow. And if going to ArcObjects is not in your game, there is a python code through arcpy that clears selections really fast:
lyr.setSelectionSet("NEW",[])
Note: this is calling a method of the layer object, it is not an arcpy function.