kyanuk1

Using tool from one ArcMap session in another opened MXD

Discussion created by kyanuk1 on May 10, 2012
Hello -

My goal with this script is to run a tool from a "Quality Control" .mxd that I work in every day, which will open a target .mxd that will be editted to show QC issue points in a basemap .pdf for field crew to examine.

I want the script to take a GetParameterAsText ArcMap document - which is currently seen as baseMXD using the "CURRENT" map session - open it, save a created .lyr file from a feature in my QC .mxd, and add it to the new base map.

updateMaps() creates the said layer I will be adding to the new map.
removeNulls() just do a pseudo-query on the new lyr to display relevant issues to my project.
TOC() does table of contents updates (symbology, turning layers off, etc)
saveClose() just creates a new path for the pdf and saves that and the mxd.

run() is self explanatory.

When I use this script as-is - meaning - I open the basemap that I want to edit, add the toolbox with script, and run it, it works perfectly as intended.

I had tried running the script and setting the arcpy.mapping.MapDocument("CURRENT") to a variable that calls GetParameterAsText as a map doc, and the script seems to run "without" problem, but nothing in the basemap is updated or changed at all.  The script just runs and stops and nothing happens.


Is there a way to do this?

Thank you-


import arcpy,os
from arcpy import env
arcpy.env.overwriteOutput = True

baseMXD = arcpy.mapping.MapDocument("CURRENT") #C:\\Users\\kyanuk\\Desktop\\Tools\\foo.mxd
baseDF = arcpy.mapping.ListDataFrames(baseMXD)[0]

#def openBM():
#    baseDoc = arcpy.GetParameterAsText(0)
#    os.startfile(baseDoc)
    
def updateMaps():
    arcpy.AddMessage("Updating layer file...")
    qcMXD = arcpy.mapping.MapDocument("C:\\Users\\kyanuk\\Desktop\\QCTest.mxd")
    QCI = arcpy.mapping.ListLayers(qcMXD)[2]
    qcSave = arcpy.SaveToLayerFile_management(QCI, "C:\\Users\\kyanuk\\Desktop\\shapes\\QC Issues.lyr")
    arcpy.mapping.Layer("C:\\Users\\kyanuk\\Desktop\\shapes\\QC Issues.lyr")
    
def removeNulls():
    arcpy.AddMessage("Selecting relevant QC Issues...")
    rows = arcpy.UpdateCursor("C:\\Users\\kyanuk\\Desktop\\shapes\\QC Issues.lyr")
    for row in rows:
        if row.QC_ID == None or row.QC_ID < 90000 or row.ISSUE_TYPE == 2:
            rows.deleteRow(row)
            
def TOC():
    arcpy.AddMessage("Updating Table of Contents and map... man, you're lazy.")
    for row in arcpy.mapping.ListLayers(baseMXD):
        if row.name == "School" or row.name == "SDE_USER_PUBLIC_SCHOOLS_2008":
            row.visible=False
        if row.name == "New Castle County Collection Areas":
            symLyr = row.name
            row.visible=False
    arcpy.mapping.ListLayers(baseMXD)[1].name = "New Castle County Collection Area"
    colLyr = arcpy.mapping.ListLayers(baseMXD)[1]
    arcpy.ApplySymbologyFromLayer_management(colLyr, symLyr)
    colLyr.visible=True
    arcpy.RefreshTOC()
    arcpy.RefreshActiveView()

def saveClose():
    pdfPath=""
    pathSplit = baseMXD.filePath.split(".")
    del pathSplit[1:]
    for i in pathSplit:
        pdfPath = i+".pdf"
    arcpy.mapping.ExportToPDF(baseMXD, pdfPath)
    baseMXD.save()
    os.startfile(pdfPath)

def run():
    try:
        #openBM()
        updateMaps()
        removeNulls()
        arcpy.AddMessage("Adding layer to basemap...")
        arcpy.mapping.AddLayer(baseDF,arcpy.mapping.Layer("C:\\Users\\kyanuk\\Desktop\\shapes\\QC Issues.lyr"))
        TOC()
        saveClose()
    except:
        arcpy.GetMessages()

#-----------------end-------------------------#
run()

Outcomes