Issue on Adding Feature to Open MXD in Real Time Using ArcPy

1205
4
Jump to solution
05-18-2017 02:23 PM
BehrouzHosseini
Occasional Contributor

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?

0 Kudos
1 Solution

Accepted Solutions
DanPatterson_Retired
MVP Emeritus

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

Reference a map document on disk or use the CURRENT keyword within ArcMap

View solution in original post

4 Replies
DanPatterson_Retired
MVP Emeritus

if it is open did you try

arcpy.mapping.MapDocument("current") # or capital c... can't remember offhand
BehrouzHosseini
Occasional Contributor

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)

enter image description here
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:

enter image description here

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

0 Kudos
DanPatterson_Retired
MVP Emeritus

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

Reference a map document on disk or use the CURRENT keyword within ArcMap

BehrouzHosseini
Occasional Contributor

Thanks Dan it was helpful

0 Kudos