Layers

2817
7
Jump to solution
08-17-2010 05:07 AM
DanielTuracek
New Contributor
i have dataframe with these layers:

and i have this code:
Sub a()
    Dim s As String
    Dim aLayer As ILayer
    Dim a, c As String
    Dim pMxdoc As IMxDocument
    Set pMxdoc = Application.Document
    Dim pMap As iMap
    Set pMap = pMxdoc.FocusMap
    Dim b As String
    b = pMap.name + ":"
    Dim pEnumLayer As IEnumLayer
    Dim pId As New uid
    Dim pparentlayer As ILayer
    Dim kolekcia As New Collection
    Dim out As Variant
    Set pEnumLayer = pMap.LAyers
    Set aLayer = pEnumLayer.Next
   
Do Until aLayer Is Nothing
  Set pparentlayer = GetParent(pMxdoc.FocusMap, _
                    aLayer)
    If Not pparentlayer Is Nothing Then
        a = a + pparentlayer.name + ":"
       
    End If
   
        s = b + a + aLayer.name
        out = s & Chr(13)
        kolekcia.Add out
    Set aLayer = pEnumLayer.Next
Loop
   
    For Each out In kolekcia
        List = List & out
    Next
   
MsgBox b & Chr(13) & List

End Sub
Function GetParent(pMap As iMap, aLayer As ILayer) As ILayer
    If pMap.LayerCount = 0 Then Exit Function
    Dim pUID As New uid
    pUID.value = "{EDAD6644-1810-11D1-86AE-0000F8751720}"
    Dim pEnumLayer As IEnumLayer
    Set pEnumLayer = pMap.LAyers(pUID, True)
    Dim pCLayer As ICompositeLayer
    Set pCLayer = pEnumLayer.Next
Do Until pCLayer Is Nothing
    Dim k As Integer
    For k = 0 To pCLayer.Count - 1
        If pCLayer.layer(m) Is aLayer Then
            Set GetParent = pCLayer
        End If
    Next k

        Set pCLayer = pEnumLayer.Next
Loop
End Function

which gives me this msgbox:



As you can see i want to get something like layers tree from this code. but there is mistake."New group layer" is repeatly appearing there where it isnt correct.
can anybody responsible who is good programmer explain what i doing bad?
Or have anyone of you another code for that?
0 Kudos
1 Solution

Accepted Solutions
by Anonymous User
Not applicable
Original User: thaviti

Please find the explanation against lines

Sub GetLayerNames()
Dim s As String
Dim aLayer As ILayer
Dim a, c As String
Dim pMxdoc As IMxDocument
Set pMxdoc = Application.Document
Dim pMap As IMap
Set pMap = pMxdoc.FocusMap ''get the active map
Dim b As String
b = pMap.Name + ":"
Dim pEnumLayer As IEnumLayer
Dim pId As New UID
Dim pparentlayer As ILayer
Dim coll As New Collection
Dim out As Variant
Set pEnumLayer = pMap.Layers(Nothing, False)
''As second parameter is False, this Gets the immediate Layers in the map. i.e. only the direct layers ''under map

Set aLayer = pEnumLayer.Next ''loop through retrieved layer enumeration
Dim parentLayerName As String

Do Until aLayer Is Nothing
coll.Add b & aLayer.Name ''add the layer name appended with map name to the collection

''If the layer is GroupLayer then process its child layers
If (TypeOf aLayer Is IGroupLayer) Then

''Pass the layer and the layer full name (Map name + layer name) to the method along with the ''collection variable

AddLayerNames aLayer, b & ":" & aLayer.Name, coll

End If

''Get the next layer in the enumeration and process the same

Set aLayer = pEnumLayer.Next
Loop

For Each out In coll
List = List & out & vbNewLine
Next

MsgBox b & Chr(13) & List

End Sub

''This method adds the child layer name's in the group layer to the collection. If the child layer is ''also a group layer then that child layer itself is proceed with this method to get its child layer ''names and the same to the collection variable.

Sub AddLayerNames(pGroupLayer As IGroupLayer, pGroupLayerFullName As String, ByRef collectionVariable As Collection)

If pGroupLayer Is Nothing Then Exit Sub

Dim compLayer As ICompositeLayer
Set compLayer = pGroupLayer
Dim childLayer As ILayer


Dim i As Integer
For i = 0 To compLayer.count - 1
Set childLayer = compLayer.Layer(i)


collectionVariable.Add pGroupLayerFullName & ":" & childLayer.Name

''append the child layer name to parent layer full name and pass the same to the method to process ''for child layers

If TypeOf childLayer Is IGroupLayer Then
AddLayerNames childLayer, pGroupLayerFullName & ":" & childLayer.Name, collectionVariable
End If

Next

End Sub

View solution in original post

0 Kudos
7 Replies
by Anonymous User
Not applicable
Original User: thaviti

Following code may help u.

