I worked out a Python Add-In Combox, that combines different Fields from different Feature Class in one Drop-Down-List. The user can select one ID/Code in the Drop-Donw list and the active view zooms to the selection. So far it's not very fast, but it works. My problem is, that Arc Map crashes, when I use the Combobox and a Feature Layer is selected. This I can't fix. Did anybody has an idea?
Here is my code:
import sys, os, arcpy, zipfile, traceback, pythonaddins
from arcpy import mapping, env
class SiteComboBoxClass43(object):
"""Implementation for Site_Python_Addins_addin.combobox (ComboBox)"""
def __init__(self):
self.items = []
self.editable = True
self.enabled = True
self.dropdownWidth = 'WWWWWWWWW'
self.width = 'WWWWWWWWW'
def onFocus(self, focused):
if focused:
self.mxd = arcpy.mapping.MapDocument('current')
global items, site, akode, fc_biotop_pol, fc_biotop_poi, fc_biotop_li, fc_pro1_pol, fc_pro1_poi, fc_pro1_li
items = []
fields_site = "SITE_ID"
akode = "AKODE"
fc_biotop_pol = "Biotop (Polygon)"
fc_biotop_poi = "Biotop (Point)"
fc_biotop_li = "Biotop (Line)"
fc_pro1_pol = "Protection Area 1 (Polygon)"
fc_pro1_poi = "Protection Area 1 (Point)"
fc_pro1_li = "Protection Area 1 (Line)"
# build lists and populate the Combobox with sorted values
fcsites1 = [fc_biotop_pol, fc_biotop_poi, fc_biotop_li, fc_pro1_pol, fc_pro1_poi, fc_pro1_li]
fcakode1 = [fc_pro1_pol, fc_pro1_poi, fc_pro1_li]
for x in fcsites1:
for row in arcpy.da.SearchCursor(x,fields_site): items.append(row[0])
for x in fcakode1:
for row in arcpy.da.SearchCursor(x,akode): items.append(row[0])
self.items = sorted(items)
pass
def onSelChange(self, selection):
combobox.enabled = True
self.value = selection
self.mxd = arcpy.mapping.MapDocument('current')
self.df = arcpy.mapping.ListDataFrames(self.mxd)[0]
# looking for the selected ID or code in the attributetable
if len(selection) > 8:
fcsites2 = [fc_biotop_pol, fc_biotop_poi, fc_biotop_li, fc_pro1_pol, fc_pro1_poi, fc_pro1_li]
for x in fcsites2:
arcpy.SelectLayerByAttribute_management(x, "NEW_SELECTION", fields_site + "='" + selection + "'")
else:
fcakode2 = [fc_pro1_pol, fc_pro1_poi, fc_pro1_li]
for x in fcakode2:
arcpy.SelectLayerByAttribute_management(x, "NEW_SELECTION", akode + "='" + selection + "'")
# zoom to Selection
self.df.zoomToSelectedFeatures()
self.df.scale = 15000
pass
Hello,
finally I found a solution. You have to install the comtypes modul to integrate ArcObjects methods and with this you can unselect the layer:
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