So is there a way you can use arcpy code from within a vb.net project? Currently have a pretty big custom set of addin's written in vb.net and can't figure out how to do one part of what i want with .net code. In acpy it is easy so i was wondering if i could somehow stick a little arcpy code in the visual studio project?
Solved! Go to Solution.
Duncan,
Thanks SO MUCH
Here is the working result:
Private Sub Export_to_shp()
Dim GP As IGeoProcessor = New ESRI.ArcGIS.Geoprocessing.GeoProcessor
With GP
.OverwriteOutput = True
.LogHistory = False
.AddOutputsToMap = False
.TemporaryMapLayers = True
End With
Dim m_pMxDoc As ESRI.ArcGIS.ArcMapUI.IMxDocument
m_pMxDoc = My.ArcMap.Document
Dim in_features As String = m_pMxDoc.FocusMap.Layer(2).Name
Dim pVA As ESRI.ArcGIS.esriSystem.IVariantArray = New ESRI.ArcGIS.esriSystem.VarArray
With pVA
.Add(in_features)
.Add("H:\SwiftReach")
End With
GP.Execute("FeatureClassToShapefile", pVA, Nothing)
End SubIf you see anything that could still use some fixing let me know. I am no programmer, but I try my best...
Glad you got it working! I would pass the result of the call to the tool into a pGeoProcessorResult object, you can then test the status, did your code succeed or fail, you can also retrieve from this object the error message which may give some indication to why it failed. It's just better coding to be defensive.
i was wondering how to do that!
better?
thx max
I've always went the route of using the managed assemblies when using the Geoprocessor in .NET. Here's an example of how I run the Dissolve tool, using inputs representing either feature classes or feature layers (either as path names or as ArcObjects) and all the associated functions to execute the Geoprocessor and return a feature class.
Dim GP As ESRI.ArcGIS.Geoprocessor.Geoprocessor 'set in the beginning of the code
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
Using releaser As New ESRI.ArcGIS.ADF.ComReleaser
releaser.ManageLifetime(DissolveDS)
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 ReturnObjectfromResult(Result, "Feature Class")
End Using
Catch ex As Exception
System.Windows.Forms.MessageBox.Show(ex.ToString, "Dissolve error")
Return Nothing
End Try
End Function
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
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 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)
End Select
Catch ex As Exception
System.Windows.Forms.MessageBox.Show(ex.ToString, "Return FeatureClass error")
Return Nothing
End Try
End Function
Hi Ken Buja,
I've always wondered if there was an advantage in using managed assemblies (the way you have shown) compared to the way I have done it? I've continued to use the method I use to call geo-processing tools simply because it was the method I first learnt and I can remember how to do it! ![]()
Duncan
I don't know if there are computational advantages of using the managed assemblies. I've gravitated to them because you know exactly which parameter is getting set (and in no particular order) as opposed to having to remember their correct order when adding them to the IVariantArray