1

I am trying to access an output parameter from a Python toolbox tool and cannot find any information about how this is done:

I am developing an Esri Add-In for ArcGIS 10.1 SP1 for Desktop (Build 3143) and programming against ArcObjects SDK for the Microsoft .NET Framework 10.1 SP1 using C#.

I am calling a Python Toolbox tool using the following code:

// Call scripting tool from toolbox and show GUI
string assemblyFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
string toolbox = Path.Combine(Path.Combine(assemblyFolder, "Toolbox"), @"Amec Foster Wheeler Groundwater Tools.pyt");
string toolname = "CreateBlankGroundwaterGeodatabase";
// Open Dialog for entering parameters
IGPToolCommandHelper2 toolHelper = new GPToolCommandHelperClass() as IGPToolCommandHelper2;
toolHelper.SetToolByName(toolbox, toolname);
IGPUtilities gpUtils = new GPUtilities();
IGPTool tool = toolHelper.Tool;
IArray parameters = tool.ParameterInfo;
IGPMessages gpMessages = new GPMessagesClass();
bool pok;
toolHelper.InvokeModal(0, parameters, out pok, out gpMessages);

// What needs to be done here to get the values of the parameters, the user entered during the execution? 
// How can I retrieve output parameter values here?

This is working as expected. The tool dialog opens up in ArcMap and the tool is running without any errors.

In the Python tool itself I am defining one parameter as output parameter

def getParameterInfo(self):
# [..]
param5 = arcpy.Parameter(
    displayName="out_fcpath",
    name="out_fcpath",
    datatype="GPString",
    parameterType="Derived",
    direction="Output"
)

params = [param0, param1, param2, param3, param4, param5]
return params

And in the execute function I am setting this parameter to a string value.

def execute(self, parameters, messages):
    # [..]
    arcpy.SetParameterAsText(5,"Test")

    return

But how can I retrieve that parameter by code in C# after the tool has been executed? Perhaps you can point me into the right direction?

References:

  1. Get results from programmatically-launched geoprocessing tool
  2. Running a geoprocessing tool using background geoprocessing with custom tool
  • You might be able to add a GPMessage in the arcpy script and access it via gpMessages out variable. Not sure about it, but it would be the process I would start investigating. You would need to do more work parsing the messages, but it might facilitate communication between the two. – Branco Jun 17 '16 at 12:13
  • @Branco Thank you for this hint! It feels a little bit like a hack but it might work for simple return parameters. – Christian Junk Jun 20 '16 at 7:45
  • @Branco Do you know how to add a message in the python code, so that it could be retrieved in the GPMessages variable in the C# code? I have tried {{arcpy.AddMessage}} but with no luck. – Christian Junk Jun 20 '16 at 15:26
  • I am not really sure. I saw pieces here and there that say it may not actually support passing messages like that. I don't have a GIS system nearby to test with. It may be beneficial to take one of the methods in my answer and create a generic communication service for this and future projects. – Branco Jun 20 '16 at 16:26
  • I have spend all the day, finding a way to populate the IGPMessages object, but with no luck. After the call of InvokeModal the variable is always null. – Christian Junk Jun 21 '16 at 12:31
2

To go along with my comment.

The problem you have is having to figure out a way to implement interprocess communication. Whenever you start up a python script, you're creating another process to run (python.exe) to go along with your already running process (ArcMap.exe). The problem here is that there needs to be a way to communicate back and forth.

You need to either read in the standard output of one side to the other or somehow set up another form of communication between the two processes.

Some examples I have seen include:

  • Using a mutex
  • Using a Semiphore
  • Using a shared file directory (monitor for changes, etc)
  • Using a network connection (i.e. TCP)
  • Reading standard output from one to the other
  • Monitoring blocks of memory

ESRI already provides a means to do this with the GPMessages. You could dig further into their source code and discern their particular means for this or just utilize it.

Here are a few sites I found and examples I found.

I think the biggest thing is getting the terminology down and knowing what to search. If you develop a good solution, please post it as it would definitely be something to help the greater good here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.