Select to view content in your preferred language

List all layers in an arcmap session

979
7
01-24-2012 12:31 PM
WilliamIde
Emerging Contributor
I would like to fill a listbox  with all of the FCs in a working Arcmap session.

I and do a list from a GDB like

env.workspace = "d:/foo/DefWrkCpc/Mystuff.gdb"

fc_list = arcpy.ListFeatureClasses()

for item in fc_list:
     myLstBox(.insert(END,item)

I can't seem to get

mxd = arcpy.mapping.MapDocument("CURRENT")

nlaylst = arcpy.mapping.ListLayers(mxd)

nfc_list = []
for layer in nlaylst:
     nfc_list += arcpy.ListFeatureClasses(layer)


To work  any ideas?
Tags (2)
0 Kudos
7 Replies
HenryColgate
Regular Contributor
I think I know what you are asking for given your question but your code really doesn't make sense.

Maybe try

nfc_list.append(layer)


Maybe if you commented exactly what each part is suppoosed to be doing.

For example I am not sure why you are trying to get a list of feature classes from layers.  I don't think 'layer' is an appropriate argument for arcpy.ListFeatureClasses()

You could also try

fcList = arcpy.listFeatureClasses()

for fc in fcList:
    nfc_list.append(fc)
0 Kudos
WilliamIde
Emerging Contributor
env.workspace = "d:/foo/DefWrkCpc/Mystuff.gdb"  # Set environment

fc_list = arcpy.ListFeatureClasses()  #  Get FeatureClass list from  a given gdb. This works except I forget the strip

fc_list = [field.rstrip() for field in fc_list]

# This loads the list into a LB This works.
for item in fc_list:
myLstBox(.insert(END,item)

# I can't seem to get the following code to work.  I will try the append.
# This
mxd = arcpy.mapping.MapDocument("CURRENT")

nlaylst = arcpy.mapping.ListLayers(mxd)

nfc_list = []
for layer in nlaylst:
nfc_list += arcpy.ListFeatureClasses(layer)
0 Kudos
WilliamIde
Emerging Contributor
I tried
nfc_list.append(layer)

which seems to be that same as the += assignment.

The problem is I was setting input parameters in properties of add script.  I changed it and it works.
0 Kudos
WilliamIde
Emerging Contributor
The problem I am having is that to test I set the env.workspace ="d:/myproj/mastergdb.gdb"

so it always looks in mastergdb.  What I need to find out is how to set the env.workspace to what "CURRENT" is looking at.

I want to display all of the feature classes in the current Arcmap session.
0 Kudos
JoshuaChan
Deactivated User
if you're trying to list the layers inside of your current mxd's map document then you don't want to be using the gdb at all.

this worked for me in an arcmap session:
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd,"Layers")[0]
mxdlyrs = arcpy.mapping.ListLayers(mxd, "*")

for lyr in mxdlyrs: 
   print lyr






I would like to fill a listbox  with all of the FCs in a working Arcmap session.

I and do a list from a GDB like

env.workspace = "d:/foo/DefWrkCpc/Mystuff.gdb"

fc_list = arcpy.ListFeatureClasses()

for item in fc_list:
     myLstBox(.insert(END,item)

I can't seem to get

mxd = arcpy.mapping.MapDocument("CURRENT")

nlaylst = arcpy.mapping.ListLayers(mxd)

nfc_list = []
for layer in nlaylst:
     nfc_list += arcpy.ListFeatureClasses(layer)


To work  any ideas?
0 Kudos
LoganPugh
Frequent Contributor
The existing geoprocessing framework has facilities for this sort of thing without custom GUIs or code.

Simply create a script tool and set one of the input parameters to be of type Feature Layer and that parameter will become a listbox on the tool dialog with a list of all the feature layers in the current ArcMap document as well as temporary feature layers that have been created during the current session through other means such as Make Feature Layer. This works similarly with Table Views and possibly other parameter types.

I would strongly recommend taking advantage the geoprocessing framework where possible instead of reinventing the wheel.
0 Kudos
WilliamIde
Emerging Contributor
That got it thank you sir.

Logan Yea I know, this is only a small part of a bigger script that I need to put together.  I almost always stay in the framework except when I don't.:)
0 Kudos