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
using System.Collections.Generic;
 
using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
 
namespace PdmSwPlugin.Common.Util
{
    /// <summary>
    /// 为 SolidWorks 文档得到或设置自定义属性的类
    /// </summary>
    public static class CustomerPropertiesWrapper
    {
        /// <summary>
        /// 从指定 SolidWorks 文档得到当前配置里的自定义属性管理器
        /// </summary>
        /// <param name="currentModelDoc">SolidWorks 文档</param>
        /// <returns>自定义属性管理器</returns>
        public static CustomPropertyManager GetCustomPropertyManager(ModelDoc2 currentModelDoc)
        {
            if (currentModelDoc == null)
                return null;
            try
            {
                if (currentModelDoc.GetType() == (int)swDocumentTypes_e.swDocDRAWING)
                {
                    return currentModelDoc.Extension.CustomPropertyManager[""];
                }
                else
                {
                    Configuration activeConfig = currentModelDoc.GetActiveConfiguration() as Configuration;
                    if (activeConfig == null)
                        return null;
                    return currentModelDoc.Extension.CustomPropertyManager[activeConfig.Name];
                }
            }
            catch
            {
            }
            return null;
        }
 
        /// <summary>
        /// 得到指定 SolidWorks 文档的当前配置里的属性的值
        /// </summary>
        /// <param name="currentModelDoc">SolidWorks 文档</param>
        /// <param name="propertiesNames">属性名称</param>
        /// <param name="fullyResolve">完全还原属性的值吗?指的是如果属性值为 $PRP:SW - FileName,是否要还原成不带后缀的文件名</param>
        /// <returns>属性的值, key 是名称,value 是值</returns>
        public static Dictionary<string, string> Get(ModelDoc2 currentModelDoc, IEnumerable<string> propertiesNames, bool fullyResolve)
        {
            if (currentModelDoc == null || propertiesNames == null)
                return null;
            try
            {
                CustomPropertyManager customPropertyManager = GetCustomPropertyManager(currentModelDoc);
                if (customPropertyManager == null)
                {
                    return null;
                }
                string valueOut = string.Empty;
                string resolvedValueOut = string.Empty;
                bool resolved = false;
                Dictionary<string, string> propertiesValue = new Dictionary<string, string>();
                foreach (var v in propertiesNames)
                {
                    if (customPropertyManager.Get5(v, false, out valueOut, out resolvedValueOut, out resolved) != (int)swCustomInfoGetResult_e.swCustomInfoGetResult_NotPresent)
                    {
                       if (valueOut == "$PRP:\"SW-File Name\"") // 如果是表达式,需要 resolve
                        {
                            if (fullyResolve)
                            {
                                //propertiesValue[v] = resolvedValueOut;
                                string fileNameWithoutExt = System.IO.Path.GetFileNameWithoutExtension(currentModelDoc.GetPathName());
                                propertiesValue[v] = fileNameWithoutExt;
                            }
                            else
                                propertiesValue[v] = valueOut;
                        }
                        else if (resolved) // 不是表达式的,都要还原
                        {
                            propertiesValue[v] = resolvedValueOut;
                        }
                        else
                            propertiesValue[v] = valueOut;
                    }
                }
                return propertiesValue;
            }
            catch
            {
            }
            return null;
        }
 
        /// <summary>
        /// 得到指定 SolidWorks 文档的当前配置里的属性的值
        /// </summary>
        /// <param name="currentModelDoc">SolidWorks 文档</param>
        /// <param name="propertyName">属性名称</param>
        /// <param name="fullyResolve">完全还原属性的值吗?指的是如果属性值为 $PRP:SW - FileName,是否要还原成不带后缀的文件名</param>
        /// <returns>属性的值</returns>
        public static string GetSingle(ModelDoc2 currentModelDoc, string propertyName, bool fullyResolve)
        {
            List<string> properties = new List<string>();
            properties.Add(propertyName);
            Dictionary<string, string> keyValuePairs = Get(currentModelDoc, properties, fullyResolve);
            if (keyValuePairs == null || !keyValuePairs.ContainsKey(propertyName))
                return string.Empty;
            return keyValuePairs[propertyName];
        }
 
