chr
2025-03-04 3f62d18e4361cd1d7a49c126765d95b2ad9c8246
PdmSwPlugin.Common/Util/CustomPropertyUtil.cs
@@ -1,5 +1,6 @@
using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
using System;
using System.Collections.Generic;
namespace PdmSwPlugin.Common.Util
@@ -31,53 +32,6 @@
            }
        }
        /// <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)
@@ -106,6 +60,53 @@
            return Properties;
        }
        /// <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,
            HashSet<string> skipResolveName)
        {
            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 (skipName != null && skipName.Contains(name)) continue;
                if (customPropertyManager.Get6(name, false, out string valueOut, out string resolvedValueOut, out _, out _)
                    != (int)swCustomInfoGetResult_e.swCustomInfoGetResult_NotPresent)
                {
                    string value = (needResolve && (skipResolveName == null || !skipResolveName.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 string GetCustomValue(this Dictionary<string, string> datas, string name, bool ignoreCase = false)
        {
@@ -181,6 +182,17 @@
            return values[5].ToString();
        }
        public static string GetMassBySWDM(ModelDoc2 doc)
        {
            if (doc == null) return null;
            double[] values = SwDMUtil.GetMassProperty(doc.GetPathName(), out string errMSg);
            if (values == null)
            {
                return null;
            }
            return values[5].ToString();
        }
        public static bool SetCustomProperties(ModelDoc2 doc, string key, string value)
        {
            CustomPropertyManager cpm = doc.Extension.CustomPropertyManager[""];