GPPointFileInfo\GPPointFileInfoFunction.vb
Creating a non-trival custom GP tool - inputs, outputs, and responding to environment settings
GPPointFileInfo\GPPointFileInfoFunction.vb
' 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.
' 

Imports Microsoft.VisualBasic
Imports System
Imports System.Runtime.InteropServices
Imports ESRI.ArcGIS.esriSystem
Imports ESRI.ArcGIS.Geoprocessing

Namespace GPPointFileInfo
  Public Class GPPointFileInfoFunction
        Implements IGPFunction
        Private _gpUtilities As IGPUtilities
    Private _name As String = "PointFileInfo"
    Private _displayName As String = "Point File Information"
    Private _parameterInfo As IArray
    Private _fullName As IName

        Public Sub New()
            _gpUtilities = New GPUtilities()

            _parameterInfo = GetParameterInfo()

            _fullName = GetFullName()
        End Sub

    #Region "IGPFunction Members"

    Public ReadOnly Property Name() As String Implements IGPFunction.Name
      Get
        Return _name
      End Get
    End Property

    Public ReadOnly Property DisplayName() As String Implements IGPFunction.DisplayName
      Get
        Return _displayName
      End Get
    End Property

    Public ReadOnly Property ParameterInfo() As IArray Implements IGPFunction.ParameterInfo
      Get
        Return _parameterInfo
      End Get
    End Property

    Public ReadOnly Property FullName() As IName Implements IGPFunction.FullName
      Get
        Return _fullName
      End Get
    End Property

    Public Function GetRenderer(ByVal gpParameter As IGPParameter) As Object Implements IGPFunction.GetRenderer
      Return Nothing
    End Function

    Public ReadOnly Property HelpContext() As Integer Implements IGPFunction.HelpContext
      Get
        Return 0
      End Get
    End Property

    Public ReadOnly Property HelpFile() As String Implements IGPFunction.HelpFile
      Get
        Return Nothing
      End Get
    End Property

        Public Function IsLicensed() As Boolean Implements IGPFunction.IsLicensed

            Dim blnIsLicensed As Boolean

            Dim licenseManager As ILicenseManager = New LicenseManager()

            blnIsLicensed = licenseManager.IsLicensed

            Return blnIsLicensed
        End Function

    Public ReadOnly Property MetadataFile() As String Implements IGPFunction.MetadataFile
      Get
        Return Nothing
      End Get
    End Property

    Public ReadOnly Property DialogCLSID() As UID Implements IGPFunction.DialogCLSID
      Get
        Return Nothing
      End Get
    End Property

    Private Function GetFullName() As IName
      Dim fullName As IName

            Dim gpFunctionFactory As IGPFunctionFactory = New GPFunctionFactory()

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

      fullName = TryCast(gpName, ESRI.ArcGIS.esriSystem.IName)

      Return fullName
    End Function

    Private Function GetParameterInfo() As IArray
      Dim parameterInfo As IArray = New ArrayClass()

            Dim parameterIndex As Integer

            parameterIndex = 0
            parameterInfo.Add(GPInputFolder.GetGPParameterEdit())
            GPInputFolder.SetGPParameterIndex(parameterIndex)

            parameterIndex += 1
      parameterInfo.Add(GPRecurseInputSubfolders.GetGPParameterEdit())
            GPRecurseInputSubfolders.SetGPParameterIndex(parameterIndex)

            parameterIndex += 1
      parameterInfo.Add(GPInputFileFormat.GetGPParameterEdit())
            GPInputFileFormat.SetGPParameterIndex(parameterIndex)

            parameterIndex += 1
      parameterInfo.Add(GPInputFileSuffix.GetGPParameterEdit())
            GPInputFileSuffix.SetGPParameterIndex(parameterIndex)

            parameterIndex += 1
      parameterInfo.Add(GPInputCoordinateSystem.GetGPParameterEdit())
            GPInputCoordinateSystem.SetGPParameterIndex(parameterIndex)

            parameterIndex += 1
      parameterInfo.Add(GPOutputGeometryDimensions.GetGPParameterEdit())
            GPOutputGeometryDimensions.SetGPParameterIndex(parameterIndex)

            parameterIndex += 1
      parameterInfo.Add(GPOutputFeatureClass.GetGPParameterEdit())
            GPOutputFeatureClass.SetGPParameterIndex(parameterIndex)

            parameterIndex += 1
      parameterInfo.Add(GPOutputMinAvgPtSpc.GetGPParameterEdit())
            GPOutputMinAvgPtSpc.SetGPParameterIndex(parameterIndex)

      Return parameterInfo
    End Function

    Public Function Validate(ByVal _parameterValueArray As ESRI.ArcGIS.esriSystem.IArray, ByVal _updateValues As Boolean, ByVal _gpEnvironmentManager As ESRI.ArcGIS.Geoprocessing.IGPEnvironmentManager) As ESRI.ArcGIS.Geodatabase.IGPMessages Implements IGPFunction.Validate
      Const HonorDerivedFlag As Boolean = True

      Dim gpMessages As ESRI.ArcGIS.Geodatabase.IGPMessages

      Dim gpUtilities As IGPUtilities = New GPUtilitiesClass()

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

      Return gpMessages
    End Function

    Public Sub Execute(ByVal _parameterValueArray As ESRI.ArcGIS.esriSystem.IArray, ByVal _trackCancel As ESRI.ArcGIS.esriSystem.ITrackCancel, ByVal _gpEnvironmentManager As ESRI.ArcGIS.Geoprocessing.IGPEnvironmentManager, ByVal _gpMessages As ESRI.ArcGIS.Geodatabase.IGPMessages) Implements IGPFunction.Execute

            Dim licenseManager As ILicenseManager = New LicenseManager()
            licenseManager.Initialize()

      Dim gpMessage As ESRI.ArcGIS.Geodatabase.IGPMessage = TryCast(_gpMessages, ESRI.ArcGIS.Geodatabase.IGPMessage)

      If (Not gpMessage.IsError()) Then
        Dim gpMessages As IGPMessages = New GPMessages(_gpMessages)

        Try
                    Dim parameterValueArray As IParameterValueArray = New ParameterValueArray(_gpUtilities, _parameterValueArray)

          Dim trackCancel As ITrackCancel = New TrackCancel(_trackCancel)

          Dim gpEnvironmentManager As IGPEnvironmentManager = New GPEnvironmentManager(_gpEnvironmentManager)

          Dim gpInputFolder As IGPInputFolder = New GPInputFolder(parameterValueArray)

          Dim gpRecurseInputSubfolders As IGPRecurseInputSubfolders = New GPRecurseInputSubfolders(parameterValueArray)

          Dim gpInputFileFormat As IGPInputFileFormat = New GPInputFileFormat(parameterValueArray)

          Dim gpInputFileSuffix As IGPInputFileSuffix = New GPInputFileSuffix(parameterValueArray)

          Dim gpInputCoordinateSystem As IGPInputCoordinateSystem = New GPInputCoordinateSystem(parameterValueArray)

          Dim inputSpatialReference As ISpatialReference = New SpatialReferenceClass(gpInputCoordinateSystem)

          Dim gpOutputGeometryDimensions As IGPOutputGeometryDimensions = New GPOutputGeometryDimensions(parameterValueArray)

          Dim esOutputZFlag As IESOutputZFlag = New ESOutputZFlag(gpEnvironmentManager)

          Dim outputHasZ As IOutputHasZ = New OutputHasZ(gpOutputGeometryDimensions, esOutputZFlag)

          Dim esOutputCoordinateSystem As IESOutputCoordinateSystem = New ESOutputCoordinateSystem(gpEnvironmentManager)

          Dim outputSpatialReference As ISpatialReference = New SpatialReferenceClass(esOutputCoordinateSystem)

          Dim esOverwriteOutput As IESOverwriteOutput = New ESOverwriteOutput(_gpEnvironmentManager)

          Dim esConfigKeyword As IESConfigKeyword = New ESConfigKeyword(gpEnvironmentManager)

          Dim esScratchWorkspace As IESScratchWorkspace = New ESScratchWorkspace(gpEnvironmentManager)

          Dim esSpatialGrid1 As IESSpatialGrid1 = New ESSpatialGrid1(gpEnvironmentManager)

          Dim esSpatialGrid2 As IESSpatialGrid2 = New ESSpatialGrid2(gpEnvironmentManager)

          Dim esSpatialGrid3 As IESSpatialGrid3 = New ESSpatialGrid3(gpEnvironmentManager)

          Dim gpOutputFeatureClass As IGPOutputFeatureClass = New GPOutputFeatureClass(parameterValueArray, gpInputFolder, inputSpatialReference, outputSpatialReference, outputHasZ, esOverwriteOutput, esConfigKeyword, esScratchWorkspace, esSpatialGrid1, esSpatialGrid2, esSpatialGrid3)

          Dim outputFeatureCursor As IOutputFeatureCursor = New OutputFeatureCursor(gpOutputFeatureClass)

          Dim esExtent As IESExtent = New ESExtent(gpEnvironmentManager)

          Dim inputFileArray As IInputFileArray = New InputFileArray(gpInputFolder, gpInputFileSuffix, gpRecurseInputSubfolders)

          Dim i As Integer = 0
          Do While i < inputFileArray.Length
            If trackCancel.IsCancelPressed Then
              trackCancel.Cancel()
              Exit Do
            End If

            Try
              Dim inputFile As IInputFile = New InputFile(inputFileArray, i)

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

              Dim outputFeatureBuffer As IOutputFeatureBuffer = New OutputFeatureBuffer(inputFile, gpInputFileFormat, inputSpatialReference, outputSpatialReference, outputHasZ, gpOutputFeatureClass, esExtent)

              outputFeatureCursor.InsertFeatureBuffer(outputFeatureBuffer)
            Catch ex As GPException
              ex.LogException(gpMessages)
                        End Try
            i += 1
          Loop

          outputFeatureCursor.Flush()

          GPOutputMinAvgPtSpc.SetMinAvgPtSpc(parameterValueArray, gpOutputFeatureClass)
        Catch ex As GPException
          ex.LogException(gpMessages)
                End Try
      End If

            licenseManager.Shutdown()
    End Sub

    #End Region
  End Class
End Namespace