Exploring Arc Objects Article
Exploring Arc Objects Article
Sub ClearLayers()
If ArcMap displays a document with three data frames in layout view,
‘ get the active view
there will be three Maps attached to the MxDocument, the ActiveView Dim mxDoc As IMxDocument
property will return the PageLayout, and the FocusMap will return the Set mxDoc = Application.Document
Map corresponding to the data frame that is highlighted. The VBA
script listed in Figure 2 illustrates this part of the object model. The Dim activeView As IActiveView
script changes the ActiveView’s extent and zooms in by 75 percent. Set activeView = mxDoc.activeView
This script is fairly straightforward. The real work is getting the current If TypeOf activeView Is IMap Then
Dim map As IMap
extent, which is an IEnvelope interface, from the ActiveView, shrinking
Set map = activeView ‘ acquire the IMap interface
it, and making the modified envelope the new extent. This script operates
on the ActiveView through the IActiveView interface. The ActiveView map.ClearLayers ‘ remove the layers
can be either the PageLayout or a Map depending on what the end user is mxDoc.UpdateContents ‘ refresh the table of contents
looking at. This script will function correctly in either case. activeView.Refresh ‘ refresh the display
End If
28 ArcUser April—June2000 End Sub www.esri.com
Developer’s Corner
Looking at ScreenDisplay In the example in Figure 5, note that the drawing calls SetSymbol and
Both the PageLayout and the Map have an associated ScreenDisplay DrawText are bracketed within the StartDrawing and FinishDrawing
object that is used for all graphic rendering. ScreenDisplay maintains a methods.
DisplayTransformation. You can access the properties of DisplayTrans-
formation such as VisibleBounds and use the methods to convert coor- Working with Layers
dinates and distances between map and device units. This is illustrated Maps maintain a collection of layers. A layer is any object that imple-
by the diagram in Figure 4. ments the ILayer interface. Examples of layer objects include Feature-
Layer, GroupLayer, GraphicsLayer, AnnotationLayer, CadLayer, Tin-
Layer, and RasterLayer. The Layer(index) and LayerCount properties
IActiveView of the IMap interface can be used to navigate the layer collection. The
function illustrated in Figure 6 finds and returns the layer with the spec-
ified name.
Figure 6
FeatureLayers manage a feature selection and expose the IFeatureSe- Responding to Events
lection interface to manipulate the selection. The script in Figure 8 Up until now we’ve only considered manipulating ArcObjects using
makes a selection using a logical query with a SQL WHERE clause. scripts that start with the top level object and navigate to the object that
will be manipulated. An interesting aspect of ArcObjects is the ability
Figure 8 to wire into the object model and attach code that will be executed
Sub SelectFeatures() whenever something noteworthy happens such as the map’s selection
Dim mxDoc As IMxDocument changing. Many parts of the object model fire events. COM provides a
Set mxDoc = Application.Document framework for listening and responding to events.
‘ find the layer we’re going to select from
Dim lyr As IFeatureLayer If you are using VBA or Visual Basic, most of the plumbing required
Set lyr = FindLayer(mxDoc.FocusMap, “BigCypress”) to respond to events is done for you behind the scenes. You just have
to write a script for each event you are interested in and a script to
‘ get the layer’s selection interface start listening for events. The example shown in Figure 9 illustrates
Dim sel As IFeatureSelection how to work with events in the VBA environment. The following code
Set sel = lyr declares a global variable using the WithEvents statement. WithEvents
tells the development environment that the object variable will be used
‘ create a query filter
Dim filter As IQueryFilter
to respond to the object’s events.
Set filter = New QueryFilter
Dim WithEvents g_Map As map
‘ set up the where clause and the spatial reference
filter.WhereClause = “NAME = ‘Hampton’” After the variable is declared, the variable can be chosen from the
Object drop-down list in the VBA interface. Clicking the Procedure
Dim shapeField As String drop-down list will show the various events that are exposed by the
shapeField = lyr.FeatureClass.ShapeFieldName Map. This example uses the SelectionChanged method to attach code
Set filter.OutputSpatialReference(shapeField) =_
mxDoc.FocusMap.SpatialReference
to that event. The script in Figure 9 responds to the SelectionChanged
event that is fired by the map whenever the selection changes and popu-
‘ invalidate the current selection on the display lates a ListBox in a UserForm with the names of the selected features.
mxDoc.ActiveView.PartialRefresh_
esriViewGeoSelection, Nothing, Nothing Figure 9
Private Sub g_Map_SelectionChanged()
‘ select features and invalidate the new selection ‘ show the form if it’s not visible
sel.SelectFeatures filter,_ If Not UserForm1.Visible Then
esriSelectionResultNew, False UserForm1.Show vbModeless
mxDoc.ActiveView.PartialRefresh_ End If
esriViewGeoSelection, Nothing, Nothing
‘ clear any previous results in the list
‘ notify everyone that we’ve modified the selection UserForm1.ListBox1.Clear
Dim selEvents As ISelectionEvents
Set selEvents = mxDoc.FocusMap ‘ get the map’s selection
selEvents.SelectionChanged Dim activeView As IActiveView
End Sub Set activeView = g_Map
Figure 10
Sub StartListening()
Dim mxDoc As IMxDocument
Set mxDoc = Application.Document
Set g_Map = mxDoc.FocusMap
End Sub