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 Answer 1
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.
- Interprocess Communication Mechanisms
- Simplest way to communication between Python and C# using IPC? (SO)
- Interprocess communication between C# and Python (SO)
- Inter-process communcation between C# and Python (MSDN)
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.
GPMessage
in the arcpy script and access it viagpMessages
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.IGPMessages
object, but with no luck. After the call ofInvokeModal
the variable is alwaysnull
.