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.
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 use it, or net stuff in general, but there is a whole section on arcpy access which you will have to troll through
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.
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:
GP.FeatureclassToFeatureclass_conversion(in_features, out_path, out_name)
but to have it reference a layer within the mxd i am working for the in_features.
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.
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.
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:
GP.FeatureclassToFeatureclass_conversion(in_features, out_path, out_name)
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!
It looks like you are mixing geoprocessors, there is a COM object and a .Net object (well thats how I understand it), look at this page:
Duncan,
Again thanks for your continued help!
I feel like with that last tip i am getting closer for sure!
Here is the current state of the code:
Private Sub Export_to_shp() Dim GP As IGeoProcessor = New ESRI.ArcGIS.Geoprocessing.GeoProcessor Dim fctofcconversion As ESRI.ArcGIS.ConversionTools.FeatureClassToShapefile = New ESRI.ArcGIS.ConversionTools.FeatureClassToShapefile() Dim m_pMxDoc As ESRI.ArcGIS.ArcMapUI.IMxDocument m_pMxDoc = My.ArcMap.Document Dim in_features As IFeatureLayer = m_pMxDoc.FocusMap.Layer(2) fctofcconversion.Input_Features = in_features fctofcconversion.Output_Folder = "H:\SwiftReach" GP.Execute(fctofcconversion, Nothing) End Sub
the problem in this one is in the GP.Execute; error is:
Argument not specified for parameter 'pTrackCancel' of "Public Function Execute (Name as String, iValues as ESRI.arcGIS.esriSystem.IVariantArray, pTrackCancel as ESRI.arcGIS.esriSystem.ITrackCancel) As ESRI.ArcGIS.Geoprocessing.IGeoProcessorResult
So i tried to mess with it a little based on the error:
Private Sub Export_to_shp() Dim GP As IGeoProcessor = New ESRI.ArcGIS.Geoprocessing.GeoProcessor Dim fctofcconversion As ESRI.ArcGIS.ConversionTools.FeatureClassToShapefile = New ESRI.ArcGIS.Geoprocessing.GeoProcessorResult 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 'fctofcconversion.Input_Features = in_features 'fctofcconversion.Output_Folder = "H:\SwiftReach" GP.Execute(fctofcconversion, pVA, Nothing) End Sub
In this version the error is still in the gp.execute but specifically on the fctofcconversion variable:
Value of type 'ESRI.arcGIS.ConversionTools.FeatureClassToShapefile' cannot be converted to String
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)