using SolidWorks.Interop.sldworks; using SolidWorks.Interop.swconst; using System.Collections.Generic; namespace PdmSwPlugin.Common.Util { public static class CustomPropertyUtil { /// /// 从指定 SolidWorks 文档得到当前配置里的自定义属性管理器 /// /// SolidWorks 文档 /// 自定义属性管理器 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; } } /// /// 获取文档属性 /// /// 文档 /// 是否转换值 /// 跳过的name /// public static Dictionary GetCustomProperties2(ModelDoc2 currentModelDoc, bool needResolve, HashSet skipName = null) { CustomPropertyManager customPropertyManager = GetCustomPropertyManager(currentModelDoc); if (customPropertyManager == null) { return null; } Dictionary Properties = new Dictionary(); 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 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 GetCustomProperties(ModelDoc2 currentModelDoc, bool needResolve, HashSet skipValue = null) { CustomPropertyManager customPropertyManager = GetCustomPropertyManager(currentModelDoc); if (customPropertyManager == null) { return null; } Dictionary Properties = new Dictionary(); 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 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 properties) { CustomPropertyManager cpm = doc.Extension.CustomPropertyManager[""]; if (cpm == null) { return false; } // 只要该配置的自定义属性管理器不为空 foreach (KeyValuePair 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) { } }*/ } /// /// 清理文档【配置特定】里的属性 /// /// /// public static bool ClearAllCustomProperties(ModelDoc2 doc) { 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); } return true; } } }