GPPointFileInfo\GPPointFileInfoFunction.cs
Creating a non-trival custom GP tool - inputs, outputs, and responding to environment settings
GPPointFileInfo\GPPointFileInfoFunction.cs
// Copyright 2008 ESRI
// 
// All rights reserved under the copyright laws of the United States
// and applicable international laws, treaties, and conventions.
// 
// You may freely redistribute and use this sample code, with or
// without modification, provided you include the original copyright
// notice and use restrictions.
// 
// See use restrictions at <your ArcGIS install location>/developerkit/userestrictions.txt.
// 

using System;
using System.Runtime.InteropServices;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geoprocessing;

namespace GPPointFileInfo
{
    public class GPPointFileInfoFunction : IGPFunction
    {
        private IGPUtilities _gpUtilities;
        private string _name = "PointFileInfo";
        private string _displayName = "Point File Information";
        private IArray _parameterInfo;
        private IName _fullName;

        public GPPointFileInfoFunction()
        {
            _gpUtilities = new GPUtilitiesClass();

            _parameterInfo = GetParameterInfo();
            
            _fullName = GetFullName();
        }

        #region IGPFunction Members

        public string Name
        {
            get
            {
                return _name; 
            }
        }

        public string DisplayName
        {
            get
            {
                return _displayName;
            }
        }

        public IArray ParameterInfo
        {                 
            get 
            {
                return _parameterInfo;
            }
        }

        public IName FullName
        {
            get
            {
                return _fullName;
            }
        }

        public object GetRenderer(IGPParameter gpParameter)
        {
            return null;
        }

        public int HelpContext
        {
            get
            {
                return 0;
            }
        }

        public string HelpFile
        {
            get
            {
                return null;
            }
        }

        public bool IsLicensed()
        {
            bool isLicensed;

            ILicenseManager licenseManager = new LicenseManager();

            isLicensed = licenseManager.IsLicensed;

            return isLicensed;
        }

        public string MetadataFile
        {
            get
            {
                return null; 
            }
        }

        public UID DialogCLSID
        {
            get
            {
                return null; 
            }
        }

        private IName GetFullName()
        {
            IName fullName;

            IGPFunctionFactory gpFunctionFactory = new GPFunctionFactory();

            ESRI.ArcGIS.Geodatabase.IGPName gpName = gpFunctionFactory.GetFunctionName(_name);

            fullName = gpName as ESRI.ArcGIS.esriSystem.IName;

            return fullName;
        }

        private IArray GetParameterInfo()
        {
            IArray parameterInfo = new ArrayClass();

            int parameterIndex = 0;

            parameterInfo.Add(GPInputFolder.GetGPParameterEdit());

            GPInputFolder.SetGPParameterIndex(parameterIndex++);

            parameterInfo.Add(GPRecurseInputSubfolders.GetGPParameterEdit());

            GPRecurseInputSubfolders.SetGPParameterIndex(parameterIndex++);

            parameterInfo.Add(GPInputFileFormat.GetGPParameterEdit());

            GPInputFileFormat.SetGPParameterIndex(parameterIndex++);

            parameterInfo.Add(GPInputFileSuffix.GetGPParameterEdit());

            GPInputFileSuffix.SetGPParameterIndex(parameterIndex++);

            parameterInfo.Add(GPInputCoordinateSystem.GetGPParameterEdit());

            GPInputCoordinateSystem.SetGPParameterIndex(parameterIndex++);

            parameterInfo.Add(GPOutputGeometryDimensions.GetGPParameterEdit());

            GPOutputGeometryDimensions.SetGPParameterIndex(parameterIndex++);

            parameterInfo.Add(GPOutputFeatureClass.GetGPParameterEdit());

            GPOutputFeatureClass.SetGPParameterIndex(parameterIndex++);

            parameterInfo.Add(GPOutputMinAvgPtSpc.GetGPParameterEdit());

            GPOutputMinAvgPtSpc.SetGPParameterIndex(parameterIndex++);
            
            return parameterInfo;
        }

        public ESRI.ArcGIS.Geodatabase.IGPMessages Validate(ESRI.ArcGIS.esriSystem.IArray _parameterValueArray, bool _updateValues, ESRI.ArcGIS.Geoprocessing.IGPEnvironmentManager _gpEnvironmentManager)
        {
            const bool HonorDerivedFlag = true;

            ESRI.ArcGIS.Geodatabase.IGPMessages gpMessages;

            gpMessages = _gpUtilities.InternalValidate(_parameterInfo, _parameterValueArray, _updateValues, HonorDerivedFlag, _gpEnvironmentManager);

            return gpMessages;
        }

