Select to view content in your preferred language

Problem Converting VBA Script to Python

3156
3
05-27-2010 05:23 AM
ElizabethTaylor
Emerging Contributor
I'm trying to learn Python and am struggling to change the following VBA code to Python. Obviously not something for a begginer. One of my biggest struggles is understanding which modules to use. Help??!!!

Private Sub AsbuiltCopy_Click()

'Opens a dialog box to specify which directory the asbuilt files will be saved to

Dim pGxDialog As IGxDialog
Set pGxDialog = New GxDialog

pGxDialog.Title = "Select output folder"
pGxDialog.ButtonCaption = "OK"
pGxDialog.StartingLocation = "Catalog"
pGxDialog.AllowMultiSelect = False

Dim pBasicFilter As IGxObjectFilter
Set pBasicFilter = New GxFilterBasicTypes 'Filter for folders

Dim pFilterCol As IGxObjectFilterCollection
Set pFilterCol = pGxDialog
pFilterCol.AddFilter pBasicFilter, True

Dim pEnumGxObject As IEnumGxObject

If (pGxDialog.DoModalOpen(Application.hWnd, pEnumGxObject)) Then
Dim pGxObject As IGxObject
Set pGxObject = pEnumGxObject.Next
MsgBox "The files will be saved to:" & vbNewLine & pGxObject.FullName
vpath = pGxObject.FullName
End If

'Will display a progress bar as the files copy
Dim pStatus As IStatusBar
Dim pProgress As IStepProgressor
Dim i As Long

Set pStatus = Application.StatusBar
Set pProgress = pStatus.ProgressBar
pProgress.Position = 0
pStatus.ShowProgressBar "Copying .tiff Files...", 0, 500000, 1, True

For i = 0 To 500000
pStatus.StepProgressBar
Next

'code for copying asbuilt records

Dim pMxDocument As IMxDocument
Dim pMap As IMap
Dim pActiveView As IActiveView
Dim pFeatureLayer As IFeatureLayer
Dim pFeatureClass As IFeatureClass
Dim pFeatureSelection As IFeatureSelection
Dim pFeatureCursor As IFeatureCursor
Dim pFeature As IFeature
Dim dblSectionID As String
Dim x As Integer
Dim pfeaturecounter As Integer


Set pMxDocument = ThisDocument
Set pMap = pMxDocument.FocusMap
Set pActiveView = pMxDocument.ActiveView

Set pFeatureLayer = pMxDocument.SelectedLayer 'pMap.Layer(0)
Set pFeatureClass = pFeatureLayer.FeatureClass
Set pFeatureSelection = pFeatureLayer

'finds the layer with the selected features
pFeatureSelection.SelectionSet.Search Nothing, False, pFeatureCursor
Set pFeature = pFeatureCursor.NextFeature

'checks to make sure a line feature is selected
If pFeatureSelection.SelectionSet.Count = 0 Then

MsgBox "Please Select a line", vbCritical, "Error"

Else

'counts the number of selected features and sets it up as the loop ending value
pfeaturecounter = pMap.SelectionCount
x = 0

'begin loop
Do Until x = pfeaturecounter

'captures information from line feature's database
On Error Resume Next
dblpathname = pFeature.Value(pFeature.Fields.FindField("ASBUILT"))
dblendpathname = pFeature.Value(pFeature.Fields.FindField("PLAT_SHEET"))

'copies the asbuilt files to the folder previously specified
PathBeg = dblpathname
PathEnd = vpath & "\" & dblendpathname & ".tif"
FileCopy PathBeg, PathEnd

x = x + 1
Set pFeature = pFeatureCursor.NextFeature

Loop
End If
pStatus.HideProgressBar
End Sub
0 Kudos
3 Replies
JoelCalhoun
Deactivated User
I don't know VBA but it appears your script will find the layer with the selected line records in an open MXD and copy them.

Unless you can do advance python scripting using com components to work with ArcObjects there is not a way to replicate the same functionality you are wanting.  The standard level of python access is currently limited to geoprocessing tools.  In ArcGIS 10 with the arcpy.mapping the ability to work with map documents and layer files might provide the ability to do what you want.

Perhaps if you explained exactly what you need your script to accomplish there might be a way to replicate the functionality with python outside of an open map document.
0 Kudos
ElizabethTaylor
Emerging Contributor
You are correct in what the script does.  What I need is this:
1. Select a line
2. Read the selected line's attribute in a field (record #)
3. Go find the record # in a file folder (the record #'s TIFF image)
4. Copy the record # (TIFF image) into a different folder (be able to select the folder or create a new one)

I haven't been able find Python code for selecting a line within an MXD - not by a location or attribute, but by using the Feature Selection tool.  I might be able to figure out the rest, but it sounds to me that your statement is saying that unless an ArcObject already exists to perform the task, I need to be able to do advance Python scripting with com to get this script to work in ArcGIS 10.  I'm hoping arcpy.mapping offers the ability to do this, but without having Arcgis 10 yet, I'm not able to know.
0 Kudos
CedricWannaz
Emerging Contributor
Using Python and the Geoprocessor from ArcGIS Desktop 9.3(.1) allows you to do "easily" whatever you can do using the Desktop toolbox, and a little more regarding access to the internal data structures. So if you know exactly how you would do something with the toolbox, or if you want to access a table, a feature class, etc, element by element, it is likely that you will be able to do it using basic Python+Geoprocessor in a quite straightforward manner.

If what you want to do is outside of this scope on the GIS side, it is likely that you will have to dig deeper into the Arc' layers and use ArcObjects. The chunk of knowledge that you will need to use it in Python is much bigger/heavier than what is required for using the simpler (Desktop-) Geoprocessor.

That said, I never did specifically what you are trying to do I guess, so I don't know which of the Geoprocessor or ArcObjects you will need to use. Hopefully a further post will give you this information.

Cheers,
Cedric
0 Kudos