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?
Again it looks like you are mixing the two types of geo-processors. See the VBA code below, note the type of geoprocessor I am using and how I call the tool by it's name
Dim pGeoProcessor As IGeoProcessor Set pGeoProcessor = New GeoProcessor With pGeoProcessor .OverwriteOutput = True .LogHistory = False .AddOutputsToMap = False .TemporaryMapLayers = True End With Dim pGeoProcessorResult As IGeoProcessorResult Dim pVariantArray As IVariantArray Set pVariantArray = New VarArray Set pVariantArray = New VarArray With pVariantArray .Add pFeatureLayer_Urban .Add "INTERSECT" .Add pFeatureLayer_Catchments .Add "" .Add "NEW_SELECTION" End With Set pGeoProcessorResult = pGeoProcessor.Execute("SelectLayerByLocation", pVariantArray, Nothing)
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
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'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
i was wondering how to do that!
better?
thx max
Duncan,
Thanks for trying to help me through this...
I am not sure i understand what you mean though. Do you mean pass the variables in in a traditional vb.net sense like?
Private Sub Export_to_shp()
Dim GP As IGeoProcessor = New ESRI.ArcGIS.Geoprocessing.GeoProcessor
'Declare variables for input
Dim in_features As IFeatureLayer
Dim out_path As String
Dim out_name As String
Dim m_pMxDoc As ESRI.ArcGIS.ArcMapUI.IMxDocument
m_pMxDoc = My.ArcMap.Document
in_features = m_pMxDoc.FocusMap.Layer(2)
out_path = "H:\SwiftReach"
out_name = "test.shp"
GP.FeatureclassToFeatureclass_conversion(in_features, out_path, out_name)
End Sub
Because when i try that in like in the code above; it blows up at:
with the error
NullReferenceException was unhandled
That is why i started to try to go down the rabbit hole of trying to call the variables through some other more "arcpy" like means.
I am beginning to think as Dan had mentioned above that there is too much mixing of styles here maybe? And that this may not work as i'd imagined it... Would be cool though; the arcpy tools are so much more intuitive to me.
Any continued ideas would be greatly appreciated!
Firstly you would not set your pointer to nothing as you have you need to point it to the GeoProcessor
Dim GP As IGeoProcessor = New GeoProcessor GP.Execute(.....)
If you want to access individual layers you need to use IMXDocument.focusmap.layer(x) where x is the index position. Suggest searching for examples in help and on forum.
I am beginning to think that trying to consolidate everything into one, might not be such a good idea. Arcobjects I am sure can accommodate this, but arcpy and python are the preferred combinations I suspect... perhaps that is the py part of arcpy.
Dan and Duncan,
thanks for the replys!
I was able to access the IGeoProcessor
Dim GP As IGeoProcessor = Nothing (works!)
My problem is hooking into the active map document and using the layers within the mxd i am working with in the vb.net project.
stuff like
mxd = arcpy.mapping.MapDocument('CURRENT')
in_features = arcpy.mapping.ListLayers(mxd)[0]
(stolen from arcpy scripts i found on the internet)
my first goal is to get something like this to run:
but to have it reference a layer within the mxd i am working for the in_features.
Just an idea but if what you can do easily in arcpy is actually calling one or more geo-processing tools then you could call the same tools within ArcObjects. The key interface I use is IGeoProcessor. There are loads of examples on this forum showing you how to call existing geo-processing tools.
I don't use it, or net stuff in general, but there is a whole section on arcpy access which you will have to troll through
http://desktop.arcgis.com/en/arcobjects/latest/net/webframe.htm#aecfa7f0-ac98-4a1c-ae36-f012211efb3c.htm
Les membres connectés peuvent publier, suivre les mises à jour, et plus encore. Nouveau ici ? Inscrivez-vous gratuitement.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.