AnsweredAssumed Answered

Clear Selection with Arcpy

Question asked by geonetadmin on Nov 1, 2010
Latest reply on Mar 19, 2015 by ldeng
Original User: farsterbj

In my script ToolValidator class, I am using arcpy.SelectLayerByLocation_management to clear selected features in all of my data frames.  Events in my ToolValidator will not work properly if there is a selection on certain layers, thus the need to clear selection.

When I run the tool, the routine successfully clears selected features in all data frames, but it is ugly!  For each layer that I clear selection on, a geoprocessing result box pops up and the map is locked up until all the 'geoprocessing' operations are complete.  This happens anytime a user alters parameters in the tool dialog.

Does anyone know how to clear selected features in a data frame in a cleaner fashion; either by executing arcpy.SelectLayerByLocation_management in the background or by simply leveraging the functionality of the 'Clear Selected Features' button on the 'Tools' toolbar?

Here's a subset of my code:
# Clear selections on the MAPPING_EXTENTS and TOWNS layers in each data frame
# This code could be modified to clear selections on every layer in every data frame
    dfCount = 0
    for df in arcpy.mapping.ListDataFrames(mxd):
      dfCount += 1
    for dfIndex in range(dfCount):
      df = arcpy.mapping.ListDataFrames(mxd)[dfIndex]
      clearLyrs = ["MAPPING_EXTENTS", "TOWNS"]
      for clearLyr in clearLyrs:
        lyr = arcpy.mapping.ListLayers(mxd, clearLyr, df)[0]
        arcpy.SelectLayerByAttribute_management(lyr, "CLEAR_SELECTION")