< Summary

Information
Class: SolidEdgeCommunity.SolidEdgeUtils
Assembly: SolidEdgeCommunity
File(s): D:\a\SolidEdge.Community\SolidEdge.Community\src\SolidEdgeCommunity\SolidEdgeUtils.cs
Line coverage
34%
Covered lines: 14
Uncovered lines: 27
Coverable lines: 41
Total lines: 205
Line coverage: 34.1%
Branch coverage
14%
Covered branches: 2
Total branches: 14
Branch coverage: 14.2%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
Connect()100%11100%
Connect(...)25%7442.85%
Connect(...)16.66%18630.76%
GetInstalledPath()100%1133.33%
GetInstalledLanguage()0%4216.66%
GetProgramFolderPath()0%4216.66%
GetTrainingFolderPath()100%11100%
GetVersion()100%1150%
Start()100%1150%

File(s)

D:\a\SolidEdge.Community\SolidEdge.Community\src\SolidEdgeCommunity\SolidEdgeUtils.cs

#LineLine coverage
 1using System;
 2using System.IO;
 3
 4namespace SolidEdgeCommunity;
 5
 6/// <summary>
 7/// Helper class for interaction with Solid Edge.
 8/// </summary>
 9public 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>
 126    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.
 240            return (Application)Marshal.GetActiveObject(SolidEdgeSDK.PROGID.SolidEdge_Application);
 41        }
 42        catch (System.Runtime.InteropServices.COMException ex)
 43        {
 244            switch (ex.ErrorCode)
 45            {
 46                // Solid Edge is not running.
 47                case _mk_E_UNAVAILABLE:
 048                    if (startIfNotRunning)
 49                    {
 50                        // Start Solid Edge.
 051                        return Start();
 52                    }
 53                    else
 54                    {
 55                        // Rethrow exception.
 056                        throw;
 57                    }
 58                default:
 59                    // Rethrow exception.
 260                    throw;
 61            }
 62        }
 063    }
 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    {
 175        Application application = null;
 76
 77        try
 78        {
 79            // Attempt to connect to a running instance of Solid Edge.
 180            application = (Application)Marshal.GetActiveObject(SolidEdgeSDK.PROGID.SolidEdge_Application);
 081        }
 82        catch (System.Runtime.InteropServices.COMException ex)
 83        {
 184            switch (ex.ErrorCode)
 85            {
 86                // Solid Edge is not running.
 87                case _mk_E_UNAVAILABLE:
 088                    if (startIfNotRunning)
 89                    {
 90                        // Start Solid Edge.
 091                        application = Start();
 092                        break;
 93                    }
 94                    else
 95                    {
 96                        // Rethrow exception.
 097                        throw;
 98                    }
 99                default:
 100                    // Rethrow exception.
 1101                    throw;
 102            }
 0103        }
 104
 0105        if ((application != null) && ensureVisible)
 106        {
 0107            application.Visible = true;
 108        }
 109
 0110        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. */
 2122        var programDirectory = new DirectoryInfo(GetProgramFolderPath());
 123
 124        /* Get path to Solid Edge installation directory. */
 0125        var installationDirectory = programDirectory.Parent;
 126
 0127        return installationDirectory.FullName;
 128    }
 129
 130    public static System.Globalization.CultureInfo GetInstalledLanguage()
 131    {
 1132        var installData = new SEInstallDataLib.SEInstallData();
 133
 134        try
 135        {
 0136            return System.Globalization.CultureInfo.GetCultureInfo(installData.GetLanguageID());
 137        }
 138        finally
 139        {
 0140            if (installData != null)
 141            {
 0142                System.Runtime.InteropServices.Marshal.ReleaseComObject(installData);
 143            }
 0144        }
 0145    }
 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    {
 3155        var installData = new SEInstallDataLib.SEInstallData();
 156
 157        try
 158        {
 159            /* Get path to Solid Edge program directory. */
 0160            return installData.GetInstalledPath();
 161        }
 162        finally
 163        {
 0164            if (installData != null)
 165            {
 0166                System.Runtime.InteropServices.Marshal.ReleaseComObject(installData);
 167            }
 0168        }
 0169    }
 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>
 1177    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    {
 1185        var installData = new SEInstallDataLib.SEInstallData();
 186
 0187        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
 1200        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.
 0203        return (Application)Activator.CreateInstance(type: t);
 204    }
 205}