The problem was that the GP service is not able to use Feature Classes directly as input parameters. The solution to this is to use a parsing technique which would take a string from a user (name of the feature class) and then append this string variable to the full path of the feature class (I specify the geodatabase name forehand in the code).
It seemed as this is much easier to implement in Python rather than in ModelBuilder, so I've implemented this logic by using arcpy module in ArcGIS 10.1:
import arcpy
from arcpy import env
#Setting the env workspace to be an SDE geodatabase
#
env.workspace = r'C:\sde_at_DB101.sde'
#Getting the existing polygon feature class as input parameter
#
GetExistingPolyFC = arcpy.GetParameterAsText(0)
GetPolyFC = GetExistingPolyFC
# Truncate a feature class if it exists
#
if arcpy.Exists(GetPolyFC):
arcpy.AddMessage("Polygon feature class exists, start working...")
arcpy.TruncateTable_management(r'C:\sde_at_DB_101.sde\%s' %GetPolyFC)
arcpy.AddMessage("Truncating table is complete")
else:
arcpy.AddMessage("Polygon feature class does not exist")
I've used this logic for other bits of my bigger model and it works just fine as a GP service now. I've decided to rewrite the whole model as a Python script from scratch and it was much easier to implement and it performs much faster too when running the GP task.
I would try registering an eventlistener, to help debug what's going on inside of the gp tool.
public static void Test()
{
InvokeTool(@"C:\Program Files (x86)\ArcGIS\Desktop10.1\ArcToolbox\Toolboxes\3D Analyst Tools.tbx",
"AddSurfaceInformation");
}
public static void InvokeTool(string tbxPath, string toolName)
{
var gp = new GeoProcessorClass() as IGeoProcessor2;
// Add the toolbox.
gp.AddToolbox(tbxPath);
//// Generate the array of parameters.
IVariantArray parameters = new VarArrayClass();
parameters.Add(@"a bad parameter");
// Execute the model tool by name.
var evts = new GpEvents();
gp.RegisterGeoProcessorEvents3(evts);
var result = gp.Execute(toolName, parameters,null);
}
Simple callback to see what the tool is experiencing ...
public class GpEvents : IGeoProcessorEvents3, IGeoProcessorEvents2, IGeoProcessorEvents
{
private void ListMsgs(IGPMessages msgs)
{
for (int i = 0; i < msgs.Count; i++)
{
var msg = msgs.GetMessage(i) as IGPMessage;
Console.WriteLine("{0} {1}", msg.ErrorCode,msg.Description);
}
}
public void OnProcessMessages(IGeoProcessorResult result, IGPMessages pMsgs)
{
Console.WriteLine("OnProcessMessages");
ListMsgs(pMsgs);
}
public void OnProgressMessage(IGeoProcessorResult result, string message)
{
Console.WriteLine("OnProgressMessage");
}
public void OnProgressPercentage(IGeoProcessorResult result, double percentage)
{
Console.WriteLine("OnProgressPercentage");
}
public void OnProgressShow(IGeoProcessorResult result, bool Show)
{
Console.WriteLine("OnProgressShow");
}
public void PostToolExecute(IGeoProcessorResult result)
{
Console.WriteLine("PostToolExecute");
}
public void PreToolExecute(IGeoProcessorResult result)
{
Console.WriteLine("PreToolExecute");
}
public void OnMessageAdded(IGPMessage message)
{
Console.WriteLine("OnMessageAdded {0} {1}",message.ErrorCode, message.Description);
}
public void PostToolExecute(int result, IMdProcess Process)
{
Console.WriteLine("PostToolExecute");
}
public void PreToolExecute(IMdProcess Process)
{
Console.WriteLine("PreToolExecute");
}
public void ToolboxChange()
{
Console.WriteLine("ToolboxChange");
}
public void PostToolExecute(IGPTool Tool, IArray Values, int result, IGPMessages Messages)
{
Console.WriteLine("PostToolExecute");
ListMsgs(Messages);
}
public void PreToolExecute(IGPTool Tool, IArray Values, int processID)
{
Console.WriteLine("PreToolExecute");
}
}
The method above won't tell you when the toolname is not valid, so this can be used to list toolnames in a toolbox:
public static void ListTools(string tbxPath)
{
var t = Type.GetTypeFromProgID("esriGeoprocessing.ToolboxWorkspaceFactory");
var twf = Activator.CreateInstance(t) as IWorkspaceFactory;
var fi = new System.IO.FileInfo(tbxPath);
if (!fi.Exists)
throw new System.IO.FileNotFoundException(tbxPath);
var ws = twf.OpenFromFile(fi.DirectoryName, 0) as IToolboxWorkspace;
var tbx = ws.OpenToolbox(fi.Name);
var enumName = tbx.ToolNames;
enumName.Reset();
IGPToolName name;
while ((name = enumName.Next()) != null)
{
Console.WriteLine("{0} : {1}", ((IDatasetName)name).Name, name.DisplayName);
}
}
Best Answer
If the extension were installed but not enabled you'd see a padlock symbol, not a red cross. If a model or script tool has lost its reference you would see such a symbol and you'd need to fix the reference to the tool/model location. See the 10.2 Desktop Help article: Repairing a model