Python Addin - Add Layer While in Layout View

4636
10
04-26-2016 12:39 AM
TrilliumLevine1
Occasional Contributor

Hello All,

I've developed a python addin toolbar that adds a desired layer file (chosen from 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 sollution to this issue.  Anyone have any ideas?  Feedback is greatly appreciated!

Using: ArcGIS 10.2.2, Python 2.7

Sample code:

# -*- coding: utf-8 -*-
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'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

EDIT: I was able to get this to work in both views with the following code:

class FD_Bau_Fischaufstiege(object):
    """Implementation for Themenmanager_addin.button (Button)"""
    def __init__(self):
        self.enabled = True
##        self.checked = False
    def onClick(self):
        mxd = arcpy.mapping.MapDocument("CURRENT")
        df = 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("Die Datei ist nicht verfugbar.\nBitte kontaktieren Sie die GIS Abteilung.", "Datei nicht verfugbar", 0)
        pass

0 Kudos
10 Replies
WesMiller
Regular Contributor III

Have you also tried switching the views?

arcgis 10.0 - Changing from layout view to data view using ArcPy? - Geographic Information Systems S...

>>> mxd = arcpy.mapping.MapDocument('CURRENT')

>>> mxd.activeView = "Layers"

>>> mxd.activeView = "PAGE_LAYOUT"

0 Kudos