2

I've developed a python addin toolbar that adds a desired layer file (chosen from a button nested in a menu in the toolbar) to the table of contents in a project. The functionality only works, however, when the project is in the data frame view -- clicking a layer in the menu does nothing if the project is in the layout view. I've googled around, but haven't found a solution to this issue. Anyone have any ideas?

Using: ArcGIS 10.2.2, Python 2.7

Working sample code to add layer in Data Frame View:

import arcpy
import pythonaddins

class FD_Bau_Fischaufstiege(object):
    """Implementation for Themenmanager_addin.button (Button)"""
    def __init__(self):
        self.enabled = True
##        self.checked = False
        self.mxd = arcpy.mapping.MapDocument('current')
    def onClick(self):
##        layer = r'\\msds.wv.de\dfsroot\HV\GROUP\Gis\DATEN\wvdat\Querbauwerke\Fischaufstieg.lyr'
        layer = r'G:\Gis\DATEN\wvdat\Querbauwerke\Fischaufstieg.lyr'
        activeDataFrame = self.mxd.activeView
        df = arcpy.mapping.ListDataFrames(self.mxd, activeDataFrame)[0]
        if arcpy.Exists(layer):
            layerToAdd = arcpy.mapping.Layer(layer)
            arcpy.mapping.AddLayer(df, layerToAdd, 'TOP')
        else:
            warningButton = pythonaddins.MessageBox("Die Datei ist nicht verfugbar.\nBitte kontaktieren Sie die GIS Abteilung.", "Datei nicht verfugbar", 0)
        pass

I have tried the following, but it still won't add the layer when I'm in the Layout View:

import arcpy
import pythonaddins

class ButtonClass1(object):
    """Implementation for TEST_addin.button (Button)"""
    def __init__(self):
        self.enabled = True
##        self.checked = False
        self.mxd = arcpy.mapping.MapDocument('current')
    def onClick(self):
##        layer = r'\\msds.wv.de\dfsroot\HV\GROUP\Gis\DATEN\wvdat\Querbauwerke\Fischaufstieg.lyr'
        layer = r'G:\Gis\DATEN\wvdat\Querbauwerke\Fischaufstieg.lyr'
        activeDataFrame = self.mxd.activeView
        df = arcpy.mapping.ListDataFrames(self.mxd, activeDataFrame)[0]
        if activeDataFrame == 'PAGE_LAYOUT':
            if arcpy.Exists(layer):
                layerToAdd = arcpy.mapping.Layer(layer)
                arcpy.mapping.AddLayer(df, layerToAdd, 'TOP')
            else:
                warningButton = pythonaddins.MessageBox("Die Datei ist nicht verfugbar.\nBitte kontaktieren Sie die GIS Abteilung.", "Datei nicht verfugbar", 0)
        else:
            if arcpy.Exists(layer):
                layerToAdd = arcpy.mapping.Layer(layer)
                arcpy.mapping.AddLayer(df, layerToAdd, 'TOP')
            else:
                warningButton = pythonaddins.MessageBox("Die Datei ist nicht verfugbar.\nBitte kontaktieren Sie die GIS Abteilung.", "Datei nicht verfugbar", 0)
        pass
1

You can simplify your code somewhat by using:

df = self.mxd.activeDataFrame
layer = r'G:\Gis\DATEN\wvdat\Querbauwerke\Fischaufstieg.lyr'
if arcpy.Exists(layer):
    layerToAdd = arcpy.mapping.Layer(layer)
    arcpy.mapping.AddLayer(df, layerToAdd, 'TOP')
else:
    warningButton = pythonaddins.MessageBox("warning")

You might also want to set the mxd variable in the OnClick function rather than in init. I can't say for certain, but I know in some cases ArcGIS creates several instances of a python class before it is used.

  • thanks for your feedback, but this doesn't address the issue. – user151631 Apr 27 '16 at 7:16
  • Can you provide some detail about what you did and why it doesn't work? – dslamb Apr 27 '16 at 13:03
  • The OP provides the code I've tried. I also tried setting the mxd variable in the OnClick, but that didn't change anything. In the layout view, nothing happens when I click on the button to add the layer, not even any output in the python window. – user151631 Apr 27 '16 at 13:40
  • What happens if you try the code I provide, using df = self.mxd.activeDataFrame? Still nothing? Sometimes if the tool doesn't do anything you have an error somewhere in the formatting (missing an indentation for example). – dslamb Apr 27 '16 at 13:59
  • 1
    Try this instead. mxd = arcpy.mapping.MapDocument("CURRENT"') then df = mxd.activeDataFrame. The error refers to using self.mxd which was based on the code in the OP. If you've moved the mxd variable into your OnClick function then you don't need self anymore. – dslamb Apr 27 '16 at 15:24

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.