Hi Guys,
I am currently trying to find out how to get return back value from python tool box.
Below is the snippet of Python and C#, not sure why when I run the code whole ArcGIS pro hang.
I have done similar way before but working properly but not sure why it is happening with these.
Python snippet (ouputLayerInfo value is the one want to return to c# side)
<SPAN class="comment token"># -*- coding: utf-8 -*-</SPAN>
<SPAN class="keyword token">import</SPAN> arcpy
<SPAN class="keyword token">import</SPAN> json
<SPAN class="keyword token">import</SPAN> sys
<SPAN class="keyword token">class</SPAN> <SPAN class="token class-name">Toolbox</SPAN><SPAN class="punctuation token">(</SPAN>object<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">def</SPAN> <SPAN class="token function">__init__</SPAN><SPAN class="punctuation token">(</SPAN>self<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="string token">"""Define the toolbox (the name of the toolbox is the name of the
.pyt file)."""</SPAN>
self<SPAN class="punctuation token">.</SPAN>label <SPAN class="operator token">=</SPAN> <SPAN class="string token">"Toolbox"</SPAN>
self<SPAN class="punctuation token">.</SPAN>alias <SPAN class="operator token">=</SPAN> <SPAN class="string token">""</SPAN>
<SPAN class="comment token"># List of tool classes associated with this toolbox</SPAN>
self<SPAN class="punctuation token">.</SPAN>tools <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN>GetLayerDataConnections<SPAN class="punctuation token">]</SPAN>
<SPAN class="keyword token">class</SPAN> <SPAN class="token class-name">GetLayerDataConnections</SPAN><SPAN class="punctuation token">(</SPAN>object<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">def</SPAN> <SPAN class="token function">__init__</SPAN><SPAN class="punctuation token">(</SPAN>self<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="string token">"""Define the tool (tool name is the name of the class)."""</SPAN>
self<SPAN class="punctuation token">.</SPAN>label <SPAN class="operator token">=</SPAN> <SPAN class="string token">"GetLayerDataConnections"</SPAN>
self<SPAN class="punctuation token">.</SPAN>description <SPAN class="operator token">=</SPAN> <SPAN class="string token">"Get Layer Data Connections"</SPAN>
self<SPAN class="punctuation token">.</SPAN>canRunInBackground <SPAN class="operator token">=</SPAN> <SPAN class="token boolean">True</SPAN>
<SPAN class="keyword token">def</SPAN> <SPAN class="token function">getParameterInfo</SPAN><SPAN class="punctuation token">(</SPAN>self<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="string token">"""Define parameter definitions"""</SPAN>
<SPAN class="comment token"># First parameter</SPAN>
param0 <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>Parameter<SPAN class="punctuation token">(</SPAN>
displayName<SPAN class="operator token">=</SPAN><SPAN class="string token">"Layer File Location"</SPAN><SPAN class="punctuation token">,</SPAN>
name<SPAN class="operator token">=</SPAN><SPAN class="string token">"Layerfile_location"</SPAN><SPAN class="punctuation token">,</SPAN>
datatype<SPAN class="operator token">=</SPAN><SPAN class="string token">"GPString"</SPAN><SPAN class="punctuation token">,</SPAN>
parameterType<SPAN class="operator token">=</SPAN><SPAN class="string token">"Required"</SPAN><SPAN class="punctuation token">,</SPAN>
direction<SPAN class="operator token">=</SPAN><SPAN class="string token">"Input"</SPAN><SPAN class="punctuation token">)</SPAN>
params <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN>param0<SPAN class="punctuation token">]</SPAN>
<SPAN class="keyword token">return</SPAN> params
<SPAN class="keyword token">def</SPAN> <SPAN class="token function">isLicensed</SPAN><SPAN class="punctuation token">(</SPAN>self<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="string token">"""Set whether tool is licensed to execute."""</SPAN>
<SPAN class="keyword token">return</SPAN> <SPAN class="token boolean">True</SPAN>
<SPAN class="keyword token">def</SPAN> <SPAN class="token function">updateParameters</SPAN><SPAN class="punctuation token">(</SPAN>self<SPAN class="punctuation token">,</SPAN> parameters<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="string token">"""Modify the values and properties of parameters before internal
validation is performed. This method is called whenever a parameter
has been changed."""</SPAN>
<SPAN class="keyword token">return</SPAN>
<SPAN class="keyword token">def</SPAN> <SPAN class="token function">updateMessages</SPAN><SPAN class="punctuation token">(</SPAN>self<SPAN class="punctuation token">,</SPAN> parameters<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="string token">"""Modify the messages created by internal validation for each tool
parameter. This method is called after internal validation."""</SPAN>
<SPAN class="keyword token">return</SPAN>
<SPAN class="keyword token">def</SPAN> <SPAN class="token function">execute</SPAN><SPAN class="punctuation token">(</SPAN>self<SPAN class="punctuation token">,</SPAN> parameters<SPAN class="punctuation token">,</SPAN> messages<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
layerfileLocation <SPAN class="operator token">=</SPAN> parameters<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">.</SPAN>valueAsText
ouputLayerInfo <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">]</SPAN>
aLayerFile <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>mp<SPAN class="punctuation token">.</SPAN>LayerFile<SPAN class="punctuation token">(</SPAN>layerfileLocation<SPAN class="punctuation token">)</SPAN>
layerList <SPAN class="operator token">=</SPAN> aLayerFile<SPAN class="punctuation token">.</SPAN>listLayers<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">for</SPAN> lyr <SPAN class="keyword token">in</SPAN> layerList<SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">if</SPAN> lyr<SPAN class="punctuation token">.</SPAN>supports<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"DATASOURCE"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
conProp <SPAN class="operator token">=</SPAN> lyr<SPAN class="punctuation token">.</SPAN>connectionProperties
name <SPAN class="operator token">=</SPAN> lyr<SPAN class="punctuation token">.</SPAN>name
tmpStr <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="string token">"{LayerName:'"</SPAN><SPAN class="operator token">+</SPAN>name<SPAN class="operator token">+</SPAN><SPAN class="string token">"',ConnectionProperties:"</SPAN><SPAN class="operator token">+</SPAN> json<SPAN class="punctuation token">.</SPAN>dumps<SPAN class="punctuation token">(</SPAN>conProp<SPAN class="punctuation token">)</SPAN><SPAN class="operator token">+</SPAN><SPAN class="string token">"}"</SPAN><SPAN class="punctuation token">)</SPAN>
ouputLayerInfo<SPAN class="punctuation token">.</SPAN>append<SPAN class="punctuation token">(</SPAN>tmpStr<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">return</SPAN>
<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>c# snippet
<SPAN class="keyword token">private</SPAN> <SPAN class="keyword token">void</SPAN> <SPAN class="token function">GetLayerDataSourceInfo</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="keyword token">string</SPAN> layerfilePath<SPAN class="punctuation token">,</SPAN> <SPAN class="keyword token">string</SPAN> toolboxPath<SPAN class="punctuation token">)</SPAN>
<SPAN class="punctuation token">{</SPAN>
<SPAN class="keyword token">try</SPAN>
<SPAN class="punctuation token">{</SPAN>
<SPAN class="keyword token">var</SPAN> ps <SPAN class="operator token">=</SPAN> <SPAN class="keyword token">new</SPAN> <SPAN class="token class-name">ProgressorSource</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Start retrieving datasource....."</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN>
<SPAN class="keyword token">var</SPAN> arguments <SPAN class="operator token">=</SPAN> Geoprocessing<SPAN class="punctuation token">.</SPAN><SPAN class="token function">MakeValueArray</SPAN><SPAN class="punctuation token">(</SPAN>layerfilePath<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN>
<SPAN class="keyword token">var</SPAN> gpResult <SPAN class="operator token">=</SPAN> Geoprocessing<SPAN class="punctuation token">.</SPAN><SPAN class="token function">ExecuteToolAsync</SPAN><SPAN class="punctuation token">(</SPAN>toolboxPath<SPAN class="punctuation token">,</SPAN> arguments<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN>
<SPAN class="keyword token">using</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="keyword token">var</SPAN> progress <SPAN class="operator token">=</SPAN> <SPAN class="keyword token">new</SPAN> <SPAN class="token class-name">ProgressDialog</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"Retrieving datasources"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="punctuation token">{</SPAN>
<SPAN class="keyword token">var</SPAN> status <SPAN class="operator token">=</SPAN> <SPAN class="keyword token">new</SPAN> <SPAN class="token class-name">ProgressorSource</SPAN><SPAN class="punctuation token">(</SPAN>progress<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN>
progress<SPAN class="punctuation token">.</SPAN><SPAN class="token function">Show</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN>
gpResult<SPAN class="punctuation token">.</SPAN><SPAN class="token function">Wait</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN>
progress<SPAN class="punctuation token">.</SPAN><SPAN class="token function">Hide</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN>
<SPAN class="punctuation token">}</SPAN>
IGPResult ProcessingResult <SPAN class="operator token">=</SPAN> gpResult<SPAN class="punctuation token">.</SPAN>Result<SPAN class="punctuation token">;</SPAN>
<SPAN class="keyword token">if</SPAN> <SPAN class="punctuation token">(</SPAN>ProcessingResult<SPAN class="punctuation token">.</SPAN>IsFailed<SPAN class="punctuation token">)</SPAN>
<SPAN class="punctuation token">{</SPAN>
<SPAN class="keyword token">string</SPAN> errorMessage <SPAN class="operator token">=</SPAN> <SPAN class="string token">""</SPAN><SPAN class="punctuation token">;</SPAN>
<SPAN class="keyword token">foreach</SPAN> <SPAN class="punctuation token">(</SPAN>IGPMessage gpMessage <SPAN class="keyword token">in</SPAN> ProcessingResult<SPAN class="punctuation token">.</SPAN>Messages<SPAN class="punctuation token">)</SPAN>
<SPAN class="punctuation token">{</SPAN>
errorMessage <SPAN class="operator token">+</SPAN><SPAN class="operator token">=</SPAN> $<SPAN class="string token">"{{Error Code: {gpMessage.ErrorCode}, Text : {gpMessage.Text} }}"</SPAN><SPAN class="punctuation token">;</SPAN>
<SPAN class="punctuation token">}</SPAN>
MessageBox<SPAN class="punctuation token">.</SPAN><SPAN class="token function">Show</SPAN><SPAN class="punctuation token">(</SPAN>$<SPAN class="string token">"Geoprocessing fail {Environment.NewLine}{errorMessage}"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN>
<SPAN class="punctuation token">}</SPAN>
<SPAN class="keyword token">else</SPAN>
<SPAN class="punctuation token">{</SPAN>
<SPAN class="keyword token">string</SPAN> outvalue <SPAN class="operator token">=</SPAN> ProcessingResult<SPAN class="punctuation token">.</SPAN>ReturnValue<SPAN class="punctuation token">;</SPAN>
Console<SPAN class="punctuation token">.</SPAN><SPAN class="token function">WriteLine</SPAN><SPAN class="punctuation token">(</SPAN>outvalue<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN>
MessageBox<SPAN class="punctuation token">.</SPAN><SPAN class="token function">Show</SPAN><SPAN class="punctuation token">(</SPAN>$<SPAN class="string token">"Return value {Environment.NewLine}"</SPAN> <SPAN class="operator token">+</SPAN> outvalue<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN>
<SPAN class="punctuation token">}</SPAN>
<SPAN class="punctuation token">}</SPAN>
<SPAN class="keyword token">catch</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="token class-name">Exception</SPAN> exce<SPAN class="punctuation token">)</SPAN>
<SPAN class="punctuation token">{</SPAN>
<SPAN class="comment token">//log</SPAN>
<SPAN class="punctuation token">}</SPAN>
<SPAN class="punctuation token">}</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>Uma Harano, Wolfgang Kaiser