Sub GetLayerNames()
Dim s As String
Dim aLayer As ILayer
Dim a, c As String
Dim pMxdoc As IMxDocument
Set pMxdoc = Application.Document
Dim pMap As IMap
Set pMap = pMxdoc.FocusMap
Dim b As String
b = pMap.Name + ":"
Dim pEnumLayer As IEnumLayer
Dim pId As New UID
Dim pparentlayer As ILayer
Dim coll As New Collection
Dim out As Variant
Set pEnumLayer = pMap.Layers(Nothing, False)
Set aLayer = pEnumLayer.Next
Dim parentLayerName As String

Do Until aLayer Is Nothing
coll.Add b & aLayer.Name

If (TypeOf aLayer Is IGroupLayer) Then
AddLayerNames aLayer, b & ":" & aLayer.Name, coll
End If

Set aLayer = pEnumLayer.Next
Loop

For Each out In coll
List = List & out & vbNewLine
Next

MsgBox b & Chr(13) & List

End Sub

Sub AddLayerNames(pGroupLayer As IGroupLayer, pGroupLayerFullName As String, ByRef collectionVariable As Collection)

If pGroupLayer Is Nothing Then Exit Sub

Dim compLayer As ICompositeLayer
Set compLayer = pGroupLayer
Dim childLayer As ILayer


Dim i As Integer
For i = 0 To compLayer.count - 1
Set childLayer = compLayer.Layer(i)
collectionVariable.Add pGroupLayerFullName & ":" & childLayer.Name

If TypeOf childLayer Is IGroupLayer Then
AddLayerNames childLayer, pGroupLayerFullName & ":" & childLayer.Name, collectionVariable
End If

Next

End Sub
0 Kudos
DanielTuracek
New Contributor
thank you very much
you saved my job 🙂
0 Kudos
DanielTuracek
New Contributor
One more questions:
I used your code(btw: code is great) but i need add "layer id number" for each layer. Can you give me one more advice.
Thanks

Sub NazvyVrstiev()
Dim s As String:                    Dim a, c As String
Dim aLayer As ILayer
Dim pMxdoc As IMxDocument:          Set pMxdoc = Application.Document
Dim pMap As iMap:                   Set pMap = pMxdoc.FocusMap
Dim b As String:                    b = pMap.name + ":"
Dim pEnumLayer As IEnumLayer
Dim kolekcia2 As New Collection:    Dim kolekcia3 As New Collection
Dim out0 As Variant:                Dim out00 As Variant
Dim i As Integer
i = -1
Set pEnumLayer = pMap.LAyers:       Set aLayer = pEnumLayer.Next
''I NEED ADD LAYER ID NUMBER FOR EACH LAYERS
Do Until aLayer Is Nothing
    i = i + 1
    out0 = i
    kolekcia3.Add out0 & "/" & b & ":" & aLayer.name
Set aLayer = pEnumLayer.Next
Loop

For Each out0 In kolekcia3
List2 = List2 & out0 & vbNewLine
Next
MsgBox List2
'' I NEED THIS ID FOR THIS COLLECTION
Set pEnumLayer = pMap.LAyers(Nothing, False):   Set aLayer = pEnumLayer.Next
Do Until aLayer Is Nothing
    out00 = b & ":" & aLayer.name
    kolekcia2.Add out00
    If (TypeOf aLayer Is IGroupLayer) Then
        Vrstvy aLayer, b & ":" & aLayer.name, kolekcia2
    End If

    Set aLayer = pEnumLayer.Next
Loop

For Each out00 In kolekcia2
List = List & out00 & vbNewLine
Next

MsgBox b & vbNewLine & List

End Sub
Sub Vrstvy(pGroupLayer As IGroupLayer, pGroupLayerFullName As String, ByRef kolekcia As Collection)
If pGroupLayer Is Nothing Then Exit Sub
Dim compLayer As ICompositeLayer
Set compLayer = pGroupLayer
Dim SubLayer As ILayer
Dim i As Integer
For i = 0 To compLayer.Count - 1
    Set SubLayer = compLayer.layer(i)
    Dim s As String
    s = pGroupLayerFullName & ":" & SubLayer.name
   
    kolekcia.Add s
 
    If TypeOf SubLayer Is IGroupLayer Then
    Vrstvy SubLayer, s, kolekcia
    End If
Next
End Sub
0 Kudos
by Anonymous User
Not applicable
Original User: thaviti

what do u mean by Layer ID?

If it is the Layer index in the map, then the index exists only for top most layers in the map / Data frame.
Child layers in the GroupLayer will have an indexing starts from 0 gain.
Hence be very clear what u would like to gather
0 Kudos
DanielTuracek
New Contributor
Ok, so I need get specifications for layers something like ID.
For Each layer. this is example:


Dim i As Integer
i = -1
Set pEnumLayer = pMap.LAyers: Set aLayer = pEnumLayer.Next
''I NEED ADD LAYER ID NUMBER FOR EACH LAYERS
Do Until aLayer Is Nothing
i = i + 1
out0 = i
kolekcia3.Add out0 & "/" & b & ":" & aLayer.name
Set aLayer = pEnumLayer.Next
Loop

For Each out0 In kolekcia3
List2 = List2 & out0 & vbNewLine
Next
MsgBox List2


