/// <summary> /// Called when a step of this type is executed in the workflow. /// </summary> /// <param name="JobID">ID of the job being executed</param> /// <param name="StepID">ID of the step being executed</param> /// <param name="argv">Array of arguments passed into the step's execution</param> /// <param name="ipFeedback">Feedback object to return status messages and files</param> /// <returns>Return code of execution for workflow path traversal</returns> public int Execute(int jobID, int stepID, ref object[] argv, ref IJTXCustomStepFeedback ipFeedback) { #if (!SERVER) StatusForm sfd = null; #endif bool success = true; // Reset the message logging this.ClearCachedLogMessages(); try { Log("ExecuteGPTool.Execute beginning.."); // Define variables string strToolboxPath = ""; string strTool = ""; string strToolboxRoot = ""; string strToolboxName = ""; // Get the toolbox name (check for errors) if (!StepUtilities.GetArgument(ref argv, "toolboxpath", true, out strToolboxPath) || strToolboxPath.Equals(String.Empty)) { success = false; Log("Error getting 'toolboxpath' argument"); #if (!SERVER) MessageBox.Show("Missing toolbox argument."); #endif } // Get the tool name (check for errors) else if (!StepUtilities.GetArgument(ref argv, "tool", true, out strTool) || strTool.Equals(String.Empty)) { success = false; Log("Error getting 'tool' argument"); #if (!SERVER) MessageBox.Show("Missing tool argument."); #endif } else { #if (!SERVER) // Display status dialog sfd = new StatusForm(); sfd.ToolName = strTool; sfd.Show(); sfd.Refresh(); #endif // Get directory of toolbox strToolboxRoot = Path.GetDirectoryName(strToolboxPath); // Get toolbox name without .tbx extension strToolboxName = Path.GetFileNameWithoutExtension(strToolboxPath); // Open toolbox and tool IWorkspaceFactory pToolboxWorkspaceFactory = new ToolboxWorkspaceFactoryClass(); IToolboxWorkspace pToolboxWorkspace = pToolboxWorkspaceFactory.OpenFromFile(strToolboxRoot, 0) as IToolboxWorkspace; IGPToolbox pGPToolbox = pToolboxWorkspace.OpenToolbox(strToolboxName); IGPTool pGPTool = pGPToolbox.OpenTool(strTool); Log("ExecuteGPTool.Execute successfully opened Tool " + strTool + " in Toolbox " + strToolboxPath + ".."); // Generate the arrays for parameters IVariantArray parameters = new VarArrayClass(); // For Execute method ESRI.ArcGIS.esriSystem.IArray pParameterArray = pGPTool.ParameterInfo; // For Validate and InvokeModal methods // Get parameter "pairs"; "GetDoubleArguments" supports the GP-style, // two-colon argument strings, like "/param:tool_param_name:tool_param_value" string[] argNames; string[] argValues; if (StepUtilities.GetDoubleArguments(ref argv, "param", true, out argNames, out argValues)) { // Stash away the variables that were passed in Dictionary<string, string> argTable = new Dictionary<string, string>(); for (int i = 0; i < argNames.Length; i++) { string uppercaseName = argNames[i].ToUpper(); argTable[uppercaseName] = argValues[i]; } // The GP tool's parameter list may include parameters that weren't specified in the // JTX step arguments. So iterate through the tool's parameters, setting the values // from the input where you can. Where no value was passed in, just skip the // parameter and go with the default. for (int i = 0; i < pParameterArray.Count; i++) { IGPParameter pGPParam = (IGPParameter)pParameterArray.get_Element(i); IGPParameterEdit pGPParamEdit = (IGPParameterEdit)pGPParam; string uppercaseName = pGPParam.Name.ToUpper(); // Override the default value, if something was passed in if (argTable.ContainsKey(uppercaseName)) { IGPDataType pGPType = pGPParam.DataType; string strValue = argTable[uppercaseName]; pGPParamEdit.Value = pGPType.CreateValue(strValue); Log("ExecuteGPTool.Execute Tool Parameter = " + uppercaseName + ", Value = " + strValue + ".."); } // Always stash away the current parameter value, since we need the complete list // for "Execute" below. parameters.Add(pGPParam.Value); } } Log("ExecuteGPTool.Execute successfully setup parameter values.."); // Initialize the geoprocessor IGeoProcessor pGP = new GeoProcessorClass(); // Create callback object for GP messages JTXGPCallback pCallback = new JTXGPCallback(); // Set up the geoprocessor pGP.AddToolbox(strToolboxPath); pGP.RegisterGeoProcessorEvents(pCallback); Log("ExecuteGPTool.Execute created and registered GeoProcessor.."); // Create the messages object and a bool to pass to InvokeModal method IGPMessages pGPMessages = pGPTool.Validate(pParameterArray, true, null); IGPMessages pInvokeMessages = new GPMessagesClass(); string strMessage = ""; // Check for error messages if (pGPMessages.MaxSeverity == esriGPMessageSeverity.esriGPMessageSeverityError) { #if (!SERVER) // Only want to invoke a modal dialog if we're running on a workstation // Set a reference to IGPCommandHelper2 interface IGPToolCommandHelper2 pToolHelper = new GPToolCommandHelperClass() as IGPToolCommandHelper2; pToolHelper.SetTool(pGPTool); bool pOK = true; // Open tool GUI pToolHelper.InvokeModal(0, pParameterArray, out pOK, out pInvokeMessages); if (pOK == true) { bool bFailureMessages; strMessage = ConvertGPMessagesToString(pInvokeMessages, out bFailureMessages); success = !bFailureMessages; } else { success = false; } #else Log("ExecuteGPTool.Execute Tool Validate failed.."); // If we're running on a server, then just indicate a failure. (Someone will // have to use the JTX application to fix the step arguments, if they can be // fixed.) success = false; #endif } else // If there are no error messages, execute the tool { Log("ExecuteGPTool.Execute successfully validated parameter values, About to Execute.."); IGPMessages ipMessages = new GPMessagesClass(); try { pGPTool.Execute(pParameterArray, null, new GPEnvironmentManagerClass(), ipMessages); Log("ExecuteGPTool.Execute completed call to pGPTool.Execute()"); } catch (System.Runtime.InteropServices.COMException ex) { success = false; Log("ExecuteGPTool.Execute Tool Execute failed, Message = " + ex.Message + ", DataCode = " + ex.ErrorCode + ", Data = " + ex.StackTrace); } // Get Messages bool bFailureMessages = false; strMessage += this.ConvertGPMessagesToString(ipMessages, out bFailureMessages); Log("ExecuteGPTool.Execute got messages from tool"); Log("*** GP MESSAGES ***" + System.Environment.NewLine + strMessage + System.Environment.NewLine); Log("*** END GP MESSAGES ***"); // If tool failed during execution, indicate a failure if (pGP.MaxSeverity == (int)esriGPMessageSeverity.esriGPMessageSeverityError) { success = false; Log("ExecuteGPTool.Execute Found Error messages from the tool.."); } } // Call AttachMsg try { IJTXJobManager ipJobManager = m_ipDatabase.JobManager; IJTXJob ipJob = ipJobManager.GetJob(jobID); AttachMsg(ipJob, strTool, this.GetCachedLogMessages()); this.ClearCachedLogMessages(); } catch (Exception ex) { string strEx = ex.Message; Log("Caught exception: " + ex.Message); } pGP.UnRegisterGeoProcessorEvents(pCallback); } } catch (Exception ex2) { success = false; Log("Caught exception: " + ex2.Message); #if (!SERVER) string msg = ""; if (ex2.InnerException == null) { msg = "Inner Exception is null"; } else { msg = "Inner Exception is not null"; } MessageBox.Show("Stack Trace: " + ex2.StackTrace + Environment.NewLine + "Message: " + ex2.Message + Environment.NewLine + "Source: " + ex2.Source + Environment.NewLine + msg); #endif } // Clean up #if (!SERVER) if (sfd != null) { sfd.Close(); } #endif // Indicate success or failure if (success == true) { return 1; } else { return 0; } }