Running a geoprocessing tool using background geoprocessing with custom tool

962
4
04-04-2012 10:44 AM
KennethBunzel
New Contributor
Hello,
I am trying to run a custom python script from an ArcMap toolbar and display messages created in the python script to the user in the ArcMap interface.  I am using ArcGIS 10 and the sample for this at http://help.arcgis.com/en/sdk/10.0/arcobjects_net/conceptualhelp/index.html#/d/0001000001z2000000.ht....  The sample works after I copied the sample data from my ArcGIS install folder to the same folder as the sample and modified the path to the data in the sample code.

After I added my own custom toolbox/tool through _gp.AddToolbox(toolboxPath) and started the process with _gp.ExecuteAsync("TestScript", parameters), my custom tool also ran fine.  However, my problem is I can't figure out how to get it to display messages from the python script to the user.  The MessagesCreated event is not triggered from python code such as gp.AddMessage("test") or arcpy.AddMessage("test").  The sample only runs built in ArcToolbox tools such as clip and buffer with no associated python script that I can examine.  I'm wondering what needs to be done inside the python script to trigger the MessagesCreated event so I can display those message to the user?
Thanks,
Ken
0 Kudos
4 Replies
JohnNelson3
New Contributor III
I write my messages to stdout in python and read those within the application.  Doesn't answer your question but another approach.

http://webhelp.esri.com/arcgisdesktop/9.3/index.cfm?TopicName=Writing_messages_in_script_tools

Python
sys.stdout.write("false"") or print "false"
sys.stdout.write(arcpy.GetMessages())


In My C# Program
string result;
Process pProcess = new Process();
result = pProcess.StandardOutput.ReadToEnd();
0 Kudos
KennethBunzel
New Contributor
Thanks John,
I might try your method if I can't get it to work using the method I'm using now.  I think I would have to call the script from a process.  What I am using now is the ESRI.ArcGIS.Geoprocessor.Geoprocessor class that calls the tool with gp.executeAsync.  I have also tried using ESRI.ArcGIS.Geoprocessing.GeoProcessor and calling the tool with gp.execute as in the following:
      Dim GP As ESRI.ArcGIS.Geoprocessing.GeoProcessor = New ESRI.ArcGIS.Geoprocessing.GeoProcessor()
      Dim geoProcessor As IGeoProcessor2 = DirectCast(GP, IGeoProcessor2)
      Dim gpEvents As gpEvents = New gpEvents(progressDialog)
      geoProcessor.RegisterGeoProcessorEvents(gpEvents)
      Dim geoprocessorResult As IGeoProcessorResult = GP.Execute(toolName, parameters, Nothing)
      MsgBox(geoprocessorResult.GetMessages(0))
      geoProcessor.UnRegisterGeoProcessorEvents(gpEvents)

In the above code gpEvents is a class that implements IGeoProcessorEvents.  This class exposes an event called OnMessageAdded.  This event is supposed to get triggered when the python script has gp.AddMessage("message").  The PreToolExecute event in this class is properly triggered, but not the OnMessageAdded event.  Curiously, if I remove the geoProcessor.UnRegisterGeoProcessorEvents(gpEvents) code above, and run the same tool from within ArcToolbox (after first calling the above code from my form), the OnMessageAdded event does get triggered.  But I need to run it from a form in ArcMap. Does anyone have any samples that use a custom python script?
Ken
0 Kudos
KennethBunzel
New Contributor
I have an update for this issue.  I have now tried running this on a computer with ArcGIS 9.3 with the same code.  There is no problem on this computer.  The OnMessageAdded event in ArcGIS is triggered by gp.AddMessage calls in the Python script.  So it works on a computer with ArcGIS 9.3 Windows Vista, but it does not work on a computer with ArcGIS 10 Windows 7.  I'm not sure if it is computer system related or if it is a difference between ArcGIS 9.3 and 10.
Ken
0 Kudos
KennethBunzel
New Contributor
Hello,
I have one last update for this issue.  We have since tried running the code on another computer with ArcGIS 10.  The same issue occurs with this computer as well.  I have since revised my code to use the GPToolCommandHelper class in GeoprocessingUI.dll.  This invokes the same dialog that is used when you manually run a tool from ArcToolbox.  This works perfectly and is a good workaround to the issue.  See http://resources.esri.com/help/9.3/arcgisengine/dotnet/002c4e81-d8c4-4865-9e9b-374f5c174224.htm for more information.
Ken
0 Kudos