Adding table to CURRENT map document in arcpy (works on 2nd run only)

3341
4
Jump to solution
04-07-2016 02:05 PM
RebeccaStrauch__GISP
MVP Esteemed Contributor

As part of my script, I am use arcpy.Frequency_analysis to summarize some values.  When run interactively, it automatically adds the table.  In the script, the table is created (and I get back "Exists", but does not add it.

I am using the sample in the TableView—Help | ArcGIS for Desktop​ 

If I run the tool, it does tell me the table exists, so runs the arcpy.mapping.AddTableView command, but the table is not added.   If I rerun the exact script from the Results table, it adds the table on the second run.  Strange

It's a very fast script, so a rerun is not a big deal (nor is dragging the table over to add), but for future use by other users, it would be nice if it worked as expected.

Any suggestions?

# ...

# running frequency to get length summary

sum_table = arcpy.os.path.join(theWorkspace, "{0}_SumLen".format(fishnetFCdd))

arcpy.Frequency_analysis(fishnetFCdd, sum_table, "TransType", "Len_m;Len_km")

sum_tableview = arcpy.mapping.TableView(sum_table)

# If running this in ArcMap, can add the new layer...will have to manually save at least during testing

#    if not in ArcMap, will skip.

print(" If running in ArcMap, will add the two pts files.")

try:       

    # my variables finalOutdd and finalOutProj are set to full paths of the output

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

except RuntimeError:

    print("Not using ArcMap")

else:

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

    layerList = [fishnetFC, fishnetFCdd]

    for layer in layerList:

        addLayer = arcpy.mapping.Layer(layer)

        if arcpy.Exists(addLayer):

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

            print("-> Added {0} to map.".format(layer)) #, finalLayer)) 

    if arcpy.Exists(sum_table):

        print("exists")

        arcpy.mapping.AddTableView(df, sum_tableview)

        #arcpy.mapping.AddTableView(df, sum_tableview)  #tried this, didn't work

    else:

        print("can't find it")

    #mxd.save()    #haven't needed this

   

#....

0 Kudos
1 Solution

Accepted Solutions
DanPatterson_Retired
MVP Esteemed Contributor

RefreshTOC—Help | ArcGIS for Desktop

RefreshActiveView—Help | ArcGIS for Desktop

do either have any effect?  I was wondering if the first would work if the data view were selected in the TOC

and just in case check to make sure results are added to display, although supposedly for tools only

Using geoprocessing options to control tool execution—Help | ArcGIS for Desktop

View solution in original post

4 Replies
DanPatterson_Retired
MVP Esteemed Contributor

RefreshTOC—Help | ArcGIS for Desktop

RefreshActiveView—Help | ArcGIS for Desktop

do either have any effect?  I was wondering if the first would work if the data view were selected in the TOC

and just in case check to make sure results are added to display, although supposedly for tools only

Using geoprocessing options to control tool execution—Help | ArcGIS for Desktop

View solution in original post

JoshuaBixby
MVP Esteemed Contributor

Does running RefreshTOC make a difference? 

RebeccaStrauch__GISP
MVP Esteemed Contributor

Thank you Dan and Joshua.  RefreshTOC was the missing piece.  I adding the refresh right after adding it worked.

arcpy.mapping.AddTableView(df, sum_tableview)

arcpy.RefreshActiveView()

Dan responded first, so he got the check. But I appreciate the answeres from both.

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

check to Joshua as well

0 Kudos