chr
2025-01-03 31a636e735a0addc56e4f4527f500b7aa0874eb5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
using log4net;
using PdmSwPlugin.Common.Util;
using SolidWorks.Interop.sldworks;
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");
        protected MainControl mainControl;
 
        protected ISldWorks swApp;
        protected SldWorks SwEventPtr;
        protected ModelDoc2 lastActiveDoc;
        protected ModelDoc2 currentDoc;
 
        public ISldWorks SwApp => swApp;
 
        public BaseAttacher(ISldWorks swApp, MainControl mainControl)
        {
            this.swApp = swApp;
            this.mainControl = mainControl;
            this.SwEventPtr = swApp as SldWorks;
        }
 
        /// <summary>
        /// 全局附加事件
        /// </summary>
        /// <returns></returns>
        public virtual bool AttachEventHandlers()
        {
            // 添加应用事件监听
            AttachSwEvents();
            // 添加文档事件监听
            AttachEventsToAllDocuments();
            return true;
        }
 
        public bool AttachSwEvents()
        {
            try
            {
                // 当前活动文档发生变化时执行
                SwEventPtr.ActiveDocChangeNotify += new DSldWorksEvents_ActiveDocChangeNotifyEventHandler(OnActiveDocChange);
                SwEventPtr.DocumentLoadNotify2 += new DSldWorksEvents_DocumentLoadNotify2EventHandler(OnDocLoad);
                SwEventPtr.FileNewNotify2 += new DSldWorksEvents_FileNewNotify2EventHandler(OnFileNew);
                SwEventPtr.ActiveModelDocChangeNotify += new DSldWorksEvents_ActiveModelDocChangeNotifyEventHandler(OnActiveModelChange);
                SwEventPtr.FileOpenPostNotify += new DSldWorksEvents_FileOpenPostNotifyEventHandler(FileOpenPostNotify);
                return true;
            }
            catch (Exception e)
            {
                Logger.Error("SolidWorks事件附加异常!", e);
                return false;
            }
        }
 
        private bool AttachEventsToAllDocuments()
        {
            //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)
            {
                Task[] tasks = new Task[objs.Length];
                for (int i = 0; i < objs.Length; i++)
                {
                    var obj = objs[i];
                    if (obj is ModelDoc2 modDoc)
                    {
                        tasks[i] = Task.Run(() =>
                        {
                            if (!OpenDocContext.IsOpenDoc(modDoc))
                            {
                                AttachModelDocEventHandler(modDoc);
                            }
                        });
                    }
                }
                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;
        }
 
        #region 附加事件
        public bool AttachModelDocEventHandler(ModelDoc2 modDoc)
        {
            if (modDoc == null) return false;
            BaseEventHandler docHandler;
            if (!OpenDocContext.IsOpenDoc(modDoc))
            {
                string sb = modDoc.GetPathName();
                switch (modDoc.GetType())
                {
                    case (int)swDocumentTypes_e.swDocPART:
                        {
                            docHandler = new PartDocEventHandler(modDoc, this);
                            break;
                        }
                    case (int)swDocumentTypes_e.swDocASSEMBLY:
                        {
                            docHandler = new AssDocEventHandler(modDoc, this);
                            break;
                        }
                    case (int)swDocumentTypes_e.swDocDRAWING:
                        {
                            docHandler = new DrwDocEventHandler(modDoc, this);
                            break;
                        }
                    default:
                        {
                            return false; //Unsupported document type
                        }
                }
                docHandler.AttachEventHandlers();
                if (!OpenDocContext.IsOpenDoc(modDoc))
                {
                    OpenDocContext.AddDoc(modDoc, docHandler);
                }
            }
            return true;
        }
 
        /// <summary>
        /// 移除事件处理
        /// </summary>
        /// <returns></returns>
        private bool DetachSwEvents()
        {
            try
            {
                SwEventPtr.ActiveDocChangeNotify -= new DSldWorksEvents_ActiveDocChangeNotifyEventHandler(OnActiveDocChange);
                SwEventPtr.DocumentLoadNotify2 -= new DSldWorksEvents_DocumentLoadNotify2EventHandler(OnDocLoad);
                SwEventPtr.FileNewNotify2 -= new DSldWorksEvents_FileNewNotify2EventHandler(OnFileNew);
                SwEventPtr.ActiveModelDocChangeNotify -= new DSldWorksEvents_ActiveModelDocChangeNotifyEventHandler(OnActiveModelChange);
                SwEventPtr.FileOpenPostNotify -= new DSldWorksEvents_FileOpenPostNotifyEventHandler(FileOpenPostNotify);
                return true;
            }
            catch (Exception e)
            {
                Logger.Error("SolidWorks事件移除异常!", e);
                return false;
            }
        }
 
        /// <summary>
        /// 释放设置的事件处理器
        /// </summary>
        /// <returns></returns>
        public bool DetachEventHandlers()
        {
            DetachSwEvents();
 
            //Close events on all currently open docs
            BaseEventHandler docHandler;
            int numKeys = OpenDocContext.OpenDocs.Count;
            object[] keys = new Object[numKeys];
 
            //Remove all document event handlers
            OpenDocContext.OpenDocs.Keys.CopyTo(keys, 0);
            foreach (ModelDoc2 key in keys)
            {
                docHandler = (BaseEventHandler)OpenDocContext.OpenDocs[key];
                docHandler.DetachEventHandlers(); //This also removes the pair from the hash
                docHandler = null;
            }
            return true;
        }
 
        public bool DetachModelEventHandler(ModelDoc2 modDoc)
        {
            BaseEventHandler docHandler;
            docHandler = (BaseEventHandler)OpenDocContext.GetHandler(modDoc);
            OpenDocContext.RemoveDoc(modDoc);
            modDoc = null;
            docHandler = null;
            return true;
        }
        #endregion
 
        public int OnActiveDocCustomPropertyAdded(string propName, string Configuration, string NewValue, int valueType)
        {
            mainControl.OnCustomPropertyChange(propName, Configuration, null, NewValue, valueType);
            return 0;
        }
 
        public int OnActiveDocCustomPropertyChanged(string propName, string Configuration, string oldValue, string NewValue, int valueType)
        {
            mainControl.OnCustomPropertyChange(propName, Configuration, oldValue, NewValue, valueType);
            return 0;
        }
 
        public int OnActiveDocSaved(int type, string fileName)
        {
            var doc = SwApp.IActiveDoc2;
            mainControl.OnDocSaved(doc, null);
            lastActiveDoc = null;
            return 0;
        }
 
        #region Event Handlers 各种事件处理
        /// <summary>
        /// 当前活动文档发生变化时
        /// </summary>
        /// <returns>0 返回成功,其它的失败</returns>
        public int OnActiveDocChange()
        {
            lastActiveDoc = currentDoc;
            currentDoc = SwApp.IActiveDoc2;
            mainControl.OnActiveDocChange(lastActiveDoc, currentDoc, null);
            return 0;
        }
 
        public int OnDocLoad(string docTitle, string docPath)
        {
            return 0;
        }
 
        public int OnFileNew(object newDoc, int docType, string templateName)
        {
            AttachEventsToAllDocuments((ModelDoc2)newDoc);
            return 0;
        }
 
        public int OnActiveModelChange()
        {
            return 0;
        }
 
        /// <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;
        }
 
        public int OnDocDestory(ModelDoc2 doc)
        {
            return mainControl.OnDocDestroy(doc);
        }
 
        public void AfterDocDestroy()
        {
            mainControl.OnAfterDocDestroy();
        }
        #endregion
    }
}