ExecuteToolAsync not adding resulting layer to map.

2048
16
08-02-2019 08:34 AM
JamalWest2
Occasional Contributor

I have created a tool with python that I am running from an add-in using Geoprocessing.ExecuteToolAsync. My code gets the (x,y) center of the current map extent and makes x,y event layer from that point. I then attempt to run arcpy.conversion.LayerToKML tool. What I am finding is that the layer I create never gets added to the map/project even though I have tried specifying "GPExecuteToolFlags.AddOutputsToMap" and "GPExecuteToolFlags.Default ", which both are supposed to add all resulting layers to the map. The LayerToKML tool doesn't work if the layer being used isn't added to the project. I can get the code to run and create the layer that I want and save it using SaveToLayerFile_mangement, but how do I get it to add to the map. I have looked at the documentation extensively and haven't found anything helpful. Also I know the LayerToKML tool works through the ExecuteToolAsync because I can run it by itself (commented out other code and ran on layer added to map after creating with 1st part of script).

var xE = middlePoint.X.ToString();
var yE = middlePoint.Y.ToString();

var parameters = Geoprocessing.MakeValueArray(xE, yE)
string tool_path = @"PATH\Jwtools.tbx\Script";


var progDlg = new ProgressDialog("Running Geoprocessing Tool", "Cancel", 100, true);
var progSrc = new CancelableProgressorSource(progDlg);
progDlg.Show();
var gp_result =  await Geoprocessing.ExecuteToolAsync(tool_path, parameters, null, new CancelableProgressorSource(progDlg).Progressor, GPExecuteToolFlags.AddOutputsToMap); 
Geoprocessing.ShowMessageBox(gp_result.Messages, "GP Messages", gp_result.IsFailed ? GPMessageBoxStyle.Error : GPMessageBoxStyle.Default);
progDlg.Hide();‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
16 Replies
MatthewDriscoll
MVP Alum

I am saying execute both tools in the python script using paths.  Then add what ever resulting data you want at the end in .NET using the method I gave you.  With the SDK ExecuteTool I don't think you can add outputs in the middle of a python script and then start it back up where it left off.  The ExecuteTool runs the entire python script and then adds the output. 

0 Kudos
JamalWest2
Occasional Contributor

It also would mean I would have to create 2 separate python tools. One to create the layer, then use sdk code to ad it then execute a 2nd to run the process that I want.

0 Kudos
MatthewDriscoll
MVP Alum

Yes this would work and only be a couple additional lines of code.

0 Kudos
MatthewDriscoll
MVP Alum

You are trying to add multiple outputs from the python script into the map.  The first is the XY event tool results, which you need to for the XML tool results.  So if you want to do it that way you will need two run to different scritps in two different Tasks probably. The SDK ExecuteTool will wait for the script to completely run before adding any outputs, it probably cannot know to stop python after one tool is ran and add the output and then restart python until the next output of the next tool.  So in this case yes you will need two scripts.  

0 Kudos
JamalWest2
Occasional Contributor

Might be what I have to do for now.

0 Kudos
MatthewDriscoll
MVP Alum

You could also try adding the XY layer to the map with the python script using addLayer and then use the XML tool, if you do not want to use paths or two different scripts.  I just don't think the ExecuteTool can add any outputs until the full python script has been executed.  Good luck!  I might be over thinking things and someone can correct me.  

0 Kudos
NobbirAhmed
Esri Regular Contributor

Before you do anything, first make sure the Geoprocessing options are set properly from Project menu. 

From there, select Options > Geoprocessing and make sure to check mark 'Add Output datasets to an open ma' option - 

.Net code cannot override if the option is not set.

0 Kudos