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.

3

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.
    }
}
3
  • 1
    Also note that this is not python specific. It can be used to run any external command. Oct 4, 2011 at 22:26
  • But did it work, @Tanner?
    – Richard
    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.
    – Tanner
    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.

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.
    – Tanner
    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...

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.