Consuming a geoprocessing custom model in .NET


This document was published with and applies to ArcGIS 9.3.
A 10 version also exists.
Summary This walkthrough demonstrates the required steps to create a custom model in ArcToolbox and consume the model in a .NET Visual Studio 2005 project.

Click here to get the sample associated with this walkthrough.

Development licensing Deployment licensing
ArcView ArcView
ArcEditor ArcEditor
ArcInfo ArcInfo
Engine Developer Kit Engine Runtime

In this topic


Project description

This walkthrough consists of the following areas:

Creating a custom model (using ArcGIS Desktop)

Do the following steps to create the shell for your custom model:
  1. Open Windows Explorer and create a clean test location to complete this walkthrough. Create the following directories on your hard drive:

    C:\gp
    C:\gp\AirportsAndGolf
    C:\gp\output
 
As part of ESRI's .NET ArcGIS Desktop software development kit (SDK) and .NET ArcGIS Engine SDK, sample data to complete this walkthrough can be found in <your install location>\ArcGIS\DeveloperKit\SamplesNET\data\AirportsAndGolf.
If you chose the default install options for ArcGIS and the SDKs, sample data can be found in C:\Program Files\ArcGIS\DeveloperKit\SamplesNET\data\AirportsAndGolf.
  1. Copy the shapefiles in the <your install location>\ArcGIS\DeveloperKit\SamplesNET\data\AirportsAndGolf folder to C:\gp\AirportsAndGolf.
  2. Open ArcMap. Add all the shapefile sample datasets in C:\gp\AirportsAndGolf. After you add the sample datasets, your ArcMap application resembles the following screen shot:

  3. Open ArcToolbox. In the ArcToolbox directory, right-click the ArcToolbox icon and click New Toolbox. See the following screen shot:

  4. Name the new toolbox MY CUSTOM TOOLBOX.
Custom toolboxes created by the user are saved with a .tbx file extension in the following location: C:\Documents and Settings\<your login name>\Application Data\ESRI\ArcToolbox\My Toolboxes.
  1. Right-click the new toolbox. Click New and click Model. An empty model editor window opens to begin constructing the custom model to find golf courses near airports.
  2. Click Model and click Model Properties to open the Model Properties dialog box.
  3. On the Model Properties dialog box, type GolfFinder in the Name text field and GolfFinder in the Label text field.
  4. Click OK to close the dialog box.
  5. Click Save on the GolfFinder model window.
For the model name, only use alphanumeric characters and the underscore symbol; however, the label name can have other human-readable characters.
See the following screen shot of the ArcToolbox:
 
 
The model editor window and ArcMap open simultaneously. Reposition and resize the two windows so you can work with each one without obstructing the other.
Generating a polygon shapefile with the Buffer tool
Do the following to generate a polygon shapefile with the Buffer tool:
  1. In ArcMap, click the Search tab in ArcToolbox. In the text field, type buffer as the search string and click Search. See the following screen shot:

  2. Click and drag the Buffer tool from the Search results onto the GolfFinder model. See the following screen shot:

  3. To specify the buffer distance, right-click the Buffer tool. Click Make Variable, click From Parameter, and click Distance [value or field].
  4. Right-click the Distance [value or field] variable previously added and click Model Parameter. 
When a variable is specified as a parameter, the letter P appears near the upper-right area of that variable.
  1. Right-click the Distance [value or field] parameter, click Rename and type BufferDistance in the Rename dialog box. 
  2. Double-click the BufferDistance parameter, specify an initial linear unit of 26400 for testing the model, and specify Feet as the unit type.
There are 5280 feet in a mile; the model will perform a buffer of 5 miles (5280 * 5 = 26400). 
Specifying a linear unit in the model does not mean it is hard-coded when programming against the model in Visual Studio. Setting the linear unit allows easy testing of the initial parameters in ArcMap and ensures the model is correct before it is consumed in Visual Studio.
See the following screen shot:
 
 
The BufferDistance parameter variable is now cyan because an initial linear unit value was specified. Had we not specified a linear unit value, the BufferDistance parameter variable graphic would have remained colorless.
  1. To specify the input dataset for the buffer, on the GolfFinder model, right-click the Buffer tool and click Make Variable, From Parameter, and Input Features. See the following screen shot:

  2. Right-click the Input Features parameter variable and click Model Parameter.
  3. Double-click the Input Features parameter variable, click the drop-down arrow in the Input Features dialog box, and click the AIRPORT layer. See the following screen shot:

  4. To specify the output dataset for the buffer, right-click the Output Feature Class derived data variable and click Model Parameter.
  5. Right-click the Output Feature Class derived data variable, click Rename, and type AirportBuffer as the new name in the Rename dialog box.
  6. Double-click the AirportBuffer derived data variable and in the dialog box that opens, specify C:\gp\output\AIRPORT_BUFFER.shp as the location and name of the output shapefile.
