+ Reply to Thread
Results 1 to 3 of 3

Thread: using a python script to create a custom tool

  1. #1
    David Schmidt
    Join Date
    Mar 2013
    Posts
    4
    Points
    0
    Answers Provided
    0


    0

    Default using a python script to create a custom tool

    Greetings,

    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?

    Thanks

  2. #2
    Curtis Price

    Join Date
    Oct 2009
    Posts
    1,798
    Points
    874
    Answers Provided
    127


    0

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

    Quote Originally Posted by Mr_Maps View Post
    Greetings,

    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
    http://resources.arcgis.com/en/help/...00000022000000

  3. #3
    John Dye
    Join Date
    Dec 2010
    Posts
    135
    Points
    30
    Answers Provided
    8


    0

    Post 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.
    Code:
    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'.

+ Reply to Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts