Select to view content in your preferred language

Add-in Button Help - Failed to open tool [toolname] (mytoolbox.tbx)

2940
8
10-25-2016 02:08 AM
davidround
Emerging Contributor

Hi,

I'm having trouble adding a button using add-ins for a script tool that I made in ArcMap 10.4. I followed the instructions to the letter but I'm getting the error message "Failed to open tool [toolname] (mytoolbox.tbx). I don't understand why. I guess it's found the toolbox but won't run the tool. The code itself is very simple. It's just a reconstruction of the select by location tool to enable Geodesic selections (which really should be standard!) and the tool runs absolutely fine when I run it from the toolbox. The code to run the addin is a simple GPToolDialog call.

Here's the code for the add-in:

import arcpy
import pythonaddins

class ButtonClass1(object):
      """Implementation for SelectByLocation_addin.button (Button)"""
      def __init__(self):
            self.enabled = True
            self.checked = False
      def onClick(self):
            pythonaddins.GPToolDialog(r"C:\AddIns\SelectByLocation\Install\mytoolbox.tbx", "SelectByLocation")

I have double, triple and quadruple checked the spelling, path name and locations of all the relevant files. Just for your info here's the code for the python tool itself but like I say there;'s no issue running it from the toolbox:

import arcpy


inputfeats=str(arcpy.GetParameter(0))
targetlay=str(arcpy.GetParameter(1))
selectmethod=arcpy.GetParameterAsText(2)
distance=arcpy.GetParameterAsText(3)
distancetype=arcpy.GetParameterAsText(4)
distancemerge=distance+" "+distancetype
selectiontype=arcpy.GetParameterAsText(5)
inverstsel=arcpy.GetParameterAsText(6)

arcpy.SelectLayerByLocation_management(arcpy.GetParameterAsText(0),overlap_type=selectmethod,select_features=targetlay,search_distance=distancemerge,selection_type=selectiontype,invert_spatial_relationship=inverstsel)

If anyone can see what's wrong here I would very much appreciate any help with this.

Apologies for the formatting by the way. This is my first post and I can't figure out how to do the script boxes.Time to bang my head against a brick wall!

0 Kudos
8 Replies
BlakeTerhune
MVP Regular Contributor
davidround
Emerging Contributor

Thanks for this. 

0 Kudos
LukeWebb
Frequent Contributor

Make sure the tool is called "SelectByLocation" when you set up the toolbox in Arc through the GUI, that might be the label or description instead!

Im not sure, but you could try open arcmap, then open the python window, then drag your tool from the toolbox into the window, it should maybe tell you what its name is there.

0 Kudos
davidround
Emerging Contributor

Thanks. Unfortunately this just gives me >>>SelectByLocation(

You may have helped my indirectly though. When I ran the tool afterwards with the python window open it said ButtonClass1 object is not callable. I'll go back and check my class definitions to see if this is the problem.

0 Kudos
davidround
Emerging Contributor

It all looks good to me. Maybe someone can spot the issue in the config file here. I know the builder is notoriously bad at writing this file.

<ESRI.Configuration xmlns="http://schemas.esri.com/Desktop/AddIns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><Name>Select by Location</Name><AddInID>{e002960a-47b7-48cd-a903-93e51be400f7}</AddInID><Description>New Addin</Description><Version>0.1</Version><Image>Images\AddInDesktop32.png</Image><Author>Untitled</Author><Company>Untitled</Company><Date>10/24/2016</Date><Targets><Target name="Desktop" version="10.4" /></Targets><AddIn language="PYTHON" library="SelectbyLocation_addin.py" namespace="SelectbyLocation_addin"><ArcMap>
 <Commands>
 <Button caption="Select by Location" category="Select by Location" class="ButtonClass1" id="SelectbyLocation_addin.button" image="Images\SelectionSelectByLocation16.png" message="Select by Location" tip="Select by Location"><Help heading="Select by Location">Select by Location</Help></Button>
 </Commands>
 <Extensions>
 </Extensions>
 <Toolbars>
 <Toolbar caption="select by location" category="Select by Location" id="SelectbyLocation_addin.toolbar" showInitially="true"><Items><Button refID="SelectbyLocation_addin.button" /></Items></Toolbar>
 </Toolbars>
 <Menus>
 </Menus>
 </ArcMap></AddIn></ESRI.Configuration>
0 Kudos
LukeWebb
Frequent Contributor

The creators pretty bad but I have a workaround that works for me every time:

1) Take a copy of my "Addin.py" file with the UI (button) code in.

2) Delete the config.xml

3) Resave the addin using the wizard. (change a text box and then back if the save button is greyed out.)

4) Delete the new Addin.py file, and put mine back.

I have no idea if this is it, but in In my config.xml files, in the 'toolbar' line of code, the refID matches the Class in the buttonas, in yours the Toolbar simply references 'button' not 'ButtonClass1'

 <Button caption="Select by Location" category="Select by Location" class="ButtonClass1" id="SelectbyLocation_addin.button" image="Images\SelectionSelectByLocation16.png" message="Select by Location" tip="Select by Location"><Help heading="Select by Location">Select by Location</Help></Button>
Button refID="SelectbyLocation_addin.button"  
# --->  Change to
Button refID="SelectbyLocation_addin.ButtonClass1"
in this line of code:

 <Toolbar caption="select by location" category="Select by Location" id="SelectbyLocation_addin.toolbar" showInitially="true"><Items><Button refID="SelectbyLocation_addin.button" /></Items></Toolbar>
0 Kudos
davidround
Emerging Contributor

Hi Luke,

Just to let you know I'm not ignoring you. My times just been swallowed up with another project. I will test this out soon hopefully!

0 Kudos
RebeccaStrauch__GISP
MVP Emeritus

I'm using 10.3.1 (originally developed on 10.2.x), but I found that setting a relative path variable and using that helps.  Some notes I have Tip: Python Addin - getting custom tools/toolbox to work - GPToolDialog 

I setup my scripts to be in a "scripts" folder (case sensitive) under my Install folder for the toolbox.  There is a better explanation on that link, but here is a sample of how I use it...

import os
import arcpy
import pythonaddins
relPath = os.path.dirname(__file__)


class btnCopySDE(object):
    """Implementation for CopySDE.button (Button)"""
    def __init__(self):
        self.enabled = True
        self.checked = False
    def onClick(self):
        toolPath = relPath + r"\UpdateTools.tbx"
        pythonaddins.GPToolDialog(toolPath, "01-CopyDWCMasterSDEtoFGDB")

class btnUpdateDomains(object):
    """Implementation for UpdateDomains.button (Button)"""
    def __init__(self):
        self.enabled = True
        self.checked = False
    def onClick(self):
        toolPath = relPath + r"\UpdateTools.tbx"
        pythonaddins.GPToolDialog(toolPath, "02-CreateDWCDomainsForFGDB")

I could probably have the toolPath separated out...but it works so I'm not messing with it these days.

See if that helps.

0 Kudos