Select to view content in your preferred language

Outputting from an custom geoprocessing tool (IGPFunction)

2233
1
06-11-2013 02:56 PM
GuénoléChoné
New Contributor
Hi,

I'm creating a custom geoprocessing tool in C# (implementing IGPFunction2), and I cannot succeed in creating the output of the gp tool (in my case, a point feature class, but the kind of output I try to create doesn't really matter, it could be a simple table or even a raster...). I guess I need to start by creating the IGPValue (same thing than for the tool's inputs), like that:

public void Execute(IArray paramvalues, ITrackCancel trackcancel, IGPEnvironmentManager envMgr, IGPMessages message)
{
[...]
IGPValue gp_output = m_GPUtilities.UnpackGPValue((IGPParameter3)paramvalues.get_Element(3));

Than what should I do ? How to create a empty (to begin with...) feature class from this IGPValue  ?
0 Kudos
1 Reply
TimSexton1
Frequent Contributor

You have to define an output parameter in the ParameterInfo array of your GP tool, then set the value of that output parameter in the Execute() method.  I've included a small code sample below:

        // This is the location where the parameters to the Function Tool are defined.

        // This property returns an IArray of parameter objects (IGPParameter).

        // These objects define the characteristics of the input and output parameters.

        public IArray ParameterInfo

        {               

            get

            {

                //Array to the hold the parameters

                IArray parameters = new ArrayClass();

                //Input DataType is GPStringType

                IGPParameterEdit3 inputParameter = new GPParameterClass();

                inputParameter.DataType = new GPStringTypeClass();

                // Default Value object is GPString

                inputParameter.Value = new GPStringClass();

                // Set Input Parameter properties

                inputParameter.Direction = esriGPParameterDirection.esriGPParameterDirectionInput;

                inputParameter.DisplayName = "Station ID";

                inputParameter.Name = "stationid";

                inputParameter.ParameterType = esriGPParameterType.esriGPParameterTypeRequired;

                parameters.Add(inputParameter);

                //Out DataType is GPStringType

                IGPParameterEdit3 outputParameter = new GPParameterClass();

                outputParameter.DataType = new GPStringTypeClass();

                // Default Value object is GPString

                outputParameter.Value = new GPStringClass();

                // Set Output Parameter properties

                outputParameter.Direction = esriGPParameterDirection.esriGPParameterDirectionOutput;

                outputParameter.DisplayName = "Output";

                outputParameter.Name = "output";

                outputParameter.ParameterType = esriGPParameterType.esriGPParameterTypeDerived;

                parameters.Add(outputParameter);

                return parameters;

            }

        }

... and then

        // Execute: Execute the function given the array of the parameters

        public void Execute(IArray paramvalues, ITrackCancel trackcancel, IGPEnvironmentManager envMgr, IGPMessages message)

        {

            bool success = false;

            // Get the first Input Parameter

            IGPParameter inputParameter = (IGPParameter)paramvalues.get_Element(0);

            // UnPackGPValue. This ensures you get the value either form the dataelement or GpVariable (ModelBuilder)

            IGPValue inputValue = m_GPUtilities.UnpackGPValue(inputParameter);

            // input station ID

            string inputStringValue = inputValue.GetAsText();

            if (!string.IsNullOrEmpty(inputStringValue))

                success = true;

            if (!success)

                message.AddError(2, "An error occurred during the test.");

            else

            {

                message.AddMessage("Test completed successfully!");

            }

            // Set the Output Parameter

            IGPParameter outputParameter = (IGPParameter)paramvalues.get_Element(1);

            IGPString outValue = new GPStringClass();

            outValue.Value = "This is my output showing input value of '" + inputStringValue + "'";

            m_GPUtilities.PackGPValue((IGPValue)outValue, outputParameter);

        }

0 Kudos