0

I am using this code to add a feature from GeoDatabase dataset to an open MXD file

import arcpy
mxd = arcpy.mapping.MapDocument(r"C:\Temp\Streets.mxd")
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
targetGroupLayer = arcpy.mapping.ListLayers(mxd, "Main", df)[0]
addLayer = arcpy.mapping.Layer(r"C:\Data\NetMap.gdb\Streets\OneWay")
arcpy.mapping.AddLayerToGroup(df, targetGroupLayer, addLayer, "BOTTOM")‍‍‍‍‍‍

‍ ‍This is somehow adding the OneWay feature class to the Main Group Layer at Streets.mxd but I have to close the map and reopen it to see the data there.

I tried to refresh the TOC and Active view like (As it work in IDE inside the Current Map)

arcpy.RefreshActiveView()
arcpy.RefreshTOC()

‍ ‍

but it is not doing anything with stand-alone script and MXD. Can you please let me know if there is a way to do this?

  • 2
    To add a layer to an open MXD I believe you need to run the python script tool from within ArcMap, and use mxd = arcpy.mapping.MapDocument("CURRENT"). Otherwise your arcpy is running in a different process and hits file or application locks. – Midavalo May 18 '17 at 21:25
  • What error do you get when you try to do this? I would expect that to lead you pretty much straight to a duplicate. – PolyGeo May 18 '17 at 21:31
  • I am not getting any Error! on this and as I said it is adding the layer to the map but not in real time and I have to close the map and re-open it to see the added map – Behseini May 18 '17 at 21:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.