/
SldWorksExtension.cs
342 lines (311 loc) · 12.1 KB
/
SldWorksExtension.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
//using stdole;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media.Imaging;
namespace Du.SolidWorks.Extension
{
/// <summary>
/// Extension methods for <see cref="ISldWorks"/> Interface
/// </summary>
public static class SldWorksExtension
{
/// <summary>
/// 对内存中所有文档进行操作
/// </summary>
/// <param name="swApp"></param>
/// <param name="action"></param>
public static void UsingOpenDoc(this SldWorks swApp, Action<IModelDoc2> action)
{
var docs = (swApp.GetDocuments() as Object[]).Cast<IModelDoc2>();
if (docs == null)
{
return;
}
foreach (var item in docs)
{
action?.Invoke(item);
}
}
/// <summary>
/// 判断此文档是否打开
/// </summary>
/// <param name="app"></param>
/// <param name="DocFilePathName"></param>
/// <returns></returns>
public static bool HasDocOpened(this SldWorks app, string DocFilePathName)
{
bool exist = false;
app.UsingOpenDoc(d =>
{
if (d.GetPathName() == DocFilePathName)
{
exist = true;
}
}
);
return exist;
}
/// <summary>
/// 对活动文档进行操作
/// </summary>
/// <param name="swApp"></param>
/// <param name="action"></param>
public static void UsingActiveDoc(this SldWorks swApp, Action<IModelDoc2> action)
{
if (swApp.ActiveDoc != null)
{
action?.Invoke((IModelDoc2)swApp.ActiveDoc);
}
}
/// <summary>
/// 以不可见模式打开文档 Open a document invisibly. It will not be shown to the user but you will be
/// able to interact with it through the API as if it is loaded.
/// </summary>
/// <param name="sldWorks"></param>
/// <param name="toolFile"></param>
/// <param name="visible">是否对用户可见</param>
/// <param name="type"><see cref="swDocumentTypes_e"/> SolidWorks 文件类型 ,默认为零件</param>
/// <returns></returns>
public static ModelDoc2 OpenInvisibleReadOnly(this ISldWorks sldWorks, string toolFile, bool visible = false, swDocumentTypes_e type = swDocumentTypes_e.swDocPART)
{
try
{
if (!visible)
sldWorks.DocumentVisible(false, (int)type);
var spec = (IDocumentSpecification)sldWorks.GetOpenDocSpec(toolFile);
if (!visible)
{
spec.Silent = true;
spec.ReadOnly = true;
}
var doc = sldWorks.OpenDoc7(spec);
doc.Visible = visible;
return doc;
}
finally
{
if (!visible)
sldWorks.DocumentVisible
(true,
(int)
type);
}
}
/// <summary>
/// 静默模式打开隐藏文件
/// </summary>
/// <param name="app"><see cref="ISldWorks"/></param>
/// <param name="filePath">文件路径</param>
/// <param name="Hidden">是否隐藏 默认隐藏</param>
/// <returns></returns>
public static ModelDoc2 OpenInvisibleDocClient(this SldWorks app, string filePath, bool Hidden = true)
{
int Errors = -1, Warning = -1;
var type = app.FileType(filePath);
try
{
if (Hidden) app.DocumentVisible(false, (int)type);
ModelDoc2 doc = app.OpenDoc6(filePath, type.SWToInt(), swOpenDocOptions_e.swOpenDocOptions_Silent.SWToInt(),
"", ref Errors, ref Warning) as ModelDoc2;
if (doc == null)
{
throw new Exception(string.Format("errors:{0},warings{1}",
Errors.CastObj<swFileLoadError_e>().ToString(),
Warning.CastObj<swFileLoadWarning_e>().ToString()));
}
return doc;
}
catch (Exception)
{
throw;
}
finally
{
app.DocumentVisible(true, (int)type);
}
}
/// <summary>
/// 判断文件类型
/// </summary>
/// <param name="app"><see cref="ISldWorks"/></param>
/// <param name="filePath">文件全路径</param>
/// <returns></returns>
public static swDocumentTypes_e FileType(this SldWorks app, string filePath)
{
string extension = Path.GetExtension(filePath).ToLower();
swDocumentTypes_e type = swDocumentTypes_e.swDocPART;
switch (extension)
{
case ".sldprt":
type = swDocumentTypes_e.swDocPART;
break;
case ".sldasm":
type = swDocumentTypes_e.swDocASSEMBLY;
break;
case ".slddrw":
type = swDocumentTypes_e.swDocDRAWING;
break;
default:
throw new FileFormatException("不支持的文件类型:" + extension);
}
return type;
}
/// <summary>
/// 静默加载 Iges文件 Tries to load an iges file invisibly. Throws an exception if it doesn't work.
/// </summary>
/// <param name="sldWorks"></param>
/// <param name="igesFile"></param>
/// <param name="visible"></param>
/// <returns></returns>
public static ModelDoc2 LoadIgesInvisible(this ISldWorks sldWorks, string igesFile, bool visible = false)
{
var swDocPart = (int)swDocumentTypes_e.swDocPART;
try
{
if (!visible)
sldWorks.DocumentVisible(false, swDocPart);
ImportIgesData swImportData =
(ImportIgesData)sldWorks.GetImportFileData(igesFile);
int err = 0;
var newDoc = sldWorks.LoadFile4(igesFile, "r", swImportData, ref err);
if (err != 0)
throw new Exception(@"Unable to load file {igesFile");
return newDoc;
}
finally
{
if (!visible)
sldWorks.DocumentVisible
(true,
swDocPart);
}
}
/// <summary>
/// 根据模板创建文档
/// </summary>
/// <param name="sldWorks"><see cref="ISldWorks"/></param>
/// <param name="templatePath">模板路径 支持的文件类型为<para>.prtdot</para><para>.asmdot</para><para>.drwdot</para></param>
/// <param name="hidden">是否对用户可见 默认为不可见</param>
/// <returns><see cref="ModelDoc2"/></returns>
public static ModelDoc2 CreateDocument
(this ISldWorks sldWorks, string templatePath,bool hidden = true)
{
if (!File.Exists(templatePath))
{
throw new FileNotFoundException(string.Format("未找到此种类型的模板文件"));
}
var type = SWPath.GetTemFileType(templatePath);
try
{
if (hidden)
sldWorks.DocumentVisible(false, (int)type);
//TODO:设置替换字符串
var doc = (ModelDoc2)sldWorks.NewDocument(templatePath, (int)swDwgPaperSizes_e.swDwgPaperA4size, 1, 1);
return doc;
}
finally
{
if (hidden)
sldWorks.DocumentVisible
(true,
(int)
type);
}
}
/// <summary>
/// 以默认设置的模板创建文档
/// </summary>
/// <param name="sldWorks"><see cref="ISldWorks"/></param>
/// <param name="type"><see cref="swDocumentTypes_e"/></param>
/// <returns></returns>
public static IModelDoc2 CreateDocument(this ISldWorks sldWorks, swDocumentTypes_e type)
{
string templatePath = string.Empty;
switch (type)
{
case swDocumentTypes_e.swDocPART:
templatePath = sldWorks.GetUserPreferenceStringValue((int)swUserPreferenceStringValue_e.swDefaultTemplatePart);
break;
case swDocumentTypes_e.swDocASSEMBLY:
templatePath = sldWorks.GetUserPreferenceStringValue((int)swUserPreferenceStringValue_e.swDefaultTemplateAssembly);
break;
case swDocumentTypes_e.swDocDRAWING:
templatePath = sldWorks.GetUserPreferenceStringValue((int)swUserPreferenceStringValue_e.swDefaultTemplateDrawing);
break;
default:
break;
}
if (!File.Exists(templatePath))
{
templatePath =
Path.GetDirectoryName(templatePath) + "\\" +
Path.GetFileName(templatePath).
Replace("零件", "gb_part").
Replace("装配体", "gb_assembly").Replace("工程图", "gb_a1");
}
if (!File.Exists(templatePath))
{
throw new FileNotFoundException("无法找到SolidWorks文件--" + templatePath);
}
return sldWorks.CreateDocument(templatePath);
}
/// <summary>
/// 将 <see cref="IModelDoc2"/> 转换为 IModelDoc2
/// </summary>
/// <param name="app"><see cref="ISldWorks"/></param>
/// <returns></returns>
public static IModelDoc2 ActiveDocEx(this SldWorks app)
{
return app.ActiveDoc as ModelDoc2;
}
/// <summary>
/// 通过模板创建新零件或者新项目
/// </summary>
/// <param name="app"></param>
/// <param name="TemFilePath">*.asmdot 或者 *.prtdot</param>
/// <returns></returns>
public static ModelDoc2 NewPartOrAssembly(this SldWorks app, string TemFilePath)
{
return app.NewDocument(TemFilePath, (int)swDwgPaperSizes_e.swDwgPaperA4size, 100, 100) as ModelDoc2;
}
/// <summary>
/// 设置一个选项后执行动作,执行完后设置回系统默认值
/// </summary>
/// <param name="app">ISldwork interface</param>
/// <param name="toggleSetting">需要设置的枚举值</param>
/// <param name="value">值</param>
/// <param name="action">执行的动作</param>
public static void WithToggleState(this ISldWorks app, swUserPreferenceToggle_e toggleSetting, bool value, Action action)
{
try
{
bool fileLockState = app.GetUserPreferenceToggle(toggleSetting.SWToInt());
app.SetUserPreferenceToggle(swUserPreferenceToggle_e.swLockRecentDocumentsList.SWToInt(), true);
action?.Invoke();
}
catch (Exception)
{
throw;
}
finally
{
app.SetUserPreferenceToggle(toggleSetting.SWToInt(), value);
}
}
//public static BitmapImage GetPreViewBitMapImage(this SldWorks app,string fileName,string configName)
//{
// var data = app.GetPreviewBitmap(fileName, configName);
// if (data == null)
// {
// return null;
// }
// return IPictureDispHelper.GetBitmapFromImage(IPictureDispHelper.PictureDispToImage((IPictureDisp)data));
//}
}
}