DanEvans83

Pan/Zoom to selected features in Python Add-in

Discussion created by DanEvans83 on Mar 11, 2014
Latest reply on Mar 12, 2014 by DanEvans83
Hi,

I am trying to write a simple Python add-in that provides a combo box allowing the user to enter a project number. They can then click buttons to 'draw' (i.e. select all the pipe features in that project), pan to (select the pipes in the project, then pan to them, keeping the current scale) or zoom to (select the pipes in the project, then zoom to their extent). If the user enters a project number and presses enter, the default behaviour is to just 'draw'/select the pipes in the project.

The 'draw'/select functionality works fine, either by pressing enter or clicking the button. The pan and zoom to buttons don't pan or zoom though! They do the selection okay, but the view stays in the same extent and scale.

My code is below. Any help would be greatly appreciated!

Dan

import arcpy
import pythonaddins

class DrawProject(object):
    """Implementation for MRPAssistant_addin.panbutton (Button)"""
    def __init__(self):
        self.enabled = True
        self.checked = False
    def onClick(self):
        mxd = arcpy.mapping.MapDocument('CURRENT')
        df = arcpy.mapping.ListDataFrames(mxd)[0]    
        layer = arcpy.mapping.ListLayers(mxd, "DistributionMain", df)[0]
        arcpy.SelectLayerByAttribute_management(layer, "NEW_SELECTION", "[MRP_EZRESULTS.PROJECTID] = " + projectsearchcombobox.value)
        arcpy.RefreshActiveView()
    
class PanToProject(object):
    """Implementation for MRPAssistant_addin.panbutton (Button)"""
    def __init__(self):
        self.enabled = True
        self.checked = False
    def onClick(self):
        mxd = arcpy.mapping.MapDocument('CURRENT')
        df = arcpy.mapping.ListDataFrames(mxd)[0]
        layer = arcpy.mapping.ListLayers(mxd, "DistributionMain", df)[0]
        arcpy.SelectLayerByAttribute_management(layer, "NEW_SELECTION", "[MRP_EZRESULTS.PROJECTID] = " + projectsearchcombobox.value)
        df.panToExtent(layer.getSelectedExtent())
        arcpy.RefreshActiveView()

class ProjectSearchComboBox(object):
    """Implementation for MRPAssistant_addin.projectsearchcombobox (ComboBox)"""
    def __init__(self):
        self.items = [""]
        self.editable = True
        self.enabled = True
        self.dropdownWidth = 'WWWWWW'
        self.width = 'WWWWWW'
    def onSelChange(self, selection):
        pass
    def onEditChange(self, text):
        global project
        project = text
    def onFocus(self, focused):
        pass
    def onEnter(self):
        mxd = arcpy.mapping.MapDocument('CURRENT')
        df = arcpy.mapping.ListDataFrames(mxd)[0]
        layer = arcpy.mapping.ListLayers(mxd, "DistributionMain", df)[0]
        arcpy.SelectLayerByAttribute_management(layer, "NEW_SELECTION", "[MRP_EZRESULTS.PROJECTID] = " + project)
        arcpy.RefreshActiveView()
    def refresh(self):
        pass

class ZoomToProject(object):
    """Implementation for MRPAssistant_addin.zoombutton (Button)"""
    def __init__(self):
        self.enabled = True
        self.checked = False
    def onClick(self):
        mxd = arcpy.mapping.MapDocument('CURRENT')
        df = arcpy.mapping.ListDataFrames(mxd)[0]
        layer = arcpy.mapping.ListLayers(mxd, "DistributionMain", df)[0]
        arcpy.SelectLayerByAttribute_management(layer, "NEW_SELECTION", "[MRP_EZRESULTS.PROJECTID] = " + projectsearchcombobox.value)
        df.extent = layer.getSelectedExtent()
        arcpy.RefreshActiveView()

Outcomes