ArcGIS secondary development practices-Traverse tools in ToolBox !, Arcgistoolbox
In AO, the basic process for opening file A is:
1. Create A WorkspaceFactory corresponding to "file;
2. Use WorkspaceFactory to create the Workspace of "file A". The Workspace can be the folder where the file is located (for example, the Workspace of the ToolBox to be created later in this article ), it can also be a spatial database (if you store files in the database or want to open the data in the database ).
3. Open "file A" In Workspace to obtain the corresponding type of "file A" object;
------------------------------------------------ Now, start (here, my Toolbox path is F: \ Root \ Tools \ MagmaDistribution. tbx ).
Every Toolbox of ArcGIS is A. tbx file in windows.
You can see through the object model graph of AO, in.. Net uses the GPToolbox object to represent the Toolbox. Therefore, we must first open the prepared. and create a GPToolbox object to manage it.
1. Create a ToolboxWorkspaceFactory object.
2. ToolboxWorkspaceFactory is used to create a ToolboxWorkspace for hitting the. tbx file.
3. Use ToolboxWorkspaceFactory to create a GPToolbox object through the path of the. tbx file.
In this way, an object representing ToolBox is created. The Code is as follows:
1 ToolboxWorkspaceFactory toolBoxWSF = new ToolboxWorkspaceFactoryClass();2 IToolboxWorkspace toolBoxWS = (IToolboxWorkspace)toolBoxWSF.OpenFromFile("F:\\Root\\Tools", 0);3 IGPToolbox toolbox = toolBoxWS.OpenToolbox("MagmaDistribution.tbx");
Three tools are put in my MagmaDistribution. tbx. Here we get the names of the four tools and output them to the console:
1 // output the toolbox path information 2 Console. writeLine ("PathName of IGPToolBox:" + toolbox. pathName); 3 // obtain the names of the three tools in the toolbox from the ToolNames attribute of the IGPToolbox. 4 ESRI. ArcGIS. Geoprocessing. IEnumGPToolName toolName = toolbox. ToolNames; 5 IGPToolName gpn = toolName. Next (); 6 while (gpn! = Null) 7 {// name of the cyclic output tool 8 Console. WriteLine (gpn. DisplayName); 9 gpn = toolName. Next (); 10}
Running effect:
Complete code:
1 using System; 2 using System. collections. generic; 3 using System. text; 4 using ESRI. arcGIS. esriSystem; 5 using ESRI. arcGIS. geodatabase; 6 using ESRI. arcGIS. dataSourcesGDB; 7 using ESRI. arcGIS. geoprocessing; 8 9 namespace esriTestConsole10 {11 class Program12 {13 private static LicenseInitializer m_AOLicenseInitializer = new esriTestConsole. licenseInitializer (); 14 15 [STAThread ()] 16 static void Ma In (string [] args) 17 {18 // ESRI License Initializer generated code. // This is the 19 m_AOLicenseInitializer.InitializeApplication (new esriLicenseProductCode [] {esriLicenseProductCode. esriLicenseProductCodeEngine, esriLicenseProductCode. esriLicenseProductCodeEngineGeoDB, esriLicenseProductCode. esriLicenseProductCodeBasic, esriLicenseProductCode. esriLicenseProductCodeStandard, esriLicensePro DuctCode. esriLicenseProductCodeAdvanced}, 20 new esriLicenseExtensionCode [] {esriLicenseExtensionCode. esriLicenseExtensionCodeNetwork, esriLicenseExtensionCode. token}); 21 // ESRI License Initializer generated code.22 try23 {24 ToolboxWorkspaceFactory toolBoxWSF = new ToolboxWorkspaceFactoryClass (); 25 IToolboxWorkspace toolBoxWS = (IToolboxWorkspace) toolBoxWSF. open FromFile ("F :\\ Root \ Tools", 0); 26 IGPToolbox toolbox = toolBoxWS. openToolbox ("MagmaDistribution. tbx "); 27 // output toolbox path information 28 Console. writeLine ("PathName of IGPToolBox:" + toolbox. pathName); 29 // obtain the names of the four tools in the toolbox from the ToolNames attribute of the IGPToolbox. 30 ESRI. ArcGIS. Geoprocessing. IEnumGPToolName toolName = toolbox. ToolNames; 31 IGPToolName gpn = toolName. Next (); 32 while (gpn! = Null) 33 {// name of the cyclic output tool 34 Console. WriteLine (gpn. DisplayName); 35 gpn = toolName. Next (); 36}
3738} 39 catch (Exception ex) 40 {41 Console. writeLine (ex. message); 42} 43 // Do not make any call to ArcObjects after ShutDownApplication () 44 finally45 {46 Console. writeLine ("enter any character to exit the program:"); 47 Console. read (); 48 m_AOLicenseInitializer.ShutdownApplication (); 49} 50} 51} 52}