[C008] 15 examples of ArcGIS VBA + AO

Source: Internet
Author: User

1.

Sub MyMacro ()
Dim pMxDocument As IMxDocument 'map document
Set pMxDocument = Application. Document 'to obtain the documents of the current Application
MsgBox pMxDocument. FocusMap. name' displays the name of the current map
End Sub

2.

Sub MyMacro ()
Dim pMxDocument As IMxDocument 'map document
Dim pMaps As IMaps Gallery
Dim pMap As IMap Map
Set pMxDocument = Application. Document 'to obtain the documents of the current Application
Set pMaps = pMxDocument. Maps 'get the gallery of the current map document
If pMaps. Count> 1 then', If the number of maps in the gallery is greater than 1
Set pMap = pMaps. Item (1) 'gets the first map in the gallery
MsgBox pMap. name' displays the Map name
End If
End Sub

3.

Sub MyMacro ()
Dim pMxDocument As IMxDocument 'map document
Dim pMap As IMap Map
Dim lCount As Long
Dim lIndex As Long
Set pMxDocument = Application. Document 'to obtain the documents of the current Application
Set pMap = pMxDocument. FocusMap 'get current map
LCount = 0
For lIndex = 0 To (pMap. LayerCount-1)
If TypeOf pMap. Layer (lIndex) Is IFeatureLayer Then ', If the lIndex Layer of the current map Is of the IFeatureLayer type
LCount = lCount + 1' counter plus 1
End If
Next lIndex
MsgBox "Number of the feature layers "&_
"In the active map:" & lCount "displays the total number of element layers of the current map.
End Sub

4.

Sub MyMacro ()
Dim pMxDocument As IMxDocument 'get the documents of the current application
Dim pMaps As IMaps Gallery
Dim pMap As IMap Map
On Error GoTo SUB_ERROR 'Error handling
Set pMxDocument = Application. Document 'to obtain the documents of the current Application
Set pMaps = pMxDocument. Maps 'get the gallery of the current map document
Set pMap = pMaps. Item (1) 'gets the first map in the gallery
MsgBox pMap. name' displays the Map name
Exit Sub
SUB_ERROR: 'row tag
MsgBox "Error:" & Err. Number & "-" & Err. Descripttion "displays Error count and Error message
End Sub

5.

'Is layer visibility
Public Sub MakeLayerVisible ()
Dim pMxDocument As IMxDocument 'map document
Dim pMap As IMap Map
Dim pFeatureLayer As IFeatureLayer
Dim pActiveView As IActiveView activity View
Dim pContentsView As IContentsView window content table

'Obtain the first layer of the map
Set pMxDocument = ThisDocument 'get the document of the current application
Set pMap = pMxDocument. FocusMap 'get current map
Set pFeatureLayer = pMap. Layer (0) 'to obtain the first Layer of the current map

'If the element layer is invisible, make it visible
If Not pFeatureLayer. Visible Then
PFeatureLayer. Visible = True
End If

'Refresh the map
Set pActiveView = pMap Set the current map as an active Map
PActiveView. Refresh 'refresh

'Refresh the window content table
Set pContentsView = pMxDocument. CurrentContentsView get the window content table of the current map document
PContentsView. Refresh pFeatureLayer 'refresh
End Sub

6.

'Query elements by name'
Private Function GetCountyFeature (pFeatureLayer As IFeatureLayer, strCountyName As String) As IFeature

'Search for element classes
Dim pFeatureClass As IFeatureClass 'element class
Dim pQueryFilter As iqueryfilter' query Filter
Dim pFeatureCursor As IFeatureCursor

Set pFeatureClass = pFeatureLayer. FeatureClass 'gets the element class from the element layer
Set pQueryFilter = New QueryFilter to create a New query filter.
PQueryFilter. WhereClause = "NAME = '" & strCountyName & "'" 'search by county NAME
Set pFeatureCursor = pFeatureClass. Search (pQueryFilter, False) 'to obtain the queried Element Object

