kyanuk1

Trouble using script/tool outside ArcMap

Discussion created by kyanuk1 on May 14, 2012
Hello -

I've been trying to execute a small script from outside an ArcMap session - simply adding a layer and refreshing symbology, and nothing seems to work.  The .mxd is open, and I am trying to add a newly layer and refresh the TOC by using this script from either another mxd document (as a tool), or just running it in the IDLE environment / CMD prompt.  Is this not possible?  The createLayerFile() creates the layer, but nothing is added or refreshed in the TOC.

However, when I add the code to the python window of the map doc I want to edit, or run it as a tool from that same map doc, it runs fine.  I'd like to be able to this without having to run it from that specific map document - so I can simply open the .mxd (os.startfile), add the layer I create, and update the symbology.  

Again, the tool works fine if I run it from the .mxd being edited, but I'd like to be able to do this without digging through the folder of maps I want to edit, and just open it using the script.


I hope this makes sense ... Thank you!


def createLayerFile():
    arcpy.AddMessage("Querying and updating layer file...")
    qcMXD = arcpy.mapping.MapDocument("C:\\Users\\kyanuk\\Desktop\\QCTest.mxd")   #MXD WHERE DATA LYR IS CREATED

    for lyrs in arcpy.mapping.ListLayers(qcMXD):
        if lyrs.name == "QC Issues":
            QCI = lyrs

    #rows = arcpy.SearchCursor(QCI, "QC_ID <> NULL OR QC_ID >= 90000")

    qcSave = arcpy.SaveToLayerFile_management(QCI, "C:\\Users\\kyanuk\\Desktop\\shapes\\QC Issues.lyr")
    arcpy.mapping.Layer("C:\\Users\\kyanuk\\Desktop\\shapes\\QC Issues.lyr")
    arcpy.AddMessage("Adding layer to basemap...")

    arcpy.mapping.AddLayer(baseDF,arcpy.mapping.Layer("C:\\Users\\kyanuk\\Desktop\\shapes\\QC Issues.lyr")) #ADD NEW LAYER TO FOO.MXD
            


def updateTOC():
    arcpy.AddMessage("Updating Table of Contents and map... ")
    for row in arcpy.mapping.ListLayers(baseMXD):
        arcpy.AddMessage(row.name)
        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.RefreshupdateTOC()
    arcpy.RefreshActiveView()

Outcomes