I have a simple project which involves the use of several buttons in order to make various datasets visible depending on which category is required at the time.
It works fine using the popular GetLayerByName function (http://bit.ly/Ha2iGK) but unfortunately there are duplicate layer names scattered across the mxd. To get around this I modified it slightly to look at both the layer name and the layer description, but as raster catalogs are unable to store a description (this is unfortunately not in a geodatabase in case that makes any difference) the code falls over.
I've got a separate routine that clears all of the layers once the button is clicked and then it turns on the layers listed under another sub using [layername], [layerdescription]:
Set pLayer = GetLayerByName("Wet (483)", "L6_Dry")
If Not pLayer Is Nothing Then
pLayer.Visible = True
End If
Does anyone have any ideas or suggestions on how I can get it to skip/ignore the catalogs? I've never had much dealing with them inside VBA so any help at all would be greatly appreciated.
Public Function GetLayerByName(sLayerName As String, sLayerDesc As String) As ILayer
Dim pMxDocument As IMxDocument
Dim pCompositeLayer As ICompositeLayer
Dim pMap As IMap
Dim l As Long
Dim m As Long
Set pMxDocument = ThisDocument
Set pMap = pMxDocument.FocusMap
For l = 0 To pMap.LayerCount - 1
Set GetLayerByName = LayerByName(pMap.Layer(l), sLayerName, sLayerDesc)
If Not GetLayerByName Is Nothing Then Exit Function
Next l
End Function
Private Function LayerByName(pLayer As ILayer, sName As String, sLayerDesc As String) As ILayer
Dim pReturnLayer As ILayer
Dim pCompositeLayer As ICompositeLayer
Dim l As Long
Dim pLayerGenProp As ILayerGeneralProperties
'#### This is where the error appears!####
Set pLayerGenProp = pLayer
Dim thestring As String
thestring = pLayerGenProp.LayerDescription
If UCase$(pLayer.Name) = UCase$(sName) And UCase$(thestring) = UCase$(sLayerDesc) Then
Set LayerByName = pLayer
Exit Function
End If
If Not TypeOf pLayer Is IGroupLayer Then Exit Function
Set pCompositeLayer = pLayer
For l = 0 To pCompositeLayer.Count - 1
Set pReturnLayer = LayerByName(pCompositeLayer.Layer(l), sName, sLayerDesc)
If Not pReturnLayer Is Nothing Then
Set LayerByName = pReturnLayer
Exit Function
End If
Next l
End Function
-
It is better to use IMap.get_Layers, there you can specify layer type using uid and recursive option that elminates need in loops through composite layers help.arcgis.com/en/sdk/10.0/arcobjects_net/componenthelp/…megadrofan– megadrofan2012年04月02日 15:24:26 +00:00Commented Apr 2, 2012 at 15:24
1 Answer 1
You should test whether the layer is a Raster Catalog layer by using the code
If TypeOf pLayer Is IRasterCatalogLayer Then Exit Function
-
doh! thanks both, kenbuja's suggestion worked perfectly.Rich Stokes– Rich Stokes2012年04月03日 10:35:53 +00:00Commented Apr 3, 2012 at 10:35
-
Glad to help...don't forget to mark this as the answer.kenbuja– kenbuja2012年04月03日 13:24:12 +00:00Commented Apr 3, 2012 at 13:24