1

I have a Python script for ArcGIS that makes a new feature class. I can add this to ArcGIS as a new Script (without parameters), however the resulting feature class is not automatically added to the map layers. How does this happen? Here is an example of my Python script:

import os
import arcgisscripting

out_feat_class = 'out.shp'
workspace = r'C:\temp'

os.chdir(workspace)
gp = arcgisscripting.create(9.3)
gp.toolbox = 'management'
gp.workspace = workspace

# Create feature class
gp.CreateFeatureclass(workspace, out_feat_class, 'POINT')

# Add a field to it
gp.AddField(out_feat_class, 'Value', 'DOUBLE')

# Get insert cursor
rows = gp.InsertCursor(out_feat_class)
feat = rows.NewRow()

# Set attribute data
feat.SetValue('Value', 123.45)

# Set geometry
ptObj = gp.CreateObject('Point')
ptObj.x = 101.224
ptObj.y = 442.440
feat.Shape = ptObj

# Add to feature class
rows.InsertRow(feat)

# Done, clean up
del ptObj, feat, rows

There are no errors, a feature class is made, but it does not automatically appear in the map. I'm using ArcGIS 10.0, but I'd like this to be backwards compatible with ArcGIS 9.3.

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
Mike T
  • 42,095
  • 10
  • 126
  • 187

2 Answers2

5

On your script tool parameters definitions, create a derived, output parameter and set it using SetParameterAsText in your script.

This still depends on the Add results of geoprocessing operations to the display setting under Geoprocessing-Geoprocessing Options being checked, however.

Screenshot

(ignore the ModelBuilder part)

See also:

Glorfindel
  • 1,096
  • 2
  • 9
  • 14
blah238
  • 35,793
  • 7
  • 94
  • 195
  • This is working well with ArcGIS 10.0, but I'm having difficulties testing this with ArcGIS 9.3. There are no error messages with SetParameterAsText, and the derived result does not make it back to the map. Any ideas? – Mike T Feb 07 '13 at 04:26
  • Sorry, I don't have a 9.3 machine to test with, so no. It should work on that version with this method, though. – blah238 Feb 07 '13 at 04:29
  • Perhaps 9.3 requires a Feature Layer type parameter and MakeFeatureLayer_management instead of a Feature Class. Something you can try anyways. – blah238 Feb 07 '13 at 04:30
  • 1
    True, this solution works for 9.3. I found the registry setting on this ArcGIS 9.3 test computer: HKEY_CURRENT_USER\Software\ESRI\ArcToolbox\Settings\AddOutputsToDisplay was set to 0 and not 1. – Mike T Feb 07 '13 at 05:12
  • That checkbox should also be present in 9.3: http://resources.esri.com/help/9.3/arcgisdesktop/com/gp_toolref/using_geoprocessing_tools/controlling_tool_results.htm – blah238 Feb 07 '13 at 05:15
2

Try this:

tempLayer = "layer"
# Make a layer from the feature class
arcpy.MakeFeatureLayer_management("C:/data/mexico.gdb/cities",tempLayer)
addLayer = arcpy.mapping.Layer(tempLayer)
arcpy.mapping.AddLayer(df, addLayer, "AUTO_ARRANGE")
arcpy.RefreshTOC()
PolyGeo
  • 65,136
  • 29
  • 109
  • 338
Emi
  • 2,405
  • 2
  • 22
  • 40