Managing the state of Python add-in types

Communication and coordination between add-in types

In some cases, a means for communicating between add-in types is needed so that one customization can activate or obtain the state from another. For example, a particular add-in button, when clicked, may alter the add-in state of a tool from disabled to enabled.

The following Python code demonstrates how add-in communication is managed. In the onClick() function of the button, the tool ID (Variable Name) is used to change its state from disabled to enabled. You do not need to specify the namespace prefix as part of the ID.

class ButtonClass(object):
    def __init__(self):
        self.enabled = True
        self.checked = False
    def onClick(self):
        # tool1 is the tool ID
        tool1.enabled = True

The ID (Variable Name) of the tool is specified during the creation of the add-in type using the Python Add-In Wizard and stored in the config.xml file. The ID is prefixed by the namespace of the add-in. This is done to prevent name conflicts between add-ins. However, you do not need to prefix the namespace with the ID when using it inside the script. See the example below.

In most circumstances, the state of the add-in type is managed by the add-in's extension where it can be updated and stored in response to various application events. For example, you may only want to enable a tool or set of tools when a map document is in data view.

The following Python code demonstrates how an extension class is used to disable a tool when the active view is changed to the layout view. Do not include the namespace prefix.

class AVExtension(object):

    def activeViewChanged(self):
        mxd = arcpy.mapping.MapDocument('current')
        active_view = mxd.activeView
        # tool1 is the tool ID (without the namespace prefix)
        if active_view == 'PAGE_LAYOUT':
            tool1.enabled = False
        else:
            tool1.enabled = True
        arcpy.RefreshActiveView()
        return

Enabled and checked properties

Add-in types--buttons, tools, and combo boxes--are periodically notified to update their enabled and checked state. In Python, add-in types contain Boolean properties for enabled and checked. For example, you can set the add-in to be enabled or disabled and checked or unchecked during its initialization.

The following Python code demonstrates how to set the enabled or checked state of an add-in type.

class MyButton(object):
  def __init__(self):
    self.checked = False
    self.enabled = True

The checked property is only available for button types. The enabled property is available for buttons, tools, and combo boxes.

10/29/2012