Select to view content in your preferred language

Arcmap Python Add-in extention will not run on open

1370
3
10-13-2016 11:12 AM
RobWilkes
Emerging Contributor

ArcMap 10.4, Windows 7, Python 2.7.10

I am currently working on a python add-in extension that will join a feature class layer with a feature service layer whenever the mxd is opened. When I take the script I have and run it in the python window it joins the two layer perfectly. When I make it into an add-in extension it fails to execute. I have run a test with a text file and it is running the script when the mxd is opened but not the part I need. I tried adding in a timer so it would execute after the mxd is fully up and running but still nothing. I have made this into a toolbar and you can execute when you press the button but this is not ideal. This is being made for people who use arc map to view our database but have basic knowledge and may forget or mess things up.

import arcpy
import pythonaddins

class JoinRoads(object):
       """Implementation for RoadsScript_addin.extension2 (Extension)"""
       def openDocument (self):
             def __init__(self):
                   self.enabled = True
       def startup(self):
                   pass
             # Local variables:
Roads_2015 = "Roads 2015"
Roads_2015__2_ = Roads_2015
All_roads = "private\\transportation_FA_wa\\All roads"

time.sleep(30)
             # Process: Add Join
arcpy.AddJoin_management(Roads_2015, "BRUCE_ID", All_roads, "BRUCE_ID", "KEEP_ALL")

 

0 Kudos
3 Replies
RebeccaStrauch__GISP
MVP Emeritus

I'm not near a machine I can test anything on right now, but to give you some things to think about..

I'm assuming you have looked thru the help docs, for example Add Join—Help | ArcGIS for Desktop 

Are you getting any errors? are you sure that it isn't running? Or is it maybe running but not keeping the results or returning the results you expect? I'm wondering if the 'persists thru the session' ends when the script finishes?

Or more likely, it is maybe not seeing your variables as feature layers and therefore is not sure what to do.  Try creating feature layers and seeing if that helps (see the help doc for samples).

LukeWebb
Frequent Contributor

Hi there!

The "OpenDocument" trigger, does not call an init, your code should probably look like this.

import arcpy
import pythonaddins

class JoinRoads(object):
       """Implementation for RoadsScript_addin.extension2 (Extension)"""
       def openDocument (self):    
            Roads_2015 = "Roads 2015"
            Roads_2015__2_ = Roads_2015
            All_roads = "private\\transportation_FA_wa\\All roads"
            
            # Process: Add Join
            arcpy.AddJoin_management(Roads_2015, "BRUCE_ID", All_roads, "BRUCE_ID", "KEEP_ALL")