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.
Answer
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.
}
}
No comments:
Post a Comment