        /// <summary>
        /// 设置自定义属性的值
        /// </summary>
        /// <param name="currentModelDoc">SolidWorks 文档</param>
        /// <param name="propertiesNamesAndValues">属性的名称与值, key 是名称,value 是值</param>
        /// <returns>任何失败都将返回 false, 全部成功则返回 true</returns>
        public static bool Set(ModelDoc2 currentModelDoc, IDictionary<string, string> propertiesNamesAndValues)
        {
            if (currentModelDoc == null || propertiesNamesAndValues == null)
                return false;
            try
            {
                CustomPropertyManager customPropertyManager = GetCustomPropertyManager(currentModelDoc);
                if (customPropertyManager == null)
                    return false;
                foreach (KeyValuePair<string, string> v in propertiesNamesAndValues)
                {
                    /*
                    swCustomInfoSetResult_e setResult = (swCustomInfoSetResult_e)customPropertyManager.Set2(v.Key, v.Value);
                    switch (setResult)
                    {
                        case swCustomInfoSetResult_e.swCustomInfoSetResult_NotPresent: // 如果设置失败的原因是不存在该属性,那就增加
                            if (customPropertyManager.Add3(v.Key, (int)swCustomInfoType_e.swCustomInfoText, v.Value, 1) != (int)swCustomInfoAddResult_e.swCustomInfoAddResult_AddedOrChanged)
                                return false;
                            else
                                break;
                        case swCustomInfoSetResult_e.swCustomInfoSetResult_OK:
                            break;
                        default:
                            return false; // 失败了
                    }*/ // Set2 竟然返回类型不匹配
                    swCustomInfoDeleteResult_e resultOfDelete = (swCustomInfoDeleteResult_e)customPropertyManager.Delete2(v.Key);
                    return customPropertyManager.Add3(v.Key, (int)swCustomInfoType_e.swCustomInfoText, v.Value, 1) == (int)swCustomInfoAddResult_e.swCustomInfoAddResult_AddedOrChanged;
 
                }
                return true;
            }
            catch
            {
            }
            return false;
        }
 
        /// <summary>
        /// 设置自定义属性的值
        /// </summary>
        /// <param name="currentModelDoc">SolidWorks 文档</param>
        /// <param name="propertyName">属性的名称</param>
        /// <param name="propertyValue">属性的值</param>
        /// <returns>任何失败都将返回 false, 全部成功则返回 true</returns>
        public static bool SetSingle(ModelDoc2 currentModelDoc, string propertyName, string propertyValue)
        {
            if (string.IsNullOrEmpty(propertyName))
                return false;
            Dictionary<string, string> name_value = new Dictionary<string, string>
            {
                [propertyName] = propertyValue
            };
            return Set(currentModelDoc, name_value);
        }
 
        /// <summary>
        /// 设置自定义属性的值
        /// </summary>
        /// <param name="currentModelDoc">SolidWorks 文档</param>
        /// <param name="propertiesNamesAndValues">属性的名称与值, key 是名称,value 是值</param>
        /// <returns>任何失败都将返回 false, 全部成功则返回 true</returns>
        public static bool Delete(ModelDoc2 currentModelDoc, string propertyName)
        {
            if (currentModelDoc == null || string.IsNullOrEmpty(propertyName))
                return false;
            try
            {
                CustomPropertyManager customPropertyManager = GetCustomPropertyManager(currentModelDoc);
                if (customPropertyManager == null)
                    return false;
                return customPropertyManager.Delete2(propertyName) == (int)swCustomInfoDeleteResult_e.swCustomInfoDeleteResult_OK;
            }
            catch
            {
            }
            return false;
        }
    }
}