chr
2024-11-02 b5234c5ab1e9e6826b8d8fc1e95fa752aaa40b74
PdmSw/SwAddin.cs
@@ -126,6 +126,8 @@
        private BaseAttacher attacher;
        private ISldWorks swApp;
        private TaskpaneView panel;
        private Form mainForm;
        // Public Properties
        public ISldWorks SwApp
        {
@@ -274,7 +276,7 @@
                    Path.Combine(imageFolder, @"MatChooser_128X128.png")
            };
            // 创建Panel
            TaskpaneView panel = SwApp.CreateTaskpaneView3(bitmaps, "PdmSwPlugin" + PluginConst.Version);
            panel = SwApp.CreateTaskpaneView3(bitmaps, "PdmSwPlugin" + PluginConst.Version);
            mainControl = new MainControl((SldWorks)SwApp);
            // 只能用winform,代理wpf
@@ -284,7 +286,7 @@
                AutoSize = true,
                Dock = DockStyle.Fill
            };
            Form form = new Form
            mainForm = new Form
            {
                Text = "PdmSwPlugin",
                ControlBox = false,
@@ -292,10 +294,11 @@
                KeyPreview = true,
                Dock = DockStyle.Fill
            };
            form.Controls.Add(host);
            IntPtr ptr = form.Handle;
            mainForm.Controls.Add(host);
            IntPtr ptr = mainForm.Handle;
            // IntPtr ptr = WindowIntPtrUtil.FindWindow(null, "Window1");
            panel.DisplayWindowFromHandlex64(ptr.ToInt64());
            mainForm.WindowState = FormWindowState.Maximized;
        }
        private void LoadPlugins()
@@ -327,23 +330,36 @@
        /// <returns></returns>
        public bool DisconnectFromSW()
        {
            RemoveCommandMgr();
            RemovePMP();
            mainControl.StopTcpServer();
            attacher.DetachEventHandlers();
            attacher = null;
            try
            {
                RemoveCommandMgr();
                RemovePMP();
                mainControl.StopTcpServer();
                mainControl.DeActiveHandler();
                attacher.DetachEventHandlers();
                attacher = null;
            System.Runtime.InteropServices.Marshal.ReleaseComObject(iCmdMgr);
            iCmdMgr = null;
            System.Runtime.InteropServices.Marshal.ReleaseComObject(SwApp);
            swApp = null;
            //The addin _must_ call GC.Collect() here in order to retrieve all managed code pointers
            GC.Collect();
            GC.WaitForPendingFinalizers();
                mainForm.Close();
                mainForm = null;
                mainControl = null;
                panel.DeleteView();
                panel = null;
            GC.Collect();
            GC.WaitForPendingFinalizers();
                Marshal.ReleaseComObject(iCmdMgr);
                iCmdMgr = null;
                Marshal.ReleaseComObject(SwApp);
                swApp = null;
                //The addin _must_ call GC.Collect() here in order to retrieve all managed code pointers
                GC.Collect();
                GC.WaitForPendingFinalizers();
                //GC.Collect();
                //GC.WaitForPendingFinalizers();
            }
            catch (Exception ex)
            {
                Logger.Error("DisconnectFromSW Failed!", ex);
            }
            return true;
        }
        #endregion