chr
7 天以前 43a0207d207390abdeeb3ab9155eebf03edd7b1a
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
using System.Collections;
 
namespace PdmSw
{
 
    /// <summary>
    /// Í¨ÓÃÎĵµÊ¼þ´¦ÀíÆ÷
    /// </summary>
    public class DocumentEventHandler
    {
        protected ISldWorks iSwApp;
        protected ModelDoc2 document;
        protected SwAddin userAddin;
 
        protected Hashtable openModelViews;
 
        public DocumentEventHandler(ModelDoc2 modDoc, SwAddin addIn)
        {
            document = modDoc;
            iSwApp = addIn.SwApp;
            userAddin = addIn;
            openModelViews = new Hashtable();
        }
 
        /// <summary>
        /// ¸½¼Óʼþ
        /// </summary>
        /// <returns></returns>
        virtual public bool AttachEventHandlers()
        {
            return true;
        }
 
        /// <summary>
        /// ÒƳýʼþ
        /// </summary>
        /// <returns></returns>
        virtual public bool DetachEventHandlers()
        {
            return true;
        }
 
        /// <summary>
        /// ¸½¼ÓÊÓͼʼþ
        /// </summary>
        /// <returns></returns>
        public bool ConnectModelViews()
        {
            IModelView mView;
            mView = (IModelView)document.GetFirstModelView();
 
            while (mView != null)
            {
                if (!openModelViews.Contains(mView))
                {
                    DocViewEventHandler dView = new DocViewEventHandler(userAddin, mView, this);
                    dView.AttachEventHandlers();
                    openModelViews.Add(mView, dView);
                }
                mView = (IModelView)mView.GetNext();
            }
            return true;
        }
 
        /// <summary>
        /// ÒƳýÊÓͼʼþ
        /// </summary>
        /// <returns></returns>
        public bool DisconnectModelViews()
        {
            //Close events on all currently open docs
            DocViewEventHandler dView;
            int numKeys;
            numKeys = openModelViews.Count;
 
            if (numKeys == 0)
            {
                return false;
            }
 
            object[] keys = new object[numKeys];
 
            //Remove all ModelView event handlers
            openModelViews.Keys.CopyTo(keys, 0);
            foreach (ModelView key in keys)
            {
                dView = (DocViewEventHandler)openModelViews[key];
                dView.DetachEventHandlers();
                openModelViews.Remove(key);
                dView = null;
            }
            return true;
        }
 
        public bool DetachModelViewEventHandler(ModelView mView)
        {
            DocViewEventHandler dView;
            if (openModelViews.Contains(mView))
            {
                dView = (DocViewEventHandler)openModelViews[mView];
                openModelViews.Remove(mView);
                mView = null;
                dView = null;
            }
            return true;
        }
    }
    /// <summary>
    /// Áã¼þʼþ´¦ÀíÆ÷
    /// </summary>
    public class PartEventHandler : DocumentEventHandler
    {
        PartDoc doc;
        SwAddin swPlugin;
 
        public PartEventHandler(ModelDoc2 modDoc, SwAddin addin) : base(modDoc, addin)
        {
            doc = (PartDoc)document;
            swPlugin = addin;
        }
 
        override public bool AttachEventHandlers()
        {
            doc.DestroyNotify += new DPartDocEvents_DestroyNotifyEventHandler(OnDestroy);
            doc.NewSelectionNotify += new DPartDocEvents_NewSelectionNotifyEventHandler(OnNewSelection);
            // ×Ô¶¨ÒåÊôÐÔ±ä¸üʼþ
            doc.ChangeCustomPropertyNotify += new DPartDocEvents_ChangeCustomPropertyNotifyEventHandler(swPlugin.OnActiveDocCustomPropertyChanged);
            ConnectModelViews();
            return true;
        }
 
