Show All Layers

Created:12/6/2001
Description:

This function will set the visible property of all layers in the current document to 'true', and refresh the display. The intent of this sample is to demonstrate the programmatic interaction with the document and layers.

How to use:
  1. Copy and paste the following code into an ArcMap or ArcScene VBA session and call the macro 'ShowAllLayers'.
'
' set the visibility of all layers to TRUE
'
Public Sub ShowAllLayers()
  TurnAllLayers True
End Sub
'
' set the visibility of all layers to bVisible State
'
Private Sub TurnAllLayers(bVisible As Boolean)
  On Error GoTo TurnAllLayers_ERR
  
  Dim i As Integer
  Dim pSxDoc As ISxDocument
  Dim pLayerArray  As IArray
  Dim pLayer As ILayer
  Dim pDoc As IBasicDocument
  Dim pMxDoc As IMxDocument
  
  Set pLayerArray = GetDocLayers()
  
' exit if no layers in the doc:
  If pLayerArray Is Nothing Then Exit Sub
  
' get the current document:
  Set pDoc = Application.Document
  
  If Not pDoc Is Nothing Then
' QI for Scene or Map document:
      If TypeOf pDoc Is ISxDocument Then
          Set pSxDoc = pDoc
      ElseIf TypeOf pDoc Is IMxDocument Then
          Set pMxDoc = pDoc
      End If
  End If
  
  If pLayerArray.Count <  1 Then Exit Sub

' set visibility for each layer:
  For i = 0 To pLayerArray.Count - 1
      Set pLayer = pLayerArray.Element(i)
  
      pLayer.Visible = bVisible
      
      If Not pSxDoc Is Nothing Then
          pSxDoc.Scene.SceneGraph.Invalidate pLayer, True, False
          pSxDoc.ContentsView(0).Refresh pLayer
      ElseIf Not pMxDoc Is Nothing Then
          pMxDoc.ContentsView(0).Refresh pLayer
      End If
      

  Next
  
  RefreshDocument
  

  Exit Sub
  
TurnAllLayers_ERR:
  Debug.Print "TurnAllLayers_ERR: " & Err.Description
  Debug.Assert 0
End Sub
'
' return an IEnumLayer of layers in current document
'
Private Function GetDocLayers(Optional bOnlySelected As Boolean) As IArray
  Dim pSxDoc As ISxDocument
  Dim pMxDoc As IMxDocument
  Dim pTOC  As IContentsView
  Dim i As Integer
  Dim pScene As IScene
  Dim ppSet As ISet
  Dim p
  Dim pLayers As IArray
  Dim pLayer As ILayer
  
  On Error GoTo GetDocLayers_ERR

  Set GetDocLayers = New esriCore.Array
  
  If TypeOf Application.Document Is ISxDocument Then
      Set pSxDoc = Application.Document
      Set pScene = pSxDoc.Scene
      
      If Not bOnlySelected Then
          Set pLayers = New esriCore.Array
          For i = 0 To pScene.layerCount - 1
              pLayers.Add pScene.Layer(i)
          Next
          Set GetDocLayers = pLayers
          Exit Function
      Else
          Dim pSxTOC As ISxContentsView
          Set pSxTOC = pSxDoc.ContentsView(0)
      End If
      
  ElseIf TypeOf Application.Document Is IMxDocument Then
      Set pMxDoc = Application.Document
      
      If Not bOnlySelected Then
          Set pLayers = New esriCore.Array
          For i = 0 To pMxDoc.FocusMap.layerCount - 1
              pLayers.Add pMxDoc.FocusMap.Layer(i)
          Next
          Set GetDocLayers = pLayers
          Exit Function
      Else
          Set pTOC = pMxDoc.ContentsView(0)
      End If
      
  End If
  
  If Not pTOC Is Nothing Then
      If IsNull(pTOC.SelectedItem) Then Exit Function
      Set p = pTOC.SelectedItem
  ElseIf Not pSxTOC Is Nothing Then
      If IsNull(pSxTOC.SelectedItem) Then Exit Function
      Set p = pSxTOC.SelectedItem
  End If
  
  Set pLayers = New esriCore.Array
  

  If TypeOf p Is ISet Then
      Set ppSet = p
      ppSet.Reset
      For i = 0 To ppSet.Count
          Set pLayer = ppSet.Next
          If Not pLayer Is Nothing Then
              pLayers.Add pLayer
          End If
      Next
  ElseIf TypeOf p Is ILayer Then
      Set pLayer = p
      pLayers.Add pLayer
  End If
  
  Set GetDocLayers = pLayers
  
  Exit Function
  
GetDocLayers_ERR:
  Debug.Print "GetDocLayers_ERR: " & Err.Description
  Debug.Assert 0

End Function


Public Sub RefreshDocument(Optional bInvalidateSelection As Boolean)
  
  On Error GoTo RefreshDocument_ERR
  
  If TypeOf Application.Document Is ISxDocument Then
      Dim pSxDoc As ISxDocument
      Set pSxDoc = Application.Document
      pSxDoc.Scene.SceneGraph.RefreshViewers
  Else
      Dim pMxDoc As IMxDocument
      Set pMxDoc = Application.Document
      pMxDoc.ActiveView.Refresh
  End If
  
  Exit Sub
  
RefreshDocument_ERR:
  Debug.Print "RefreshDocument_ERR: " & Err.Description
  Debug.Assert 0
  
End Sub