-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTaskpaneIntegration.cs
More file actions
530 lines (457 loc) · 16.2 KB
/
TaskpaneIntegration.cs
File metadata and controls
530 lines (457 loc) · 16.2 KB
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
using DWM.TaskPaneHost;
using DWM.ExSw.Addin.Core;
using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
using SolidWorks.Interop.swpublished;
using System;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Net.NetworkInformation;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Shapes;
using Path = System.IO.Path;
using DWM.ExSw.Addin.DataSRV;
using Microsoft.Win32;
using System.Threading;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;
using System.Text;
using System.IO;
namespace DWM.ExSw.Addin
{
[ComVisible(true)]
[Guid("7f17322d-15fe-4b8a-ba52-2390c2a60a1f")]
[DisplayName("DailyWorkManager")]
[Description("DWM SOLIDWORKS Add-in")]
public class TaskpaneIntegration : ISwAddin
{
#region Event Handler Variables
Hashtable openDocs;
SldWorks SwEventPtr;
#endregion
#region Private Menbers
private int mSwCookie;
private TaskpaneView mTaskpaneView;
private SldWorks mSolidWorksApplication;
private TaskpaneHostUI mTaskpaneHost;
#endregion
#region Public Menbers
public int buttonIdx;
private ICommandManager iCmdMgr;
private ISldWorks iSwApp;
public const string SWTASKPANE_PROGID = "DWM.ExSw.Addin.Taskpane";
#endregion
#region Registration
private const string ADDIN_KEY_TEMPLATE = @"SOFTWARE\SolidWorks\Addins\{{{0}}}";
private const string ADDIN_STARTUP_KEY_TEMPLATE = @"Software\SolidWorks\AddInsStartup\{{{0}}}";
private const string ADD_IN_TITLE_REG_KEY_NAME = "Title";
private const string ADD_IN_DESCRIPTION_REG_KEY_NAME = "Description";
[ComRegisterFunction]
public static void RegisterFunction(Type t)
{
try
{
var addInTitle = "";
var loadAtStartup = true;
var addInDesc = "";
var dispNameAtt = t.GetCustomAttributes(false).OfType<DisplayNameAttribute>().FirstOrDefault();
if (dispNameAtt != null)
{
addInTitle = dispNameAtt.DisplayName;
}
else
{
addInTitle = t.ToString();
}
var descAtt = t.GetCustomAttributes(false).OfType<DescriptionAttribute>().FirstOrDefault();
if (descAtt != null)
{
addInDesc = descAtt.Description;
}
else
{
addInDesc = t.ToString();
}
var addInkey = Microsoft.Win32.Registry.LocalMachine.CreateSubKey(
string.Format(ADDIN_KEY_TEMPLATE, t.GUID));
addInkey.SetValue(null, 0);
addInkey.SetValue(ADD_IN_TITLE_REG_KEY_NAME, addInTitle);
addInkey.SetValue(ADD_IN_DESCRIPTION_REG_KEY_NAME, addInDesc);
var addInStartupkey = Microsoft.Win32.Registry.CurrentUser.CreateSubKey(
string.Format(ADDIN_STARTUP_KEY_TEMPLATE, t.GUID));
addInStartupkey.SetValue(null, Convert.ToInt32(loadAtStartup), Microsoft.Win32.RegistryValueKind.DWord);
}
catch (Exception ex)
{
Console.WriteLine("Error while registering the addin: " + ex.Message);
}
}
[ComUnregisterFunction]
public static void UnregisterFunction(Type t)
{
try
{
Microsoft.Win32.Registry.LocalMachine.DeleteSubKey(
string.Format(ADDIN_KEY_TEMPLATE, t.GUID));
Microsoft.Win32.Registry.CurrentUser.DeleteSubKey(
string.Format(ADDIN_STARTUP_KEY_TEMPLATE, t.GUID));
}
catch (Exception e)
{
Console.WriteLine("Error while unregistering the addin: " + e.Message);
}
}
#endregion
public ISldWorks SwApp
{
get { return iSwApp; }
}
public ICommandManager CmdMgr
{
get { return iCmdMgr; }
}
public Hashtable OpenDocs
{
get { return openDocs; }
}
#region SolidWorks Add-in Implementation
public bool ConnectToSW(object ThisSW, int Cookie)
{
mSolidWorksApplication = (SldWorks)ThisSW;
mSwCookie = Cookie;
var ok = mSolidWorksApplication.SetAddinCallbackInfo2(0, this, mSwCookie);
LoadUI();
VortexIntegration();
#region Setup the Event Handlers
//SwEventPtr = mSolidWorksApplication;
iSwApp = (ISldWorks)ThisSW;
SwEventPtr = (SldWorks)iSwApp;
openDocs = new Hashtable();
AttachEventHandlers();
#endregion
return true;
}
public bool DisconnectFromSW()
{
UnloadUI();
DetachEventHandlers();
iSwApp = null;
return true;
}
#endregion
#region UI Methods
public bool LoadUI()
{
//Coletando o diretorio da pasta com o icone
var imagePath = Path.Combine(Path.GetDirectoryName(typeof(TaskpaneIntegration).Assembly.CodeBase).Replace(@"file:\", ""), "main.png");
//Criando o painel lateral
mTaskpaneView = mSolidWorksApplication.CreateTaskpaneView2(imagePath, "DWM SOLIDWORKS");
mTaskpaneHost = (TaskpaneHostUI)mTaskpaneView.AddControl(SWTASKPANE_PROGID, string.Empty);
//mTaskpaneView.AddCustomButton(imagePath, "Save (custom png)");
if (mTaskpaneHost != null)
{
mTaskpaneHost.swApp = mSolidWorksApplication;
}
return true;
}
private void UnloadUI()
{
mTaskpaneHost = null;
mTaskpaneView.DeleteView();
Marshal.ReleaseComObject(mTaskpaneView);
mTaskpaneView = null;
}
#endregion
#region Events Methods SolidWorks
public bool AttachEventHandlers()
{
AttachSwEvents();
AttachEventsToAllDocuments();
return true;
}
public bool AttachSwEvents()
{
try
{
mTaskpaneView.TaskPaneToolbarButtonClicked += this.swTaskPane_TaskPaneToolbarButtonClicked;
SwEventPtr.ActiveDocChangeNotify += new DSldWorksEvents_ActiveDocChangeNotifyEventHandler(OnDocChange);
SwEventPtr.DocumentLoadNotify2 += new DSldWorksEvents_DocumentLoadNotify2EventHandler(OnDocLoad);
SwEventPtr.FileNewNotify2 += new DSldWorksEvents_FileNewNotify2EventHandler(OnFileNew);
SwEventPtr.ActiveModelDocChangeNotify += new DSldWorksEvents_ActiveModelDocChangeNotifyEventHandler(OnModelChange);
SwEventPtr.FileOpenPostNotify += new DSldWorksEvents_FileOpenPostNotifyEventHandler(FileOpenPostNotify);
return true;
}
catch (Exception e)
{
Console.WriteLine(e.Message);
return false;
}
}
public bool DetachSwEvents()
{
try
{
SwEventPtr.ActiveDocChangeNotify -= new DSldWorksEvents_ActiveDocChangeNotifyEventHandler(OnDocChange);
SwEventPtr.DocumentLoadNotify2 -= new DSldWorksEvents_DocumentLoadNotify2EventHandler(OnDocLoad);
SwEventPtr.FileNewNotify2 -= new DSldWorksEvents_FileNewNotify2EventHandler(OnFileNew);
SwEventPtr.ActiveModelDocChangeNotify -= new DSldWorksEvents_ActiveModelDocChangeNotifyEventHandler(OnModelChange);
SwEventPtr.FileOpenPostNotify -= new DSldWorksEvents_FileOpenPostNotifyEventHandler(FileOpenPostNotify);
return true;
}
catch (Exception e)
{
Console.WriteLine(e.Message);
return false;
}
}
public void AttachEventsToAllDocuments()
{
ModelDoc2 modDoc;
modDoc = mSolidWorksApplication.ActiveDoc as ModelDoc2;
if (modDoc != null)
{
if (!openDocs.Contains(modDoc))
{
AttachModelDocEventHandler(modDoc);
}
else
{
mTaskpaneHost.Verficacao(mSolidWorksApplication, modDoc);
}
}
}
public bool AttachModelDocEventHandler(ModelDoc2 modDoc)
{
if (modDoc == null)
return false;
DocumentEventHandler docHandler = null;
if (!openDocs.Contains(modDoc))
{
switch (modDoc.GetType())
{
case (int)swDocumentTypes_e.swDocPART:
{
docHandler = new PartEventHandler(modDoc, this);
break;
}
case (int)swDocumentTypes_e.swDocASSEMBLY:
{
docHandler = new AssemblyEventHandler(modDoc, this);
break;
}
case (int)swDocumentTypes_e.swDocDRAWING:
{
docHandler = new DrawingEventHandler(modDoc, this);
break;
}
default:
{
return false; //Unsupported document type
}
}
docHandler.AttachEventHandlers(mSolidWorksApplication, modDoc,mTaskpaneHost,this);
openDocs.Add(modDoc, docHandler);
}
return true;
}
public bool DetachModelEventHandler(ModelDoc2 modDoc)
{
DocumentEventHandler docHandler;
docHandler = (DocumentEventHandler)openDocs[modDoc];
openDocs.Remove(modDoc);
if (openDocs.Count==0)
{
mTaskpaneHost.DefaultForms(mSolidWorksApplication);
}
modDoc = null;
docHandler = null;
return true;
}
public bool DetachEventHandlers()
{
DetachSwEvents();
//Close events on all currently open docs
DocumentEventHandler docHandler;
int numKeys = openDocs.Count;
object[] keys = new Object[numKeys];
//Remove all document event handlers
openDocs.Keys.CopyTo(keys, 0);
foreach (ModelDoc2 key in keys)
{
docHandler = (DocumentEventHandler)openDocs[key];
docHandler.DetachEventHandlers(); //This also removes the pair from the hash
docHandler = null;
}
return true;
}
#region Events
public int OnDocChange()
{
//if (VortexIntegration())
//{
// mTaskpaneHost.PanePrincipal.Visible = true;
//}
//else
//{
// mTaskpaneHost.PanePrincipal.Visible = false;
//}
return 0;
}
public int FileOpenPreNotify(string FileName)
{
//if (VortexIntegration())
//{
// mTaskpaneHost.PanePrincipal.Visible = true;
//}
//else
//{
// mTaskpaneHost.PanePrincipal.Visible = false;
//}
return 0;
}
public int OnDocLoad(string docTitle, string docPath)
{
//if (VortexIntegration())
//{
// mTaskpaneHost.PanePrincipal.Visible = true;
//}
//else
//{
// mTaskpaneHost.PanePrincipal.Visible = false;
//}
return 0;
}
int FileOpenPostNotify(string FileName)
{
if (VortexIntegration())
{
mTaskpaneHost.PanePrincipal.Visible = true;
}
else
{
mTaskpaneHost.PanePrincipal.Visible = false;
}
return 0;
}
public int OnFileNew(object newDoc, int docType, string templateName)
{
//if (VortexIntegration())
//{
// mTaskpaneHost.PanePrincipal.Visible = true;
//}
//else
//{
// mTaskpaneHost.PanePrincipal.Visible = false;
//}
return 0;
}
public int OnModelChange()
{
//if (VortexIntegration())
//{
// mTaskpaneHost.PanePrincipal.Visible = true;
AttachEventsToAllDocuments();
//}
//else
//{
// mTaskpaneHost.PanePrincipal.Visible = false;
//}
return 0;
}
public int swTaskPane_TaskPaneToolbarButtonClicked(int ButtonIndex)
{
switch ((ButtonIndex + 1))
{
case 1:
Debug.Print("Save (custom png) button clicked.");
break;
case 2:
Debug.Print("Next button clicked.");
break;
case 3:
Debug.Print("Back button clicked.");
break;
case 4:
Debug.Print("Okay button clicked.");
break;
case 5:
Debug.Print("Close button clicked and tab deleted.");
break;
}
return 1;
}
#endregion
#endregion
bool VortexIntegration()
{
RegistryKey key = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\VortexTeste");
if (key != null)
{
string status = (string)key.GetValue("App1Status", "logged_out");
key.Close();
if (status != "logged_in")
{
OpenVortex vortex = new OpenVortex();
vortex.Main();
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
class OpenVortex
{
public void Main()
{
TcpClient client = new TcpClient("localhost", 5000);
NetworkStream stream = client.GetStream();
byte[] data = Encoding.UTF8.GetBytes("executarTarefa");
stream.Write(data, 0, data.Length);
stream.Close();
client.Close();
}
}
}
#region DocumentHandler
internal class DrawingEventHandler : DocumentEventHandler
{
private ModelDoc2 modDoc;
private TaskpaneIntegration taskpaneIntegration;
public DrawingEventHandler(ModelDoc2 modDoc, TaskpaneIntegration taskpaneIntegration)
{
this.modDoc = modDoc;
this.taskpaneIntegration = taskpaneIntegration;
}
}
internal class AssemblyEventHandler : DocumentEventHandler
{
private ModelDoc2 modDoc;
private TaskpaneIntegration taskpaneIntegration;
public AssemblyEventHandler(ModelDoc2 modDoc, TaskpaneIntegration taskpaneIntegration)
{
this.modDoc = modDoc;
this.taskpaneIntegration = taskpaneIntegration;
}
}
internal class PartEventHandler : DocumentEventHandler
{
private ModelDoc2 modDoc;
private TaskpaneIntegration taskpaneIntegration;
public PartEventHandler(ModelDoc2 modDoc, TaskpaneIntegration taskpaneIntegration)
{
this.modDoc = modDoc;
this.taskpaneIntegration = taskpaneIntegration;
}
}
#endregion
}