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?
Solved! Go to Solution.
http://desktop.arcgis.com/en/arcmap/latest/analyze/arcpy-mapping/guidelinesforarcpymapping.htm
has to be run from within arcmap (I suspect its python window) not catalog and background geoprocessing disabled... see the section
if it is open did you try
arcpy.mapping.MapDocument("current") # or capital c... can't remember offhand
Thanks Dan but now I am getting another error
I followed all of these steps
As:
1- Start up the Streets.mxd
in ArcMap
2- Run the mxd=arcpy.mapping.MapDocument("CURRENT")
inside the ArcMap (without no error)
3- Add the same mxd=arcpy.mapping.MapDocument("CURRENT")
to my code in PyScripter
IDE as
import arcpy
####mxd = arcpy.mapping.MapDocument(r"C:\Temp\Street.mxd")
mxd=arcpy.mapping.MapDocument("CURRENT")
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")
arcpy.RefreshActiveView()
arcpy.RefreshTOC()
but I am getting this runtime error message:
Traceback (most recent call last): File "C:\Users\bhosseini\Documents\ArcPy\Trace With Graphics\module3.py", line 3, in mxd=arcpy.mapping.MapDocument("CURRENT") File "C:\Program Files (x86)\ArcGIS\Desktop10.4\ArcPy\arcpy\arcobjects\mixins.py", line 625, in initsuper(MapDocumentMethods, self).init(mxd) File "C:\Program Files (x86)\ArcGIS\Desktop10.4\ArcPy\arcpy\arcobjects_base.py", line 47, in init for arg in args)) RuntimeError: Object: CreateObject cannot open map document
http://desktop.arcgis.com/en/arcmap/latest/analyze/arcpy-mapping/guidelinesforarcpymapping.htm
has to be run from within arcmap (I suspect its python window) not catalog and background geoprocessing disabled... see the section
Thanks Dan it was helpful