Clear Selection with Arcpy

22481
4
11-01-2010 11:00 AM
by Anonymous User
Not applicable
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")


Thanks,

Brad
0 Kudos
4 Replies
by Anonymous User
Not applicable
Original User: jbarrette

Hello Brad,

Your clear selection code is very similar to code that I use in scripts and I've never experienced these popups before.  I tried to reproduce this using your code logic and was not able to generate popups.  It worked as expected.  I assume this is all runing in a validation script. I tried running the script tool from ArcMap and ArcCatalog.  I used "CURRENT" and a path to an MXD.

Could you please provide me more context for further testing.  If possible could you send me your script tool?  Please email jbarrette@esri.com.

Thanks,
Jeff
0 Kudos
AmandaS
New Contributor
Hi Brad and/or Jeff.  Was there any solution to this?  I've actually tried to do the exact same thing I agree...it's pretty ugly.  It takes about 5 minutes to run through each layer, run the Select Layer By Attribute tool, clear the selection and provide a pop-up result window.  

Here is the code I'm using in the Validation code of my script:

mxd = arcpy.mapping.MapDocument(r"C:\Temp\test.mxd")
df = arcpy.mapping.ListDataFrames(mxd)[0]
for lyr in arcpy.mapping.ListLayers(mxd):
    print lyr.name
    arcpy.SelectLayerByAttribute_management(lyr, "CLEAR_SELECTION")
for aTable in arcpy.mapping.ListTableViews(mxd):
    print aTable.name
    arcpy.SelectLayerByAttribute_management(aTable, "CLEAR_SELECTION")

Any help would be appreciated! 

-Amanda
0 Kudos
LangDeng
New Contributor

Amanda,

You may have figured this out already.

In Geoprocessing options, change the notification time  (it's a bar) to 0, you'll not see the popup window any more, but you may not know your processing is done.

Hope this helps.

Lang

0 Kudos
DougWolfinger2
New Contributor III

Could I use this code to find and unselected selected features that have a value (not null) in a certain attribute? How would I modify it to do that? Thanks

Doug

0 Kudos