Solved! Go to Solution.
Friend Function ExportSelectedFeatures(ByVal pFClass As ESRI.ArcGIS.Geodatabase.IFeatureClass, ByVal Query As String, ByVal OutPath As String, ByVal OutFC As String) As ESRI.ArcGIS.Geodatabase.IFeatureClass Dim CopyFC2FC As New ESRI.ArcGIS.ConversionTools.FeatureClassToFeatureClass Dim Result As ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult2 Try Using releaser As New ESRI.ArcGIS.ADF.ComReleaser releaser.ManageLifetime(CopyFC2FC) CopyFC2FC.in_features = pFClass CopyFC2FC.where_clause = Query CopyFC2FC.out_path = OutPath CopyFC2FC.out_name = OutFC Result = RunTool(CopyFC2FC, Nothing, True) If Result Is Nothing Then Return Nothing Return ReturnObjectfromResult(Result, "Feature Class") End Using Catch ex As Exception System.Windows.Forms.MessageBox.Show(ex.ToString & vbNewLine & ex.StackTrace.ToString, "Export Selected Features") Return Nothing End Try End Function Friend Function RunTool(ByVal Process As ESRI.ArcGIS.Geoprocessor.IGPProcess, ByVal TC As ESRI.ArcGIS.esriSystem.ITrackCancel, Optional ByVal AddOutput As Boolean = False) As ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult Dim Result As ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult Try System.Windows.Forms.Cursor.Current = Windows.Forms.Cursors.WaitCursor Dim GP As New ESRI.ArcGIS.Geoprocessor.Geoprocessor GP.OverwriteOutput = True GP.AddOutputsToMap = AddOutput Result = CType(GP.Execute(Process, Nothing), ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult) If Result.Status <> ESRI.ArcGIS.esriSystem.esriJobStatus.esriJobSucceeded Then If GP.MessageCount > 0 Then For Count = 0 To Globals.GP.MessageCount - 1 Message += GP.GetMessage(Count) & vbNewLine Next System.Windows.Forms.MessageBox.Show(Message, "GP Failure") End If Return Nothing End If Return Result Catch ex As Exception System.Windows.Forms.MessageBox.Show(ex.ToString & vbNewLine & ex.StackTrace, "Run Geoprocessor") Return Nothing End Try End Function Friend Function ReturnObjectfromResult(ByVal result As ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult2, ByVal Type As String) As Object Dim GPVal As ESRI.ArcGIS.Geodatabase.IGPValue Dim InMemFC As String Dim GPUtil As ESRI.ArcGIS.Geoprocessing.IGPUtilities3 = New ESRI.ArcGIS.Geoprocessing.GPUtilities Try GPVal = result.GetOutput(0) InMemFC = GPVal.GetAsText() Select Case Type Case "Feature Class" Return GPUtil.OpenFeatureClassFromString(InMemFC) Case "Table" Return GPUtil.OpenTableFromString(InMemFC) Case "Feature Layer" Return GPUtil.OpenFeatureLayerFromString(InMemFC) End Select Catch ex As Exception System.Windows.Forms.MessageBox.Show(ex.ToString, "Return FeatureClass error") Return Nothing End Try End Function
Friend Function ExportSelectedFeatures(ByVal pFClass As ESRI.ArcGIS.Geodatabase.IFeatureClass, ByVal Query As String, ByVal OutPath As String, ByVal OutFC As String) As ESRI.ArcGIS.Geodatabase.IFeatureClass Dim CopyFC2FC As New ESRI.ArcGIS.ConversionTools.FeatureClassToFeatureClass Dim Result As ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult2 Try Using releaser As New ESRI.ArcGIS.ADF.ComReleaser releaser.ManageLifetime(CopyFC2FC) CopyFC2FC.in_features = pFClass CopyFC2FC.where_clause = Query CopyFC2FC.out_path = OutPath CopyFC2FC.out_name = OutFC Result = RunTool(CopyFC2FC, Nothing, True) If Result Is Nothing Then Return Nothing Return ReturnObjectfromResult(Result, "Feature Class") End Using Catch ex As Exception System.Windows.Forms.MessageBox.Show(ex.ToString & vbNewLine & ex.StackTrace.ToString, "Export Selected Features") Return Nothing End Try End Function Friend Function RunTool(ByVal Process As ESRI.ArcGIS.Geoprocessor.IGPProcess, ByVal TC As ESRI.ArcGIS.esriSystem.ITrackCancel, Optional ByVal AddOutput As Boolean = False) As ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult Dim Result As ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult Try System.Windows.Forms.Cursor.Current = Windows.Forms.Cursors.WaitCursor Dim GP As New ESRI.ArcGIS.Geoprocessor.Geoprocessor GP.OverwriteOutput = True GP.AddOutputsToMap = AddOutput Result = CType(GP.Execute(Process, Nothing), ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult) If Result.Status <> ESRI.ArcGIS.esriSystem.esriJobStatus.esriJobSucceeded Then If GP.MessageCount > 0 Then For Count = 0 To Globals.GP.MessageCount - 1 Message += GP.GetMessage(Count) & vbNewLine Next System.Windows.Forms.MessageBox.Show(Message, "GP Failure") End If Return Nothing End If Return Result Catch ex As Exception System.Windows.Forms.MessageBox.Show(ex.ToString & vbNewLine & ex.StackTrace, "Run Geoprocessor") Return Nothing End Try End Function Friend Function ReturnObjectfromResult(ByVal result As ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult2, ByVal Type As String) As Object Dim GPVal As ESRI.ArcGIS.Geodatabase.IGPValue Dim InMemFC As String Dim GPUtil As ESRI.ArcGIS.Geoprocessing.IGPUtilities3 = New ESRI.ArcGIS.Geoprocessing.GPUtilities Try GPVal = result.GetOutput(0) InMemFC = GPVal.GetAsText() Select Case Type Case "Feature Class" Return GPUtil.OpenFeatureClassFromString(InMemFC) Case "Table" Return GPUtil.OpenTableFromString(InMemFC) Case "Feature Layer" Return GPUtil.OpenFeatureLayerFromString(InMemFC) End Select Catch ex As Exception System.Windows.Forms.MessageBox.Show(ex.ToString, "Return FeatureClass error") Return Nothing End Try End Function
kenbuja;190847 wrote:
Here's the code I use in one of my applications. It uses the Geoprocessing task "Feature Class to Feature Class"
You have to add in the correct reference for the ADF assembly. You can't find it by using "Add ArcGIS Reference", but instead, use the Add Reference, select the .NET tab, and select ESRI.ArcGIS.ADF.Connection.Local
And don't forget to mark this question as answered.
Try using the fully qualified name as in the code
Using releaser As New ESRI.ArcGIS.ADF.ComReleaser
And when marking a question as answered, you should really mark the post that supplied the correct answer, not the original question 🙂