'Retrieve Elements
Dim pFeature As IFeature

Set pFeature = pFeatureCursor. NextFeature 'gets The next element of the query result
If pFeature Is Nothing Then 'If this element does not exist
Set GetCountyFeature = Nothing 'Return value is Set to null
Else
Set GetCountyFeature = pFeature Set this element to return value
End If
End Function

7.

'Zoom in/out
Sub MyZoom ()

Dim pDoc As IMxDocument 'map document
Dim pActiveView As IActiveView Activity Map
Dim pEnv As IEnvelope 'display range

Set pDoc = Application. Document 'to get the current Document, which is equivalent to ThisDoucument
Set pActiveView = pDoc. activeView to get the current activity map

Set pEnv = pActiveView. Extent 'get the current display range
PEnv. Expand 0.5, 0.5, and True are scaled up by two times. If you change 0.5 to 2, the value is halved.
PActiveView. Extent = pEnv 'update display range
PActiveView. Refresh 'refresh

End Sub


MxApplication represents ArcMap itself and only manages one document MxDocument (ArcMap is a single document interface ). MxDocument manages a set of Map objects and a PageLayout object. In the data view, ActiveView is a Map, while in the page view, ActiveView is PageLayout. In any view, there is always only one FocusMap, and the display operation is performed on ActiveView.

8.

'Full graph:
Sub FullExtentPlus ()

Dim pDoc As IMxDocument 'map document
Dim pActiveView As IActiveView Activity Map

Set pDoc = Application. Document 'Get the current map Document
Set pActiveView = pDoc. activeView to get the current activity map

PActiveView. Extent = pDoc. activeView. FullExtent full Graph Display
PActiveView. Refresh 'refresh the current view

End Sub

9.

'Clear Layers
Private Sub ClearLayers ()

Dim pDoc As IMxDocument 'map document
Dim pActiveView As IActiveView Activity Map
Dim pMap As IMap Map

Set pDoc = Application. Document 'Get the current map Document
Set pActiveView = pDoc. activeView to get the current activity map

If TypeOf pActiveView Is IMap then', If the current active map Is in data view mode
Set pMap = pActiveView to get the current map
PMap. ClearLayers clear all layers
PDoc. UpdateContents update window content table
PActiveView. Refresh 'refresh
End If

End Sub

10.

'Find Layers
Function FindLayer (map As IMap, name As String) As ILayer

Dim I As Integer

For I = 0 To map. LayerCount-1 'the index at the first layer is 1
If map. Layer (I). name = name then', If the name of Layer I is name
Set FindLayer = map. Layer (I) 'gets and returns this Layer
Exit Function
End If
Next

End Function

11.

'Add Layers
Sub AddLayer ()

Dim wksFact As IWorkspaceFactory 'Workspace Manager
Dim wks As IFeatureWorkspace 'element Workspace
Dim fc As IFeatureClass 'element class
Dim lyr As IFeatureLayer
Dim ds As IDataset Dataset
Dim mxDoc As IMxDocument 'map document
Dim map As IMap map

Set wksFact = New ShapefileWorkspaceFactory 'create a Shape Workspace Manager
Set wks = wksFact. OpenFromFile ("c: \ Data \ shp", 0) 'to get the Workspace
Set fc = wks. OpenFeatureClass ("BigCypress") 'Get element class
Set lyr = New FeatureLayer create element layer
Set lyr. FeatureClass = fc 'add element classes to the element layer
Set ds = fc 'get the dataset
Lyr. name = ds. name' use the name of the element class to name the element layer.
Set pDoc = Application. Document 'Get the current map Document
Set mxmap = mxDoc. FocusMap 'get current map
Map. AddLayer lyr 'add Layers

End Sub

12.

'Add text
Private Sub Hello ()

Dim pDoc As IMxDocument 'map document
Dim pActiveView As IActiveView Activity Map
Dim sym As itextsymbol' text symbol
Dim bnds As IArea

