-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
322 lines (281 loc) · 11 KB
/
Program.cs
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading;
using SolidEdgeFramework;
namespace SENomexLayers
{
public class MarshalHelper
{
public static object? GetActiveObject(string progId, bool throwOnError = false)
{
ArgumentNullException.ThrowIfNull(progId);
int hr = CLSIDFromProgIDEx(progId, out Guid clsid);
if (hr < 0)
{
if (throwOnError)
{
Marshal.ThrowExceptionForHR(hr);
}
return null;
}
hr = GetActiveObject(ref clsid, IntPtr.Zero, out object? obj);
if (hr < 0)
{
if (throwOnError)
{
Marshal.ThrowExceptionForHR(hr);
}
return null;
}
return obj;
}
[DllImport("ole32")]
private static extern int CLSIDFromProgIDEx([MarshalAs(UnmanagedType.LPWStr)] string lpszProgID, out Guid lpclsid);
[DllImport("oleaut32")]
private static extern int GetActiveObject(ref Guid rclsid, IntPtr pvReserved, [MarshalAs(UnmanagedType.IUnknown)] out object ppunk);
}
public class OleMessageFilter : IOleMessageFilter
{
public static void Register()
{
IOleMessageFilter newFilter = new OleMessageFilter();
IOleMessageFilter? oldFilter = null;
int iRetVal;
if (Thread.CurrentThread.GetApartmentState() == ApartmentState.STA)
{
iRetVal = CoRegisterMessageFilter(newFilter, out oldFilter);
}
else
{
throw new COMException("Unable to register message filter because the current thread apartment state is not STA.");
}
}
public static void Revoke()
{
IOleMessageFilter? oldFilter = null;
CoRegisterMessageFilter(null, out oldFilter);
}
public int HandleInComingCall(int dwCallType, IntPtr hTaskCaller, int dwTickCount, IntPtr lpInterfaceInfo)
{
return (int)SERVERCALL.SERVERCALL_ISHANDLED;
}
public int RetryRejectedCall(IntPtr hTaskCallee, int dwTickCount, int dwRejectType)
{
if (dwRejectType == (int)SERVERCALL.SERVERCALL_RETRYLATER)
{
return 99;
}
return -1;
}
public int MessagePending(IntPtr hTaskCallee, int dwTickCount, int dwPendingType)
{
return (int)PENDINGMSG.PENDINGMSG_WAITDEFPROCESS;
}
[DllImport("Ole32.dll")]
private static extern int CoRegisterMessageFilter(IOleMessageFilter? newFilter, out IOleMessageFilter oldFilter);
}
public enum SERVERCALL
{
SERVERCALL_ISHANDLED = 0,
SERVERCALL_REJECTED = 1,
SERVERCALL_RETRYLATER = 2
}
public enum PENDINGMSG
{
PENDINGMSG_CANCELCALL = 0,
PENDINGMSG_WAITNOPROCESS = 1,
PENDINGMSG_WAITDEFPROCESS = 2
}
[ComImport, Guid("00000016-0000-0000-C000-000000000046"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IOleMessageFilter
{
[PreserveSig]
int HandleInComingCall(int dwCallType, IntPtr hTaskCaller, int dwTickCount, IntPtr lpInterfaceInfo);
[PreserveSig]
int RetryRejectedCall(IntPtr hTaskCallee, int dwTickCount, int dwRejectType);
[PreserveSig]
int MessagePending(IntPtr hTaskCallee, int dwTickCount, int dwPendingType);
}
public class SolidEdgeConnector
{
public SolidEdgePart.SheetMetalDocument ConnectToSolidEdge(string filePath)
{
SolidEdgePart.SheetMetalDocument? objDocument = null;
try
{
OleMessageFilter.Register();
// Try to connect to a running instance of Solid Edge
Application? objApplication = (Application?)MarshalHelper.GetActiveObject("SolidEdge.Application");
if (objApplication != null)
{
// Ensure Solid Edge is in visible mode and display alerts are disabled
objApplication.Visible = true;
objApplication.DisplayAlerts = false;
// Open the document
objDocument = (SolidEdgePart.SheetMetalDocument)objApplication.Documents.Open(filePath);
}
else
{
// If no running instance, create a new instance of Solid Edge
objApplication = (Application?)Activator.CreateInstance(Type.GetTypeFromProgID("SolidEdge.Application")!);
if (objApplication != null)
{
// Ensure Solid Edge is in non-visible mode and display alerts are disabled
objApplication.Visible = false;
objApplication.DisplayAlerts = false;
// Open the document
objDocument = (SolidEdgePart.SheetMetalDocument)objApplication.Documents.Open(filePath);
}
else
{
throw new InvalidOperationException("Failed to create a new instance of SolidEdge.Application.");
}
}
}
finally
{
OleMessageFilter.Revoke();
}
return objDocument ?? throw new InvalidOperationException("Failed to open the document.");
}
public List<string> GetCustomProperties(string filePath)
{
var customPropertiesList = new List<string>();
try
{
var propertySets = new SolidEdgeFileProperties.PropertySets();
propertySets.Open(filePath, true);
foreach (SolidEdgeFileProperties.Properties properties in propertySets)
{
if (properties.Name == "Custom")
{
foreach (SolidEdgeFileProperties.Property property in properties)
{
customPropertiesList.Add($"{property.Name}: {property.Value}");
}
}
}
}
// If the custom sections happen to not be there, it's likely because the document is open in Solid Edge
catch (Exception ex)
{
Console.WriteLine($"Failed to retrieve file properties without opening the document: {ex.Message}");
// If failed, open the document in Solid Edge using SolidEdgeConnector Function
SolidEdgePart.SheetMetalDocument? objDocument = null;
Application? objApplication = null;
try
{
objDocument = ConnectToSolidEdge(filePath);
objApplication = objDocument?.Application;
if (objDocument != null)
{
var objPropSets = objDocument.Properties;
foreach (Properties objProps in objPropSets)
{
if (objProps.Name == "Custom")
{
for (int i = 1; i <= objProps.Count; i++)
{
var objProp = objProps.Item(i) as PropertyEx;
if (objProp != null)
{
var value = ((dynamic)objProp).Value;
customPropertiesList.Add($"{objProp.Name}: {value}");
}
}
}
}
}
else
{
Console.WriteLine($"Failed to open document: {filePath}");
}
}
catch (Exception innerEx)
{
Console.WriteLine($"Failed to retrieve custom properties by opening the document: {innerEx.Message}");
}
finally
{
objDocument?.Close(false);
if (objApplication != null)
{
objApplication.Quit();
Marshal.ReleaseComObject(objApplication);
}
}
}
return customPropertiesList;
}
public List<string> GetNomexLayers(List<string> uniqueCodes, string searchFolder)
{
var nomexLayers = new List<string>();
foreach (var uniqueCode in uniqueCodes)
{
var filePath = Path.Combine(searchFolder, $"{uniqueCode}.psm");
if (File.Exists(filePath))
{
try
{
// Attempt to get properties
var customProperties = GetCustomProperties(filePath);
if (customProperties != null)
{
foreach (var prop in customProperties)
{
if (prop.StartsWith("NOMEX_LAYERS"))
{
nomexLayers.Add($"{uniqueCode}: {prop}");
}
}
}
}
catch (Exception ex)
{
Console.WriteLine($"Failed to retrieve NOMEX_LAYERS for {uniqueCode}: {ex.Message}");
}
}
else
{
Console.WriteLine($"File not found: {filePath}");
}
}
return nomexLayers;
}
}
}
class Program
{
[STAThread]
static void Main(string[] args)
{
if (args.Length < 2)
{
Console.WriteLine("Insufficient arguments provided. Please provide the search folder followed by a comma-separated list of unique codes.");
return;
}
var searchFolder = args[0];
var uniqueCodes = new List<string>(args[1].Split(','));
var thread = new Thread(() =>
{
try
{
var solidEdgeConnector = new SENomexLayers.SolidEdgeConnector();
var nomexLayerValues = solidEdgeConnector.GetNomexLayers(uniqueCodes, searchFolder);
foreach (var value in nomexLayerValues)
{
Console.WriteLine(value);
}
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
}
});
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
thread.Join();
}
}