        public void Execute(ESRI.ArcGIS.esriSystem.IArray _parameterValueArray, ESRI.ArcGIS.esriSystem.ITrackCancel _trackCancel, ESRI.ArcGIS.Geoprocessing.IGPEnvironmentManager _gpEnvironmentManager, ESRI.ArcGIS.Geodatabase.IGPMessages _gpMessages)
        {
            ILicenseManager licenseManager = new LicenseManager();

            licenseManager.Initialize();

            ESRI.ArcGIS.Geodatabase.IGPMessage gpMessage = _gpMessages as ESRI.ArcGIS.Geodatabase.IGPMessage;

            if (!gpMessage.IsError())
            {
                IGPMessages gpMessages = new GPMessages(_gpMessages);

                try
                {
                    IParameterValueArray parameterValueArray = new ParameterValueArray(_gpUtilities, _parameterValueArray);

                    ITrackCancel trackCancel = new TrackCancel(_trackCancel);

                    IGPEnvironmentManager gpEnvironmentManager = new GPEnvironmentManager(_gpEnvironmentManager);

                    IGPInputFolder gpInputFolder = new GPInputFolder(parameterValueArray);

                    IGPRecurseInputSubfolders gpRecurseInputSubfolders = new GPRecurseInputSubfolders(parameterValueArray);

                    IGPInputFileFormat gpInputFileFormat = new GPInputFileFormat(parameterValueArray);

                    IGPInputFileSuffix gpInputFileSuffix = new GPInputFileSuffix(parameterValueArray);

                    IGPInputCoordinateSystem gpInputCoordinateSystem = new GPInputCoordinateSystem(parameterValueArray);

                    ISpatialReference inputSpatialReference = new SpatialReferenceClass(gpInputCoordinateSystem);

                    IGPOutputGeometryDimensions gpOutputGeometryDimensions = new GPOutputGeometryDimensions(parameterValueArray);

                    IESOutputZFlag esOutputZFlag = new ESOutputZFlag(gpEnvironmentManager);

                    IOutputHasZ outputHasZ = new OutputHasZ(gpOutputGeometryDimensions, esOutputZFlag);

                    IESOutputCoordinateSystem esOutputCoordinateSystem = new ESOutputCoordinateSystem(gpEnvironmentManager);

                    ISpatialReference outputSpatialReference = new SpatialReferenceClass(esOutputCoordinateSystem);

                    IESOverwriteOutput esOverwriteOutput = new ESOverwriteOutput(_gpEnvironmentManager);

                    IESConfigKeyword esConfigKeyword = new ESConfigKeyword(gpEnvironmentManager);

                    IESScratchWorkspace esScratchWorkspace = new ESScratchWorkspace(gpEnvironmentManager);

                    IESSpatialGrid1 esSpatialGrid1 = new ESSpatialGrid1(gpEnvironmentManager);

                    IESSpatialGrid2 esSpatialGrid2 = new ESSpatialGrid2(gpEnvironmentManager);

                    IESSpatialGrid3 esSpatialGrid3 = new ESSpatialGrid3(gpEnvironmentManager);

                    IGPOutputFeatureClass gpOutputFeatureClass = new GPOutputFeatureClass(parameterValueArray, gpInputFolder, inputSpatialReference, outputSpatialReference, outputHasZ, esOverwriteOutput, esConfigKeyword, esScratchWorkspace, esSpatialGrid1, esSpatialGrid2, esSpatialGrid3);

                    IOutputFeatureCursor outputFeatureCursor = new OutputFeatureCursor(gpOutputFeatureClass);

                    IESExtent esExtent = new ESExtent(gpEnvironmentManager);

                    IInputFileArray inputFileArray = new InputFileArray(gpInputFolder, gpInputFileSuffix, gpRecurseInputSubfolders);

                    for (int i = 0; i < inputFileArray.Length; i++)
                    {
                        if (trackCancel.IsCancelPressed)
                        {
                            trackCancel.Cancel();
                            break;
                        }

                        try
                        {
                            IInputFile inputFile = new InputFile(inputFileArray, i);

                            gpMessages.AddInfo("Processing: " + inputFile.Name + " [" + (inputFile.Index + 1) + " / " + (inputFileArray.Length) + "]");

                            IOutputFeatureBuffer outputFeatureBuffer = new OutputFeatureBuffer(inputFile, gpInputFileFormat, inputSpatialReference, outputSpatialReference, outputHasZ, gpOutputFeatureClass, esExtent);

                            outputFeatureCursor.InsertFeatureBuffer(outputFeatureBuffer);
                        }
                        catch (GPException ex)
                        {
                            ex.LogException(gpMessages);
                        }
                    }

                    outputFeatureCursor.Flush();

                    GPOutputMinAvgPtSpc.SetMinAvgPtSpc(parameterValueArray, gpOutputFeatureClass);
                }
                catch (GPException ex)
                {
                    ex.LogException(gpMessages);
                }
            }

            licenseManager.Shutdown();
        }

        #endregion
    }
}