Ejemplo n.º 1
0
        /// <summary>
        /// Adds a plug-in based on it's <see cref="SolidPlugIn"/> implementation
        /// </summary>
        /// <typeparam name="fullPath">The absolute path to the plug-in dll</typeparam>
        public static void AddPlugIn(string fullPath)
        {
            if (UseDetachedAppDomain)
            {
                // Add it to the plug-in integration domain also
                PluginCrossDomain.AddPlugIn(fullPath);
            }
            else
            {
                // Create list if one doesn't exist
                if (!PlugInDetails.ContainsKey(fullPath))
                {
                    PlugInDetails[fullPath] = new List <PlugInDetails>();
                }

                List <PlugInDetails> plugins;

                plugins = GetPlugInDetails(fullPath);

                // Add any found plug-ins
                if (plugins?.Count > 0)
                {
                    PlugInDetails[fullPath].AddRange(plugins);
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Adds a plug-in based on it's <see cref="SolidPlugIn"/> implementation
        /// </summary>
        /// <typeparam name="T">The class that implements the <see cref="SolidPlugIn"/></typeparam>
        /// </param>
        public static void AddPlugIn <T>()
        {
            if (UseDetachedAppDomain)
            {
                PluginCrossDomain.AddPlugIn <T>();
            }
            else
            {
                // Get the full path to the assembly
                var fullPath = typeof(T).Assembly.CodeBase.Replace(@"file:\", "").Replace(@"file:///", "");

                // Create list if one doesn't exist
                if (!PlugInDetails.ContainsKey(fullPath))
                {
                    PlugInDetails[fullPath] = new List <PlugInDetails>();
                }

                // Add it
                PlugInDetails[fullPath].Add(new PlugInDetails
                {
                    FullPath         = fullPath,
                    AssemblyFullName = AssemblyName.GetAssemblyName(fullPath).FullName,
                    TypeFullName     = typeof(T).FullName,
                });
            }
        }
Ejemplo n.º 3
0
 public static void OnCallback(string name)
 {
     // Inform plug-in domain of event
     if (UseDetachedAppDomain)
     {
         PluginCrossDomain.OnCallback(name);
     }
     else
     {
         // Inform listeners
         CallbackFired(name);
     }
 }
Ejemplo n.º 4
0
        /// <summary>
        /// Must be called to setup the PlugInIntegration
        /// </summary>
        /// <param name="addinPath">The path to the add-in that is calling this setup (typically acquired using GetType().Assembly.Location)</param>
        /// <param name="cookie">The cookie Id of the SolidWorks instance</param>
        /// <param name="version">The version of the currently connected SolidWorks instance</param>
        public static void Setup(string addinPath, string version, int cookie)
        {
            if (UseDetachedAppDomain)
            {
                // Log it
                Logger.LogDebugSource($"Detached AppDomain PlugIn Setup...");

                // Make sure we resolve assemblies in this domain, as it seems to use this domain to resolve
                // assemblies not the appDomain when crossing boundaries
                AppDomain.CurrentDomain.AssemblyResolve += PlugInIntegrationMarshal.AppDomain_AssemblyResolve;

                PlugInAppDomain = AppDomain.CreateDomain("SolidDnaPlugInDomain", null, new AppDomainSetup
                {
                    // Use plug-in folder for resolving plug-ins
                    ApplicationBase = addinPath,
                });

                // Make sure we load our own marshal
                AssembliesToResolve.Add(typeof(PlugInIntegrationMarshal).Assembly.FullName);

                // Run code on new app-domain to configure
                PluginCrossDomain = (PlugInIntegrationMarshal)PlugInAppDomain.CreateInstanceAndUnwrap(typeof(PlugInIntegrationMarshal).Assembly.FullName, typeof(PlugInIntegrationMarshal).FullName);

                // Setup
                PluginCrossDomain.SetupAppDomain(addinPath, version, cookie);
            }
            else
            {
                // Log it
                Logger.LogDebugSource($"PlugIn Setup...");

                // Get the version number (such as 25 for 2016)
                var postFix = "";
                if (version != null && version.Contains("."))
                {
                    postFix = "." + version.Substring(0, version.IndexOf('.'));
                }

                // Store a reference to the current SolidWorks instance
                // Initialize SolidWorks (SolidDNA class)
                AddInIntegration.SolidWorks = new SolidWorksApplication((SldWorks)Activator.CreateInstance(Type.GetTypeFromProgID("SldWorks.Application" + postFix)), cookie);

                // Log it
                Logger.LogDebugSource($"SolidWorks Instance Created? {AddInIntegration.SolidWorks != null}");
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Called when the add-in has disconnected from SolidWorks
        /// </summary>
        public static void DisconnectedFromSolidWorks()
        {
            if (UseDetachedAppDomain)
            {
                PluginCrossDomain.DisconnectedFromSolidWorks();
            }
            else
            {
                AddInIntegration.OnDisconnectedFromSolidWorks();

                // Inform plug-ins
                PlugIns.ForEach(plugin =>
                {
                    // Log it
                    Logger.LogDebugSource($"Firing DisconnectedFromSolidWorks event for plugin `{plugin.AddInTitle}`...");

                    plugin.DisconnectedFromSolidWorks();
                });
            }
        }
        /// <summary>
        /// Cleans up the plug-in app domain so that the plug-in dll files can be edited after unloading
        /// </summary>
        public static void Teardown()
        {
            // Run code on new app-domain to tear down
            if (UseDetachedAppDomain)
            {
                // Tear down
                PluginCrossDomain.Teardown();

                // Log it
                Logger.LogDebugSource($"Unloading cross-domain...");

                // Unload our domain
                AppDomain.Unload(PlugInAppDomain);
            }
            else
            {
                // Tear down SolidWorks references
                AddInIntegration.TearDown();
            }
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Cleans up the plug-in app domain so that the plug-in dll files can be edited after unloading
        /// </summary>
        public static void Teardown()
        {
            // Run code on new app-domain to tear down
            if (UseDetachedAppDomain)
            {
                // Tear down
                PluginCrossDomain.Teardown();

                // Log it
                Logger.LogDebugSource($"Unloading cross-domain...");

                // Unload our domain
                AppDomain.Unload(PlugInAppDomain);
            }
            else
            {
                // Log it
                Logger.LogDebugSource($"Disposing SolidWorks COM reference...");

                // Dispose SolidWorks COM
                AddInIntegration.SolidWorks?.Dispose();
                AddInIntegration.SolidWorks = null;
            }
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Runs any initialization code required on plug-ins
        /// </summary>
        /// <param name="addinPath">The path to the add-in that is calling this setup (typically acquired using GetType().Assembly.Location)</param>
        /// <param name="log">True to log the output of the loading. Use false when registering for COM</param>
        public static void ConfigurePlugIns(string addinPath, bool log = true)
        {
            if (UseDetachedAppDomain)
            {
                // Log it
                Logger.LogDebugSource($"Cross-domain ConfigurePlugIns...");

                PluginCrossDomain.ConfigurePlugIns(addinPath);
            }
            else
            {
                // This is usually run for the ComRegister function

                // *********************************************************************************
                //
                // WARNING:
                //
                //   If SolidWorks is loading our add-ins and we have multiple that use SolidDna
                //   it loads and makes use of the existing AngelSix.SolidDna.dll file from
                //   the first add-in loaded and shares it for all future add-ins
                //
                //   This results in any static instances being shared and only one version
                //   of SolidDna being usable on an individual SolidWorks instance
                //
                //   I am not sure of the reason for this but I feel it is a bug in SolidWorks
                //   as changing the GUID of the AngelSix.SolidDna.dll assembly and its
                //   Assembly and File versions doesn't change what gets loaded by SolidWorks
                //
                //   Perhaps when we make this a NuGet package the way it references may
                //   make it work. Until then the only thing to keep in mind is any
                //   static values inside the AngelSix.SolidDna class could be shared between
                //   add-ins so things like PlugIns list will come in here initially at this
                //   point with the last PlugIns list from the previous add-in. This is not an
                //   issue here as we override it straight away before making use of it,
                //   but it is something to bare in mind until we find a better solution
                //
                //
                // *********************************************************************************

                // Log it
                // Logger.LogDebugSource($"ConfigurePlugIns...");

                // Try and find the title from the first plug-in found
                var plugins = SolidDnaPlugIns(addinPath, log: log, loadAll: true);
                var firstPlugInWithTitle = plugins.FirstOrDefault(f => !string.IsNullOrEmpty(f.AddInTitle));

                if (firstPlugInWithTitle != null)
                {
                    // Log it
                    //Logger.LogDebugSource($"Setting AddIn Title to {firstPlugInWithTitle.AddInTitle}");
                    // Logger.LogDebugSource($"Setting AddIn Description to {firstPlugInWithTitle.AddInDescription}");

                    AddInIntegration.SolidWorksAddInTitle       = firstPlugInWithTitle.AddInTitle;
                    AddInIntegration.SolidWorksAddInDescription = firstPlugInWithTitle.AddInDescription;
                }

                // Log it
                //Logger.LogDebugSource($"Loading PlugIns...");

                // Load all plug-in's at this stage for faster lookup
                PlugIns = SolidDnaPlugIns(addinPath, log);
            }
        }