2

With python-addin-wizard I created a combobox which enables the user to select a specific ID of features in multiple featureclasses of the Table Of Content and then it zooms to the selected feature. So far so easy. It's not really fast, but it works okay.

My problem is, if a featureclass is selected in the TOC e.g. enter image description here,

items in the TOC appear to flicker and the processing is delayed considereably. I searched a lot in the arcpy functionalty, but can't find a bit of code to solve this Problem (quasi unselect the feature in the TOC). The only thing related to my Problem, that I found is pythonaddins.GetSelectedTOCLayerOrDataFrame() > But I have no idea how to unselect the layer.

Here is the part of the code which makes the selection and zoom:

def onSelChange(self, selection):
    self.value = selection
    fcsites2 = [fc_site_pol, fc_site_punkt, fc_site_linie, fc_neg_pol, fc_neg_punkt, fc_neg_linie, fc_unter_pol, fc_unter_punkt, fc_unter_linie]
    for x in fcsites2:
        arcpy.SelectLayerByAttribute_management(x, "NEW_SELECTION", fields_site + "='" + selection + "'")
        d = arcpy.Describe(x) 
        n = len(d.FIDset)
        if n > 0:
            break
    self.df.zoomToSelectedFeatures()
    self.df.scale = 5000

@artwork21 helped me start to solve my problem using the comtypes module. I have no much experience with integrating ArcObjects in Python. I reached to reference the TOC and the selected layer once. Then I can unselect the layer with the code below.But when I try a second unselect it doesn't work. It seems like the first selected layer is still known as selected like in a cache.

mU = GetModule(r"C:\Program Files (x86)\ArcGIS\Desktop10.2\com\esriArcMapUI.olb")
mF = GetModule(r"C:\Program Files (x86)\ArcGIS\Desktop10.2\com\esriFramework.olb")

app = CreateObject(mF.AppROT, interface=mF.IAppROT) 
pDoc = CType(app.Item(0).Document,mU.IMxDocument) 
pMap = pDoc.FocusMap 
ptoc = CType(pDoc.ContentsView(0),mU.IContentsView) 
pselLayer = pDoc.SelectedLayer

if pselLayer != 0: 
    print pselLayer.Name
    ptoc.RemoveFromSelectedItems(pselLayer)
    ptoc.Refresh
  • What about using Select Layer By Attribute with the selection_type parameter set to CLEAR_SELECTION? – GISGe Nov 9 '16 at 12:21
  • @GISGe: Reasonably, this is a misunderstanding. I mean the selection of layers in the Table of Content, when a layer is marked with a underlying blue. This situation makes nearly crash some Select-Layer-By-Attribute-Loops in my python-addin. I will add a picture in my question. – Franziska0601 Nov 9 '16 at 13:08
  • Just an observation, Selectedlayer returns an ILayer object or Nothing. You test if the object is not equal to zero.Well zero is not nothing. So maybe you should be asking if it is not "None"? Just an idea? – Hornbydd Nov 14 '16 at 9:24
2

I do not think this method exists in arcpy. You may have to call the IContentsView.RemoveFromSelectedItems arcobject method using python to call comtypes objects.

This old code sample shows how to use IContentsView.RemoveFromSelectedItems (in vb). This q/a shows how to call arcobjects using python:

Accessing ArcObjects from Python?

1

Finaly I found out, that it solves the problem in the comtypes-code, when I just don't put the selectedLayer in a variable. So I only use this bit of code to deselect the selected layer using the comtypes-modul.

mU = GetModule(r"C:\Program Files (x86)\ArcGIS\Desktop10.2\com\esriArcMapUI.olb")
mF = GetModule(r"C:\Program Files (x86)\ArcGIS\Desktop10.2\com\esriFramework.olb")
app = CreateObject(mF.AppROT, interface=mF.IAppROT) 
pDoc = CType(app.Item(0).Document,mU.IMxDocument)  
ptoc = CType(pDoc.ContentsView(0),mU.IContentsView) 
ptoc.RemoveFromSelectedItems(pDoc.SelectedLayer)
ptoc.RemoveFromSelectedItems(pDoc.FocusMap) #deselects the dataframe name as well, if it's necessary like in my case  
ptoc.Refresh

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.