Monthly Archives: May 2010

ESRI Snippet: Adding a graphic to a map

Really handy sometimes for debugging to make sure you are dealing with the correct geometry:

Public Sub AddGraphicToMap(ByVal map As IMap, ByVal geometry As IGeometry)

 Dim graphicsContainer As IGraphicsContainer
 Set graphicsContainer = map

 Dim element As IElement
 Set element = Nothing

 Dim rgbColor As IRgbColor
 Set rgbColor = New rgbColor
 With rgbColor
 .Red = 111
 .Green = 111
 .Blue = 111
 .UseWindowsDithering = True
 End With

 Dim simpleFillSymbol As ISimpleFillSymbol
 Set simpleFillSymbol = New simpleFillSymbol

 simpleFillSymbol.Color = rgbColor
 simpleFillSymbol.Style = esriSFSForwardDiagonal

 Dim fillShapeElement As IFillShapeElement
 Set fillShapeElement = New PolygonElement

 fillShapeElement.Symbol = simpleFillSymbol
 Set element = fillShapeElement

 If Not (element Is Nothing) Then
 element.geometry = geometry
 graphicsContainer.AddElement element, 0
 End If

End Sub

ESRI Code Snippet: Find Layer by Name

A handy code snippet from Neil Clemmons who is a prolific trouble shooter on the ESRI forums.  Sometimes it’s handy to have a bit of VBA code to find a layer by name and this code does the job very well.  Of course you can apply the same logic in any language and application context you see fit.

Public Function GetLayerByFeatureClass(vMap As Variant, sFeatureClassName As String, Optional bMessages As Boolean = True) As ILayer
     Dim pApplication As IApplication
     Dim pMxDocument As IMxDocument
     Dim pCompositeLayer As ICompositeLayer
     Dim pMap As IMap
     Dim l As Long
     Dim m As Long
     On Error GoTo ErrorHandler
     ' Get map reference from vMap parameter.
     If TypeOf vMap Is IMxApplication Then
          Set pApplication = vMap
          Set pMxDocument = pApplication.Document
          Set pMap = pMxDocument.FocusMap
     ElseIf TypeOf vMap Is IMxDocument Then
          Set pMxDocument = vMap
          Set pMap = pMxDocument.FocusMap
     ElseIf TypeOf vMap Is IMap Then
          Set pMap = vMap
          If bMessages Then MsgBox "vMap parameter does not implement an interface supported by this function.", vbOKOnly, "GetLayerByFeatureClass()"
          Exit Function
     End If
     ' Process each layer in the map.
     For l = 0 To pMap.LayerCount - 1
          Set GetLayerByFeatureClass = LayerByFeatureClass(pMap.Layer(l), sFeatureClassName, bMessages)
          If Not GetLayerByFeatureClass Is Nothing Then Exit Function
     Next l
     ' Give layer not found message.
     If bMessages Then MsgBox "The layer '" & sFeatureClassName & "' could not be found in the map.", vbOKOnly, "GetLayerByFeatureClass()"
Exit Function
     If bMessages Then ErrorMessage "modAOUtilities:GetLayerByFeatureClass()"
End Function

' Written by: Neil Clemmons
' Date Created: 12/26/2002
' Date Modified:
' Description:  Helper function for GetLayerByFeatureClass().  Uses recursion to find layers that may be inside nested
'               group layers.  If this function returns a value other than Nothing, the layer has been found and
'               the search will stop.  Otherwise, the search will continue until all layers have been processed.
Private Function LayerByFeatureClass(pLayer As ILayer, sName As String, bMessages As Boolean) As ILayer
     Dim pDataset As IDataset
     Dim pReturnLayer As ILayer
     Dim pFeatureLayer As IFeatureLayer
     Dim pCompositeLayer As ICompositeLayer
     Dim l As Long
     On Error GoTo ErrorHandler
     ' If layer is a feature layer, get it's underlying dataset.
     If TypeOf pLayer Is IFeatureLayer Then
          Set pFeatureLayer = pLayer
          Set pDataset = pFeatureLayer.FeatureClass
          ' Set return layer and exit if this layer is the layer being sought.
          If UCase$(pDataset.Name) = UCase$(sName) Then
               Set LayerByFeatureClass = pLayer
               Exit Function
          End If
     End If
     ' Exit if layer is not a group layer.  Not setting return value will cause search to continue.
     If Not TypeOf pLayer Is IGroupLayer Then Exit Function
     ' Process each layer in the group layer.
     Set pCompositeLayer = pLayer
     For l = 0 To pCompositeLayer.Count - 1
          ' Use recursion to traverse nested group layers.
          Set pReturnLayer = LayerByFeatureClass(pCompositeLayer.Layer(l), sName, bMessages)
          ' If layer was found, set return value and exit.
          If Not pReturnLayer Is Nothing Then
               Set LayerByFeatureClass = pReturnLayer
               Exit Function
          End If
     Next l
Exit Function
     If bMessages Then ErrorMessage "modAOUtilitiesLayerByName()"
End Function