Feature-Linked Annotation

3067
5
09-25-2013 09:26 AM
JacobDrvar
New Contributor II
All,

I have a script that works when the desired Feature Class is listed above Feature-Linked Annotations within ArcMap TOC.  But, the script does not work when the Feature Class is displayed below Feature-Linked Annotations.  What is the logic behind why this occurs?

[ATTACH=CONFIG]27752[/ATTACH]

[ATTACH=CONFIG]27753[/ATTACH]
0 Kudos
5 Replies
RichardFairhurst
MVP Honored Contributor
All,

I have a script that works when the desired Feature Class is listed above Feature-Linked Annotations within ArcMap TOC.  But, the script does not work when the Feature Class is displayed below Feature-Linked Annotations.  What is the logic behind why this occurs?

[ATTACH=CONFIG]27752[/ATTACH]

[ATTACH=CONFIG]27753[/ATTACH]


Probably the script is just getting the first layer in the map period.  It is the lazy, easy way to write a script.  Logic can be added to search the layer tree for a given layer name and get it at other levels, but it requires knowing the layer name in the code.  Or the selected layer could be used if the code is written to do that.  If layers are placed inside group layers, recursive layer searches need to be used (an additional aspect to the logic and set up).  Basically it is up to the programmers skill and experience to access the needed layer and to give or restrict the user's flexibility on where it is in the layer tree.
0 Kudos
by Anonymous User
Not applicable
Original User: jdrvar

Probably the script is just getting the first layer in the map period.  It is the lazy, easy way to write a script.  Logic can be added to search the layer tree for a given layer name and get it at other levels, but it requires knowing the layer name in the code.  Or the selected layer could be used if the code is written to do that.  If layers are placed inside group layers, recursive layer searches need to be used (an additional aspect to the logic and set up).  Basically it is up to the programmers skill and experience to access the needed layer and to give or restrict the user's flexibility on where it is in the layer tree.


I can place other Feature Classes above the Lot_Lines Feature Class and the script works.  The problem is moving the Feature-Linked Annotation Feature Classes above Lot_Lines.

fc = arcpy.mapping.ListLayers(mxd, "Lot_Lines", df)[0]


I have the Lot_Lines Feature Class set to be called in the script.  I do not have any other Feature Classes set to be referenced.
0 Kudos
JacobDrvar
New Contributor II
Setting up a for loop solved the issue.  Thanks.
0 Kudos
by Anonymous User
Not applicable
Original User: jdrvar

I have other scripts that I would like to modify.  The scripts work when the Feature Class is the top Layer within the TOC.  Can you provide an example of a recursive layer search?  Thanks.  Previously I used

for lyr in arcpy.mapping.ListLayers(lyr):.
0 Kudos
by Anonymous User
Not applicable
Original User: rfairhur24

I have other scripts that I would like to modify.  The scripts work when the Feature Class is the top Layer within the TOC.  Can you provide an example of a recursive layer search?  Thanks.  Previously I used

for lyr in arcpy.mapping.ListLayers(lyr):.


See this post for a way to do it one group layer deep.

import arcpy

inMXD = r'c:\test.mxd'
outMXD = r'c:\test1.mxd'
rLayer = 'test3' #Name of reference layer
mLayer = 'test4' #Name of layer to be replaced and moved
lyrFile = arcpy.mapping.Layer(r'c:\Test4.lyr') #Layer file used to replace layer

mxd = arcpy.mapping.MapDocument(inMXD)
for df in arcpy.mapping.ListDataFrames(mxd):           #loop data frames
    for lyr in arcpy.mapping.ListLayers(mxd,"",df):    #loop layers
        if lyr.isGroupLayer == 1:                      #Is layer a group layer
            for glyr in arcpy.mapping.ListLayers(lyr): #loop layer in group layer
                if glyr != lyr:                        #Not Group Layer Name
                    if glyr.name.lower() == mLayer:    #Layer to be worked on


Recursion involves setting up a second method that can call itself when it encounters each group layer, so it does not matter how deep the groups go.  Basically it would be something like this function (untested) which is called for every layer until you find the layer you want or you have no more layers:

def layerInGroup(lyr, layerName):
  if lyr.isGroupLayer == 1:
     for glyr in arcpy.mapping.ListLayers(lyr): #loop layer in group layer
        if glyr != lyr:                        #Not Group Layer Name
           resultlyr = layerInGroup(glyr, layerName)  # Recursively call the function for group in a group
           if resultlyr != None:
              return resultlyr        # We found the layer deeper inside the group so return it and stop function
  elif lyr.name.lower() == layerName: # Not a group layer so test to see it is the one we want.
    return lyr      # This is the layer we want so return it and stop function.
  return None     # All tests failed to find layer we want so return None
0 Kudos