Ensure there is a valid directory specified for the output to write to.
See the following screen shot:

 
Generating a selection set of points
Now that you have a model that creates a shapefile of buffer polygons around airport locations, identify the golf courses that fall within these polygons. Do the following steps to generate a selection set of points that fall inside the polygons with the Select Layer By Location tool and the Make Feature Layer tool:
  1. To add the Select Layer By Location tool, click the Search tab in ArcToolbox. Type select layer by location as the search string and click Search. See the following screen shot:

  2. Drag the Select Layer By Location tool onto the GolfFinder model. See the following screen shot:

The Select Layer By Location tool requires all input layers (input layer and an optional select layer) to be feature layers. ArcMap automatically converts a feature class to a feature layer when using the Select Layer By Location tool. ArcCatalog and ArcGIS Engine applications do not perform this conversion. Therefore, to ensure that any custom models you create will work under all conditions, convert all feature class types to feature layer types by using the Make Feature Layer tool.
Because the result of the Buffer tool is a feature class, convert it to a feature layer so that it can serve as the input select layer parameter of the Select Layer By Location tool. Do the following to convert the AirportBuffer derived data variable from a feature class to a feature layer:
  1. In ArcMap, click the Search tab in ArcToolbox.
  2. Type make feature layer as the search string and click Search. See the following screen shot:

  3. Click and drag the Make Feature Layer tool onto the GolfFinder model. See the following screen shot:

  4. Right-click the Output Layer derived data variable, click Rename, and type AirportBuffer Feature Layer as the new name in the Rename dialog box.
  5. Click the Add Connection button on the GolfFinder model toolbar to connect the AirportBuffer derived data parameter variable to the Make Feature Layer tool. See the following screen shot:



    After you connect the AirportBuffer to the Make Feature Layer tool, the GolfFinder model window resembles the following screen shot:

The Select Layer By Location tool takes two input feature layer types; an input layer and an optional select layer. The AirportBuffer Feature Layer derived data variable becomes the select layer. Do not use the Add Connection tool to wire up the Select Layer By Location tool until you obtain the input layer. The Add Connection tool automatically sets up certain elements together; therefore, connect the required input layer before the optional select layer.
Do the following to specify the input golf courses dataset and convert to a feature layer:
  1. In ArcMap, drag the Make Feature Layer tool onto the GolfFinder model a second time so there are two Select Layer By Location tools contained in the model. See the following screen shot:


  2. Right-click the Output Layer derived data variable, click Rename, and type Golf Feature Layer as the new name in the Rename dialog box.
  3. Double-click the Make Feature Layer (2) tool and in the resulting dialog box, click the Input Features drop-down arrow and choose GOLF.
  4. Click OK. See the following screen shot:

  5. Right-click the GOLF derived data variable and click Model Parameter. See the following screen shot:

  6. Click the Add Connection button. Specify the Select Layer By Location tool input parameters in the following order:

  7. See the following screen shot:

It is important that the correct input options be specified in the Select Layer By Location tool. Double-click the Select Layer By Location tool to ensure that the input is correct. In the resulting dialog box ensure that the options are correct and modify accordingly.
Verify the following values are in the model and use the default values for the remaining areas:
  1. Click OK to close the dialog box. See the following screen shot:


Also verify the AirportBuffer Feature Layer has completed processing before making the GolfFeature Layer. If not, the model results in an error. To verify processing is complete, do the following to specify a precondition:
  1. Right-click the Make Feature Layer (2) tool and click Properties.
  2. On the Make Feature Layer (2) Properties dialog box, click the Preconditions tab.
  3. Select the AirportBuffer Feature Layer check box.
  4. Click OK. See the following screen shot:



    After you click OK in the preceding dialog box, the GolfFinder model window resembles the following screen shot:

 