        override public bool DetachEventHandlers()
        {
            doc.DestroyNotify -= new DPartDocEvents_DestroyNotifyEventHandler(OnDestroy);
            doc.NewSelectionNotify -= new DPartDocEvents_NewSelectionNotifyEventHandler(OnNewSelection);
 
            doc.ChangeCustomPropertyNotify -= new DPartDocEvents_ChangeCustomPropertyNotifyEventHandler(swPlugin.OnActiveDocCustomPropertyChanged);
 
            DisconnectModelViews();
 
            userAddin.DetachModelEventHandler(document);
            return true;
        }
 
        //Event Handlers
        public int OnDestroy()
        {
            DetachEventHandlers();
            return 0;
        }
 
        public int OnNewSelection()
        {
            return 0;
        }
    }
 
    public class AssemblyEventHandler : DocumentEventHandler
    {
        AssemblyDoc doc;
        SwAddin swAddin;
 
        public AssemblyEventHandler(ModelDoc2 modDoc, SwAddin addin)
            : base(modDoc, addin)
        {
            doc = (AssemblyDoc)document;
            swAddin = addin;
        }
 
        override public bool AttachEventHandlers()
        {
            doc.DestroyNotify += new DAssemblyDocEvents_DestroyNotifyEventHandler(OnDestroy);
            doc.NewSelectionNotify += new DAssemblyDocEvents_NewSelectionNotifyEventHandler(OnNewSelection);
            // ×é¼þ״̬±ä¸ü
            doc.ComponentStateChangeNotify3 += new DAssemblyDocEvents_ComponentStateChangeNotify3EventHandler(ComponentStateChangeNotify3);
            doc.ComponentVisualPropertiesChangeNotify += new DAssemblyDocEvents_ComponentVisualPropertiesChangeNotifyEventHandler(ComponentVisualPropertiesChangeNotify);
            doc.ComponentDisplayStateChangeNotify += new DAssemblyDocEvents_ComponentDisplayStateChangeNotifyEventHandler(ComponentDisplayStateChangeNotify);
            ConnectModelViews();
            return true;
        }
 
        override public bool DetachEventHandlers()
        {
            doc.DestroyNotify -= new DAssemblyDocEvents_DestroyNotifyEventHandler(OnDestroy);
            doc.NewSelectionNotify -= new DAssemblyDocEvents_NewSelectionNotifyEventHandler(OnNewSelection);
            doc.ComponentStateChangeNotify3 -= new DAssemblyDocEvents_ComponentStateChangeNotify3EventHandler(ComponentStateChangeNotify3);
            doc.ComponentVisualPropertiesChangeNotify -= new DAssemblyDocEvents_ComponentVisualPropertiesChangeNotifyEventHandler(ComponentVisualPropertiesChangeNotify);
            doc.ComponentDisplayStateChangeNotify -= new DAssemblyDocEvents_ComponentDisplayStateChangeNotifyEventHandler(ComponentDisplayStateChangeNotify);
            DisconnectModelViews();
 
            userAddin.DetachModelEventHandler(document);
            return true;
        }
 
        //Event Handlers
        public int OnDestroy()
        {
            // DetachEventHandlers();
            return 0;
        }
 
        public int OnNewSelection()
        {
            return 0;
        }
 
        //attach events to a component if it becomes resolved
        protected int ComponentStateChange(object componentModel, short newCompState)
        {
            ModelDoc2 modDoc = (ModelDoc2)componentModel;
            swComponentSuppressionState_e newState = (swComponentSuppressionState_e)newCompState;
            switch (newState)
            {
                case swComponentSuppressionState_e.swComponentFullyResolved:
                    {
                        if ((modDoc != null) & !this.swAddin.OpenDocs.Contains(modDoc))
                        {
                            this.swAddin.AttachModelDocEventHandler(modDoc);
                        }
                        break;
                    }
 
                case swComponentSuppressionState_e.swComponentResolved:
                    {
                        if ((modDoc != null) & !this.swAddin.OpenDocs.Contains(modDoc))
                        {
                            this.swAddin.AttachModelDocEventHandler(modDoc);
                        }
                        break;
                    }
 
            }
            return 0;
        }
 
