-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSOLIDWORKSInstanceManager.cs
More file actions
143 lines (109 loc) · 4.4 KB
/
Copy pathSOLIDWORKSInstanceManager.cs
File metadata and controls
143 lines (109 loc) · 4.4 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
using BlueByte.SOLIDWORKS.Extensions.Helpers;
using Microsoft.Win32;
using SolidWorks.Interop.sldworks;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Runtime.ConstrainedExecution;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
using System.Security;
using System.Security.Principal;
using System.Threading;
namespace BlueByte.SOLIDWORKS.Extensions
{
public partial class SOLIDWORKSInstanceManager : ISOLIDWORKSInstanceManager
{
public const string startSWNoJournalDialogAndSuppressAllDialogs = "/r";
public const string startSWBatch = "/b";
[DllImport("ole32.dll")]
public static extern int GetRunningObjectTable(int reserved, out IRunningObjectTable port);
[DllImport("ole32.dll")]
public static extern int CreateBindCtx(int reserved, out IBindCtx ppbc);
/// <summary>
/// Gets the SOLIDWORKS instance from process identifier.
/// </summary>
/// <param name="PID">The process ID.</param>
/// <returns></returns>
public SldWorks GetSOLIDWORKSInstanceFromProcessID(int PID)
{
IntPtr numFetched = IntPtr.Zero;
IRunningObjectTable runningObjectTable;
IEnumMoniker monikerEnumerator;
IMoniker[] monikers = new IMoniker[1];
GetRunningObjectTable(0, out runningObjectTable);
runningObjectTable.EnumRunning(out monikerEnumerator);
IBindCtx b;
monikerEnumerator.Reset();
while (monikerEnumerator.Next(1, monikers, numFetched) == 0)
{
IBindCtx ctx;
CreateBindCtx(0, out ctx);
string runningObjectName;
monikers[0].GetDisplayName(ctx, null, out runningObjectName);
if (runningObjectName.ToLower().Contains("solidworks"))
{
object runningObjectVal;
runningObjectTable.GetObject(monikers[0], out runningObjectVal);
// we should be safe to cast to our "real" solidworks object
SldWorks swObj = runningObjectVal as SldWorks;
if (swObj != null && swObj.GetProcessID() == PID)
{
return swObj;
}
}
}
return null;
}
/// <summary>
/// Creates a new instance of SOLIDWORKS
/// </summary>
/// <param name="suppressDialog">Suppress any dialogs.</param>
/// <param name="timeout">30 seconds for time out.</param>
/// <returns></returns>
public SldWorks GetNewInstance(string commandlineParameters = startSWNoJournalDialogAndSuppressAllDialogs, Year_e year = Year_e.Latest, int timeout = 30)
{
var swApp = Extension.CreateSldWorks(commandlineParameters, year, timeout);
return swApp;
}
/// <summary>
/// Attempts to restart SOLIDWORKS.
/// </summary>
/// <param name="swApp"></param>
public void RestartInstance(ref SldWorks swApp, string commandLineParameters = startSWNoJournalDialogAndSuppressAllDialogs, Year_e year_ = Year_e.Latest, int timeout = 30, int attempts = 5)
{
if (attempts <= 2)
attempts = 5;
if (swApp != null)
{
swApp.CloseAllDocuments(true);
swApp.ExitApp();
ReleaseInstance(swApp);
swApp = null;
}
for (int i = 1; i <= attempts; i++)
{
try
{
swApp = GetNewInstance(commandLineParameters, year_, timeout);
if (swApp != null)
break;
}
catch (System.TimeoutException e)
{
}
}
}
/// <summary>
/// Releases sldworks from memory since it is a com object not managed by garbage collector.
/// </summary>
/// <param name="swApp"></param>
public void ReleaseInstance(SldWorks swApp)
{
if (swApp != null)
System.Runtime.InteropServices.Marshal.ReleaseComObject(swApp);
}
}
}