Execute Arctool from ArcObjects

1598
9
07-10-2012 07:24 AM
geogeekgeogeek
New Contributor III
i cannot execute Arcgis Toolbox tools using the code below, VS 2010 indicates that the error comes from the gp.execute() function, i've tried this code with the "Buffer" tool but it seems the i can't run any tool using this code.

Any help please

i use ArcObjects 10.1 , VS 2010 , Framework .NET 4.0

Thanks in advance


   IVariantArray parameters = new VarArrayClass();
            IGeoProcessor2 gp = new GeoProcessorClass();
            try
             {

            gp.AddToolbox(@"C:\Program Files\ArcGIS\ArcPad10.0\DesktopTools10\ArcPad Tools.tbx");
            //gp.AddToolbox(@"c:\program files\arcgis\desktop10.1\ArcToolbox\Toolboxes\Analysis Tools.tbx"); 
            gp.SetEnvironmentValue("Workspace", @"C:\Documents and Settings\Administrateur\Mes documents\ArcGIS\Default.gdb");
            parameters.Add(@"C:\Documents and Settings\Administrateur\Bureau\geo_db\test.axf");
            //parameters.Add(@"C:\Documents and Settings\Administrateur\Mes documents\ArcGIS\Default.gdb\gone");
            //parameters.Add(@"C:\Documents and Settings\Administrateur\Mes documents\ArcGIS\Default.gdb\gone_bif");
            parameters.Add("");
            parameters.Add("");
            gp.Execute("ArcPadCheckin", parameters, null); /// the problem in this line

                }
               catch (Exception ex)
               {
                   var sev = 2;
                   label1.Text = label1.Text + gp.GetMessages(sev);
                   label1.Text = label1.Text + ex.StackTrace;
               }
0 Kudos
9 Replies
NobbirAhmed
Esri Regular Contributor
When Execute fails, you get to catch block - what geoprocessing error messages you get?
0 Kudos
geogeekgeogeek
New Contributor III
it gimme this error

   at ESRI.ArcGIS.Geoprocessing.GeoProcessorClass.IGeoProcessor2_Execute(String Name, IVariantArray ipValues, ITrackCancel pTrackCancel)
   at DesktopWindowsApplication2.Form1.Form1_Load(Object sender, EventArgs e) in C:\Documents and Settings\Administrateur\Mes documents\Visual Studio 2010\Projects\DesktopWindowsApplication2\DesktopWindowsApplication2\Form1.cs:line 49Finished 


i could'nt find the source of the problem from this error 😞
0 Kudos
NobbirAhmed
Esri Regular Contributor
Please add few more lines and let me know what messages you are getting.

Your code (I've excluded other lines for clarity) should look like this.

object sev = 2;
try
{
    gp.Execute("ArcPadCheckin", parameters, null);
    MessageBox.Show(gp.GetMessages(ref sev));
}
catch (Exception ex)
{
    //var sev = 2;
    MessageBox.Show(gp.GetMessages(ref sev));
    //label1.Text = label1.Text + gp.GetMessages(sev);
    label1.Text = label1.Text + ex.StackTrace;
}
0 Kudos
geogeekgeogeek
New Contributor III
Hello NobbirAhmed ,

i've updated the code, to pop up the messages , but when we launch this code the msgbox contains nothing , it seems empty with no messages from the tool.

below the updated code:

            IGeoProcessor2 gp = new GeoProcessorClass();

            object obj = 2; 
            try
                {
           gp.AddToolbox(@"c:\program files\arcgis\desktop10.1\ArcToolbox\Toolboxes\Analysis Tools.tbx"); 
           parameters.Add("gone_Buffer1");
           parameters.Add("gone");
           parameters.Add(@"C:\Documents and Settings\Administrateur\Mes documents\ArcGIS\Default.gdb\gone_Erase");
           gp.Execute("Erase", parameters, null);
           MessageBox.Show( gp.GetMessages(ref obj)); //// this line is not executed

   }
               catch (Exception ex)
               {

                   MessageBox.Show("in Except " + gp.GetMessages(ref obj)); /// this is the executed msgbox but it's empty ""
                 //  label1.Text = label1.Text + gp.GetMessages(obj);
                   label1.Text = label1.Text + ex.StackTrace;
               }
0 Kudos
NobbirAhmed
Esri Regular Contributor
No message? Could you please try to Execute as follows:

gp.Execute("Erase_analysis", parameters, null);


Let me know if you get any message now.

If your code is not confidential - could you send me your project as a zip file to nahmed@esri.com?
0 Kudos
geogeekgeogeek
New Contributor III
i've made the update to "Erase_analysis" , then i got an error message, "provide you licence server administration with the folowwing information error code = -42 .. ".

i have a floating licence  Arcinfo advanced for 10.1 .
0 Kudos
NobbirAhmed
Esri Regular Contributor
Talk to your system admin people. There may be some issues with license manager.

One more thing, just try to run the same tool with same parameter values from ArcMap. Do the tool run successfully?
0 Kudos
geogeekgeogeek
New Contributor III
Hello mr Nobbir,

the tool run perfectly from arcmap 10.1 , and i can run arcpy equivalent code , i tried to add the axMapControl to the form and luckily the geoprocessing  works 😄 , but the there's another weird "error".
the new error is as below:

*when the name of the result of geoprocessing exists already , it pop ups an error message saying that and the form appears normally.

*when the name of the output doesn't exists or if "gp.OverwriteOutput = true; " i activated the overwrite, the geoprocessing is  executed , because i check the result in the geodatabase , but the form doesn't appear and then the program stops.

i have sent the last project code in the mail that you gave , and i will send this update , Thanks a lot Mr Nobbir.
0 Kudos
NobbirAhmed
Esri Regular Contributor
Glad to know the geoprocessing part is working 🙂 I will check out your project on Monday.
0 Kudos