chr
2024-08-07 22beee93f14d042aa184148c53efb79e23416526
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
531
532
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using PdmSwPlugin.Common.Constants;
using PdmSwPlugin.Common.Entity;
 
// SolidWorks 的命名空间们
using SolidWorks.Interop.swdocumentmgr;
 
namespace PdmSwPlugin.Common.Util
{
    /// <summary>
    /// 通过 SolidWorks Document Management API (DM API) 得到文档属性的类
    /// </summary>
    public static class GetDocumentPropertiesViaDM
    {
        /// <summary>
        /// 凌创公司的 DM API 授权码
        /// </summary>
        public static string LinktronLicenseKey = @"SuzhouLinktronSystemsCoLtd:swdocmgr_general-11785-02051-00064-33793-08629-34307-00007-05128-58478-32321-57480-30765-00622-59999-16385-62752-47753-50558-31076-40652-29868-22926-54604-41449-54717-42289-44473-51665-47549-58701-53709-46485-03533-12733-37329-14337-29280-51290-50890-25690-25696-964";
 
        /// <summary>
        /// 经常使用的属性名
        /// 考虑到 SolidWorks 里的自定义属性名可能大小写比较随意,比较的时候请注意大小写
        /// </summary>
        public static string[] PropertyNamesOftenUsed = new string[]
        {
            // 图号,物料编码
            PropertyName.PART_NO, 
            // 物料型号
            PropertyName.PART_MODEL,
            // 物料名称
            PropertyName.PART_NAME,
            // 加工件类型
            PropertyName.JGJ_TYPE,
            // 材料名称
            PropertyName.STUFF_TYPE,
            // 表面处理
            PropertyName.SURFACE_TYPE,
            // 热处理
            PropertyName.HEAT_TYPE,
        };
 
        /// <summary>
        /// 检索一个 SolidWorks 文档里指定的属性值
        /// </summary>
        /// <param name="sldWorksPath">指定的 SolidWorks 文档</param>
        /// <param name="properties">指定的属性的名称,如果为空将返回全部</param>
        /// <param name="resolve">是否把一些表达式还原为字符串,例如如果图号设置为 $PROP:FileName, 那就还原为文件名</param>
        /// <returns>指定的属性和它们的值的集合</returns>
        public static NameValueCollection RetrieveProperties(string sldWorksPath, IEnumerable<string> properties, bool resolve)
        {
            string originalExt;
            SwDmDocumentType docType = DMSldFileExtentionChecker.CheckDM(sldWorksPath, out originalExt);
            if (docType == SwDmDocumentType.swDmDocumentUnknown)
            {
                return null;
            }
            SwDMClassFactory swDMClassFactory = new SwDMClassFactory();
            SwDMApplication swDMApp = swDMClassFactory.GetApplication(LinktronLicenseKey);
            SwDmDocumentOpenError returnValue = 0;
            SwDMDocument17 swDoc = (SwDMDocument17)swDMApp.GetDocument(sldWorksPath, docType, true, out returnValue); // 允许只读
            bool originallyReadOnly = false; // 文件最开始是只读的吗?
            FileAttributes fileAttributes = FileAttributes.Offline; // 文件属性,给任意初始值
            if (returnValue == SwDmDocumentOpenError.swDmDocumentOpenErrorFileReadOnly)
            { // 由于打开时允许只读,理论上不会运行到这里
                originallyReadOnly = true;
                fileAttributes = File.GetAttributes(sldWorksPath); // 保留文件最开始的属性
                File.SetAttributes(sldWorksPath, FileAttributes.Normal); // 设置文件属性为正常
                swDoc = (SwDMDocument17)swDMApp.GetDocument(sldWorksPath, docType, true, out returnValue); // 重新打开文档
            }
            if (swDoc == null || returnValue != SwDmDocumentOpenError.swDmDocumentOpenErrorNone)
            {
                Marshal.ReleaseComObject(swDMApp);
                Marshal.ReleaseComObject(swDMClassFactory);
                return null;
            }
            SwDMConfigurationMgr dmConfigMgr = swDoc.ConfigurationManager;
            //NameValueCollection result = RetrieveProperties_Internal(sldWorksPath, docType, dmConfigMgr, properties, resolve);
            NameValueCollection result = RetrieveProperties_Core(sldWorksPath, swDoc, properties, resolve);
            Marshal.ReleaseComObject(swDoc);
            Marshal.ReleaseComObject(swDMApp);
            Marshal.ReleaseComObject(swDMClassFactory);
            if (originallyReadOnly)
            { // 如果文件本来是只读的,在得到自定义属性之后再恢复回去
                File.SetAttributes(sldWorksPath, fileAttributes);
            }
            return result;
        }
 
