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;
|
}
|
}
|
}
|