10

I have a script that I want to run within ArcMap when a button in a toolbar is clicked, and so far I have only been able to make the script into a script tool. I need it to run as a command, not as a geoprocessing tool.

Running it as a geoprocessing tool takes much longer than when the same code is executed from the command line window. I have just started looking into ArcObjects, but I want to get started using it if it is what I'll need to use.

If anyone has any sample code, or resources for creating a button, that would be great.

PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Sep 3, 2010 at 7:58
3
  • Are you trying to do this in ArcMap or in a ArcGIS Engine standalone application? Commented Sep 3, 2010 at 12:59
  • I want to do this in ArcMap 10. Commented Sep 3, 2010 at 16:51
  • 1
    the 9.3 version of this question is Arcmap: attach python script to button Commented Oct 4, 2011 at 22:12

3 Answers 3

8

If you don't need any input or outputparameters, this sample should be possible to use to run a script in a custom command Leveraging ArcPy in a .NET application, C# example:

// Executes a shell command synchronously.
// Example of command parameter value is
// "python " + @"C:\scripts\geom_input.py".
//
public static void ExecuteCommand(object command)
{
 try
 {
 // Create the ProcessStartInfo using "cmd" as the program to be run,
 // and "/c " as the parameters.
 // "/c" tells cmd that you want it to execute the command that follows,
 // then exit.
 System.Diagnostics.ProcessStartInfo procStartInfo = new
 System.Diagnostics.ProcessStartInfo("cmd", "/c " + command);
 // The following commands are needed to redirect the standard output.
 // This means that it will be redirected to the Process.StandardOutput StreamReader.
 procStartInfo.RedirectStandardOutput = true;
 procStartInfo.UseShellExecute = false;
 // Do not create the black window.
 procStartInfo.CreateNoWindow = true;
 // Now you create a process, assign its ProcessStartInfo, and start it.
 System.Diagnostics.Process proc = new System.Diagnostics.Process();
 proc.StartInfo = procStartInfo;
 proc.Start();
 // Get the output into a string.
 string result = proc.StandardOutput.ReadToEnd();
 // Display the command output.
 Console.WriteLine(result);
 }
 catch (Exception objException)
 {
 Console.WriteLine(objException.Message);
 // Log the exception and errors.
 }
}
matt wilkie
28.3k35 gold badges150 silver badges286 bronze badges
answered Sep 4, 2010 at 9:26
3
  • 1
    Also note that this is not python specific. It can be used to run any external command. Commented Oct 4, 2011 at 22:26
  • But did it work, @Tanner? Commented Apr 19, 2012 at 17:54
  • To be honest I never got it to work, but back then my only programming experience was some limited Python. I feel like I could give it a fair shot now if I had the time and still needed to make a button. Commented Apr 20, 2012 at 21:40
5

Use [Add Tools...] in Toolbar / Customize to add the script to a category. Then pull the script to the toolbar of choice.

answered Sep 3, 2010 at 9:28
1
  • That's what I did. Using that method, the script runs as a script tool and brings up a geoprocessing window, making the script take much longer to execute. Commented Sep 3, 2010 at 19:03
3

Right click a blank area next to your toolbars> open the customize window> click on the commands tab> scroll to the bottom of the list and click [UI Control]> Select new UI Control> select the type of control that you want and then click Create and Edit. This will open the VBA editor for the new control and you can write your code inside hear and define the event that calls the code. VBA is included with 10 for free but you will need to request a license file for it and then register that license. Call ESRI for this and they should give you a license free of charge. After 10 this will go away...

answered Feb 9, 2011 at 1:03

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.