        /// <summary>
        /// 检索一个 SolidWorks 文档里指定的属性值
        /// 只从当前活动配置里检索自定义属性
        /// </summary>
        /// <param name="sldWorksPath">指定的 SolidWorks 文档</param>        
        /// <param name="doc">文档</param>
        /// <param name="properties">指定的属性的名称,如果为空将返回全部</param>
        /// <param name="resolve">是否把一些表达式还原为字符串,例如如果图号设置为 $PROP:FileName, 那就还原为文件名</param>
        /// <returns>指定的属性和它们的值的集合</returns>
        private static NameValueCollection RetrieveProperties_Core(string sldWorksPath, SwDMDocument17 doc, IEnumerable<string> properties, bool resolve)
        {
            if (doc == null)
                return null;
 
            object names;
            object types;
            object links;
            object values;
            doc.GetAllCustomPropertyNamesAndValues(out names, out types, out links, out values);
            if (names == null || values == null)
                return null;
            NameValueCollection collection = new NameValueCollection();
            List<string> expectedPropertiesInLowerCase = new List<string>();
            if (properties != null)
                expectedPropertiesInLowerCase.AddRange(properties.Select(x => x.ToLower())); // 期望的属性名称都变成小写
            string[] propNames = names as string[];
            string[] propValues = values as string[];
            if (propNames.Length != propValues.Length)
                return null; // 数量不可能不一样
            for (int i = 0; i < propNames.Length; i++)
            {
                if (expectedPropertiesInLowerCase.Count == 0) // 没有设置期望的属性名称,那就是所有的属性都要返回
                    collection.Add(propNames[i], resolve ? ResolvePropertyValue(sldWorksPath, propValues[i]) : propValues[i]);
                else if (expectedPropertiesInLowerCase.Contains(propNames[i].ToLower())) // 只要期望的属性
                    collection.Add(propNames[i], resolve ? ResolvePropertyValue(sldWorksPath, propValues[i]) : propValues[i]);
            }
            return collection;
        }
 
        /// <summary>
        /// 检索一个 SolidWorks 文档里指定的属性值
        /// 只从当前活动配置里检索自定义属性
        /// 废弃,因为工程图没有配置,请使用 RetrieveProperties_Core
        /// </summary>
        /// <param name="sldWorksPath">指定的 SolidWorks 文档</param>        
        /// <param name="docType">文档类型</param>
        /// <param name="dmConfig">文档配置管理器</param>
        /// <param name="properties">指定的属性的名称,如果为空将返回全部</param>
        /// <param name="resolve">是否把一些表达式还原为字符串,例如如果图号设置为 $PROP:FileName, 那就还原为文件名</param>
        /// <returns>指定的属性和它们的值的集合</returns>
        private static NameValueCollection RetrieveProperties_Internal(string sldWorksPath, SwDmDocumentType docType, SwDMConfigurationMgr dmConfig, IEnumerable<string> properties, bool resolve)
        {
            if (dmConfig == null)
                return null;
            SwDMConfiguration14 activeCfg = null;
            if (docType != SwDmDocumentType.swDmDocumentDrawing)
            {
                string activeConfigName = dmConfig.GetActiveConfigurationName();
                string[] configuration = (string[])dmConfig.GetConfigurationNames();
                activeCfg = (SwDMConfiguration14)dmConfig.GetConfigurationByName(activeConfigName);
            }
            else
            { // 工程图不能得到配置名的,因为它就没有配置,所以本方法只能废弃
                activeCfg = (SwDMConfiguration14)dmConfig.GetConfigurationByName(string.Empty);
            }
            if (activeCfg == null)
                return null;
            string[] allPropNameArr = (string[])activeCfg.GetCustomPropertyNames();
            if (allPropNameArr == null)
                return null;
            NameValueCollection collection = new NameValueCollection();
            List<string> expectedPropertiesInLowerCase = new List<string>();
            if (properties != null)
                expectedPropertiesInLowerCase.AddRange(properties.Select(x => x.ToLower())); // 期望的属性名称都变成小写
            foreach (string custPropName in allPropNameArr)
            {
                SwDmCustomInfoType propType = SwDmCustomInfoType.swDmCustomInfoUnknown;
                string propStr = activeCfg.GetCustomProperty2(custPropName, out propType);
                if (expectedPropertiesInLowerCase.Count == 0) // 没有设置期望的属性名称,那就是所有的属性都要返回
                    collection.Add(custPropName, resolve ? ResolvePropertyValue(sldWorksPath, propStr) : propStr);
                else if (expectedPropertiesInLowerCase.Contains(custPropName.ToLower())) // 只要期望的属性
                    collection.Add(custPropName, resolve ? ResolvePropertyValue(sldWorksPath, propStr) : propStr);
            }
            return collection;
        }
 