Set pDoc = Application. Document 'Get the current map Document
Set pActiveView = pDoc. activeView to get the current activity map

Set sym = New textsymbol' to create a text symbol
Sym. Font. size = 18 'set the Font size

With pActiveView. ScreenDisplay
Set bnds =. DisplayTransformation. VisibleBounds 'get the visible range
. StartDrawing. hDC, esriNoScreenCache
. SetSymbol sym' set the symbol to be drawn
. DrawText bnds. Centroid, "Hello" 'add text
. FinishDrawing
End

End Sub

13.

'Select Elements
Sub SelectFeatures ()

Dim mxDoc As IMxDocument 'map document
Dim lyr As IFeatureLayer
Dim sel As IFeatureSelection
Dim filter As IQueryFilter
Dim selEvents As ISelectionEvents '???

Set mxDoc = Application. Document 'to get the current map Document
Set lyr = FindLayer (mxDoc. FocusMap, "BUILDING") 'Call the FindLayer function to find the layer.
Set sel = lyr Set the layer to select Set
Set filter = New queryfilter' create a query filter
Filter. WhereClause = "BDNAME = 'lab building a'" 'sets the where clause
Sel. SelectFeatures filter, esriSelectionResultNew, false' select the elements that meet the conditions
MxDoc. activeView. PartialRefresh esriViewGeoSelection, Nothing, Nothing 'plot the selected Element
Set selEvents = mxDoc. FocusMap '???
SelEvents. SelectionChanged 'notification System Selection has changed

End Sub

14.

'Listeners

Dim WithEvents g_Map As map

Private Sub UIButtonControl1_Click ()
Dim mxDoc As IMxDocument 'map document
Dim lyr As IFeatureLayer
Dim sel As IFeatureSelection
Dim filter As IQueryFilter
Dim selEvents As ISelectionEvents '???

Set g_Map = mxDoc. FocusMap 'get current map

Set mxDoc = Application. Document 'to get the current map Document
Set lyr = FindLayer (mxDoc. FocusMap, "BUILDING") 'Call the FindLayer function to find the layer.
Set sel = lyr Set the layer to select Set
Set filter = New queryfilter' create a query filter
Filter. WhereClause = "BDNAME = 'lab building a'" 'sets the where clause
Sel. SelectFeatures filter, esriSelectionResultNew, false' select the elements that meet the conditions
MxDoc. activeView. PartialRefresh esriViewGeoSelection, Nothing, Nothing 'plot the selected Element
Set selEvents = mxDoc. FocusMap '???
SelEvents. SelectionChanged 'notification System Selection has changed

End Sub

15.

'Find Layers
Function FindLayer (map As IMap, name As String) As ILayer

Dim I As Integer

For I = 0 To map. LayerCount-1 'the index at the first layer is 1
If map. Layer (I). name = name then', If the name of Layer I is name
Set FindLayer = map. Layer (I) 'gets and returns this Layer
Exit Function
End If
Next

End Function

Private Sub g_Map_SelectionChanged ()

Dim activeView As IActiveView Activity Map
What are the elements listed by Dim featureEnum As IEnumFeature?
Dim feat As IFeature Element
Dim index As Long
Dim Msg As String

Set activeView = g_Map 'get current map
Set featureEnum = activeView. Selection list selected elements
FeatureEnum. Reset 'Restore to initial order
Set feat = featureEnum. Next 'get the first element in the selection Set
Do While Not feat Is Nothing 'If the element exists
Index = feat. Fields. FindField ("Name") 'to obtain the index value of the Name field
If index <>-1 Then MsgBox Msg & Chr (13) & Chr (10) & feat. Value (index) 'shows the Name of the element.
Set feat = featureEnum. Next 'move to the Next element in the selection Set
Loop

End Sub

Source: http://www.cnblogs.com/atravellers/archive/2010/01/13/1646606.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.