2

I'm having a bit of a problem with a script on a particular machine.

==========================================================

import arcpy,sys


class AddLayer(object):

    '''
    classdocs
    '''

   def __init__(self,sNewHabFile,workspace):

        scriptPath = sys.path[0]
        f = file(scriptPath + chr(92) +'AddLayerFile_ArcGIS_log.txt', 'w') # open for 'w'riting

        f.write("Add layer file initialised") # write text to file
        f.write("\n")

        layerFile = sNewHabFile + ".lyr"
        f.write(str(layerFile)) # write text to file
        f.write("\n")

        mxd = arcpy.mapping.MapDocument("CURRENT")

        f.write(str(mxd.filePath)) # write text to file
        f.write("\n")

        addLayer = arcpy.mapping.Layer(layerFile)

        f.write(str(addLayer.name)) # write text to file
        f.write("\n")

        df = arcpy.mapping.ListDataFrames(mxd)[0]

        f.write(str(df.name)) # write text to file
        f.write("\n")

        arcpy.mapping.AddLayer(df, addLayer, "TOP")

        f.write("Layer added?") # write text to file
        f.write("\n")

        layer = arcpy.mapping.ListLayers(mxd)[0]

        f.write("First layer is called:" + str(layer.name)) # write text to file
        f.write("\n")

        arcpy.RefreshTOC()
        '''
        end
        '''

============================================================

It's working fine on my machine, but testing the whole tool on another machine, AddLayer here and in another module I have written does not throw up an error, it just doesn't add the final outputs to the map.

As you can see above I have writen to a text file and layer = arcpy.mapping.ListLayers(mxd)[0]

and

f.write("First layer is called:" + str(layer.name))

are writing the correct layer to the text document, but I can't see anything in the TOC.

I can manually add the created data.

On my original machine, which is working I am running:

ArcView single use with Spatial Analyst, 4th or 5th service pack I think.

The machine that is not working is running ArcEditor concurrent with access to Spatial Analyst, this is likely to be a much earlier service pack (either none or 1 or 2).

Does anyone have any ideas?

3
  • I would try to update the computer that isn't working to a newer service pack. If that still doesn't work, let us know.
    – Fezter
    Dec 18, 2012 at 23:25
  • 1
    Perhaps use print statement to see what script is using as full path to that text file - and then make sure that path and file can be seen on the second machine and has right permissions for what are wanting to do with it.
    – PolyGeo
    Dec 19, 2012 at 3:31
  • @Jordan Chetcuti, Are your installation paths for ArcGIS the same on both machines (e.g. C:\Program Files\..., or C:\Program Files (86)\.... comparing 32 and 64bit?
    – artwork21
    Jan 21, 2013 at 17:36

2 Answers 2

2

Sounds like you would need to use

arcpy.RefreshTOC()

After you add the layer but before you call ListLayers method.

    ...
    arcpy.mapping.AddLayer(df, addLayer, "TOP")

    #add the refresh method to update the TOC
    arcpy.RefreshTOC()

    f.write("Layer added?") # write text to file
    f.write("\n")

    layer = arcpy.mapping.ListLayers(mxd)[0]
    ...

I see you call the refresh at the end. But you basically need it anytime you make a change and want to see the effect.


RefreshTOC has two other useful siblings...

If you need to see the change on the map view you will need

arcpy.RefreshActiveView()

In addition, if you happen to create/rename/remove anything spatial and Esri isn't recognizing that change you may need to refresh the catalog (in this case passing it the object you want refreshed)

arcpy.RefreshCatalog(someTargetObject)

1

I ran into the same problem and found that the solution was to do mxdDoc.saveACopy("C:\\mxdcopy.mxd")

Not the answer you're looking for? Browse other questions tagged or ask your own question.