If know how to mark each layer in your code, it will be great.
Your code is OK , but i dont understand how its work.
thanks again for your replying
0 Kudos
by Anonymous User
Not applicable
Original User: thaviti

Please find the explanation against lines

Sub GetLayerNames()
Dim s As String
Dim aLayer As ILayer
Dim a, c As String
Dim pMxdoc As IMxDocument
Set pMxdoc = Application.Document
Dim pMap As IMap
Set pMap = pMxdoc.FocusMap ''get the active map
Dim b As String
b = pMap.Name + ":"
Dim pEnumLayer As IEnumLayer
Dim pId As New UID
Dim pparentlayer As ILayer
Dim coll As New Collection
Dim out As Variant
Set pEnumLayer = pMap.Layers(Nothing, False)
''As second parameter is False, this Gets the immediate Layers in the map. i.e. only the direct layers ''under map

Set aLayer = pEnumLayer.Next ''loop through retrieved layer enumeration
Dim parentLayerName As String

Do Until aLayer Is Nothing
coll.Add b & aLayer.Name ''add the layer name appended with map name to the collection

''If the layer is GroupLayer then process its child layers
If (TypeOf aLayer Is IGroupLayer) Then

''Pass the layer and the layer full name (Map name + layer name) to the method along with the ''collection variable

AddLayerNames aLayer, b & ":" & aLayer.Name, coll

End If

''Get the next layer in the enumeration and process the same

Set aLayer = pEnumLayer.Next
Loop

For Each out In coll
List = List & out & vbNewLine
Next

MsgBox b & Chr(13) & List

End Sub

''This method adds the child layer name's in the group layer to the collection. If the child layer is ''also a group layer then that child layer itself is proceed with this method to get its child layer ''names and the same to the collection variable.

Sub AddLayerNames(pGroupLayer As IGroupLayer, pGroupLayerFullName As String, ByRef collectionVariable As Collection)

If pGroupLayer Is Nothing Then Exit Sub

Dim compLayer As ICompositeLayer
Set compLayer = pGroupLayer
Dim childLayer As ILayer


Dim i As Integer
For i = 0 To compLayer.count - 1
Set childLayer = compLayer.Layer(i)


collectionVariable.Add pGroupLayerFullName & ":" & childLayer.Name

''append the child layer name to parent layer full name and pass the same to the method to process ''for child layers

If TypeOf childLayer Is IGroupLayer Then
AddLayerNames childLayer, pGroupLayerFullName & ":" & childLayer.Name, collectionVariable
End If

Next

End Sub
0 Kudos
MichaelaRepkova
New Contributor
hi,
i got the same problem but in Java.
this is my code:


public class interpolacia {


public static void main(String[] args)  {

  ServerInitializer serverInitializer = null;
  ServerConnection connection = null;
 
 
  try
  {
  
     serverInitializer = new ServerInitializer();
     serverInitializer.initializeServer("esprit", "misa", "a");
      
      connection = new ServerConnection();
      connection.connect("michalavirtual");
      IServerObjectManager som = connection.getServerObjectManager();
      IServerContext context = som.createServerContext("nova", "MapServer");
   
      MapServer ms = (MapServer)context.getServerObject();
      String nazov = ms.getDefaultMapName();
     
      MapServerInfo mapinfo = (MapServerInfo)ms.getServerInfo(nazov);
      IMapLayerInfos maplayerinfo = mapinfo.getMapLayerInfos();
 
     for (int i = 0; i < maplayerinfo.getCount();  i++)
     {
     if (maplayerinfo.getElement(i).getSubLayers() == null)
      {
       int m = i;
      
       do {
       
        m = maplayerinfo.getElement(m).getParentLayerID();
        System.out.println("<"+maplayerinfo.getElement(m).getName()+">");
       }
      
       while (maplayerinfo.getElement(m).getParentLayerID() != -1);
      
       System.out.println(maplayerinfo.getElement(i).getName());
    
        }
      }
     
    
    
  }       
             catch (AutomationException ae)
       {
           System.err.println("Caught AutomationException: " + ae.getMessage() + "\n");
           ae.printStackTrace();
       }

       catch (IOException e)
       {
           System.err.println("Caught IOException: " + e.getMessage() + "\n");
           e.printStackTrace();
       }
      }

}  


the result of this is:

<pod-podgrupa>
<podgrupa1>
<grupa1>
Banska_Bystrica
<pod-pod-pod-podgrupa>
<pod-pod-podgrupa>
<pod-podgrupa>
<podgrupa1>
<grupa1>
KOTY
<pod-pod-pod-podgrupa>
<pod-pod-podgrupa>
<pod-podgrupa>
<podgrupa1>
<grupa1>
Riecna_siet_Clip
<grupa1>
Riecna_siet
<grupa2>
Banska_Bystrica_Select
<pogrupa2>
<grupa2>
katastralne_uzemie

i need make it reverse like
<grupa1>
<podgrupa1>
<pod-podgrupa>
Banska_Bystrica
<grupa1>
<podgrupa1>
<pod-podgrupa>
<pod-pod-podgrupa>
<pod-pod-pod-podgrupa>
KOTY
...

can somebody help me?
0 Kudos