        /// <summary>
        /// 还原属性值,例如属性值是个表达式 $PROP:FileName,那就还原成不带路径和后缀的文件名
        /// </summary>
        /// <param name="sldWorksPath">指定的 SolidWorks 文档</param>
        /// <param name="originalPropertyValue">原始的属性值</param>
        /// <returns>还原后的属性值</returns>
        public static string ResolvePropertyValue(string sldWorksPath, string originalPropertyValue)
        {
            if (string.IsNullOrEmpty(originalPropertyValue))
                return originalPropertyValue;
            if (originalPropertyValue.ToLower().StartsWith("$prp:")) // 一般是 $PRP:SW File Name
            {
                return Path.GetFileNameWithoutExtension(sldWorksPath);
            }
            else
            {
                return originalPropertyValue;
            }
        }
 
        /// <summary>
        /// 没有物料编码时用此字符串表示,原计划用空字符串,但也不好
        /// </summary>
        public static string NoDrawNoOfCustomProperty = @"[无物料编码]";
 
        /// <summary>
        /// 得到图纸的图号,也就是得到模型的物料编码 (DrawNo 自定义属性)
        /// </summary>
        /// <param name="modelFileName">模型文件</param>
        /// <returns>模型的物料编码</returns>
        public static string GetDrawNo(string modelFileName)
        {
            NameValueCollection nameValueCollection = RetrieveProperties(modelFileName, PropertyNamesOftenUsed.Take(1), true);
            if (nameValueCollection != null && nameValueCollection.Count == 1)
                return nameValueCollection[0];
            else
                return string.Empty;
        }
 
        /// <summary>
        /// 得到一个装配体下的所有一级部件,不用递归
        /// </summary>
        /// <param name="assemblyPath">装配体的路径</param>
        /// <param name="configurationName">配置名,如果为空就取默认的配置</param>
        /// <returns>一级子部件</returns>
        public static List<string> GetComponentsWithoutRecursion(string assemblyPath, string configurationName)
        {
            if (string.IsNullOrEmpty(assemblyPath))
                return null;
            if (!File.Exists(assemblyPath))
                return null;
            string originalExt;
            SwDmDocumentType docType = DMSldFileExtentionChecker.CheckDM(assemblyPath, out originalExt);
            if (docType != SwDmDocumentType.swDmDocumentAssembly)
            {
                return null;
            }
            SwDMClassFactory swDMClassFactory = new SwDMClassFactory();
            SwDMApplication swDMApp = swDMClassFactory.GetApplication(LinktronLicenseKey);
            SwDmDocumentOpenError returnValue = 0;
            SwDMDocument17 swDoc = (SwDMDocument17)swDMApp.GetDocument(assemblyPath, docType, true, out returnValue); // 允许只读
            if (swDoc == null || returnValue != SwDmDocumentOpenError.swDmDocumentOpenErrorNone)
            {
                Marshal.ReleaseComObject(swDMApp);
                Marshal.ReleaseComObject(swDMClassFactory);
                return null;
            }
            SwDMConfigurationMgr dmConfigMgr = swDoc.ConfigurationManager;
            string[] configurationNames = (string[])dmConfigMgr.GetConfigurationNames();
            if (configurationNames == null || configurationNames.Length <= 0)
            { // 装配体至少会有一个配置的
                Marshal.ReleaseComObject(dmConfigMgr);
                Marshal.ReleaseComObject(swDoc);
                Marshal.ReleaseComObject(swDMApp);
                Marshal.ReleaseComObject(swDMClassFactory);
                return null;
            }
            string configNameToOpen = null;
            if (string.IsNullOrEmpty(configurationName))
            { // 即将打开的配置名
                configNameToOpen = dmConfigMgr.GetActiveConfigurationName();
            }
            else
            {
                configNameToOpen = configurationName;
            }
            SwDMConfiguration14 activeCfg = (SwDMConfiguration14)dmConfigMgr.GetConfigurationByName(configNameToOpen);
            if (activeCfg == null)
                return null;
            object[] all = (object[])activeCfg.GetComponents();
            List<string> names = new List<string>();
            if (all != null)
            {
                foreach (object o in all)
                {
                    SwDMComponent9 component = o as SwDMComponent9;
                    names.Add(component.PathName);
                }
            }
 
            Marshal.ReleaseComObject(activeCfg);
            Marshal.ReleaseComObject(dmConfigMgr);
            Marshal.ReleaseComObject(swDoc);
            Marshal.ReleaseComObject(swDMApp);
            Marshal.ReleaseComObject(swDMClassFactory);
            return names;
        }
 
