2 Replies Latest reply on Apr 2, 2013 12:47 PM by johnmdye

    using a python script to create a custom tool


      My assignment is to create a custom tool on the tool bar using a python script.

      All I want to do for now is add a previously created script, set the parameters, and get it to run.

      - Can I do this within ArcMap?
      - Where can I find a script to practice on?

        • Re: using a python script to create a custom tool

          My assignment is to create a custom tool on the tool bar using a python script.

          Do you need to create an add-in? Here's the help on that:

          Help 10.1: Creating an AddIn project
          • Re: using a python script to create a custom tool
            probably the simplest thing to get you started is to just create a Python Addin with a single button on a toolbar that buffers a layer in the TOC.

            You'll need to:

            1. Download the Python Addin Wizard.

            2. Start the Python Addin Wizard and Create a Toolbar with a Single Button on it.

            3. Save the Addin and then hit the Open Folder Button.

            4. Go into the Install Folder and script contained in that folder.

            For a button, you don't need any of the functions except for the OnClick function.
            import arcpy
            import pythonaddins
            class SuperBuffer(object):
                """Implementation for LYR.THM_WkrsSqMi (Button)"""
                def __init__(self):
                    self.enabled = True
                    self.checked = False
                def onClick(self):
                    #Instantiate 'mxd' and 'df' as the Map Document and DataFrame variables
                    mxd = arcpy.mapping.MapDocument("Current")
                    print "'mxd' instantiated as Map Document variable."
                    df = arcpy.mapping.ListDataFrames(mxd)
                    print "'df' instantiated as DataFrame variable."
                    #Create a Variable for the Layer you wish to Buffer. Later, you can get this dynamically
                    #by using the 'arcpy.mapping.ListLayers' function to populate a Combobox with the layers
                    #in your TOC
                    layer = arcpy.mapping.ListLayers(mxd, "Layer you want to Buffer", df)
                    print "'layer' variable instantiated."
                    #Create a Variable to hold your Output FC so that you can add it to the Map after it
                    #is created by the buffer tool.
                    out_fc ="Layer_Buffer"
                    print "'out_fc' variable instantiated."
                    #Establish your Workspace
                    workspace = arcpy.env.workspace = "C:\Temp\MyFileGeodatabase.gdb"
                    print "Workspace set."
                    #Run the Buffer Analysis on your Layer. You could also specify the distance and unit
                    #of measurement dynamically using Comboboxes for Each and use the 'selection' for
                    #those Comboboxes as the input.
                    print "Buffering Layer..."
                    arcpy.Buffer_analysis(layer, workspace + "\"" + out_fc, "2 Miles", "FULL", "ROUND", "ALL")
                    print "Buffer Operation Complete."
                    #Add the newly created Buffer Layer to your MXD
                    print "Adding Buffer to Map Document"
                    arcpy.mapping.AddLayer(df, out_fc, "AUTO_ARRANGE")
                    print "Script Complete"

            1. Save the Script and back out of the Install Folder.

            2. Run the makeaddin.py script to compile the addin and generate the ESRI Addin file.

            3. Double click on the ESRI Addin File to install it.

            4. Open up the Python Window so you can see the Print Statements as the script progresses.

            5. Modify the script as neccesary.

            Disclaimer: I just wrote this out of my head, I didn't test or compile it so you might need to do some tweaking to get it to work, but I think it should run 'out-of-the-box'.