Link to home
Start Free TrialLog in
Avatar of justearth
justearthFlag for United States of America

asked on

Visual Basic Application (VBA) to Python?

Hello,
Can this visual basic code be converted to python? If so,  how?

Thanks,
MJ
On Error GoTo ErrorHandler
 
   Dim pmxdoc As IMxDocument
 
    Set pmxdoc = thisdocument
    Dim pmap As IMap
    Set pmap = pmxdoc.FocusMap
Dim path As String
Dim yesno As String
yesno = MsgBox("Is your grid located at the bottom of the TOC", vbYesNo)
 
If yesno = 7 Then
 
    Exit Sub
    End If
 
path = Module1.GetFolderName("Path to save new grid files")
'path = InputBox("Path for files")
Dim i As Integer
 
 
    ' Get the input raster to be clipped
    Dim player As ILayer
    Set player = pmap.Layer(pmap.LayerCount - 1)
    Dim pRLayer As IRasterLayer
    Set pRLayer = player
    Dim pInRaster As IRaster
    Set pInRaster = pRLayer.Raster
   
 
    ' Get the input for the analysis mask
    Dim pGeoDs As IGeoDataset
    Dim pLy As ILayer
    Dim pAllLayers As IEnumLayer
Set pAllLayers = pmap.Layers
Do While Not player Is Nothing
 
 
 
Set pLy = pAllLayers.Next
 
Dim rastername As String
 
 
If TypeOf pLy Is IRasterLayer Then GoTo doodie
 
 
    Dim pFeatureLy As IFeatureLayer
    Set pFeatureLy = pLy
    Set pGeoDs = pFeatureLy.FeatureClass
 
    ' Create a Spatial operator
    Dim pAlgbOp As IMapAlgebraOp
    Set pAlgbOp = New RasterMapAlgebraOp
    ' Set output workspace
    Dim pEnv As IRasterAnalysisEnvironment
    Set pEnv = pAlgbOp
    Dim pWS As IWorkspace
    Dim pWSF As IWorkspaceFactory
    Set pWSF = New RasterWorkspaceFactory
    Set pWS = pWSF.OpenFromFile(path, 0)
    Set pEnv.OutWorkspace = pWS
    Set pEnv.Mask = pGeoDs
'set the extent here
    pEnv.SetExtent esriRasterEnvValue, pGeoDs.Extent
       
    ' Bind a raster
    Call pAlgbOp.BindRaster(pInRaster, "R1")
   
    ' Perform re-evaluation of input raster
    Dim pOutRaster As IRaster
    Dim str As String
    str = "gr_" & pLy.Name & " = [R1]"
 
   
    Set pOutRaster = pAlgbOp.Execute(str)
    'Set pOutRaster = pAlgbOp.Execute ("fd = [R1]")
   
 
    ' Add it into ArcMap
    Set pRLayer = New RasterLayer
    pRLayer.CreateFromRaster pOutRaster
        pRLayer.Name = pLy.Name
    pmap.AddLayer pRLayer
 
 
        Loop
       
  
       
doodie:
MsgBox "Done."
      Dim pactive As IActiveView
      Set pactive = pmxdoc.ActiveView
      pactive.Refresh
 
  Exit Sub
ErrorHandler:
  HandleError True, "ICommand_OnClick " & c_sModuleFileName & " " & GetErrorLineNumberString(Erl), Err.Number, Err.Source, Err.Description, 3

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of pepr
pepr

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of justearth

ASKER

I use python and you, pepr, have helped me quite a bit in the past with python and ESRI Arc Map. I know that a lot of the functionality is 'called' differently in the VB vs python scripts. But I thought if the VB code was changed to python, then I could try to figure out switching the esri specific stuff.

Thanks again,
JE
Avatar of pepr
pepr

Well, I have no experience with ESRI Arc Map. I only know it exists. I can only help you to explain something from Python/other point of view and why things are done in Python this way and in COM that way. Not knowing ArcMap, I can barely rewrite the script. But I can help YOU to do that. You already know what are the ArcMap objects about.
Thanks,
I will get back to this real soon. I will research what the difference in the commands are between VB and Python.

Cheers,
JE
I may need to close /accept your answer for this question for now, as I have not been able to advance upon it yet and it may take considerable time for me to do so.

Thanks,
JE
Thanks, perhaps more to come in the future.