Code for running a tool, model, or script from within an Arc AddIn


Details

This code runs a tool from an Arc addIn. It assumes the Tool is in a Toolbox loaded into ArcToolbox. The example given takes in four parameters (a layer, a feature dataset, a distance, and an output layer). In the absence of the latter, models generally output to the default geodatabase in "My Documents/ArcGIS/AddIns/" (provided you have permission -- otherwise they'll just fail). If the output directory is where your map data is, the final layer will generally load as a layer as well as being produced.

This code differs from the original code in running inside Arc. You do not need to generate wrappers using the Eclipse ArcGIS Menu -> GeoProcessing Tool Code Generator, for tools loaded into Arc.


Original author/s: ESRI
Original location/s: How to consume custom geoprocessing tools in Java applications
Adapted by: Andy Evans
License: unknown


Imports and instance variables

	import com.esri.arcgis.geoprocessing.GeoProcessor;
	import com.esri.arcgis.system.VarArray;
	
	private GeoProcessor geoprocessor = null;

Code


	try{
			
		// GeoProcessors run tools built into Arc by being available in toolboxes.
		this.geoprocessor = new GeoProcessor();
            
		// Set up parameters
		VarArray parameters = new VarArray();

		// NOTE - CHECK AGAINST MODEL RUN MANUALLY FOR NAMES AND PATHS NEEDED
		String inputFeatures = "bomb points";
		String distance = "100";
		String featureClass = "build polygon";
		String outputFeatures1 = "m:\\GEOG5790M\\practical1\\buff";
		String outputFeatures2 = "m:\\GEOG5790M\\practical1\\intersect";
		
		// NOTE - CHECK AGAINST MODEL RUN MANUALLY FOR ORDER
		parameters.add(inputFeatures);
		parameters.add(distance);
		parameters.add(featureClass);
		parameters.add(outputFeatures1);
		parameters.add(outputFeatures2);
		
		// Run the tool. NOTE - CHECK MODEL NAME
		geoprocessor.setOverwriteOutput(true);
		geoprocessor.execute("Explosion", parameters, null);

		//Print messages, if any, from the tool.
		for (int i = 0; i < geoprocessor.getMessageCount(); i++)
			JOptionPane.showMessageDialog(null, geoprocessor.getMessage(i));

            
	} catch (Exception e) {
		JOptionPane.showMessageDialog(null, e.getStackTrace());
		for (int i = 0; i < geoprocessor.getMessageCount(); i++)
			JOptionPane.showMessageDialog(null, geoprocessor.getMessage(i));
	}