chr
2025-01-03 31a636e735a0addc56e4f4527f500b7aa0874eb5
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
using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
using System.Collections.Generic;
 
namespace PdmSwPlugin.Common.Util
{
    public static class CustomPropertyUtil
    {
        /// <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[""];
 
                Configuration activeConfig = currentModelDoc.GetActiveConfiguration() as Configuration;
                if (activeConfig == null)
                    return null;
                return currentModelDoc.Extension.CustomPropertyManager[activeConfig.Name];*/
                return currentModelDoc.Extension.CustomPropertyManager[""];
            }
            catch
            {
                return null;
            }
        }
 
        /// <summary>
        /// 获取文档属性
        /// </summary>
        /// <param name="currentModelDoc">文档</param>
        /// <param name="needResolve">是否转换值</param>
        /// <param name="skipName">跳过的name</param>
        /// <returns></returns>
        public static Dictionary<string, string> GetCustomProperties2(ModelDoc2 currentModelDoc,
            bool needResolve,
            HashSet<string> skipName = null)
        {
            CustomPropertyManager customPropertyManager = GetCustomPropertyManager(currentModelDoc);
            if (customPropertyManager == null)
            {
                return null;
            }
 
            Dictionary<string, string> Properties = new Dictionary<string, string>();
            string[] names = customPropertyManager.GetNames();
            if (names == null)
            {
                return Properties;
            }
            foreach (string name in names)
            {
                if (customPropertyManager.Get6(name, false, out string valueOut, out string resolvedValueOut, out _, out _)
                    != (int)swCustomInfoGetResult_e.swCustomInfoGetResult_NotPresent)
                {
                    string value = (needResolve && (skipName == null || !skipName.Contains(name))) ? resolvedValueOut : valueOut;
                    Properties[name] = value;
                }
            }
            return Properties;
        }
 
        public static bool SaveDoc(ModelDoc2 doc, Dictionary<string, string> props, ref int err, ref int warn)
        {
            SetCustomProperties(doc, props);
            if (doc.Save3((int)swSaveAsOptions_e.swSaveAsOptions_AvoidRebuildOnSave,
                    ref err, ref warn))
            {
                return true;
            }
            return false;
        }
 
 
        public static Dictionary<string, string> GetCustomProperties(ModelDoc2 currentModelDoc,
            bool needResolve,
            HashSet<string> skipValue = null)
        {
            CustomPropertyManager customPropertyManager = GetCustomPropertyManager(currentModelDoc);
            if (customPropertyManager == null)
            {
                return null;
            }
 
            Dictionary<string, string> Properties = new Dictionary<string, string>();
            string[] names = customPropertyManager.GetNames();
            if (names == null)
            {
                return Properties;
            }
            foreach (string name in names)
            {
                if (customPropertyManager.Get6(name, false, out string valueOut, out string resolvedValueOut, out _, out _)
                    != (int)swCustomInfoGetResult_e.swCustomInfoGetResult_NotPresent)
                {
                    string value = (needResolve && (skipValue == null || !skipValue.Contains(valueOut))) ? resolvedValueOut : valueOut;
                    Properties[name] = value;
                }
            }
            return Properties;
        }
 
 
        public static string GetCustomValue(this Dictionary<string, string> datas, string name, bool ignoreCase = false)
        {
            if (datas == null) return null;
            if (datas.ContainsKey(name))
            {
                return datas[name];
            }
            return null;
        }
 
        public static string GetCustomValue(ModelDoc2 doc, string field, bool needResolve = true)
        {
            CustomPropertyManager customPropertyManager = GetCustomPropertyManager(doc);
            if (customPropertyManager == null)
            {
                return null;
            }
            string[] names = customPropertyManager.GetNames();
            if (names == null)
            {
                return null;
            }
            foreach (string name in names)
            {
                if (name != field)
                {
                    continue;
                }
 
                if (customPropertyManager.Get6(name, false, out string valueOut, out string resolvedValueOut, out _, out _)
                    != (int)swCustomInfoGetResult_e.swCustomInfoGetResult_NotPresent)
                {
                    return needResolve ? resolvedValueOut : valueOut;
                }
            }
            return null;
        }
 
 
        public static bool SetCustomProperties(ModelDoc2 doc, Dictionary<string, string> properties)
        {
            CustomPropertyManager cpm = doc.Extension.CustomPropertyManager[""];
            if (cpm == null)
            {
                return false;
            }
            // 只要该配置的自定义属性管理器不为空
            foreach (KeyValuePair<string, string> keyValuePair in properties)
            {
                if (cpm.Add3(keyValuePair.Key, (int)swCustomInfoType_e.swCustomInfoText, keyValuePair.Value, 1)
                    == (int)swCustomInfoAddResult_e.swCustomInfoAddResult_AddedOrChanged)
                {
 
 
                }
            }
            return true;
        }
 
        public static string GetMass(ModelDoc2 doc)
        {
            var cpm = doc?.Extension;
            if (cpm == null)
            {
                return null;
            }
            double[] values = cpm.GetMassProperties2(0, out _, false);
            if (values == null)
            {
                return null;
            }
            return values[5].ToString();
        }
 
        public static bool SetCustomProperties(ModelDoc2 doc, string key, string value)
        {
            CustomPropertyManager cpm = doc.Extension.CustomPropertyManager[""];
            if (cpm == null)
            {
                return false;
            }
            return cpm.Add3(key, (int)swCustomInfoType_e.swCustomInfoText, value, 1)
                     == (int)swCustomInfoAddResult_e.swCustomInfoAddResult_AddedOrChanged;
            /* foreach (string name in doc.GetConfigurationNames())
             { // 遍历所有的配置
                 Configuration config = doc.GetConfigurationByName(name) as Configuration;
                 CustomPropertyManager cpm = config.CustomPropertyManager;
                 if (cpm == null)
                 {
                     continue;
                 }
                 if (cpm.Add3(key, (int)swCustomInfoType_e.swCustomInfoText, value, 1) 
                     == (int)swCustomInfoAddResult_e.swCustomInfoAddResult_AddedOrChanged)
                 {
 
 
                 }
             }*/
        }
 
        /// <summary>
        /// 清理文档【配置特定】里的属性
        /// </summary>
        /// <param name="doc"></param>
        /// <returns></returns>
        public static bool ClearAllCustomProperties(ModelDoc2 doc, out bool needSave)
        {
            needSave = false;
            if (doc == null) return false;
            Configuration activeConfig = doc.GetActiveConfiguration() as Configuration;
            if (activeConfig == null)
                return false;
            CustomPropertyManager cpm = doc.Extension.CustomPropertyManager[activeConfig.Name];
            if (cpm == null)
            {
                return false;
            }
            string[] names = cpm.GetNames();
            if (names == null || names.Length <= 0)
            {
                return true;
            }
            foreach (var name in names)
            {
                cpm.Delete2(name);
                needSave = true;
            }
            return true;
        }
    }
}