From 31a636e735a0addc56e4f4527f500b7aa0874eb5 Mon Sep 17 00:00:00 2001
From: chr <chr@mailcom>
Date: 星期五, 03 一月 2025 11:21:46 +0800
Subject: [PATCH] 改了一大堆;

---
 PdmSwPlugin.Main/EventHandler/BaseAttacher.cs |   68 ++++++++++++++++++++++++++++++----
 1 files changed, 60 insertions(+), 8 deletions(-)

diff --git a/PdmSwPlugin.Main/EventHandler/BaseAttacher.cs b/PdmSwPlugin.Main/EventHandler/BaseAttacher.cs
index 39d3dcb..70ce1ae 100644
--- a/PdmSwPlugin.Main/EventHandler/BaseAttacher.cs
+++ b/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;

--
Gitblit v1.9.1