| 1 | // RegisterToolboxWithArcCatalog - Adds or removes a specified ArcGIS toolbox
|
|---|
| 2 | // (.tbx file) to or from the user's list maintained by ArcCatalog.
|
|---|
| 3 | //
|
|---|
| 4 | // Copyright (C) 2007 Jason J. Roberts
|
|---|
| 5 | //
|
|---|
| 6 | // This program is free software; you can redistribute it and/or
|
|---|
| 7 | // modify it under the terms of the GNU General Public License
|
|---|
| 8 | // as published by the Free Software Foundation; either version 2
|
|---|
| 9 | // of the License, or (at your option) any later version.
|
|---|
| 10 | //
|
|---|
| 11 | // This program is distributed in the hope that it will be useful,
|
|---|
| 12 | // but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 14 | // GNU General Public License (available in the file LICENSE.TXT)
|
|---|
| 15 | // for more details.
|
|---|
| 16 | //
|
|---|
| 17 | // You should have received a copy of the GNU General Public License
|
|---|
| 18 | // along with this program; if not, write to the Free Software
|
|---|
| 19 | // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 | using System;
|
|---|
| 23 | using System.Diagnostics;
|
|---|
| 24 | using System.IO;
|
|---|
| 25 | using System.Runtime.InteropServices;
|
|---|
| 26 | using System.Threading;
|
|---|
| 27 | using System.Text;
|
|---|
| 28 | using Microsoft.Win32;
|
|---|
| 29 | using ESRI.ArcGIS.esriSystem;
|
|---|
| 30 | using ESRI.ArcGIS.Framework;
|
|---|
| 31 | using ESRI.ArcGIS.Geodatabase;
|
|---|
| 32 | using ESRI.ArcGIS.Geoprocessing;
|
|---|
| 33 | using ESRI.ArcGIS.GeoprocessingUI;
|
|---|
| 34 |
|
|---|
| 35 | namespace RegisterToolboxWithArcCatalog
|
|---|
| 36 | {
|
|---|
| 37 | class Program
|
|---|
| 38 | {
|
|---|
| 39 | static string Operation = "";
|
|---|
| 40 | static IGPToolbox Toolbox = null;
|
|---|
| 41 | static int ReturnCode = 2;
|
|---|
| 42 |
|
|---|
| 43 | static int Main(string[] args)
|
|---|
| 44 | {
|
|---|
| 45 | // Check the command line arguments.
|
|---|
| 46 |
|
|---|
| 47 | if (args.Length != 2)
|
|---|
| 48 | {
|
|---|
| 49 | Console.Error.WriteLine("ERROR: Invalid command line arguments.");
|
|---|
| 50 | Console.WriteLine("");
|
|---|
| 51 | Console.WriteLine("USAGE: RegisterToolboxWithArcCatalog {register|unregister} toolbox");
|
|---|
| 52 | Console.WriteLine("");
|
|---|
| 53 | Console.WriteLine(" toolbox - path to an ArcGIS toolbox (.tbx) file");
|
|---|
| 54 | return 1;
|
|---|
| 55 | }
|
|---|
| 56 |
|
|---|
| 57 | Program.Operation = args[0].ToLower();
|
|---|
| 58 | string toolboxPath = args[1];
|
|---|
| 59 |
|
|---|
| 60 | if (Program.Operation != "register" && Program.Operation != "unregister")
|
|---|
| 61 | {
|
|---|
| 62 | Console.Error.WriteLine("ERROR: Invalid command line arguments. The first parameter must be register or unregister.");
|
|---|
| 63 | return 1;
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | // Get the ArcGIS installation directory from the registry.
|
|---|
| 67 |
|
|---|
| 68 | RegistryKey key = null;
|
|---|
| 69 |
|
|---|
| 70 | try
|
|---|
| 71 | {
|
|---|
| 72 | key = Registry.LocalMachine.OpenSubKey(@"Software\ESRI\ArcInfo\Desktop\8.0");
|
|---|
| 73 | }
|
|---|
| 74 | catch (Exception e)
|
|---|
| 75 | {
|
|---|
| 76 | Console.Error.WriteLine("ERROR: Failed to open the registry key \"HKEY_LOCAL_MACHINE\\Software\\ESRI\\ArcInfo\\Desktop\\8.0\". ArcGIS may not be installed on this machine. Registry.LocalMachine.OpenSubKey(@\"Software\\ESRI\\ArcInfo\\Desktop\\8.0\") raised an exception: {0}", e.Message);
|
|---|
| 77 | return 1;
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 | string arcGisInstallDir = "";
|
|---|
| 81 |
|
|---|
| 82 | try
|
|---|
| 83 | {
|
|---|
| 84 | arcGisInstallDir = (string)key.GetValue("InstallDir");
|
|---|
| 85 | }
|
|---|
| 86 | catch (Exception e)
|
|---|
| 87 | {
|
|---|
| 88 | Console.Error.WriteLine("ERROR: Failed to read the \"InstallDir\" value from the registry key \"HKEY_LOCAL_MACHINE\\Software\\ESRI\\ArcInfo\\Desktop\\8.0\". AArcGIS may not be installed on this machine. RegistryKey.GetValue(\"InstallDir\") raised an exception: {0}", e.Message);
|
|---|
| 89 | return 1;
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 | // Open the toolbox.
|
|---|
| 93 |
|
|---|
| 94 | IWorkspaceFactory factory = new ToolboxWorkspaceFactoryClass();
|
|---|
| 95 | IToolboxWorkspace workspace = null;
|
|---|
| 96 |
|
|---|
| 97 | try
|
|---|
| 98 | {
|
|---|
| 99 | workspace = (IToolboxWorkspace)factory.OpenFromFile(Path.GetDirectoryName(toolboxPath), 0);
|
|---|
| 100 | }
|
|---|
| 101 | catch (Exception e)
|
|---|
| 102 | {
|
|---|
| 103 | Console.Error.WriteLine("ERROR: Failed to open the ArcGIS workspace \"{0}\". ToolboxWorkspaceFactory.OpenFromFile(\"{0}\", 0) raised an exception: {1}", Path.GetDirectoryName(toolboxPath), e.Message);
|
|---|
| 104 | return 1;
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| 107 | string fileName = Path.GetFileName(toolboxPath);
|
|---|
| 108 | if (Path.GetExtension(fileName).ToLower() == ".tbx")
|
|---|
| 109 | fileName = Path.GetFileNameWithoutExtension(fileName);
|
|---|
| 110 |
|
|---|
| 111 | try
|
|---|
| 112 | {
|
|---|
| 113 | Program.Toolbox = workspace.OpenToolbox(fileName);
|
|---|
| 114 | }
|
|---|
| 115 | catch (Exception e)
|
|---|
| 116 | {
|
|---|
| 117 | Console.Error.WriteLine("ERROR: Failed to open the ArcGIS toolbox \"{0}\" in workspace \"{1}\". IToolboxWorkspace.OpenToolbox(\"{0}\") raised an exception: {2}", fileName, Path.GetDirectoryName(toolboxPath), e.Message);
|
|---|
| 118 | return 1;
|
|---|
| 119 | }
|
|---|
| 120 |
|
|---|
| 121 | Console.WriteLine("Opened ArcGIS toolbox \"{0}\".", toolboxPath);
|
|---|
| 122 |
|
|---|
| 123 | // Set up a handler for the IAppROTEvents.AppAdded event. This event
|
|---|
| 124 | // will be fired when we start ArcCatalog.exe and allows us to
|
|---|
| 125 | // obtain an IApplication interface, which we need to add the
|
|---|
| 126 | // toolbox to the list maintained by ArcCatalog.
|
|---|
| 127 |
|
|---|
| 128 | AppROTClass appROT = new AppROTClass();
|
|---|
| 129 | appROT.AppAdded += new IAppROTEvents_AppAddedEventHandler(appROT_AppAdded);
|
|---|
| 130 |
|
|---|
| 131 | // Start ArcCatalog.exe.
|
|---|
| 132 | //
|
|---|
| 133 | // I do not know how to prevent it from displaying the splash
|
|---|
| 134 | // screen. Setting the Splash value in the registry does not work,
|
|---|
| 135 | // like it does for ArcMap. Setting
|
|---|
| 136 | //
|
|---|
| 137 | // startInfo.WindowStyle = ProcessWindowStyle.Hidden;
|
|---|
| 138 | //
|
|---|
| 139 | // does not work either. Adding the toolbox to ArcMap does not work
|
|---|
| 140 | // because then it ends up in an ArcMap document, not in the default
|
|---|
| 141 | // list of toolboxes.
|
|---|
| 142 | //
|
|---|
| 143 | // It may be possible to enumerate the top-level windows after the
|
|---|
| 144 | // process has popped it up and then make it invisible. But this is
|
|---|
| 145 | // a bunch of complex Win32 code I'm not going to get into now.
|
|---|
| 146 |
|
|---|
| 147 | ProcessStartInfo startInfo = new ProcessStartInfo(Path.Combine(arcGisInstallDir, "Bin\\ArcCatalog.exe"));
|
|---|
| 148 | startInfo.WindowStyle = ProcessWindowStyle.Hidden;
|
|---|
| 149 | Process p = null;
|
|---|
| 150 |
|
|---|
| 151 | Console.WriteLine("Starting \"{0}\"...", startInfo.FileName);
|
|---|
| 152 | try
|
|---|
| 153 | {
|
|---|
| 154 | p = Process.Start(startInfo);
|
|---|
| 155 | }
|
|---|
| 156 | catch (Exception e)
|
|---|
| 157 | {
|
|---|
| 158 | Console.Error.WriteLine("ERROR: Failed start the program \"{0}\". Process.Start(\"{0}\") raised an exception: {1}", Path.Combine(arcGisInstallDir, "Bin\\ArcCatalog.exe"), e.Message);
|
|---|
| 159 | return 1;
|
|---|
| 160 | }
|
|---|
| 161 |
|
|---|
| 162 | // Wait for ArcCatalog.exe to exit.
|
|---|
| 163 |
|
|---|
| 164 | p.WaitForExit();
|
|---|
| 165 | if (Program.ReturnCode == 2)
|
|---|
| 166 | Console.Error.WriteLine("ERROR: ArcCatalog exited before we had the opportunity to {0} the toolbox.", Program.Operation);
|
|---|
| 167 | else
|
|---|
| 168 | Console.WriteLine("ArcCatalog has exited.");
|
|---|
| 169 |
|
|---|
| 170 | // Exit.
|
|---|
| 171 |
|
|---|
| 172 | return Program.ReturnCode;
|
|---|
| 173 | }
|
|---|
| 174 |
|
|---|
| 175 | static void appROT_AppAdded(AppRef pApp)
|
|---|
| 176 | {
|
|---|
| 177 | // Make the ArcCatalog window invisible. In my testing, this has no
|
|---|
| 178 | // effect, because we are invoked here before the window is visible.
|
|---|
| 179 | // When we call pApp.Shutdown() below, it causes ArcCatalog to exit
|
|---|
| 180 | // without ever showing the window. As far as I can tell, I could
|
|---|
| 181 | // leave the Visible property alone and everything would still work
|
|---|
| 182 | // fine.
|
|---|
| 183 | //
|
|---|
| 184 | // Note that this property has no apparent relationship to the
|
|---|
| 185 | // ArcCatalog splash screen. That screen comes up before we are
|
|---|
| 186 | // invoked here.
|
|---|
| 187 |
|
|---|
| 188 | pApp.Visible = false;
|
|---|
| 189 |
|
|---|
| 190 | // Obtain the ArcToolboxExtension object, which manages the list of
|
|---|
| 191 | // toolboxes present in ArcGIS applications.
|
|---|
| 192 |
|
|---|
| 193 | UIDClass uid = new UIDClass();
|
|---|
| 194 | uid.Value = "esriGeoprocessingUI.ArcToolboxExtension";
|
|---|
| 195 | IArcToolboxExtension ext = (IArcToolboxExtension)pApp.FindExtensionByCLSID(uid);
|
|---|
| 196 |
|
|---|
| 197 | // Add or remove the toolbox.
|
|---|
| 198 |
|
|---|
| 199 | if (Program.Operation == "register")
|
|---|
| 200 | {
|
|---|
| 201 | try
|
|---|
| 202 | {
|
|---|
| 203 | ext.ArcToolbox.AddToolbox(Program.Toolbox);
|
|---|
| 204 | Console.WriteLine("Registered the toolbox with ArcCatalog.");
|
|---|
| 205 | Program.ReturnCode = 0;
|
|---|
| 206 | }
|
|---|
| 207 | catch (Exception e)
|
|---|
| 208 | {
|
|---|
| 209 | Console.Error.WriteLine("ERROR: Failed to add ArcGIS toolbox \"{0}\" to the IArcToolboxExtension.ArcToolbox object. IArcToolboxExtension.ArcToolbox.AddToolbox(<IGPToolbox object>) raised an exception: {1}", Program.Toolbox.PathName, e.Message);
|
|---|
| 210 | Program.ReturnCode = 1;
|
|---|
| 211 | }
|
|---|
| 212 | }
|
|---|
| 213 | else
|
|---|
| 214 | {
|
|---|
| 215 | // Unfortunately we can't just call ext.ArcToolbox.RemoveToolbox
|
|---|
| 216 | // and pass in Program.Toolbox. It does not seem to work. So we
|
|---|
| 217 | // have to get an appropriate IGPToolbox reference through this
|
|---|
| 218 | // circuitous logic.
|
|---|
| 219 |
|
|---|
| 220 | IGPTool tool1 = null;
|
|---|
| 221 |
|
|---|
| 222 | try
|
|---|
| 223 | {
|
|---|
| 224 | tool1 = Program.Toolbox.Tools.Next();
|
|---|
| 225 | }
|
|---|
| 226 | catch
|
|---|
| 227 | {
|
|---|
| 228 | Console.Error.WriteLine("ERROR: This toolbox cannot be unregistered from ArcCatalog because it does not have any tools inside it. Because ESRI's IArcToolbox interface does not have an API for enumerating the currently-registered toolboxes, there is no way we can check for a given toolbox unless we know the name of a tool within it. If you want to be able to unregister your toolbox using this program you must add at least one tool to it.");
|
|---|
| 229 | Program.ReturnCode = 1;
|
|---|
| 230 | }
|
|---|
| 231 |
|
|---|
| 232 | if (tool1 != null)
|
|---|
| 233 | {
|
|---|
| 234 | IGPTool tool2 = null;
|
|---|
| 235 |
|
|---|
| 236 | try
|
|---|
| 237 | {
|
|---|
| 238 | if (Program.Toolbox.Alias is string && Program.Toolbox.Alias.Length > 0)
|
|---|
| 239 | tool2 = ext.ArcToolbox.GetToolbyNameString(tool1.Name + "_" + Program.Toolbox.Alias);
|
|---|
| 240 | else
|
|---|
| 241 | tool2 = ext.ArcToolbox.GetToolbyNameString(tool1.Name);
|
|---|
| 242 | }
|
|---|
| 243 | catch
|
|---|
| 244 | {
|
|---|
| 245 | // Do nothing. GetToolbyNameString throws when the tool
|
|---|
| 246 | // does not exist. This happens when the user tries to
|
|---|
| 247 | // unregister a toolbox that is not registered.
|
|---|
| 248 |
|
|---|
| 249 | Console.WriteLine("The toolbox is not currently registered with ArcCatalog, so there is no need to unregister it.");
|
|---|
| 250 | Program.ReturnCode = 0;
|
|---|
| 251 | }
|
|---|
| 252 |
|
|---|
| 253 | if (tool2 != null && tool1.PathName == tool2.PathName)
|
|---|
| 254 | try
|
|---|
| 255 | {
|
|---|
| 256 | ext.ArcToolbox.RemoveToolbox(tool2.Toolbox);
|
|---|
| 257 | Console.WriteLine("Unregistered the toolbox from ArcCatalog.");
|
|---|
| 258 | Program.ReturnCode = 0;
|
|---|
| 259 | }
|
|---|
| 260 | catch (Exception e)
|
|---|
| 261 | {
|
|---|
| 262 | Console.Error.WriteLine("ERROR: Failed to remove ArcGIS toolbox \"{0}\" from the IArcToolboxExtension.ArcToolbox object. IArcToolboxExtension.ArcToolbox.RemoveToolbox(<IGPToolbox object>) raised an exception: {1}", Program.Toolbox.PathName, e.Message);
|
|---|
| 263 | Program.ReturnCode = 1;
|
|---|
| 264 | }
|
|---|
| 265 | }
|
|---|
| 266 | }
|
|---|
| 267 |
|
|---|
| 268 | // Signal the application to exit.
|
|---|
| 269 |
|
|---|
| 270 | try
|
|---|
| 271 | {
|
|---|
| 272 | pApp.Shutdown();
|
|---|
| 273 | }
|
|---|
| 274 | catch (Exception e)
|
|---|
| 275 | {
|
|---|
| 276 | Console.Error.WriteLine("ERROR: Failed to shut down ArcCatalog. IApplication.Shutdown() raised an exception: {0}", e.Message);
|
|---|
| 277 | Environment.Exit(1);
|
|---|
| 278 | }
|
|---|
| 279 |
|
|---|
| 280 | Console.WriteLine("Waiting for ArcCatalog to exit...");
|
|---|
| 281 | }
|
|---|
| 282 | }
|
|---|
| 283 | }
|
|---|