Can the Arc Hydro Tools be called in C# code using Arc Objects?

871
2
07-09-2012 04:33 AM
ReneeCammarere
Occasional Contributor
I've been investigating using Arc Hydro Tools for research that I've been doing involving ponding (sinks) areas.  I was just wondering if Arc Hydro Tools can be called in C# code using Arc Objects.
Thanks,
Renee
0 Kudos
2 Replies
FengZhang2
Occasional Contributor
You have a few options to call AcHydro tools in .NET.

For example,

1. Using GPDispatch

        private static void ArcHydroTool_UsingGPDispatch()
        {
            try
            {
                System.Type pSysType = System.Type.GetTypeFromProgID("esriGeoprocessing.GpDispatch.1");
                System.Object objDispatch = Activator.CreateInstance(pSysType);

                // Set the toolbox
                pSysType.InvokeMember(
                    "Toolbox",
                    BindingFlags.SetProperty,
                    null,
                    objDispatch,
                    new object[] { @"C:\Program Files (x86)\ArcGIS\Desktop10.0\ArcToolbox\Toolboxes\Arc Hydro Tools.tbx" }
                );

                // Execute the tool
                pSysType.InvokeMember(
                    "DEMReconditioning",
                    BindingFlags.InvokeMethod,
                    null,
                    objDispatch,
                    new object[] {
                    @"cedar_dem",
                    @"stream",
                    @"5",
                    @"10",
                    @"1000",
                    @"C:\fzhang\INCIDENTS\INCIDENT_972964\Layers\AgreeDEM16",
                    "NEGATIVE_NO"
                }
                );
            }
            catch (Exception err)
            {
                //output debug information
                System.Diagnostics.Debug.WriteLine(err.Message);
                System.Diagnostics.Debug.WriteLine(err.StackTrace);
                throw;
            }
        }


2. Use GPTool

       
        private static void ArcHydroTool_UsingGPTool()
        {
            try
            {
                IArcToolboxExtension arcToolboxExt;
                IArcToolbox arcToolBox;
                IGPTool arcTool;
                UID gpUID = new UID();
                gpUID.Value = "esriGeoprocessingUI.ArcToolboxExtension";
                string gpToolName = "DEMReconditioning";

                arcToolboxExt = (IArcToolboxExtension) ArcMap.Application.FindExtensionByCLSID(gpUID);
                arcToolBox = arcToolboxExt.ArcToolbox;
                arcTool = arcToolBox.GetToolbyNameString(gpToolName);

                IArray parameters = arcTool.ParameterInfo;
                // Iterate through parameters to get data types                
                for (int j = 0; j < parameters.Count; j++)
                {
                    IGPParameter gparam = (IGPParameter)parameters.get_Element(j);

                    Debug.WriteLine("Parameter name: " + gparam.Name);
                    Debug.WriteLine("Parameter Data Type Display Name: " + gparam.DataType.DisplayName);
                    Debug.WriteLine("Parameter Data Type Name: " + gparam.DataType.Name);
                    Debug.WriteLine("Is Parameter required?: " + gparam.ParameterType.ToString());
                                    //    Debug.WriteLine("Parameter Value Data Type: " + gparam.Value.DataType.Name);
                    Debug.WriteLine("Parameter Value as Text: " + gparam.Value.GetAsText());
                }
                arcTool.Execute(parameters, null, null, null);
            }
            catch (Exception err)
            {
                //output debug information
                System.Diagnostics.Debug.WriteLine(err.Message);
                System.Diagnostics.Debug.WriteLine(err.StackTrace);
                throw;
            }
        }
0 Kudos
ReneeCammarere
Occasional Contributor
Thank you Feng.  I plan to try one of these out and let you know how it goes.  Have a great day!
0 Kudos