        protected int ComponentStateChange(object componentModel)
        {
            ComponentStateChange(componentModel, (short)swComponentSuppressionState_e.swComponentResolved);
            return 0;
        }
 
 
        public int ComponentStateChangeNotify3(object componentModel, string CompName, short oldCompState, short newCompState)
        {
            return ComponentStateChange(componentModel, newCompState);
        }
 
        int ComponentDisplayStateChangeNotify(object swObject)
        {
            Component2 component = (Component2)swObject;
            ModelDoc2 modDoc = (ModelDoc2)component.GetModelDoc();
 
            return ComponentStateChange(modDoc);
        }
 
        int ComponentVisualPropertiesChangeNotify(object swObject)
        {
            Component2 component = (Component2)swObject;
            ModelDoc2 modDoc = (ModelDoc2)component.GetModelDoc();
 
            return ComponentStateChange(modDoc);
        }
    }
 
    /// <summary>
    /// ¹¤³Ìͼʼþ´¦ÀíÆ÷
    /// </summary>
    public class DrawingEventHandler : DocumentEventHandler
    {
        DrawingDoc doc;
        SwAddin swPlugin;
 
        public DrawingEventHandler(ModelDoc2 modDoc, SwAddin addin) : base(modDoc, addin)
        {
            doc = (DrawingDoc)document;
            swPlugin = addin;
        }
 
        override public bool AttachEventHandlers()
        {
            doc.DestroyNotify += new DDrawingDocEvents_DestroyNotifyEventHandler(OnDestroy);
            doc.NewSelectionNotify += new DDrawingDocEvents_NewSelectionNotifyEventHandler(OnNewSelection);
 
            doc.ChangeCustomPropertyNotify +=
                new DDrawingDocEvents_ChangeCustomPropertyNotifyEventHandler(swPlugin.OnActiveDocCustomPropertyChanged);
 
            ConnectModelViews();
 
            return true;
        }
 
        override public bool DetachEventHandlers()
        {
            doc.DestroyNotify -= new DDrawingDocEvents_DestroyNotifyEventHandler(OnDestroy);
            doc.NewSelectionNotify -= new DDrawingDocEvents_NewSelectionNotifyEventHandler(OnNewSelection);
 
            doc.ChangeCustomPropertyNotify -=
                new DDrawingDocEvents_ChangeCustomPropertyNotifyEventHandler(swPlugin.OnActiveDocCustomPropertyChanged);
 
            DisconnectModelViews();
 
            userAddin.DetachModelEventHandler(document);
            return true;
        }
 
        //Event Handlers
        public int OnDestroy()
        {
            DetachEventHandlers();
            return 0;
        }
 
        public int OnNewSelection()
        {
            return 0;
        }
    }
 
    public class DocViewEventHandler
    {
        ISldWorks iSwApp;
        SwAddin userAddin;
        ModelView mView;
        DocumentEventHandler parent;
 
        public DocViewEventHandler(SwAddin addin, IModelView mv, DocumentEventHandler doc)
        {
            userAddin = addin;
            mView = (ModelView)mv;
            iSwApp = userAddin.SwApp;
            parent = doc;
        }
 
        public bool AttachEventHandlers()
        {
            mView.DestroyNotify2 += new DModelViewEvents_DestroyNotify2EventHandler(OnDestroy);
            mView.RepaintNotify += new DModelViewEvents_RepaintNotifyEventHandler(OnRepaint);
            return true;
        }
 
        public bool DetachEventHandlers()
        {
            mView.DestroyNotify2 -= new DModelViewEvents_DestroyNotify2EventHandler(OnDestroy);
            mView.RepaintNotify -= new DModelViewEvents_RepaintNotifyEventHandler(OnRepaint);
            parent.DetachModelViewEventHandler(mView);
            return true;
        }
 
        //EventHandlers
        public int OnDestroy(int destroyType)
        {
            return 0;
        }
 
        public int OnRepaint(int repaintType)
        {
            return 0;
        }
    }
 
}