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

2394
6
03-07-2012 09:29 AM
JustinWelty
New Contributor
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"
Tags (2)
0 Kudos
6 Replies
MathewCoyle
Frequent Contributor
You can still use VBA for 10.0, 10.1 it will no longer be usable. VBA no longer comes prepacked with a standard 10.0 install, you can get a VBA license from Esri to activate it.
0 Kudos
JustinWelty
New Contributor
I know, that is why I am trying to prepare for 10.1.  I figure while I am upgrading everything else I might as well upgrade everything I will need to for 10.1 as well.
0 Kudos
LoganPugh
Occasional Contributor III
I don't think you'll be able to use arcpy for this, but you should be able to do it with COM and ArcObjects. You said you tried using comtypes, perhaps you should pursue that approach directly with another question. Also you might ask on http://gis.stackexchange.com and reference this question: http://gis.stackexchange.com/questions/80/how-do-i-access-arcobjects-from-python
0 Kudos
JustinWelty
New Contributor
Thank you for the advice.  I posted the question on the forum under the following heading: Add layer to open mxd using ArcObjects from outside the map document.  I will try to post a link to the answer, if I get one.
0 Kudos
AlessandroCinnirella
New Contributor III
didn't you miss the
mxd.save()
instruction at the end of your script?

AC
0 Kudos
JustinWelty
New Contributor
I tried mxd.save but again it saves the closed map document but does nothing to the open document.  It only works on the closed mxd forcing me to open it again.  Plus I do not want to save the changes, the idea is just to pop in the features for viewing to make sure they are correct and then discard them.  I am currently working with ArcObjecs in another post: http://gis.stackexchange.com/questions/21450/add-layer-to-open-mxd-using-arcobjects-from-outside-the..., but ArcMap continues to crash when it is adding the layer.  If anyone has an idea for either method I would be most grateful.
0 Kudos