0

In ArcMap I can set a Geoprocessing option to add geoprocessing output to my table of contents (Geoprocessing > Geoprocessing Options...)

enter image description here

If this option is enabled my script output will be added to the table-of-contents, but if it's not enabled it isn't added. This happens even if I have output parameters set in my tool and arcpy.SetParameter() in my script.

Is there a way I can force an output to be added to my ToC when a tool is run? The output is a temporary table (in_memory) that users need to be able to access once the tool is run in the current ArcMap session.

import arcpy

testabc = r"in_memory\testabc"
myTable = "mytable"
in_memory = "in_memory"

output = arcpy.TableToTable_conversion(myTable, in_memory, "testabc", "", "", "")

arcpy.SetParameter(0, output)

enter image description here

  • My first thought is "no". There is this arcpy.env.addOutputsToMap, but I'm pretty sure the application setting (arcmap's environment) trumps the environment in the tool. – KHibma Jan 23 '17 at 20:51
  • @KHibma oddly when I try that the table actually appears in the ToC for a split second before disappearing again. Without that env variable set it doesn't appear at all. – Midavalo Jan 23 '17 at 21:09
  • 1
    Is AddTableView and RefreshTOC an option? – PolyGeo Jan 23 '17 at 22:22
  • @PolyGeo I'll need to read up on AddTableView - does this need MakeTableView first? – Midavalo Jan 24 '17 at 17:14
3

@PolyGeo's suggestion of looking at AddTableView was the right one:

import arcpy

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

testabc = r"in_memory\testabc"
myTable = "mytable"
in_memory = "in_memory"

arcpy.TableToTable_conversion(myTable, in_memory, "testabc", "", "", "")

mytableview = arcpy.mapping.TableView(testabc)
arcpy.mapping.AddTableView(df, mytableview)

arcpy.RefreshTOC()

This successfully adds the in_memory table as a table view in the MXD that can be viewed by the user.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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