        /// <summary>
        /// 递归得到一个装配体或零件下的所有部件的信息
        /// </summary>
        /// <param name="assemblyPath">装配体或零件的路径</param>
        /// <param name="configurationName">配置名,如果为空就取默认的配置</param>
        /// <returns>装配体下的所有部件,包括所有子孙,递归得到的</returns>
        public static List<SldComponentInfo_DM> GetComponentsRecursively(string assemblyPath, string configurationName)
        {
            if (string.IsNullOrEmpty(assemblyPath))
                return null;
            if (!File.Exists(assemblyPath))
                return null;
            string originalExt;
            SwDmDocumentType docType = DMSldFileExtentionChecker.CheckDM(assemblyPath, out originalExt);
            if (docType != SwDmDocumentType.swDmDocumentAssembly && docType != SwDmDocumentType.swDmDocumentPart)
            {
                return null;
            }
            SwDMClassFactory swDMClassFactory = new SwDMClassFactory();
            SwDMApplication swDMApp = swDMClassFactory.GetApplication(LinktronLicenseKey);
            SwDmDocumentOpenError returnValue = 0;
            SwDMDocument17 swDoc = (SwDMDocument17)swDMApp.GetDocument(assemblyPath, docType, true, out returnValue); // 允许只读
            if (swDoc == null || returnValue != SwDmDocumentOpenError.swDmDocumentOpenErrorNone)
            {
                Marshal.ReleaseComObject(swDMApp);
                Marshal.ReleaseComObject(swDMClassFactory);
                return null;
            }
            SwDMConfigurationMgr dmConfigMgr = swDoc.ConfigurationManager;
            string[] configurationNames = (string[])dmConfigMgr.GetConfigurationNames();
            if (configurationNames == null || configurationNames.Length <= 0)
            { // 装配体至少会有一个配置的
                Marshal.ReleaseComObject(dmConfigMgr);
                Marshal.ReleaseComObject(swDoc);
                Marshal.ReleaseComObject(swDMApp);
                Marshal.ReleaseComObject(swDMClassFactory);
                return null;
            }
            string configNameToOpen = null;
            if (string.IsNullOrEmpty(configurationName))
            { // 即将打开的配置名
                configNameToOpen = dmConfigMgr.GetActiveConfigurationName();
            }
            else
            {
                configNameToOpen = configurationName;
            }
            SwDMConfiguration14 activeCfg = (SwDMConfiguration14)dmConfigMgr.GetConfigurationByName(configNameToOpen);
            if (activeCfg == null)
                return null;
            List<SldComponentInfo_DM> all = new List<SldComponentInfo_DM>();
            SldComponentInfo_DM root = GetTopDocumentInfo(ref all, assemblyPath, swDoc, activeCfg); // 一般不会返回空的
            if (root == null)
                throw new InvalidOperationException("GetComponentsRecursively: 不可能的错误,没有遍历到顶级文档的信息");
            object[] allComponents = activeCfg.GetComponents();
            if (allComponents != null)
            {
                root.ChildrenCount = allComponents.Length;
                foreach (object o in allComponents)
                {
                    SwDMComponent9 subComponent = o as SwDMComponent9;
                    GetComponentInfoWithChildren(ref all, subComponent, 1, root.Id, swDMApp);
                }
            }
            root.ConfigurationCount = configurationNames.Length;
 
            Marshal.ReleaseComObject(activeCfg);
            Marshal.ReleaseComObject(dmConfigMgr);
            Marshal.ReleaseComObject(swDoc);
            Marshal.ReleaseComObject(swDMApp);
            Marshal.ReleaseComObject(swDMClassFactory);
            return all;
        }
 
