chr
2025-01-03 31a636e735a0addc56e4f4527f500b7aa0874eb5
PdmSwPlugin.Main/EventHandler/BaseAttacher.cs
@@ -4,10 +4,14 @@
using SolidWorks.Interop.swconst;
using System;
using System.Collections;
using System.Threading.Tasks;
using System.Windows.Controls;
namespace PdmSwPlugin.Main.EventHandler
{
    /// <summary>
    /// 事件附加器
    /// </summary>
    public class BaseAttacher : IEventAttacher, IDocEventHandler
    {
        public static ILog Logger { get; } = LogManager.GetLogger("SwAddin");
@@ -33,8 +37,9 @@
        /// <returns></returns>
        public virtual bool AttachEventHandlers()
        {
            // 添加应用事件监听
            AttachSwEvents();
            //Listen for events on all currently open docs
            // 添加文档事件监听
            AttachEventsToAllDocuments();
            return true;
        }
@@ -60,14 +65,55 @@
        private bool AttachEventsToAllDocuments()
        {
            ModelDoc2 modDoc = (ModelDoc2)SwApp.GetFirstDocument();
            while (modDoc != null)
            //ModelDoc2 modDoc = (ModelDoc2)SwApp.GetFirstDocument();
            //while (modDoc != null)
            //{
            //    if (!OpenDocContext.IsOpenDoc(modDoc))
            //    {
            //        AttachModelDocEventHandler(modDoc);
            //    }
            //    modDoc = (ModelDoc2)modDoc.GetNext();
            //}
            /// 试一下异步附加事件
            var objs = SwApp.GetDocuments();
            if (objs != null && objs.Length > 0)
            {
                if (!OpenDocContext.IsOpenDoc(modDoc))
                Task[] tasks = new Task[objs.Length];
                for (int i = 0; i < objs.Length; i++)
                {
                    AttachModelDocEventHandler(modDoc);
                    var obj = objs[i];
                    if (obj is ModelDoc2 modDoc)
                    {
                        tasks[i] = Task.Run(() =>
                        {
                            if (!OpenDocContext.IsOpenDoc(modDoc))
                            {
                                AttachModelDocEventHandler(modDoc);
                            }
                        });
                    }
                }
                modDoc = (ModelDoc2)modDoc.GetNext();
                Task.WaitAll(tasks);
            }
            return true;
        }
        private bool AttachEventsToAllDocuments(ModelDoc2 modDoc)
        {
            if (!OpenDocContext.IsOpenDoc(modDoc))
            {
                AttachModelDocEventHandler(modDoc);
            }
            return true;
        }
        private bool AttachEventsToAllDocuments(string fileName)
        {
            ModelDoc2 modDoc = SwApp.GetOpenDocument(fileName);
            if (!OpenDocContext.IsOpenDoc(modDoc))
            {
                AttachModelDocEventHandler(modDoc);
            }
            return true;
        }
@@ -208,7 +254,7 @@
        public int OnFileNew(object newDoc, int docType, string templateName)
        {
            AttachEventsToAllDocuments();
            AttachEventsToAllDocuments((ModelDoc2)newDoc);
            return 0;
        }
@@ -217,7 +263,13 @@
            return 0;
        }
        public int FileOpenPostNotify(string FileName)
        /// <summary>
        /// Post-notifies an application when a file has been opened.
        /// 在打开文件时 Post-通知应用程序。
        /// </summary>
        /// <param name="FileName"></param>
        /// <returns></returns>
        public int FileOpenPostNotify(string filePath)
        {
            AttachEventsToAllDocuments();
            return 0;