How to execute a geoprocessing tool using an add-in (ArcObjects .NET 10.6 SDK)
ArcObjects Help for .NET developers > Developing with ArcGIS > Learning ArcObjects > Using geoprocessing > Executing tools > How to execute a geoprocessing tool using an add-in (ArcObjects .NET 10.6 SDK)

How to execute a geoprocessing tool using an add-in


Summary
This topic shows how to execute a geoprocessing tool using a custom add-in button.

Executing a geoprocessing tool using an add-in

The process of executing a geoprocessing tool using an add-in is similar to executing a geoprocessing tool in any .NET project. The only difference is that the code is put inside the OnClick method. You can leverage more flexibility by embedding your models and tools in an add-in and executing them by clicking a button. For more information, see Opening a geoprocessing tool's dialog box in .NET.
To execute the Buffer tool using a custom add-in button, complete the following steps:
  1. Declare the Geoprocessor, Result, and IVariantArray objects as class members. You can declare them using the OnClick method.
  2. Instantiate the objects in the button's constructor and use them in a method. In this example, they're used with the OnClick method.
  3. Add the button to ArcMap and click the button to run the code. The geoprocessing message opens in a message box.
See the following code example:
[C#]
// Add references with the Using keyword.

public class Button1: ESRI.ArcGIS.Desktop.AddIns.Button
{

    // Declare as class members.
    IGeoProcessor2 gp;
    IGeoProcessorResult result;
    IVariantArray parameters;

    // Instantiate the objects.
    public Button1()
    {
        gp=new GeoProcessorClass();
        parameters=new VarArrayClass();
    }

    // Code for tool execution.
    protected override void OnClick()
    {
        Button1 button=new Button1();
        button.Enabled=true;

        gp.SetEnvironmentValue("workspace", @"C:\testdata\analysis.gdb");
        gp.OverwriteOutput=true;

        // Populate the VarArray parameters object.
        parameters.Add("roads");
        parameters.Add("roads_buff");
        parameters.Add("500 Meters");

        object sev=null;
        try
        {
            result=gp.Execute("Buffer_analysis", parameters, null);
            System.Windows.Forms.MessageBox.Show(result.GetMessages(0));
        }
        catch (Exception ex)
        {
            System.Windows.Forms.MessageBox.Show(gp.GetMessages(ref sev));
        }
        ArcMap.Application.CurrentTool=null;
    }

    protected override void OnUpdate()
    {
        Enabled=ArcMap.Application != null;
    }

}
[VB.NET]
' Add references with the Imports keyword.

Public Class Button1 Inherits ESRI.ArcGIS.Desktop.AddIns.Button
    
    ' Declare as class members.
    Dim gp As IGeoProcessor2
    Dim result As IGeoProcessorResult
    Dim parameters As IVariantArray
    
    ' Instantiate the objects.

    Public Sub New()
        gp=New GeoProcessor
        parameters=New VarArray
    End Sub
    
    Protected Overrides Sub OnClick()
    
    Dim button As Button1=New Button1
    button.Enabled=True
    
    gp.SetEnvironmentValue("workspace", "C:\testdata\analysis.gdb")
    gp.OverwriteOutput=True
    
    ' Populate the VarArray parameters object.
    parameters.Add("roads")
    parameters.Add("roads_buff")
    parameters.Add("500 Meters")
    
    Dim sev As Object=Nothing
    Try
    result=gp.Execute("Buffer_analysis", parameters, Nothing)
    System.Windows.Forms.MessageBox.Show(result.GetMessages(0))
    Catch ex As Exception
    System.Windows.Forms.MessageBox.Show(gp.GetMessages(sev))
    End Try
End Sub

Protected Overrides Sub OnUpdate()
Enabled=My.ArcMap.Application IsNot Nothing
End Sub

End Class
Empty the IVariantArray object with parameters.RemoveAll() to use it for more than one tool.


See Also:

Building add-ins for ArcGIS Desktop
Building custom UI elements using add-ins
Walkthrough: Consuming a geoprocessing model tool in .NET




To use the code in this topic, reference the following assemblies in your Visual Studio project. In the code files, you will need using (C#) or Imports (VB .NET) directives for the corresponding namespaces (given in parenthesis below if different from the assembly name):