weltygis

Manipulate an open ArcMap document from outside the current session with arcpy

Discussion created by weltygis on Mar 7, 2012
Latest reply on Mar 13, 2012 by weltygis
I have an Access database (MS Access 2010) that allows users to enter a variety of land treatment information including associated points, lines, or polygons which users can select and the database would then load as part of the project, to a master geodatabase, and allow the user to view the features in an ArcMap.  In Arc 9.3 this was all accomplished with VBA and worked well.  Because of the upgrade to Arc 10, VBA is no longer an option and I have been learning how to program in python and call the commands through Access using the Shell feature.  I have nearly completed everything except for the ability to allow users to add their feature to the current ArcMap document.  Here are the steps that should occur.
1) User clicks a button in the Access program which identifes the feature(s) of interest and calls the python shell.
2) Access determines if the correct mxd is open and if not opens it (the document is usually open so this step is often uneccessary)
3) The Python script opens using the shell command, identifes the correct and open mxd, and adds feature to the open ArcMap document

The python script works fine if I run it within the open ArcMap document using the mxd = arcpy.mapping.MapDocument("CURRENT") arpy command code.  However, if I run the command using the python shell and mxd = arcpy.mapping.MapDocument(r"C:\CreateShapefile\CreateShapefile10x.mxd") it will manipulate the document if it is closed and I instruct python to save the changes but will not display any changes on the open ArcMap document even if I refresh the TOC and Active View. 

I have tried working with ArcObjects and Comtypes to accomplish this as well as arcpy.mapping but with no luck.  I think arcpy should be able to accomplish this but again running the program through IDLE or python shell doesn't manipulate the open ArcMap document.  I need a way to manipulate an open ArcMap document from outside the current ArcMap session.  All the code seems to work when run from within the mxd, just not if called through Python shell or IDLE.  The code attached is simply to turn on a background layer in the open mxd but even this does not work when run through IDLE, only if I save the mxd and then open it will the change appear. 

import arcpy
import arcpy.mapping
mxd = arcpy.mapping.MapDocument(r"C:\CreateShapefile\CreateShapefile10x.mxd")
for df in arcpy.mapping.ListDataFrames(mxd):
    arcpy.AddMessage("DataFrame Found: " + df.name)
    # Select the LTDL Layers Dataframe
    if df.name == "LTDL Layers":
        print df.name
        LayersOn = ["us topo map"]
        for lyr in arcpy.mapping.ListLayers(mxd, "", df):
            for lr in LayersOn:
                if lyr.name.lower() == lr:
                    lyr.visible = True
        arcpy.RefreshTOC()
        arcpy.RefreshActiveView()
        print "Done"

Outcomes