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
MapDocument—Help | ArcGIS for Desktop and its
activeView (Read and Write) property is the only way to set/get a layout as the active doc that I can see
I also tried this, but it doesn't work either:
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'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
You could add an extension to your tool that you could make the tool inactive in layout view.
Creating a Python add-in application extension—Help | ArcGIS for Desktop
there was a recommendation here about where to associate the mxd variable arcpy - Arcgis 10.2.2 Python Addin - Add Layer to Layout View? - Geographic Information Systems Stac... but it went unanswered as well
yeah, I posted this on stack overflow as well.
disabling the toolbar in layout view is plan b, but that seems like it should be unnecessary...after all, you can add data with the 'Add Data' button while in layout view, so why wouldn't a custom toolbar with the same functionality work? seems like an oversight on ESRI's part.
just tried the extension to disable the toolbar in layout view, and that doesn't work either.
Code:
import arcpy import pythonaddins class FishButton(object): """Implementation for WTF_addin.fish_button (Button)""" def __init__(self): self.enabled = True ## self.checked = False def onClick(self): self.mxd = arcpy.mapping.MapDocument('current') ## 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 class changeToolState(object): """Implementation for WTF_addin.disable (Extension)""" def __init__(self): # For performance considerations, please remove all unused methods in this class. self.enabled = True def activeViewChanged(self): mxd = arcpy.mapping.MapDocument('current') active_view = mxd.activeView if active_view == 'PAGE_LAYOUT': WTF_toolbar.enabled = False print 'button has been disabled...' else: WTF_toolbar.enabled = True print 'button re-enabled...' arcpy.RefreshActiveView() return
And the corresponding config.xml:
<ESRI.Configuration xmlns="http://schemas.esri.com/Desktop/AddIns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><Name>WTF</Name><AddInID>{8ed28a7b-5aa3-4474-baa5-7674acbb350a}</AddInID><Description>New Addin</Description><Version>0.1</Version><Image /><Author>Untitled</Author><Company>Untitled</Company><Date>04/27/2016</Date><Targets><Target name="Desktop" version="10.2" /></Targets><AddIn language="PYTHON" library="WTF_addin.py" namespace="WTF_addin"><ArcMap> <Commands> <Button caption="Fish" category="WTF" class="FishButton" id="WTF_addin.fish_button" image="" message="" tip=""><Help heading="" /></Button> </Commands> <Extensions> <Extension autoLoad="true" category="WTF" class="changeToolState" description="" id="WTF_addin.disable" name="Disable" productName="Disable" showInExtensionDialog="true" /> </Extensions> <Toolbars> <Toolbar caption="WTF_Toolbar" category="WTF" id="WTF_addin.WTF_toolbar" showInitially="true"><Items><Menu refID="WTF_addin.cat_menu" /></Items></Toolbar> </Toolbars> <Menus> <Menu caption="Category" category="WTF" id="WTF_addin.cat_menu" isRootMenu="false" isShortcutMenu="false" separator="false"><Items><Button refID="WTF_addin.fish_button" /></Items></Menu> </Menus> </ArcMap></AddIn></ESRI.Configuration>
You have to turn the extension on just like na or sa
Ah, did not know that, thanks. Now I'm getting an error in the python window:
So...where (and how) should I define WTF_toolbar?
Have you tried using disabling the menu instead of the toolbar?