Creating a point shapefile from the selection set with the Copy Features tool
Do the following to create a point shapefile with the Copy Features tool:
  1. In ArcMap, click the Search tab in ArcToolbox.
  2. Type copy features as the search string and click Search. See the following screen shot:

  3. Drag the Copy Features tool onto the GolfFinder model. The resulting output feature class will be a shapefile. See the following screen shot:

  4. Right-click the Output Feature Class derived data variable and click Model Parameter.
  5. Right-click the Output Feature Class derived data variable and click Rename. On the Rename dialog box, type Golf Courses Near Airports as the new name.
  6. Click the Add Connection tool on the GolfFinder model toolbar to connect the Output Layer Name derived data variable to the Copy Features tool. See the following screen shot:

  7. Double-click the Golf Courses Near Airports derived data variable. On the resulting dialog box, specify C:\gp\output\GOLF_NEAR_AIRPORTS.shp as the name of the output shapefile.
Ensure there is a valid directory specified for the output to write to.
  1. Click Save on the GolfFinder model window.
  2. On the GolfFinder model window, click Model and click Validate Entire Model to ensure the model has no errors. The model is now complete. See the following screen shot:


The positioning of your GolfFinder model will look different. The previous model elements were manually repositioned for easy viewing if printed. The important criteria is that all of the elements of the model have the same names, properties, values, and connections as outlined in the previous steps and screen shots.
Testing the model
Do the following to test the model:
  1. Click the Run button on the GolfFinder model toolbar. Verify your model creates output in your specified directory (C:\gp\output). See the following screen shot of the Run button:

  2. Click the Save button if your model creates the two shapefiles in the specified directory and close the GolfFinder model. If your model does not create the two shapefiles, verify you have followed the previous steps and correct as necessary. 
  3. In ArcMap, click the Add Data button on the toolbar and navigate to the output shapefiles directory (C:\gp\output). See the following screen shot of the Add Data button:

  4. Add the AIRPORT_BUFFER.shp and GOLF_NEAR_AIRPORTS.shp datasets. See the following screen shot:


Before you rerun your model, remove the GOLF_NEAR_AIRPORTS and AIRPORT_BUFFER layers from ArcMap and delete the shapefiles from the hard drive (C:\gp\output).

Consuming the custom model

Create a console application to test your model. This requires creating a driver file based on the Extensible Markup Language (XML) to hold all the model's parameters, write the code for the console application, and test the results.
 
Creating an XML file containing the application's input parameters
Since the model created in the Creating a custom model (using ArcGIS Desktop) section of this walkthrough required several parameters, it is not practical to enter all this information on the command line of the console application. Create an XML file to hold all the parameters and use it as the input on the command line to execute the application.
  1. Open Notepad or any text editor application.
  2. Create C:\gp\RunModel.xml. If you use Notepad, verify the file is saved with the .xml file extension and not the .txt file extension. Ensure the contents of the file contain the following:

    <MY_CUSTOM_TOOLBOX>
      <GolfFinder>
        <BufferDistance>26400</BufferDistance>
        <Airports>C:\gp\AirportsAndGolf\AIRPORT.shp</Airports>
        <Golf>C:\gp\AirportsAndGolf\GOLF.shp</Golf>
        <AirportBuffer>C:\gp\output\AIRPORT_BUFFER.shp</AirportBuffer>
        <GolfNearAirports>C:\gp\output\GOLF_NEAR_AIRPORTS.shp</GolfNearAirports>
      </GolfFinder>
    </MY_CUSTOM_TOOLBOX>
 
Creating a console application using the ArcGIS template IDE features
Do the following to create a console application using integrated development environment (IDE) features:
  1. Open Visual Studio 2005.
  2. Click File and click New Project.
  3. On the New Project dialog box, click to expand the ArcGIS node in the Project types pane.
  4. Click the Desktop or Engine node.
  5. Click Console Application under the Templates pane.
