GPPointFileInfo\GPInputCoordinateSystem.cs
Creating a non-trival custom GP tool - inputs, outputs, and responding to environment settings
GPPointFileInfo\GPInputCoordinateSystem.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 ESRI.ArcGIS.Geoprocessing;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;

namespace GPPointFileInfo
{
    public class GPInputCoordinateSystem : GPPointFileInfo.IGPInputCoordinateSystem
    {
        private static int _parameterIndex;

        private ESRI.ArcGIS.Geometry.ISpatialReference _spatialReference;

        public GPInputCoordinateSystem(IParameterValueArray parameterValueArray)
        {
            IGPValue gpValue = parameterValueArray.GetGPValue(_parameterIndex);

            _spatialReference = GetSpatialReference(gpValue);
        }

        public static IGPParameterEdit GetGPParameterEdit()
        {
            IGPParameterEdit gpParameterEdit = new GPParameterClass();

            IGPDataType gpDataType = new GPSpatialReferenceTypeClass();

            IGPValue gpValue = new GPSpatialReference();

            gpParameterEdit.Name = "in_input_coordinate_system";
            gpParameterEdit.DisplayName = "Input Coordinate System";
            gpParameterEdit.DataType = gpDataType;
            gpParameterEdit.Value = gpValue;
            gpParameterEdit.Direction = esriGPParameterDirection.esriGPParameterDirectionInput;
            gpParameterEdit.ParameterType = esriGPParameterType.esriGPParameterTypeOptional;

            return gpParameterEdit;
        }

        public static void SetGPParameterIndex(int index)
        {
            _parameterIndex = index;
        }

        public ESRI.ArcGIS.Geometry.ISpatialReference SpatialReference
        {
            get
            {
                return _spatialReference;
            }
        }

        private ESRI.ArcGIS.Geometry.ISpatialReference GetSpatialReference(IGPValue gpValue)
        {
            ESRI.ArcGIS.Geometry.ISpatialReference spatialReference;

            IGPSpatialReference gpSpatialReference = gpValue as IGPSpatialReference;

            if (gpSpatialReference.SpatialReference == null)
            {
                spatialReference = GetUnknownSpatialReference();
            }
            else
            {
                spatialReference = gpSpatialReference.SpatialReference;
            }

            ISpatialReferenceResolution spatialReferenceResolution = spatialReference as ISpatialReferenceResolution;

            spatialReferenceResolution.ConstructFromHorizon();
            spatialReferenceResolution.SetDefaultXYResolution();
            spatialReferenceResolution.SetDefaultZResolution();

            ISpatialReferenceTolerance spatialReferenceTolerance = spatialReferenceResolution as ISpatialReferenceTolerance;

            spatialReferenceTolerance.SetDefaultXYTolerance();
            spatialReferenceTolerance.SetDefaultZTolerance();

            return spatialReference;
        }

        private ESRI.ArcGIS.Geometry.ISpatialReference GetUnknownSpatialReference()
        {
            ESRI.ArcGIS.Geometry.ISpatialReference spatialReference;

            IUnknownCoordinateSystem unknownCoordinateSystem = new UnknownCoordinateSystemClass();

            spatialReference = unknownCoordinateSystem as ESRI.ArcGIS.Geometry.ISpatialReference;

            return spatialReference;
        }
    }
}