using System.Collections.Generic;
using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
namespace PdmSwPlugin.Common.Util
{
///
/// 为 SolidWorks 文档得到或设置自定义属性的类
///
public static class CustomerPropertiesWrapper
{
///
/// 从指定 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[""];
}
else
{
Configuration activeConfig = currentModelDoc.GetActiveConfiguration() as Configuration;
if (activeConfig == null)
return null;
return currentModelDoc.Extension.CustomPropertyManager[activeConfig.Name];
}
}
catch
{
}
return null;
}
///
/// 得到指定 SolidWorks 文档的当前配置里的属性的值
///
/// SolidWorks 文档
/// 属性名称
/// 完全还原属性的值吗?指的是如果属性值为 $PRP:SW - FileName,是否要还原成不带后缀的文件名
/// 属性的值, key 是名称,value 是值
public static Dictionary Get(ModelDoc2 currentModelDoc, IEnumerable 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 propertiesValue = new Dictionary();
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;
}
///
/// 得到指定 SolidWorks 文档的当前配置里的属性的值
///
/// SolidWorks 文档
/// 属性名称
/// 完全还原属性的值吗?指的是如果属性值为 $PRP:SW - FileName,是否要还原成不带后缀的文件名
/// 属性的值
public static string GetSingle(ModelDoc2 currentModelDoc, string propertyName, bool fullyResolve)
{
List properties = new List();
properties.Add(propertyName);
Dictionary keyValuePairs = Get(currentModelDoc, properties, fullyResolve);
if (keyValuePairs == null || !keyValuePairs.ContainsKey(propertyName))
return string.Empty;
return keyValuePairs[propertyName];
}
///
/// 设置自定义属性的值
///
/// SolidWorks 文档
/// 属性的名称与值, key 是名称,value 是值
/// 任何失败都将返回 false, 全部成功则返回 true
public static bool Set(ModelDoc2 currentModelDoc, IDictionary propertiesNamesAndValues)
{
if (currentModelDoc == null || propertiesNamesAndValues == null)
return false;
try
{
CustomPropertyManager customPropertyManager = GetCustomPropertyManager(currentModelDoc);
if (customPropertyManager == null)
return false;
foreach (KeyValuePair 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;
}
///
/// 设置自定义属性的值
///
/// SolidWorks 文档
/// 属性的名称
/// 属性的值
/// 任何失败都将返回 false, 全部成功则返回 true
public static bool SetSingle(ModelDoc2 currentModelDoc, string propertyName, string propertyValue)
{
if (string.IsNullOrEmpty(propertyName))
return false;
Dictionary name_value = new Dictionary
{
[propertyName] = propertyValue
};
return Set(currentModelDoc, name_value);
}
///
/// 设置自定义属性的值
///
/// SolidWorks 文档
/// 属性的名称与值, key 是名称,value 是值
/// 任何失败都将返回 false, 全部成功则返回 true
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;
}
}
}