AnsweredAssumed Answered

How to get return value from python tool

Question asked by taungesriaustralia-com-au-esridist Employee on Sep 25, 2020
Latest reply on Oct 7, 2020 by taungesriaustralia-com-au-esridist

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)

# -*- coding: utf-8 -*-

import arcpy
import json
import sys

class Toolbox(object):
    def __init__(self):
        """Define the toolbox (the name of the toolbox is the name of the
        .pyt file)."""

        self.label = "Toolbox"
        self.alias = ""

        # List of tool classes associated with this toolbox
        self.tools = [GetLayerDataConnections]


class GetLayerDataConnections(object):
    def __init__(self):
        """Define the tool (tool name is the name of the class)."""
        self.label = "GetLayerDataConnections"
        self.description = "Get Layer Data Connections"
        self.canRunInBackground = True

    def getParameterInfo(self):
        """Define parameter definitions"""
        # First parameter
        param0 = arcpy.Parameter(
            displayName="Layer File Location",
            name="Layerfile_location",
             datatype="GPString",
            parameterType="Required",
            direction="Input")

        params = [param0]
        return params

    def isLicensed(self):
        """Set whether tool is licensed to execute."""
        return True

    def updateParameters(self, parameters):
        """Modify the values and properties of parameters before internal
        validation is performed.  This method is called whenever a parameter
        has been changed."""

        return

    def updateMessages(self, parameters):
        """Modify the messages created by internal validation for each tool
        parameter.  This method is called after internal validation."""

        return

    def execute(self, parameters, messages):
        layerfileLocation = parameters[0].valueAsText
       
        ouputLayerInfo = []
        aLayerFile = arcpy.mp.LayerFile(layerfileLocation)
        layerList = aLayerFile.listLayers()
       
        for lyr in layerList:
            if lyr.supports("DATASOURCE"):
                conProp = lyr.connectionProperties
                name = lyr.name
                tmpStr = ("{LayerName:'"+name+"',ConnectionProperties:"+ json.dumps(conProp)+"}")
                ouputLayerInfo.append(tmpStr)
        return

 

c# snippet

 

 private void GetLayerDataSourceInfo(string layerfilePath, string toolboxPath)
        {
            try
            {
                var ps = new ProgressorSource("Start retrieving datasource.....");


                var arguments = Geoprocessing.MakeValueArray(layerfilePath);

                var gpResult = Geoprocessing.ExecuteToolAsync(toolboxPath, arguments);

                using (var progress = new ProgressDialog("Retrieving datasources"))
                {
                    var status = new ProgressorSource(progress);

                    progress.Show();
                    gpResult.Wait();
                    progress.Hide();
                }


                IGPResult ProcessingResult = gpResult.Result;
                if (ProcessingResult.IsFailed)
                {
                    string errorMessage = "";
                    foreach (IGPMessage gpMessage in ProcessingResult.Messages)
                    {
                        errorMessage += $"{{Error Code: {gpMessage.ErrorCode}, Text :  {gpMessage.Text} }}";
                    }

                    MessageBox.Show($"Geoprocessing fail {Environment.NewLine}{errorMessage}");
                }
                else
                {
                    string outvalue = ProcessingResult.ReturnValue;
                    Console.WriteLine(outvalue);
                    MessageBox.Show($"Return value {Environment.NewLine}" + outvalue);
                }

            }
            catch (Exception exce)
            {
               //log
               
            }
        }

 

Uma Harano, Wolfgang Kaiser

Outcomes