@FlaynerThis was quite a while ago, but i think this is the right code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using ArcGIS.Core.CIM;
using ArcGIS.Core.Data;
using ArcGIS.Core.Geometry;
using ArcGIS.Desktop.Catalog;
using ArcGIS.Desktop.Core;
using ArcGIS.Desktop.Core.Geoprocessing;
using ArcGIS.Desktop.Editing;
using ArcGIS.Desktop.Extensions;
using ArcGIS.Desktop.Framework;
using ArcGIS.Desktop.Framework.Contracts;
using ArcGIS.Desktop.Framework.Dialogs;
using ArcGIS.Desktop.Framework.Threading.Tasks;
using ArcGIS.Desktop.Mapping;
using Newtonsoft.Json;
namespace DAC_Calculator
{
internal class RunGpTool : Button
{
protected override async void OnClick()
{
// Tool parameters
// Use .ags file to connect to arcgis server
var projectHomeFolder = Project.Current.HomeFolderPath;
var projGDBPath = Project.Current.DefaultGeodatabasePath;
var featureClassNames = Module1.Current.DACInputFcNames;
string tool_path = projectHomeFolder + @"\site.ags\DACTest/DACTrace2\DACTrace";
string flags_fc = projGDBPath + @"\" + featureClassNames[0];
string barriers_fc = projGDBPath + @"\" + featureClassNames[1];
string skip_locations = projGDBPath + @"\" + featureClassNames[2];
var parameters = Geoprocessing.MakeValueArray(flags_fc, barriers_fc, skip_locations);
foreach(string name in featureClassNames)
{
// Check that feature class exists
if (!await Module1.FeatureClassExistsAsync(name))
{
// if not, create it
await Module1.CreateFeatureClass(name, Module1.EnumFeatureClassType.POINT);
}
}
// Set up progress dialog
ProgressDialog progDialog = new ProgressDialog("Running DAC", false);
var status = new ProgressorSource(progDialog);
progDialog.Show();
// run tool with callback
var cts = new CancellationTokenSource();
var results = Geoprocessing.ExecuteToolAsync(tool_path, parameters, null, cts.Token,
async (eventName, o) => // implement delegate and handle events, o is message object.
{
System.Diagnostics.Debug.WriteLine($@"GP event: {eventName}");
if (eventName == "OnMessage")
{
System.Diagnostics.Debug.WriteLine($@"Msg: {o}");
}
switch (eventName)
{
case "OnValidate": // stop execute if any warnings
if ((o as IGPMessage[]).Any(it => it.Type == GPMessageType.Warning))
cts.Cancel();
break;
case "OnProgressMessage":
if ((o as string) == "Updating...")
{
status.Progressor.Message = "Job Completed, Updating Map...";
}
else
{
string msg = string.Format("{0}: {1}", new object[] { eventName, (string)o });
System.Windows.MessageBox.Show(msg);
}
cts.Cancel();
break;
case "OnProgressPos":
string msg2 = string.Format("{0}: {1} %", new object[] { eventName, (int)o });
System.Windows.MessageBox.Show(msg2);
cts.Cancel();
break;
case "OnMessage":
IGPMessage msg3 = o as IGPMessage;
string messageText = msg3.Text;
// MessageBox.Show(messageText, "DAC Tool");
switch (messageText)
{
case "Submitted.":
status.Progressor.Message = "Job Submitted to server.";
break;
case "Executing...":
status.Progressor.Message = "Job executing on server...";
break;
}
break;
case "OnBeginExecute":
// MessageBox.Show("Beginning Execution", "DAC Tool");
break;
case "OnEndExecute":
progDialog.Hide();
IGPResult res = o as IGPResult;
// IGPResult.Parameters contains all tool parameters (both input and output)
// Tuple.Item1 - name
// Tuple.Item2 - datatype
// Tuple.Item3 - value
// Tuple.Item4 - input = true, output = false
var toolParams = res.Parameters;
if (toolParams == null)
{
MessageBox.Show("No Results Found");
return;
}
// string to store dac results json string
string dac_results = "";
// List to store layer names and paths of output feature classes
var outputFeatureClasses = new List<(string Name, string Path)>();
// Loop through the parameters
foreach (var p in toolParams)
{
// if it is an output parameter of type DEFeatureClass
if (p.Item4 == false && p.Item2 == "DEFeatureClass")
{
// add to the outPutFeatureClasses
outputFeatureClasses.Add((p.Item1, p.Item3));
}
// Set the dac_results string when we find the dac_results parameter
if (p.Item1 == "dac_results")
{
dac_results = p.Item3;
}
}
// Deserialize JSON dac results to DACData object
if (dac_results != "")
{
// Deserialize the json string into a DACData object
DACData dac = JsonConvert.DeserializeObject<DACData>(dac_results);
// Update dac data results on Module1
Module1.Current.ResultsData = dac;
}
// Select results
var manholeOutput = outputFeatureClasses.Where((l) => l.Name == Module1.ManholeFeatureClassName).FirstOrDefault();
await Module1.SelectResultsFeatures(manholeOutput.Path, Module1.ManholeFeatureClassName, Module1.ManholeIdField);
var gravPipeOutput = outputFeatureClasses.Where((l) => l.Name == Module1.GravityPipesFeatureClassName).FirstOrDefault();
await Module1.SelectResultsFeatures(gravPipeOutput.Path, Module1.GravityPipesFeatureClassName, Module1.GravityPipesIdField);
var presPipeOutput = outputFeatureClasses.Where((l) => l.Name == Module1.PressurePipesFeatureClassName).FirstOrDefault();
await Module1.SelectResultsFeatures(presPipeOutput.Path, Module1.PressurePipesFeatureClassName, Module1.PressurePipesIdField);
// delete output feature classes
foreach (var (Name, Path) in outputFeatureClasses)
{
await Module1.DeleteFeatureClass(Path);
}
// Call cleanup function
Module1.DoPostGpCleanUp();
break;
}
}, GPExecuteToolFlags.None);
}
}
}