| | | 1 | | using System; |
| | | 2 | | using System.IO; |
| | | 3 | | |
| | | 4 | | namespace SolidEdgeCommunity; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Helper class for interaction with Solid Edge. |
| | | 8 | | /// </summary> |
| | | 9 | | public static class SolidEdgeUtils |
| | | 10 | | { |
| | | 11 | | //[DllImport("ole32.dll")] |
| | | 12 | | //static extern int CreateBindCtx(uint reserved, out IBindCtx ppbc); |
| | | 13 | | |
| | | 14 | | //[DllImport("ole32.dll")] |
| | | 15 | | //static extern void GetRunningObjectTable(int reserved, out IRunningObjectTable prot); |
| | | 16 | | |
| | | 17 | | //const int MK_E_UNAVAILABLE = (int)(0x800401E3 - 0x100000000); |
| | | 18 | | private const int _mk_E_UNAVAILABLE = unchecked((int)0x800401E3); |
| | | 19 | | |
| | | 20 | | /// <summary> |
| | | 21 | | /// Connects to a running instance of Solid Edge. |
| | | 22 | | /// </summary> |
| | | 23 | | /// <returns> |
| | | 24 | | /// An object of type SolidEdgeFramework.Application. |
| | | 25 | | /// </returns> |
| | 1 | 26 | | public static Application Connect() => Connect(startIfNotRunning: false); |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// Connects to or starts a new instance of Solid Edge. |
| | | 30 | | /// </summary> |
| | | 31 | | /// <param name="startIfNotRunning"></param> |
| | | 32 | | /// <returns> |
| | | 33 | | /// An object of type SolidEdgeFramework.Application. |
| | | 34 | | /// </returns> |
| | | 35 | | public static Application Connect(bool startIfNotRunning) |
| | | 36 | | { |
| | | 37 | | try |
| | | 38 | | { |
| | | 39 | | // Attempt to connect to a running instance of Solid Edge. |
| | 2 | 40 | | return (Application)Marshal.GetActiveObject(SolidEdgeSDK.PROGID.SolidEdge_Application); |
| | | 41 | | } |
| | | 42 | | catch (System.Runtime.InteropServices.COMException ex) |
| | | 43 | | { |
| | 2 | 44 | | switch (ex.ErrorCode) |
| | | 45 | | { |
| | | 46 | | // Solid Edge is not running. |
| | | 47 | | case _mk_E_UNAVAILABLE: |
| | 0 | 48 | | if (startIfNotRunning) |
| | | 49 | | { |
| | | 50 | | // Start Solid Edge. |
| | 0 | 51 | | return Start(); |
| | | 52 | | } |
| | | 53 | | else |
| | | 54 | | { |
| | | 55 | | // Rethrow exception. |
| | 0 | 56 | | throw; |
| | | 57 | | } |
| | | 58 | | default: |
| | | 59 | | // Rethrow exception. |
| | 2 | 60 | | throw; |
| | | 61 | | } |
| | | 62 | | } |
| | 0 | 63 | | } |
| | | 64 | | |
| | | 65 | | /// <summary> |
| | | 66 | | /// Connects to or starts a new instance of Solid Edge. |
| | | 67 | | /// </summary> |
| | | 68 | | /// <param name="startIfNotRunning"></param> |
| | | 69 | | /// <param name="ensureVisible"></param> |
| | | 70 | | /// <returns> |
| | | 71 | | /// An object of type SolidEdgeFramework.Application. |
| | | 72 | | /// </returns> |
| | | 73 | | public static Application Connect(bool startIfNotRunning, bool ensureVisible) |
| | | 74 | | { |
| | 1 | 75 | | Application application = null; |
| | | 76 | | |
| | | 77 | | try |
| | | 78 | | { |
| | | 79 | | // Attempt to connect to a running instance of Solid Edge. |
| | 1 | 80 | | application = (Application)Marshal.GetActiveObject(SolidEdgeSDK.PROGID.SolidEdge_Application); |
| | 0 | 81 | | } |
| | | 82 | | catch (System.Runtime.InteropServices.COMException ex) |
| | | 83 | | { |
| | 1 | 84 | | switch (ex.ErrorCode) |
| | | 85 | | { |
| | | 86 | | // Solid Edge is not running. |
| | | 87 | | case _mk_E_UNAVAILABLE: |
| | 0 | 88 | | if (startIfNotRunning) |
| | | 89 | | { |
| | | 90 | | // Start Solid Edge. |
| | 0 | 91 | | application = Start(); |
| | 0 | 92 | | break; |
| | | 93 | | } |
| | | 94 | | else |
| | | 95 | | { |
| | | 96 | | // Rethrow exception. |
| | 0 | 97 | | throw; |
| | | 98 | | } |
| | | 99 | | default: |
| | | 100 | | // Rethrow exception. |
| | 1 | 101 | | throw; |
| | | 102 | | } |
| | 0 | 103 | | } |
| | | 104 | | |
| | 0 | 105 | | if ((application != null) && ensureVisible) |
| | | 106 | | { |
| | 0 | 107 | | application.Visible = true; |
| | | 108 | | } |
| | | 109 | | |
| | 0 | 110 | | return application; |
| | | 111 | | } |
| | | 112 | | |
| | | 113 | | /// <summary> |
| | | 114 | | /// Returns the path to the Solid Edge installation folder. |
| | | 115 | | /// </summary> |
| | | 116 | | /// <remarks> |
| | | 117 | | /// Typically 'C:\Program Files\Solid Edge XXX'. |
| | | 118 | | /// </remarks> |
| | | 119 | | public static string GetInstalledPath() |
| | | 120 | | { |
| | | 121 | | /* Get path to Solid Edge program directory. */ |
| | 2 | 122 | | var programDirectory = new DirectoryInfo(GetProgramFolderPath()); |
| | | 123 | | |
| | | 124 | | /* Get path to Solid Edge installation directory. */ |
| | 0 | 125 | | var installationDirectory = programDirectory.Parent; |
| | | 126 | | |
| | 0 | 127 | | return installationDirectory.FullName; |
| | | 128 | | } |
| | | 129 | | |
| | | 130 | | public static System.Globalization.CultureInfo GetInstalledLanguage() |
| | | 131 | | { |
| | 1 | 132 | | var installData = new SEInstallDataLib.SEInstallData(); |
| | | 133 | | |
| | | 134 | | try |
| | | 135 | | { |
| | 0 | 136 | | return System.Globalization.CultureInfo.GetCultureInfo(installData.GetLanguageID()); |
| | | 137 | | } |
| | | 138 | | finally |
| | | 139 | | { |
| | 0 | 140 | | if (installData != null) |
| | | 141 | | { |
| | 0 | 142 | | System.Runtime.InteropServices.Marshal.ReleaseComObject(installData); |
| | | 143 | | } |
| | 0 | 144 | | } |
| | 0 | 145 | | } |
| | | 146 | | |
| | | 147 | | /// <summary> |
| | | 148 | | /// Returns the path to the Solid Edge program folder. |
| | | 149 | | /// </summary> |
| | | 150 | | /// <remarks> |
| | | 151 | | /// Typically 'C:\Program Files\Solid Edge XXX\Program'. |
| | | 152 | | /// </remarks> |
| | | 153 | | public static string GetProgramFolderPath() |
| | | 154 | | { |
| | 3 | 155 | | var installData = new SEInstallDataLib.SEInstallData(); |
| | | 156 | | |
| | | 157 | | try |
| | | 158 | | { |
| | | 159 | | /* Get path to Solid Edge program directory. */ |
| | 0 | 160 | | return installData.GetInstalledPath(); |
| | | 161 | | } |
| | | 162 | | finally |
| | | 163 | | { |
| | 0 | 164 | | if (installData != null) |
| | | 165 | | { |
| | 0 | 166 | | System.Runtime.InteropServices.Marshal.ReleaseComObject(installData); |
| | | 167 | | } |
| | 0 | 168 | | } |
| | 0 | 169 | | } |
| | | 170 | | |
| | | 171 | | /// <summary> |
| | | 172 | | /// Returns the path to the Solid Edge training folder. |
| | | 173 | | /// </summary> |
| | | 174 | | /// <remarks> |
| | | 175 | | /// Typically 'C:\Program Files\Solid Edge XXX\Training'. |
| | | 176 | | /// </remarks> |
| | 1 | 177 | | public static string GetTrainingFolderPath() => new DirectoryInfo(Path.Combine(GetInstalledPath(), "Training")).Full |
| | | 178 | | |
| | | 179 | | /// <summary> |
| | | 180 | | /// Returns a Version object representing the installed version of Solid Edge. |
| | | 181 | | /// </summary> |
| | | 182 | | /// <returns></returns> |
| | | 183 | | public static Version GetVersion() |
| | | 184 | | { |
| | 1 | 185 | | var installData = new SEInstallDataLib.SEInstallData(); |
| | | 186 | | |
| | 0 | 187 | | return new Version(installData.GetMajorVersion(), installData.GetMinorVersion(), installData.GetServicePackVersi |
| | | 188 | | } |
| | | 189 | | |
| | | 190 | | /// <summary> |
| | | 191 | | /// Creates and returns a new instance of Solid Edge. |
| | | 192 | | /// </summary> |
| | | 193 | | /// <returns> |
| | | 194 | | /// An object of type SolidEdgeFramework.Application. |
| | | 195 | | /// </returns> |
| | | 196 | | public static Application Start() |
| | | 197 | | { |
| | | 198 | | // On a system where Solid Edge is installed, the COM ProgID will be |
| | | 199 | | // defined in registry: HKEY_CLASSES_ROOT\SolidEdge.Application |
| | 1 | 200 | | Type t = Type.GetTypeFromProgID(progID: SolidEdgeSDK.PROGID.SolidEdge_Application, throwOnError: true); |
| | | 201 | | |
| | | 202 | | // Using the discovered Type, create and return a new instance of Solid Edge. |
| | 0 | 203 | | return (Application)Activator.CreateInstance(type: t); |
| | | 204 | | } |
| | | 205 | | } |