This content has been marked as final. Show 2 replies
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
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:
- Download the Python Addin Wizard.
- Start the Python Addin Wizard and Create a Toolbar with a Single Button on it.
- Save the Addin and then hit the Open Folder Button.
- 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"
- Save the Script and back out of the Install Folder.
- Run the makeaddin.py script to compile the addin and generate the ESRI Addin file.
- Double click on the ESRI Addin File to install it.
- Open up the Python Window so you can see the Print Statements as the script progresses.
- 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'.