GP Tools - Programming

2599
2
11-22-2010 03:48 AM
shravanshravan
New Contributor
I am unable to figure it out as how to program Geoprocessing tools using c#.net, could anyone please write a program for the "dissolve" tool using C#. I appreciate for the help you guys do.
0 Kudos
2 Replies
KenBuja
MVP Esteemed Contributor
Here's an example written in VB.NET, including the dissolve geoprocessor and the associated functions I use to get the results. The InputName object can be a string of the feature class's path, or an IFeatureClass. For the DissolveField and the StateFields syntax, I would recommend running the Dissolve tool manually and inspecting the syntax of the input message.

Dim GP As New ESRI.ArcGIS.Geoprocessor.Geoprocessor

    Friend Function DissolveDataset(ByVal InputName As Object, ByVal DissolveField As String, ByVal StatsFields As String, ByVal OutputName As String) As ESRI.ArcGIS.Geodatabase.IFeatureClass

        Dim DissolveDS As New ESRI.ArcGIS.DataManagementTools.Dissolve
        Dim Result As ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult2

        Try
            DissolveDS.in_features = InputName
            DissolveDS.dissolve_field = DissolveField
            DissolveDS.statistics_fields = StatsFields
            DissolveDS.out_feature_class = OutputName

            Result = RunTool(DissolveDS, Nothing)
            If Result Is Nothing Then
                System.Windows.Forms.MessageBox.Show("Could not dissolve dataset")
                Return Nothing
            End If

            Return ReturnFeatureClassfromResult(Result)

        Catch ex As Exception
            System.Windows.Forms.MessageBox.Show(ex.ToString, "Dissolve error")
            Return Nothing
        End Try

    End Function

   Friend Function ReturnFeatureClassfromResult(ByVal result As ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult2) As ESRI.ArcGIS.Geodatabase.IFeatureClass

        Dim GPVal As ESRI.ArcGIS.Geodatabase.IGPValue
        Dim InMemFC As String
        Dim GPUtil As ESRI.ArcGIS.Geoprocessing.IGPUtilities3 = New ESRI.ArcGIS.Geoprocessing.GPUtilities
        Dim pOutputFC As ESRI.ArcGIS.Geodatabase.IFeatureClass

        Try
            GPVal = result.GetOutput(0)
            InMemFC = GPVal.GetAsText()
            pOutputFC = GPUtil.OpenFeatureClassFromString(InMemFC)

            Return pOutputFC

        Catch ex As Exception
            System.Windows.Forms.MessageBox.Show(ex.ToString, "Return FeatureClass error")
            Return Nothing
        End Try

    End Function

    Private Sub ReturnMessages(ByVal pResult As ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult2, ByVal Title As String)

        Dim ErrorMessage As String

        If pResult.MessageCount > 0 Then
            For Count As Integer = 0 To pResult.MessageCount - 1
                ErrorMessage += pResult.GetMessage(Count)
            Next
        End If

        System.Windows.Forms.MessageBox.Show(ErrorMessage, Title)

    End Sub

    Friend Function RunTool(ByVal Process As ESRI.ArcGIS.Geoprocessor.IGPProcess, ByVal TC As ESRI.ArcGIS.esriSystem.ITrackCancel2) As ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult2

        Dim Result As ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult2

        Try
            Result = CType(GP.Execute(Process, Nothing), ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult2)
            If Result.Status <> ESRI.ArcGIS.esriSystem.esriJobStatus.esriJobSucceeded Then ReturnMessages(Result, "Geoprocessing Error")
            GP.ClearMessages()
        Catch ex As Exception
            ReturnMessages(Result, "Fail")
            System.Windows.Forms.MessageBox.Show(ex.ToString, "Run Geoprocessor")
        End Try

        Return Result

    End Function
0 Kudos
shravanshravan
New Contributor
Thanks a lot for taking time from your busy schedule and replying me.
0 Kudos