        /// <summary>
        /// 检索当前 SolidWorks 配置下的所有属性值
        /// </summary>
        /// <param name="currentConfig">当前配置</param>
        /// <param name="fullPath">全路径</param>
        /// <returns>指定的属性和它们的值的集合</returns>
        public static NameValueCollection RetrieveAllProperties(SwDMConfiguration14 currentConfig, string fullPath)
        {
            if (currentConfig == null)
                return null;
            NameValueCollection result = RetrievePropertiesUnderConfig(fullPath, currentConfig, null, true);
            return result;
        }
 
        /// <summary>
        /// 得到一个部件的信息
        /// 仅仅 ChildrenCount, ConfigurationCount 没有设置,其它属性都设置了
        /// 没有递归,仅仅是本部件,不牵涉任何其它的
        /// </summary>
        /// <param name="componentInfo">当前部件的信息,一定不会为空的</param>
        /// <param name="currentComponent">当前部件</param>
        /// <param name="currentConfig">当前配置</param>
        /// <param name="level">当前部件的层级</param>
        /// <param name="parentId">父节点的 Id</param>
        /// <param name="swDMApplication">当前的 SolidWorks DM 应用</param>
        /// <returns>成功与否</returns>
        public static bool GetCurrentComponentInfo(
            ref SldComponentInfo_DM componentInfo,
            SwDMComponent9 currentComponent,
            SwDMConfiguration14 currentConfig,
            int level,
            int parentId,
            SwDMApplication swDMApplication)
        {
            if (componentInfo == null || currentComponent == null || currentConfig == null || level < 0)
                return false;
            componentInfo.ParentId = parentId;
            componentInfo.Level = level;
            componentInfo.FullPath = currentComponent.PathName;
            componentInfo.FeatureName = currentComponent.Name2;
            componentInfo.FeatureId = currentComponent.GetID();
            componentInfo.IsSuppressed = currentComponent.IsSuppressed();
            componentInfo.FileName2D = EngineeringDrawingFile.Get2DFileNameIfExisting(componentInfo.FullPath);
            componentInfo.Visible = !currentComponent.IsHidden();
            if (File.Exists(componentInfo.FullPath))
                componentInfo.AllCustomProperties = RetrieveAllProperties(currentConfig, componentInfo.FullPath);
            return true;
        }
 
        /// <summary>
        /// 得到一个部件及其子部件的信息,递归调用
        /// </summary>
        /// <param name="componentsInfo">当前所有部件的信息,一定不会为空的,新的部件将加入本列表</param>
        /// <param name="currentComponent">当前部件</param>
        /// <param name="level">当前部件的层级</param>
        /// <param name="parentId">父节点的 Id</param>
        /// <param name="swDMApplication">当前的 SolidWorks DM 应用</param>
        /// <returns>成功与否</returns>
        private static bool GetComponentInfoWithChildren(
            ref List<SldComponentInfo_DM> componentsInfo,
            SwDMComponent9 currentComponent,
            int level,
            int parentId,
            SwDMApplication swDMApplication)
        {
            if (componentsInfo == null || currentComponent == null || level <= 0 || swDMApplication == null)
                return false;
            string fullPath = currentComponent.PathName;
            string originalExt;
            SwDmDocumentType docType = DMSldFileExtentionChecker.CheckDM(fullPath, out originalExt);
            SwDmDocumentOpenError returnValue = SwDmDocumentOpenError.swDmDocumentOpenErrorFail;
            SwDMDocument17 swDoc = (SwDMDocument17)swDMApplication.GetDocument(fullPath, docType, true, out returnValue); // 允许只读
            if (swDoc == null || returnValue != SwDmDocumentOpenError.swDmDocumentOpenErrorNone)
                return true;
            SwDMConfigurationMgr swDMConfigurationMgr = swDoc.ConfigurationManager;
            string activeConfigName = swDMConfigurationMgr.GetActiveConfigurationName();
            SwDMConfiguration14 activeConfig = swDMConfigurationMgr.GetConfigurationByName(activeConfigName) as SwDMConfiguration14;
 
            SldComponentInfo_DM top = SldComponentInfo_DM.GenerateTheNext();
            GetCurrentComponentInfo(ref top, currentComponent, activeConfig, level, parentId, swDMApplication);
            componentsInfo.Add(top); // 先把根节点放进列表
 
            top.ConfigurationCount = swDoc.ConfigurationManager.GetConfigurationCount();
            //if (!File.Exists(top.FullPath))
            //  return true;
 
            object[] all = (object[])activeConfig.GetComponents();
            List<string> names = new List<string>();
            if (all != null)
            {
                top.ChildrenCount = all.Length;
                foreach (object o in all)
                {
                    SwDMComponent9 subComponent = o as SwDMComponent9;
                    GetComponentInfoWithChildren(ref componentsInfo, subComponent, level + 1, top.Id, swDMApplication); // 递归把子孙都给加载进去
                }
            }
 
            Marshal.ReleaseComObject(activeConfig);
            Marshal.ReleaseComObject(swDMConfigurationMgr);
            Marshal.ReleaseComObject(swDoc);
            return true;
        }
 
