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.
-
Are you trying to do this in ArcMap or in a ArcGIS Engine standalone application?MathiasWestin– MathiasWestin2010年09月03日 12:59:39 +00:00Commented Sep 3, 2010 at 12:59
-
I want to do this in ArcMap 10.Tanner– Tanner2010年09月03日 16:51:57 +00:00Commented Sep 3, 2010 at 16:51
-
1the 9.3 version of this question is Arcmap: attach python script to buttonmatt wilkie– matt wilkie2011年10月04日 22:12:36 +00:00Commented Oct 4, 2011 at 22:12
3 Answers 3
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.
}
}
-
1Also note that this is not python specific. It can be used to run any external command.matt wilkie– matt wilkie2011年10月04日 22:26:01 +00:00Commented Oct 4, 2011 at 22:26
-
But did it work, @Tanner?Richard– Richard2012年04月19日 17:54:32 +00:00Commented 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– Tanner2012年04月20日 21:40:02 +00:00Commented Apr 20, 2012 at 21:40
Use [Add Tools...] in Toolbar / Customize to add the script to a category. Then pull the script to the toolbar of choice.
-
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– Tanner2010年09月03日 19:03:38 +00:00Commented Sep 3, 2010 at 19:03
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...
Explore related questions
See similar questions with these tags.