English

Bug: Models that contain script tools that return output parameters are not being exported to Python properly

Description

Models that contain script tools that return output parameters are not being exported to Python properly. As an example, the following steps results in an erroneous python script.

1. Create a simple Python script with an output parameter of type double:

Code:
import win32com.client, sys, string
GP = win32com.client.Dispatch("esriGeoprocessing.GPDispatch.1")
GP.SetParameterAsText(0,"100")

2. Import the script into a toolbox and set the output parameter on the parameter tab. Name the script custom_gp_tool.

3. Create a new model and drag and drop the script tool into it. Attach the output of the script tool into any tool that will accept it. For example, 'Simplify Line'. Use the output parameter of the script as the value for the 'simplification tolerance'.

4. Run the model. This portion of the operation is successful.

5. Export the model to a script. The exported script hard codes a variable representing the output parameter of the first script tool, which is incorrect:

Code:
# ---------------------------------------------------------------------------
# model_to_script.py
# Created on: Fri Oct 14 2005 11:36:54 AM
# (generated by ArcGIS/ModelBuilder)
# ---------------------------------------------------------------------------

# Import system modules
import sys, string, os, win32com.client

# Create the Geoprocessor object
gp = win32com.client.Dispatch("esriGeoprocessing.GpDispatch.1")
gp.overwriteoutput = 1

# Set the necessary product code
gp.SetProduct("ArcEditor")

# Load required toolboxes...
gp.AddToolbox("C:/Program Files/ArcGIS/ArcToolbox/Toolboxes/Data Management Tools.tbx")
gp.AddToolbox("C:/Temp/MyToolBox.tbx")
# Local variables...
my_output = "100"
rivers_shp = "C:\\ESRI\\ESRIDATA\\MEXICO\\rivers.shp"
Output_Feature_Class = "C:\\ESRI\\ESRIDATA\\MEXICO\\rivers_SimplifyLine.shp"
Output_Point_Feature_Class = "C:\\ESRI\\ESRIDATA\\MEXICO\\rivers_SimplifyLine_Pnt.shp"

# Process: custom_gp_tool...
gp.toolbox = "C:/Temp/MyToolBox.tbx"
gp.custom_gp_tool()

# Process: Simplify Line...
gp.SimplifyLine_management(rivers_shp, Output_Feature_Class, "POINT_REMOVE", my_output,
"FLAG_ERRORS", "KEEP_COLLAPSED_POINTS")

The line, gp.custom_gp_tool() in bold above is essentially meaningless because the output has been hard-coded into a variable. The script used in custom_gp_tool as an example is very basic and is for demonstration purposes only. However, if it did calculations based on inputs, current date, random numbers, etc., the output would never reach the SimplifyLine tool.

Cause



This is a known issue.

Workaround

Manually change the exported script.

Change:

Code:
gp.custom_gp_tool()

to:

Code:
my_output = gp.custom_gp_tool()