Embedding Python Tool in Add-In File

1022
3
12-15-2020 05:50 AM
Labels (5)
JasonLearned1
New Contributor

I've successfully created a toolbar using the Esri Add-In wizard. Each button references a different tool from a custom Python script toolbox I've developed.

I've made an iteration of the Add-In that references each script tool in the toolbox file, and it works perfect.

Now my question: can I embed the script from the tools into the Add-In script, instead of just referencing the *.tbx file? And by doing so, will this let me distribute the Add-In by itself without having to also distribute the toolbox for it to work? I've seen a few articles/posts about this, but they've left me confused as to how to do this. Thank you.

0 Kudos
3 Replies
ines-mahmoud
New Contributor III

hello am a student . please can you teach me how to add a button and to do like the great job you did . please

0 Kudos
JasonLearned1
New Contributor

The how-to guide can be found here:

https://desktop.arcgis.com/en/arcmap/latest/analyze/python-addins/creating-an-add-in-project.htm

You have to download the Python Add-In Wizard first, but it is easy and shouldn't require administrative rights on your computer.

I hope this helps. Best wishes!

0 Kudos
_Spruce_
New Contributor

I'm now learning how to make a button step by step, and I'm having a problem while testing it: the toolbar is loaded successfully, I can click on it but  can't zoom to the selected feature, could someone please explain why this is happening?

Thank you!

C:\Users\lenovo\Documents\ArcGIS\AddIns\python_addin_project_Ren

_Spruce__0-1663694193541.png

#config.xml
<?xml version="1.0"?>
-<ESRI.Configuration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.esri.com/Desktop/AddIns">
<Name>Python Addin Demonstration</Name>
<AddInID>{31e63974-b41b-4801-8000-007d0e28e03e}</AddInID>
<Description>带有工具和按钮的 add-in demonstration</Description>
<Version>0.1</Version>
<Image>Images\Bug.png</Image>
<Author>Sherry</Author>
<Company>Great Fir</Company>
<Date>09/21/2022</Date>
-<Targets>
<Target version="10.1" name="Desktop"/>
</Targets>
-<AddIn language="PYTHON" namespace="python_addin_project_Ren_addin" library="python_addin_project_Ren_addin.py">
-<ArcMap>
-<Commands>
-<Button tip="Zoom to Selected Features" message="Zoom to Selected Features" image="Images\Zoom_to_Seleted_Features.png" id="python_addin_project_Ren_addin.button" class="Zoom_to_Selected_Features" category="Python Addin Demonstration" caption="Zoom to Selected Features">
<Help heading="Zoom to Selected Features">Zoom to Selected Features</Help>
</Button>
</Commands>
<Extensions> </Extensions>
-<Toolbars>
-<Toolbar id="python_addin_project_Ren_addin.toolbar" category="Python Addin Demonstration" caption="Toolbar_1" showInitially="true">
-<Items>
<Button refID="python_addin_project_Ren_addin.button"/>
</Items>
</Toolbar>
</Toolbars>
<Menus> </Menus>
</ArcMap>
</AddIn>
</ESRI.Configuration>

 C:\Users\lenovo\Documents\ArcGIS\AddIns\Desktop10.1\{31e63974-b41b-4801-8000-007d0e28e03e}  -->   .esriaddin  path

#makeaddin.py
import os
import re
import zipfile

current_path = os.path.dirname(os.path.abspath(__file__))

out_zip_name = os.path.join(current_path, 
                            os.path.basename(current_path) + ".esriaddin")

BACKUP_FILE_PATTERN = re.compile(".*_addin_[0-9]+[.]py$", re.IGNORECASE)

def looks_like_a_backup(filename):
    return bool(BACKUP_FILE_PATTERN.match(filename))

with zipfile.ZipFile(out_zip_name, 'w', zipfile.ZIP_DEFLATED) as zip_file:
    for filename in ('config.xml', 'README.txt', 'makeaddin.py'):
        zip_file.write(os.path.join(current_path, filename), filename)
    dirs_to_add = ['Images', 'Install']
    for directory in dirs_to_add:
        for (path, dirs, files) in os.walk(os.path.join(current_path,
                                                        directory)):
            archive_path = os.path.relpath(path, current_path)
            found_file = False
            for file in (f for f in files if not looks_like_a_backup(f)):
                archive_file = os.path.join(archive_path, file)
                print(archive_file)
                zip_file.write(os.path.join(path, file), archive_file)
                found_file = True
            if not found_file:
                zip_file.writestr(os.path.join(archive_path,
                                               'placeholder.txt'),
                                  "(Empty directory)")

#Zoom_to_Seleted_Features.py
# Implementation of OnClick method of Button's class
def onClick(self):
        # Get the current map document and the first data frame.
        mxd = arcpy.mapping.MapDocument('current')
        df = arcpy.mapping.ListDataFrames(mxd)[0]
        # Call the zoomToSelectedFeatures() method of the data frame class
        df.zoomToSelectedFeatures()

#python_addin_project_Ren_addin.py
#C:\Users\lenovo\Documents\ArcGIS\AddIns\python_addin_project_Ren\Install
import arcpy
import pythonaddins

class Zoom_to_Selected_Features(object):
    """Implementation for python_addin_project_Ren_addin.button (Button)"""
    def __init__(self):
        self.enabled = True
        self.checked = False
    def onClick(self):
        pass

 

Tags (5)
0 Kudos