2

I have created a python toolbox that creates separate layers based on unique field values within a feature layer. For some reason arcpy.MakeFeatureLayer_management will create the layer but will not add it to the TOC even after using RefreshTOC(). I used the same tool in the python interpreter and it adds the created layer to the TOC.

The layer doesn't need to be written to disk or saved permanently; I wanted to leave the saving up to the user. Any thoughts on why there is a difference and how to get the layer added to the TOC?

EDIT

    ws = arcpy.env.workspace
    mxd = arcpy.mapping.MapDocument("CURRENT")
    data_frame = arcpy.mapping.ListDataFrames(mxd)[0]
    layer = parameters[0].valueAsText #GPFeature Layer
    field = parameters[1].valueAsText #field name

    seach_layer = arcpy.mapping.Layer(layer)
    lfields = arcpy.ListFields(seach_layer.dataSource)

    rows = arcpy.SearchCursor(layer, fields=field)
    #get they field type to use in query
    ftype = None
    for f in lfields:
        if f.name == field:
            ftype = f.type
    # get values for unique list
    value_list = []
    for row in rows:
        value = row.getValue(field)
        value_list.append(value)

    unique = set(value_list)
    len_list = len(unique)

    try:
        for name in unique:
            if ftype in ['Float','Double','SmallInteger']:
                where_clause = "\"%s\"=%s" %(field, name)
            else:
                where_clause = "\"%s\"='%s'" %(field, name)

            temp = r"%s_temp_%s" %(layer, field)

            # make a feature layer to an "outfile" that's in memory
            # add to TOC and leave up to user to save if they want
            arcpy.MakeFeatureLayer_management(layer, temp,
                                              where_clause)

    except Exception as e:
        arcpy.AddError(e)
10
  • 1
    Do you have the output of MFL set as a parameter on the tool? Without an output, nothing is going to be added.
    – KHibma
    Jan 22, 2016 at 14:15
  • 1
    You'll probably need to add your script to the issue above to better understand the issue
    – KHibma
    Jan 22, 2016 at 14:53
  • 1
    If thats the full script, its what I thought. You dont have any parameters (arcpy.SetParameterAsText(0, temp). See this help link
    – KHibma
    Jan 22, 2016 at 15:31
  • 1
  • 1
    Also under ArcMap Geoprocessing Options, "Add results of geoprocessing operations to the display"
    – klewis
    Jan 22, 2016 at 16:06

1 Answer 1

3

When working with tools, both models and scripts, the output is only added to the ToC if the particular output is a parameter. Parameters control interaction between the tool and the map.

In a script, use something like the following to set the output

arcpy.MakeFeatureLayer_management(layer, temp, where_clause)
arcpy.SetParameterAsText(0, temp)

This Setting script tool parameters help explains parameters in more detail

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.