AnsweredAssumed Answered

Add layer to map with Python Toolbox (.pyt)

Question asked by bmillerdot on Jun 7, 2013
Latest reply on Jun 7, 2013 by JScheirer-esristaff
I'm trying to add a layer to the current MXD using an arcpy toolbox (.pyt).

When I execute the program it draws the entire layer then immediately dissappears from the TOC which basically returns to the old version of the MXD. Other times it does not draw the layer but still there is not an error message.

Here is the code I'm using for the execute routine as a toolbox:


def __init__(self):         self.label = "Add Layer"         self.description = "Adds a layer to ArcMap"         self.canRunInBackground = False ######################## def execute(self, parameters, messages):         arcpy.MakeFeatureLayer_management("C:\BILL\CB_TEMP\cb.shp", "LAYER")         mxd = arcpy.mapping.MapDocument("CURRENT")         df  = arcpy.mapping.ListDataFrames(mxd, "*")[0]         addLayer = arcpy.mapping.Layer("LAYER")         arcpy.mapping.AddLayer(df, addLayer, "TOP")           arcpy.RefreshTOC()         arcpy.RefreshActiveView()         # del mxd

BTW: This problem was discussed in 10.0 and they said it was fixed in a SP. I think the difference might be that I'm using a toolbox ".pyt" to add the layer.
http://forums.arcgis.com/threads/8331-AddLayer-only-temporarily-adds-the-layer-to-map?p=57150#post57150

Outcomes