ExecuteToolAsync not adding resulting layer to map.

805
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
NobbirAhmed
Esri Regular Contributor

Could you please check what option is set in Pro App's Project > Options > Geoprocessing and make sure 'Add output datasets to an open map' is checked? 

And which version of ArcGIS Pro you are on? 

0 Kudos
MatthewDriscoll
MVP Regular Contributor

"a"  is the path to the new layer created from the execution of the tool.  

await QueuedTask.Run(() =>
{
Map map = MapView.Active.Map;
LayerFactory.Instance.CreateLayer(new Uri(a), map);
});‍‍‍‍‍
0 Kudos
JamalWest2
Occasional Contributor

I shouldn't need to manually add the layer via sdk given that the ExecuteToolAsync is supposed to add the layer when it creates it. Using that means I have to generate the filename on the c#side or return it from my python tool. Do you know how to return it? In python I know how to generate unique file names, not in c# or the arcgis pro sdk.

0 Kudos
MatthewDriscoll
MVP Regular Contributor

Jamal West wrote:

I shouldn't need to manually add the layer via sdk given that the ExecuteToolAsync is supposed to add the layer when it creates it. Using that means I have to generate the filename on the c#side or return it from my python tool. Do you know how to return it? In python I know how to generate unique file names, not in c# or the arcgis pro sdk.

Agreed!  Just showing you the way I know how.

Yes I do think you can get the results from a python script over to the .NET, but the only way I personally would know how is if the python script is executed from the .NET solution.   I think I have done this, let me take a look and think it over.  

0 Kudos
JamalWest2
Occasional Contributor

I thought I responded to you already, but I guess it didn't post. Yes I am using the SDK to execute the python script. That's what the ExecuteToolAsync() does. I do not know how to get the results from the python script back to the SDK solution though. If you figure it out let me know please .

0 Kudos
MatthewDriscoll
MVP Regular Contributor

Do you not specify the path of the xy layer results in the python script or are you using in memory?

0 Kudos
JamalWest2
Occasional Contributor

I've tried both ways. Neither work. It only will work if that layer is added to the map.

0 Kudos
MatthewDriscoll
MVP Regular Contributor

If you have the path of the resulting XY layer path you should be able to just get it from the path and not the Map. 

From the Code samples:

out_Layer = "firestations_layer"

saved_Layer = r"c:\output\firestations.lyr"

Use arcpy.SaveToLayerFile_management(out_Layer, saved_Layer) when saving the XY layer.

Then use  arcpy.env.workspace = r"c:\output\".

Finally use layers = arcpy.ListFiles("*.lyr*") to get the Parameter layer.

0 Kudos
JamalWest2
Occasional Contributor

Are you saying in order to send it back to .NET or in order to specify the path? I have used it to specify the path and file and it doesn't work. I'm not sure how to send that back to the SDK source and then parse it.

0 Kudos