The nodes are available for the language you work with (Visual Basic or C#). This walkthrough uses screen shots for Visual Basic but the code required to complete the walkthrough is provided in Visual Basic and C#.
It doesn't matter which ArcGIS console application you choose, as it depends on the type of SDK you have installed (.NET ArcGIS Engine SDK or .NET ArcGIS Desktop SDK).
  1. In the New Project dialog box, type GeoprocessingInDotNet in the Name text field. See the following screen shot:

  2. Click OK. The ArcGIS Project Wizard appears.
  3. On the ArcGIS Project Wizard, add the following ArcGIS assemblies:

    ESRI.ArcGIS.Geoprocessing
    ESRI.ArcGIS.Geoprocessor
    ESRI.ArcGIS.System

    See the following screen shot:


    Depending on which ESRI .NET SDK you have installed, you may see different groupings for the ArcGIS assemblies when the Add Reference sections display on the ArcGIS Project Wizard. Use the Filter by icon to change to Products to see all of the ArcGIS assemblies. See the following screen shot of the Filter by icon:

  4. Click Next.
  5. Select the check box for the product for which you have a license. See the following screen shot:


Only a basic ArcGIS Engine level of licensing is required for this walkthrough. If you do not have ArcGIS Engine, but you have ArcGIS Desktop installed, choose the ArcView level of licensing.
  1. Click Finish to complete the wizard and create a Visual Studio 2005 project template.
  2. Right-click the project name in the Solution Explorer and click ArcGIS Toolbox Reference. See the following screen shot:


  3. On the ArcGIS Toolbox Reference dialog box, click the ellipsis (…) button next to the Toolbox text field. Browse to MY CUSTOM TOOLBOX.tbx that was created previously.
Custom toolboxes that are created by the user are saved in the following location on the hard drive: C:\Documents and Settings\<your login name>\Application Data\ESRI\ArcToolbox\My Toolboxes.
The remaining sections of the dialog box should have the following settings:
  1. Accept the defaults of MYCUSTOMTOOLBOX in the Generated Assembly Name and Generated Assembly Namespace text fields.
  2. Type 1.0.0.0 in the Generated Assembly Version text field.
  3. Clear the Sign the Generated Assembly check box. See the following screen shot:

  4. Click OK. The Generating the assembly dialog box appears temporarily.
  5. When finished, click the Show All Files button on the Solution Explorer. See the following screenshot:


 
Adding code to the Visual Studio 2005 project
Do the following to add code to the project:
  1. Add the following code example to the bottom of the Main() function, after the line m_AOLicenseInitializer.ShutdownApplication():

[VB.NET]
' Call the main execution of the application.
RunTheApp()

[C#]
// Call the main execution of the application.
RunTheApp();
  1. Copy the following code example into the class file outside of the Main() function:

[VB.NET]
''' <summary>
''' Main execution of the application.
''' </summary>
''' <remarks></remarks>

Public Sub RunTheApp()
    
    ' Show a message prompting the user for input.
    Console.WriteLine("Enter the full path/filename of the xml driver file for the application.")
    Console.WriteLine("Example: C:\gp\RunModel.xml")
    Console.Write(">")
    
    ' Obtain input from the user.
    Dim xmlPathFile As System.String = Console.ReadLine
    
    ' Let the user know something is happening.
    Console.WriteLine("Processing...")
    
    'Get all of the model's parameters.
    Dim modelParametersHybridDictionary As System.Collections.Specialized.HybridDictionary = ReadXMLConfigurationFile(xmlPathFile)
    
    ' Run the model.
    Dim modelProcessingResultsString As System.String = ExecuteCustomGeoprocessingFunction(modelParametersHybridDictionary)
    
    'Display results to the user.
    Console.WriteLine(modelProcessingResultsString)
    
    'Close the application after the users presses any key.
    Console.ReadLine()
    
End Sub

''' <summary>
''' Read in the arguments from the .xml file used to run the model.
''' </summary>
''' <param name="xmlPathFile">The full path and file name of the .xml file. Example: "C:\gp\RunModel.xml"</param>
''' <returns>A HybridDictionary that contains the arguments to run our model.</returns>
''' <remarks></remarks>

Public Function ReadXMLConfigurationFile(ByVal xmlPathFile As System.String) As System.Collections.Specialized.HybridDictionary
    
    Dim modelParametersHybridDictionary As System.Collections.Specialized.HybridDictionary = New System.Collections.Specialized.HybridDictionary
    
    Try
    
    'Read the XML configuration file.
    Dim XMLdoc As New Xml.XmlDocument
    XMLdoc.Load(xmlPathFile)
    
    ' MY_CUSTOM_TOOLBOX.
    Dim xMY_CUSTOM_TOOLBOX As Xml.XmlNode = XMLdoc("MY_CUSTOM_TOOLBOX")
    
    ' GolfFinder.
    Dim xGolfFinder As Xml.XmlNode = xMY_CUSTOM_TOOLBOX("GolfFinder")
    
    ' BufferDistance.
    Dim xBufferDistance As Xml.XmlNode = xGolfFinder("BufferDistance")
    modelParametersHybridDictionary.Add("BufferDistance", xBufferDistance.InnerText)
    
    ' Airports.
    Dim xAirports As Xml.XmlNode = xGolfFinder("Airports")
    modelParametersHybridDictionary.Add("Airports", xAirports.InnerText)
    
    ' Golf.
    Dim xGolf As Xml.XmlNode = xGolfFinder("Golf")
    modelParametersHybridDictionary.Add("Golf", xGolf.InnerText)
    
    ' AirportBuffer.
    Dim xAirportBuffer As Xml.XmlNode = xGolfFinder("AirportBuffer")
    modelParametersHybridDictionary.Add("AirportBuffer", xAirportBuffer.InnerText)
    
    ' GolfNearAirports.
    Dim xGolfNearAirports As Xml.XmlNode = xGolfFinder("GolfNearAirports")
    modelParametersHybridDictionary.Add("GolfNearAirports", xGolfNearAirports.InnerText)
    
    Return modelParametersHybridDictionary
    
    Catch ex As Exception
    'The XML read was unsuccessful. Return an empty HybridDictionary.
    Return modelParametersHybridDictionary
    End Try
    
End Function

''' <summary>
''' Run the geoprocessing model.
''' </summary>
''' <param name="modelParametersHybridDictionary">A HybridDictionary that contains all of the arguments to run the model</param>
''' <returns>A message of how well the model executed</returns>
''' <remarks></remarks>

Public Function ExecuteCustomGeoprocessingFunction(ByVal modelParametersHybridDictionary As System.Collections.Specialized.HybridDictionary) As System.String
    
    Try
    
    ' Create a geoprocessor object.
    Dim gp As ESRI.ArcGIS.Geoprocessor.Geoprocessor = New ESRI.ArcGIS.Geoprocessor.Geoprocessor
    
    ' Set the OverwriteOutput setting to True.
    gp.OverwriteOutput = True
    
    ' Create a new instance of the custom model.
    Dim myModel As MYCUSTOMTOOLBOX.GolfFinder = New MYCUSTOMTOOLBOX.GolfFinder
    
    ' Set the custom model's parameters.
    myModel.BufferDistance = modelParametersHybridDictionary.Item("BufferDistance")
    myModel.AIRPORT = modelParametersHybridDictionary.Item("Airports")
    myModel.GOLF = modelParametersHybridDictionary.Item("Golf")
    myModel.AirportBuffer = modelParametersHybridDictionary.Item("AirportBuffer")
    myModel.Golf_Courses_Near_Airports = modelParametersHybridDictionary.Item("GolfNearAirports")
    
    ' Execute the model and obtain the result from the run.
    Dim geoProcessorResult As ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult = gp.Execute(myModel, Nothing)
    
    If geoProcessorResult Is Nothing Then
        
        ' You have an error running the model.
        ' If the run fails, a Nothing (VB .NET) or null (C#) is returned from the gp.Execute.
        Return gp.GetMessages(2)
        
    Else
        ' The model completed successfully.
        Return "Output successful. The shapefiles are locacted at: " + geoProcessorResult.ReturnValue.ToString
    End If
    
    Catch ex As Exception
    ' Catch any other errors.
    Return "Error running the model. Debug the application and test again."
    
    End Try
End

End Function

[C#]
/// <summary>
/// Main execution of the application.
/// </summary>
/// <remarks></remarks>
public static void RunTheApp()
{

  // Give message prompting the user for input.
  Console.WriteLine(
    "Enter the full path/filename of the xml driver file for the application.");
  Console.WriteLine("Example: C:\\gp\\RunModel.xml");
  Console.Write(">");

  // Obtain input from the user.
  System.String xmlPathFile = Console.ReadLine();

  // Let the user know something is happening.
  Console.WriteLine("Processing...");

  //Get all of the model's parameters.
  System.Collections.Specialized.HybridDictionary
    modelParametersHybridDictionary = ReadXMLConfigurationFile(xmlPathFile);

  // Run the model.
  System.String modelProcessingResultsString =
    ExecuteCustomGeoprocessingFunction(modelParametersHybridDictionary);

  // Display results to the user.
  Console.WriteLine(modelProcessingResultsString);

  // Close the application after the users presses any key.
  Console.ReadLine();

}

/// <summary>
/// Read in the arguments from the .xml file used to run the model.
/// </summary>
/// <param name="xmlPathFile">The full path and file name of the .xml file. Example: "C:\gp\RunModel.xml"</param>
/// <returns>A HybridDictionary that contains the arguments to run the model.</returns>
/// <remarks></remarks>
public static System.Collections.Specialized.HybridDictionary
  ReadXMLConfigurationFile(System.String xmlPathFile)
{

  System.Collections.Specialized.HybridDictionary
    modelParametersHybridDictionary = new
    System.Collections.Specialized.HybridDictionary();

  try
  {

    //Read the XML configuration file.
    System.Xml.XmlDocument XMLdoc = new System.Xml.XmlDocument();
    XMLdoc.Load(xmlPathFile);

    // MY_CUSTOM_TOOLBOX.
    System.Xml.XmlNode xMY_CUSTOM_TOOLBOX = XMLdoc["MY_CUSTOM_TOOLBOX"];

    // GolfFinder.
    System.Xml.XmlNode xGolfFinder = xMY_CUSTOM_TOOLBOX["GolfFinder"];

    // BufferDistance.
    System.Xml.XmlNode xBufferDistance = xGolfFinder["BufferDistance"];
    modelParametersHybridDictionary.Add("BufferDistance",
      xBufferDistance.InnerText);

    // Airports.
    System.Xml.XmlNode xAirports = xGolfFinder["Airports"];
    modelParametersHybridDictionary.Add("Airports", xAirports.InnerText);

    // Golf.
    System.Xml.XmlNode xGolf = xGolfFinder["Golf"];
    modelParametersHybridDictionary.Add("Golf", xGolf.InnerText);

    // AirportBuffer.
    System.Xml.XmlNode xAirportBuffer = xGolfFinder["AirportBuffer"];
    modelParametersHybridDictionary.Add("AirportBuffer",
      xAirportBuffer.InnerText);

    // GolfNearAirports.
    System.Xml.XmlNode xGolfNearAirports = xGolfFinder["GolfNearAirports"];
    modelParametersHybridDictionary.Add("GolfNearAirports",
      xGolfNearAirports.InnerText);

    return modelParametersHybridDictionary;
  }
  catch (Exception ex)
  {

    //The XML read was unsuccessful. Return an empty HybridDictionary.
    return modelParametersHybridDictionary;

  }

}


/// <summary>
/// Run the geoprocessing model.
/// </summary>
/// <param name="modelParametersHybridDictionary">A HybridDictionary that contains all of the arguments to run the model</param>
/// <returns>A message of how well the model executed</returns>
/// <remarks></remarks>
public static System.String ExecuteCustomGeoprocessingFunction
  (System.Collections.Specialized.HybridDictionary
  modelParametersHybridDictionary)
{

  try
  {

    // Create a geoprocessor object.
    ESRI.ArcGIS.Geoprocessor.Geoprocessor gp = new
      ESRI.ArcGIS.Geoprocessor.Geoprocessor();

    // Set the OverwriteOutput setting to True.
    gp.OverwriteOutput = true;

    // Create a new instance of the custom model.
    MYCUSTOMTOOLBOX.GolfFinder myModel = new MYCUSTOMTOOLBOX.GolfFinder();

    // Set the custom model's parameters.
    myModel.BufferDistance = modelParametersHybridDictionary["BufferDistance"];
    myModel.AIRPORT = modelParametersHybridDictionary["Airports"];
    myModel.GOLF = modelParametersHybridDictionary["Golf"];
    myModel.AirportBuffer = modelParametersHybridDictionary["AirportBuffer"];
    myModel.Golf_Courses_Near_Airports = modelParametersHybridDictionary[
      "GolfNearAirports"];

    // Execute the model and obtain the result from the run.
    ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult geoProcessorResult = 
      (ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult)gp.Execute(myModel, null);

    if (geoProcessorResult == null)
    {
      // You have an error running the model. 
      // If the run fails, a Nothing (VB .NET) or null (C#) is returned from the gp.Execute.
      object sev = 2;
      string messages = gp.GetMessages(ref sev);
      return messages;
    }
    else
    {
      // The model completed successfully.
      return "Output successful. The shapefiles are locacted at: " +
        geoProcessorResult.ReturnValue.ToString();
    }

  }
  catch (Exception ex)
  {
    // Catch any other errors.
    return "Error running the model. Debug the application and test again.";
  }

}
Testing the code
Do the following to test the code, process the application, and complete the walkthrough:
  1. Click the Start Debugging button and test the application. See the following screen shot:

  2. When the console application opens and the prompt (>) appears, type the full path and file name for the .xml driver file, for example, C:\gp\RunModel.xml. The application processes and gives an output resembling the following screen shot:



See Also:

An overview of models