Generating custom geoprocessing tools

In this topic

About generating custom geoprocessing tools

This scenario shows how to consume a geoprocessing model created in ArcGIS Desktop with ModelBuilder.
This scenario uses a pre-built Routing tool in the Toolbox.tbx file that is located in the samples/data/gptoolboxes folder provided with the ArcObjects Software Development Kit (SDK) for the Java Platform installation. This routing tool creates a RouteLayer, adds stops to it using a point feature class, then solves the route and generates directions in a text file. The calculated route is exported to a route.lyr file that can be easily added to any Java Engine application. The routing model for this example is shown in the following illustration:
The P symbol in the previous illustration denotes a required parameter for the tool. This tool needs a NetworkDataset, input points for the stops, output location for the .lyr file, and output for the directions.txt file.

Generate Java code for the tool

The Java code is generated using the class. You can either use it at the command prompt or use the static methodgenerateTool to do this. See the following code example:
String arcgishome = null;
    arcgishome = System.getenv("AGSDEVKITJAVA");

catch (Error e){
    arcgishome = JOptionPane.showInputDialog(
        "Please enter the path to your ArcGIS Engine directory");

String toolLocation = arcgishome + File.separator + "java" + File.separator + 
    "samples" + File.separator + "data" + File.separator + "gptoolboxes" +
    File.separator + "Toolbox.tbx";
String outputLocation = arcgishome + File.separator + "java" + File.separator + 
    "samples" + File.separator + "output_data";
ToolGenerator.generateTool("routing", "com.arcgissamples", "Toolbox", toolLocation,
    null, outputLocation);
In the previous code example, routing is the name of the model and Toolbox is the name of the toolbox containing the model. For more information, see the Working with geoprocessing topics.

Add the toolbox

Add the custom ToolBox containing your geoprocessing models to the GeoProcessor object so that the GeoProcessor object is aware of all the different tools (use the addToolBox method on the GeoProcessor to do this).
Verify that you do not have conflicting names in the various toolboxes.

Execute the tool using geoprocessing

When the tool is created, add the source code for the tool to your current workspace or project and use it in your application. To use any tool requires the following two steps:
  1. Create the geoprocessor and the tool.
  2. Call the execute method on the GeoProcessor object to execute the tool.
For this routing tool, the previous parameters are necessary along with the data from the ArcObjects SDK for the Java Platform installation. See the following code example for executing the model:
routing tool = null;
    GeoProcessor gp = new GeoProcessor();
    String data = arcgishome + File.separator + "java" + File.separator + "samples" 
        + File.separator + "data" + File.separator + "networkanalyst" +
    tool = new routing(data + "bayarealocations.shp", data + "streets_nd.ND",
        outputLocation + File.separator + "test.txt", outputLocation +
        File.separator + "route.lyr");
    gp.execute(tool, new TrackCancel());

catch (Exception e){
The output directions and route.lyr are generated in the output_data folder. You can now add these to your map to display.
For more information on using the GeoProcessor to execute tools, see the Working with geoprocessing topics.