AnsweredAssumed Answered

AddLayer -- dynamically interact with open mxd document?

Question asked by hashimotoyuriko on Mar 10, 2014
Latest reply on Mar 11, 2014 by hashimotoyuriko
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.

Outcomes