        /// <summary>
        /// 得到顶级文档的信息,没有递归调用
        /// </summary>
        /// <param name="componentsInfo">当前所有部件的信息,一定不会为空的,新的部件将加入本列表</param>
        /// <param name="fullPath">当前顶级文档的全路径</param>
        /// <param name="currentDocument">当前顶级文档</param>
        /// <param name="currentConfig">当前的配置</param>
        /// <param name="swDMApplication">当前的 SolidWorks DM 应用</param>
        /// <returns>顶级部件信息</returns>
        private static SldComponentInfo_DM GetTopDocumentInfo(
            ref List<SldComponentInfo_DM> componentsInfo,
            string fullPath, SwDMDocument17 currentDocument,
            SwDMConfiguration14 currentConfig)
        {
            if (componentsInfo == null || string.IsNullOrEmpty(fullPath) || currentDocument == null || currentConfig == null)
                return null;
            SldComponentInfo_DM root = SldComponentInfo_DM.GenerateTheRoot();
            root.FullPath = fullPath;
            root.FeatureName = currentDocument.Title;
            //root.FeatureId = 0;
            //root.IsSuppressed = false;
            //root.FileName2D = null;
            //root.Visible = true;
            if (File.Exists(fullPath))
                root.AllCustomProperties = RetrievePropertiesUnderConfig(fullPath, currentConfig, null, true);
            componentsInfo.Add(root);
            return root;
        }
 
        /// <summary>
        /// 检索一个 SolidWorks 文档里指定配置下的指定的属性值
        /// </summary>
        /// <param name="sldWorksPath">指定的 SolidWorks 文档</param>        
        /// <param name="config">配置</param>
        /// <param name="properties">指定的属性的名称,如果为空将返回全部</param>
        /// <param name="resolve">是否把一些表达式还原为字符串,例如如果图号设置为 $PRP:FileName, 那就还原为文件名</param>
        /// <returns>指定的属性和它们的值的集合</returns>
        public static NameValueCollection RetrievePropertiesUnderConfig(
            string sldWorksPath,
            SwDMConfiguration14 config,
            IEnumerable<string> properties,
            bool resolve)
        {
            if (config == null)
                return null;
 
            string[] allPropNameArr = (string[])config.GetCustomPropertyNames();
            if (allPropNameArr == null)
                return null;
 
            NameValueCollection collection = new NameValueCollection();
            List<string> expectedPropertiesInLowerCase = new List<string>();
            if (properties != null)
                expectedPropertiesInLowerCase.AddRange(properties.Select(x => x.ToLower())); // 期望的属性名称都变成小写
            foreach (var v in allPropNameArr)
            {
                SwDmCustomInfoType propType = SwDmCustomInfoType.swDmCustomInfoUnknown;
                string propStr = config.GetCustomProperty2(v, out propType);
                if (expectedPropertiesInLowerCase.Count == 0) // 没有设置期望的属性名称,那就是所有的属性都要返回
                    collection.Add(v, resolve ? ResolvePropertyValue(sldWorksPath, propStr) : propStr);
                else if (expectedPropertiesInLowerCase.Contains(v.ToLower())) // 只要期望的属性
                    collection.Add(v, resolve ? ResolvePropertyValue(sldWorksPath, propStr) : propStr);
            }
            return collection;
        }
    }
}