1

I checked Passing Selection from Combo Box into Definition Query using Python AddIn?, but I am still stuck on code/syntax.

I created a simple add-in with a button that clips and exports tax parcels within a selected feature from the a grid. However, I want to include a combo box to prompt the user to enter or select the grid number rather than use the mouse to select from the data frame. It should accept user input in a drop-down bar listing the features for the GRID field in the grid layer and pass it as a definition query into 'select by attributes'. I'm still new to Python and am stuck on the selection process.

Script listed below:

import arcpy
import pythonaddins

log = file('//geodata/gisdata/Workspace/GIS/Rachel/Python_AddIn/Taddin4/Install/AddIn.log', 'a')
mxd = arcpy.mapping.MapDocument('current')
clipLayer = arcpy.mapping.Layer('Tax Map Small Grid')
gridCursor = arcpy.SearchCursor(clipLayer)
gridNums = []
for row in gridCursor:
    gridNums.append(row.GRID)

class Taddin4Box(object):
    """Implementation for Taddin4_addin.combobox (ComboBox)"""
    def __init__(self):
        self.items = sorted(gridNums)
        self.editable = True
        self.enabled = True
        self.dropdownWidth = 'WWWWWW'
        self.width = 'WWWWWW'
    def onSelChange(self, selection):
        clipLayer.definitionQuery = '"GRID"' + "=" + "'" + selection + "'"
        print >>log, 'About to select...'
        N = arcpy.SelectLayerByAttribute_management(clipLayer, "NEW_SELECTION", clipLayer.definitionQuery)
        print >>log, 'Selection completed...'
        return N
        arcpy.RefreshActiveView()
        pass
    def onEditChange(self, text):
        pass
    def onFocus(self, focused):
        pass
    def onEnter(self):
        pass
    def refresh(self):
        pass

class Taddin4button(object):
    """Implementation for Taddin4_addin.button (Button)"""
    def __init__(self):
        self.enabled = True
        self.checked = False
    def onClick(self):
        outpath = '//geodata/gisdata/Workspace/GIS/Rachel/Python_AddIn/Taddin4/Install/CAD/Shapefiles/'
        CADpath = '//geodata/gisdata/Workspace/GIS/Rachel/Python_AddIn/Taddin4/Install/CAD/'
        parcelsLayer = arcpy.mapping.Layer('Tax Parcels City')
        for row in gridCursor:
            gridField = row.getValue("GRID")
            num = gridField.replace('-','')
            print >>log, 'GRID number is: ' + gridField
        clipFC = 'TaxGrid'+num
        arcpy.Clip_analysis(parcelsLayer, clipLayer, outpath + clipFC)
        arcpy.ExportCAD_conversion(clipFC, "DWG_R2013", CADpath+gridField+'.dwg', "IGNORE_FILENAMES_IN_TABLES", "", "")
        print >>log, 'Done.'
        arcpy.SelectLayerByAttribute_management(clipLayer, "CLEAR_SELECTION")
        log.close()
        pass
1

The definition query worked once I changed it to this:

"GRID = '" + selection + "'"

instead of this: '"GRID"' + "=" + "'" + selection + "'"

Also, I removed the print statements from the ComboBox onSelChange function and opened the log file within the Button onClick function (instead of at the top of the script). I also re-defined the mxd, clipLayer, and gridCursor variables again within the Button onClick function (copied and pasted those lines).

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.