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).
Sub ClearSelection() Dim pApp As IApplication Set pApp = Application Dim pMxDoc As IMxDocument Set pMxDoc = ThisDocument Dim pMap As IMap Set pMap = pMxDoc.ActiveView Dim pAV As IActiveView Set pAV = pMap pMap.ClearSelection pAV.Refresh Debug.Print "done!" End Sub
I have a script that unselects all the selected features in my Map view but it loops through each layer in the dataframe and this takes a lot of time. I want to write a script that would be as fast as the Unselect the currently selected Features in all layers within ArcMap itself? This is my code below;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).
Any Suggestions? Thanks
import arcpy mxd = arcpy.mapping.MapDocument("CURRENT") dfs = arcpy.mapping.ListDataFrames(mxd) for df in dfs: for lyr in arcpy.mapping.ListLayers(mxd,"*",df): if lyr.isFeatureLayer: arcpy.SelectLayerByAttribute_management(lyr, "CLEAR_SELECTION") for tbl in arcpy.mapping.ListTableViews(mxd,"*",df): arcpy.SelectLayerByAttribute_management(tbl,"CLEAR_SELECTION")
Strange, if I run the (slightly changed) code it does run for me (in 10.2)import arcpy mxd = arcpy.mapping.MapDocument("CURRENT") dfs = arcpy.mapping.ListDataFrames(mxd) for df in dfs: for lyr in arcpy.mapping.ListLayers(mxd,"*",df): if lyr.isFeatureLayer: arcpy.SelectLayerByAttribute_management(lyr, "CLEAR_SELECTION") for tbl in arcpy.mapping.ListTableViews(mxd,"*",df): arcpy.SelectLayerByAttribute_management(tbl,"CLEAR_SELECTION")
What type of layers do you have in your TOC?
Kind regards,
Xander
Sub ClearSelection() Dim pMxDoc As IMxDocument Set pMxDoc = ThisDocument pMxDoc.FocusMap.ClearSelection pMxDoc.ActiveView.Refresh End Sub
import Snippets import comtypes from Snippets import CType a = Snippets.GetApp() from comtypes.gen.esriArcMapUI import * mxDoc = CType(a.Document, IMxDocument) mxDoc.FocusMap.ClearSelection() mxDoc.ActiveView.Refresh()
if len(arcpy.Describe(lyr).fidSet) > 0: arcpy.SelectLayerByAttribute_management(lyr, "CLEAR_SELECTION")