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
PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Apr 26, 2016 at 9:23

1 Answer 1

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.

answered Apr 26, 2016 at 14:58
7
  • thanks for your feedback, but this doesn't address the issue. Commented Apr 27, 2016 at 7:16
  • Can you provide some detail about what you did and why it doesn't work? Commented Apr 27, 2016 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. Commented Apr 27, 2016 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). Commented Apr 27, 2016 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. Commented Apr 27, 2016 at 15:24

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.