Select to view content in your preferred language

Referencing specific layers in my add-in

909
2
Jump to solution
10-17-2013 12:17 PM
JamesMcBroom1
Emerging Contributor
Hello, I am writing an add-in to work with many different MXDs that I have to work with, they all use the same naming convention for the layers. Currently my addin is structured like this:

import x, y, z  mxd = arcpy.mapping.MapDocument("current") df = arcpy.mapping.ListDataFrames(mxd)[0] layerList = arcpy.mapping.ListLayers(mxd) tableList = arcpy.mapping.ListTableViews(mxd) for layer in layerList:     if "Footprints_Edit" in layer.name:        footprints_fc = layer     if "Parcels_Edit" in layer.name:        parcels_fc = layer     etc..     etc..  class Button(object)     ... class Button2(object)     ...


This works fine, except, I'd like to be able to pass the add-in around to other people. Now, because the assigning-variables logic happens at the top of the add-in, if the add-in is present in the toolbar before the .mxd is loaded, it won't find the expected layers and using the tools will throw an error: the only way to get it to work is to remove the add-in, close ArcMap, re-open, and load the .mxd. This is a pain. Now, I tried putting the assign-variables logic as the first step in each button's onClick method, but that causes all sorts of slowness for what should be quick operations.

Is there some better way to look for and assign these layers that I am missing? I tried throwing everything in a function and then adding this to onClick:

if not footprints_fc:     assignLayers()


but that doesn't seem to work. Any thoughts? Thanks in advance.
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
JasonScheirer
Esri Alum
Since those layers can change from map to map, why not use a function instead of global variables?

def get_layers():     mxd = arcpy.mapping.MapDocument("current")     df = arcpy.mapping.ListDataFrames(mxd)[0]     layers = {         'footprints_fc': None,         'parcels_fc': None     }     for layer in arcpy.mapping.ListLayers(mxd):         if "Footprints_Edit" in layer.name:            layers['footprints_fc'] = layer         elif "Parcels_Edit" in layer.name:            layers['parcels_fc'] = layer         # ad nauseam     return layers


Then in each onclick, you'd do something like this:

layers = get_layers footprint_layer = layers['footprints_fc'] parcel_layer = layers['parcels_fc']


and it'd always be up-to-date.

View solution in original post

0 Kudos
2 Replies
JasonScheirer
Esri Alum
Since those layers can change from map to map, why not use a function instead of global variables?

def get_layers():     mxd = arcpy.mapping.MapDocument("current")     df = arcpy.mapping.ListDataFrames(mxd)[0]     layers = {         'footprints_fc': None,         'parcels_fc': None     }     for layer in arcpy.mapping.ListLayers(mxd):         if "Footprints_Edit" in layer.name:            layers['footprints_fc'] = layer         elif "Parcels_Edit" in layer.name:            layers['parcels_fc'] = layer         # ad nauseam     return layers


Then in each onclick, you'd do something like this:

layers = get_layers footprint_layer = layers['footprints_fc'] parcel_layer = layers['parcels_fc']


and it'd always be up-to-date.
0 Kudos
JamesMcBroom1
Emerging Contributor
Thanks, I implemented your changes and everything works beautifully.
0 Kudos