AddLayer -- dynamically interact with open mxd document?

756
4
Jump to solution
03-10-2014 03:12 PM
YurikoHashimoto
New Contributor II
Hello,

I have been unsuccessfully attempting to add layers programmatically to an open mxd document via arcpy scripting. I AM, however, able to add the layers to the mxd, save a copy of the mxd and then open the new mxd with the layers successfully added.

Is it not possible to add these layers and refresh the TOC and Active View dynamically with the mxd open? Or does it have to be closed, saved and reopened?

I don't receive any errors when I run my code it is that the layers aren't visibly added to the mxd on the fly.

Here is a code excerpt:

    arcpy.MakeFeatureLayer_management(fc, fl, whereClause)     outLayerFilePath = outputFL + "\\" + fl + ".lyr"     # Save to Layer file     arcpy.SaveToLayerFile_management(fl, outLayerFilePath)     out_layer = arcpy.mapping.Layer(outLayerFilePath)     # Line checks to ensure the feature layers were created with the right feature count     #countFeatures = arcpy.GetCount_management(out_layer).getOutput(0)     #print countFeatures     arcpy.mapping.AddLayer(df, out_layer)  arcpy.RefreshTOC() arcpy.RefreshActiveView() mxd.saveACopy(outputMXDpath) # only when this line is added to save to a new mxd can I see the newly added layers.
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
KimOllivier
Occasional Contributor III
There are two things you must do:
Open the current document in ArcMap using the magic constant 'CURRENT' instead of opening the mxd file.
mxd = arcpy.mapping.MapDocument("CURRENT")


You also need to run the script in foreground which is a tool property.

It is possible to save the CURRENT mxd to a file after modifying it.

View solution in original post

0 Kudos
4 Replies
KimOllivier
Occasional Contributor III
There are two things you must do:
Open the current document in ArcMap using the magic constant 'CURRENT' instead of opening the mxd file.
mxd = arcpy.mapping.MapDocument("CURRENT")


You also need to run the script in foreground which is a tool property.

It is possible to save the CURRENT mxd to a file after modifying it.
0 Kudos
YurikoHashimoto
New Contributor II
Thanks so much, Kim! Works like a charm! I wish I had only asked sooner!

Thanks again!
0 Kudos
MattEiben
Occasional Contributor
If looks like you're trying to add layer layer to your current mapping session, but you don't have a reference to your "df" (dataframe) in the arcpy.mapping.AddLayer

Here's the way I usually Add a layer I've been geoprocessing to my current table of contents:

# Get your current MXD and DF (assuming you only have one df)
mxd = arcpy.mapping.MapDocument("CURRENT") 
df = arcpy.mapping.ListDataFrames(mxd, "*")[0]

# Get a Layer refernce to the layer you want to add
fc_mapping_reference = arcpy.mapping.Layer(fc)

# Add your layer to your current df
arcpy.mapping.AddLayer(df,fc_mapping_reference,"BOTTOM")

# If you're having problems refreshing your screen
arcpy.RefreshActiveView()
0 Kudos
YurikoHashimoto
New Contributor II
Thanks Matt, indeed I wasn't actually referencing the mxd with "CURRENT" and was using a stand-alone script that referenced the open mxd which is a no-go. 😛
0 Kudos