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