chr
2025-03-04 3f62d18e4361cd1d7a49c126765d95b2ad9c8246
PdmSwPlugin.PropertySetting/CommonSettingControl.xaml.cs
@@ -25,7 +25,7 @@
using System.IO;
using System.Collections.ObjectModel;
using System.Runtime.InteropServices;
using System.Diagnostics;
using PdmSwPlugin.PropertySetting.Util;
namespace PdmSwPlugin.PropertySetting
{
@@ -67,12 +67,16 @@
            Dictionary<string, string> datas = properties as Dictionary<string, string>;
            (container as UIElement).Dispatcher.Invoke(() =>
            {
                var props = container.GetType().GetProperties().Where(prop => System.Attribute.IsDefined(prop, typeof(PropertySettingAttr)))
               .ToList();
                // PropHolder.SetPropToUI(container, datas);
                foreach (var prop in props)
                Dictionary<PropertyInfo, PropertySettingAttr> map = PropHolder.GetAttrMap(container.GetType());
                if (map == null)
                {
                    PropertySettingAttr attr = prop.GetCustomAttribute<PropertySettingAttr>();
                    return;
                }
                foreach (var prop in map.Keys)
                {
                    PropertySettingAttr attr = map[prop];
                    if (attr == null || !attr.NeedInit)
                    {
                        continue;
@@ -89,8 +93,32 @@
                        // 其他写入initData
                        if (initData != null) initData[name] = value;
                    }
                    prop.SetValue(container, value);
                    PropHolder.SetPropToUI(container, prop, value);
                }
                // var props = container.GetType().GetProperties().Where(prop => System.Attribute.IsDefined(prop, typeof(PropertySettingAttr)))
                //.ToList();
                // foreach (var prop in props)
                // {
                //     PropertySettingAttr attr = prop.GetCustomAttribute<PropertySettingAttr>();
                //     if (attr == null || !attr.NeedInit)
                //     {
                //         continue;
                //     }
                //     string name = attr.Name;
                //     string value = datas.Get(name, string.Empty);
                //     if (string.IsNullOrEmpty(value) && !string.IsNullOrEmpty(attr.defaultValue))
                //     {
                //         value = attr.defaultValue;
                //         // 默认值不写入initData
                //     }
                //     else
                //     {
                //         // 其他写入initData
                //         if (initData != null) initData[name] = value;
                //     }
                //     prop.SetValue(container, value);
                // }
            });
            return true;
        }
@@ -232,6 +260,18 @@
            this.SwApp = SwApp;
        }
        public Dictionary<string, string> GetPropsWithLog(ModelDoc2 currentModelDoc,
            bool needResolve,
            HashSet<string> skipName,
            HashSet<string> skipResolveName)
        {
            if (currentModelDoc == null) return null;
            var start = DateTime.Now;
            var data = CustomPropertyUtil.GetCustomProperties2(currentModelDoc, needResolve, skipName, skipResolveName);
            Logger.Debug($"Get Props {(DateTime.Now - start).TotalMilliseconds} ms.");
            return data;
        }
        public void InitPropertyData()
        {
            try
@@ -292,7 +332,7 @@
        private void UpdateActiveDoc()
        {
            long start = DateTime.Now.Millisecond;
            var start = DateTime.Now;
            Logger.Debug("UpdateActiveDoc Start...");
            if (activeDoc != null)
            {
@@ -309,11 +349,8 @@
                    activeDoc.ClearSelection2(true);
                }
            }
            Logger.Debug($"UpdateActiveDoc End {activeDoc?.GetPathName()} {DateTime.Now.Millisecond - start}ms.");
            start = DateTime.Now.Millisecond;
            Logger.Debug("UpdateUI Start...");
            Logger.Debug($"UpdateActiveDoc End {activeDoc?.GetPathName()} {(DateTime.Now - start).TotalMilliseconds}ms.");
            UpdateUI(activeDoc);
            Logger.Debug($"UpdateUI End {activeDoc?.GetPathName()} {DateTime.Now.Millisecond - start}ms.");
        }
        /// <summary>
@@ -390,15 +427,13 @@
        public void UpdateUI(ModelDoc2 doc)
        {
            var start = DateTime.Now;
            Logger.Debug("UpdateUI Start...");
            AlertSaveWindow();
            LastReadDoc = ReadDoc;
            ReadDoc = doc;
            Dispatcher.Invoke(() =>
            {
                // LoadDocProperties();
            });
            SwitchVisiable();
            Logger.Debug($"UpdateUI End {activeDoc?.GetPathName()} {(DateTime.Now - start).TotalMilliseconds}ms.");
        }
        private int Ass_UserSelectionPostNotify()
@@ -433,10 +468,15 @@
        /// </summary>
        private void SwitchVisiable()
        {
            var start = DateTime.Now.Millisecond;
            var start = DateTime.Now;
            Logger.Debug("SwitchVisiable Start...");
            HashSet<string> skipName = new HashSet<string> {
            HashSet<string> skipName = new HashSet<string>
            {
                NameConstant.weight
            };
            HashSet<string> skipResolveName = new HashSet<string> {
                NameConstant.materialCode
            };
@@ -457,7 +497,7 @@
            else if (docType == (int)swDocumentTypes_e.swDocPART)
            {
                ReadDocPath = ReadDoc.GetPathName();
                properties = CustomPropertyUtil.GetCustomProperties2(ReadDoc, true, skipName);
                properties = GetPropsWithLog(ReadDoc, true, skipName, skipResolveName);
                string fileType = properties.Get(NameConstant.docType);
                /*if (fileType == "加工件" || fileType == "模组")
                {
@@ -495,7 +535,7 @@
                else
                {
                    // !Equals(LastReadDoc, ReadDoc)
                    properties = CustomPropertyUtil.GetCustomProperties2(ReadDoc, true, skipName);
                    properties = GetPropsWithLog(ReadDoc, true, skipName, skipResolveName);
                    string fileType = properties.Get(NameConstant.docType);
                    /*if (fileType == "加工件" || fileType == "模组")
                    {
@@ -547,162 +587,13 @@
                contentPanel.Children.Add(control);
            });
            Logger.Debug($"SwitchVisiable End {activeDoc?.GetPathName()} {DateTime.Now.Millisecond - start}ms.");
        }
        /// <summary>
        /// 根据激活的文档类型,变更属性可视
        /// </summary>
        private void SwitchVisiable2()
        {
            HashSet<string> skipName = new HashSet<string> {
                NameConstant.materialCode
            };
            IPropertyOpt last = ActiveOpter as IPropertyOpt;
            string contentKey = "default";
            int? docType = ReadDoc?.GetType();
            UserControl control;
            IPropertyOpt newOne;
            Dictionary<string, string> properties;
            if (docType == (int)swDocumentTypes_e.swDocDRAWING)
            {
                ReadDocPath = ReadDoc.GetPathName();
                control = new DefaultPanel();
                newOne = control as IPropertyOpt;
                newOne.SetSettings(settings);
                newOne.SetProperties(null, "当前不支持工程图", true, out _);
            }
            else if (docType == (int)swDocumentTypes_e.swDocPART)
            {
                ReadDocPath = ReadDoc.GetPathName();
                properties = CustomPropertyUtil.GetCustomProperties2(ReadDoc, true, skipName);
                string fileType = properties.Get(NameConstant.docType);
                /*if (fileType == "加工件" || fileType == "模组")
                {
                    contentKey = "jgjPart";
                }*/
                if (BomIniterHolder.Instance.IsJgj(ReadDoc))
                {
                    control = new JgjPartPanel();
                }
                else
                {
                    control = new StandardPartPanel();
                }
                newOne = control as IPropertyOpt;
                newOne.SetSettings(settings);
                newOne.SetProperties(ReadDoc, properties, true, out _);
            }
            else if (docType == (int)swDocumentTypes_e.swDocASSEMBLY)
            {
                ReadDocPath = ReadDoc.GetPathName();
                // !Equals(LastReadDoc, ReadDoc)
                properties = CustomPropertyUtil.GetCustomProperties2(ReadDoc, true, skipName);
                string fileType = properties.Get(NameConstant.docType);
                /*if (fileType == "加工件" || fileType == "模组")
                {
                    contentKey = "jgjPart";
                }*/
                if (BomIniterHolder.Instance.IsJgj(ReadDoc))
                {
                    control = new JgjModulePanel();
                }
                else
                {
                    control = new StandardModulePanel();
                }
                newOne = control as IPropertyOpt;
                newOne.SetSettings(settings);
                newOne.SetProperties(ReadDoc, properties, true, out _);
            }
            else
            {
                control = new DefaultPanel();
                newOne = control as IPropertyOpt;
                string PathName = ReadComponent?.GetPathName();
                if (PathName == null)
                {
                    ReadDocPath = "";
                    newOne.SetSettings(settings);
                    newOne.SetProperties(ReadDoc, "请打开一个文档", true, out _);
                }
                else
                {
                    ReadDocPath = PathName;
                    string name = Path.GetFileNameWithoutExtension(PathName);
                    newOne.SetSettings(settings);
                    newOne.SetProperties(ReadDoc, $"【{name}】未找到文档,请检查是否被压缩或轻量化", true, out _);
                }
            }
            if (last != null)
            {
                last.UpdateProperty -= Jgj_SinglePropertyChanged;
                last = null;
            }
            ActiveOpter = control;
            contentPanel.Children.Clear();
            newOne.UpdateProperty += Jgj_SinglePropertyChanged;
            contentPanel.Children.Add(control);
        }
        /// <summary>
        /// 从激活的文档中读取参数
        /// </summary>
        private void LoadDocProperties()
        {
            IPropertyOpt opt = ActiveOpter as IPropertyOpt;
            string ErrMsg = null;
            if (ReadDoc == null)
            {
                string PathName = ReadComponent?.GetPathName();
                if (PathName == null)
                {
                    ReadDocPath = "";
                    opt.SetProperties(ReadDoc, "请打开一个文档", true, out ErrMsg);
                    return;
                }
                ReadDocPath = PathName;
                string name = Path.GetFileNameWithoutExtension(PathName);
                opt.SetProperties(ReadDoc, $"【{name}】未找到文档,请检查是否被压缩或轻量化", true, out ErrMsg);
                return;
            }
            int docType = ReadDoc.GetType();
            ReadDocPath = ReadDoc.GetPathName();
            if (docType == (int)swDocumentTypes_e.swDocDRAWING)
            {
                opt.SetProperties(null, "当前不支持工程图", true, out ErrMsg);
                return;
            }
            HashSet<string> skipName = new HashSet<string> {
                NameConstant.materialCode
            };
            Dictionary<string, string> properties = CustomPropertyUtil.GetCustomProperties2(ReadDoc, true, skipName);
            opt.SetProperties(ReadDoc, properties, !Equals(LastReadDoc, ReadDoc), out ErrMsg);
        }
        private void ClearDocProperties()
        {
            IPropertyOpt opt = ActiveOpter as IPropertyOpt;
            opt.ClearAllProperties(out string ErrMsg);
        }
        /// <summary>
        /// 将属性设置进文档,注意,并没有保存
        /// </summary>
        /// <param name="doc">文档</param>
        private void SetDocProperty(ModelDoc2 doc, Dictionary<string, string> props)
        {
            CustomPropertyUtil.SetCustomProperties(doc, props);
            Logger.Debug($"SwitchVisiable End {activeDoc?.GetPathName()} {(DateTime.Now - start).TotalMilliseconds}ms.");
        }
        public void SetSwApp(SldWorks SwApp)
        {
            this.SwApp = SwApp;
        }
        private void SaveDoc(bool qiaoqiaode)
        {
@@ -735,6 +626,8 @@
        public void RefreshData()
        {
            MaskAdorner.ShowMask(content, "属性加载中,请稍候...");
            var start = DateTime.Now;
            Logger.Debug($"Refresh [{ReadDocPath}] Data start.");
            Task.Run(() =>
            {
                try
@@ -754,6 +647,7 @@
                finally
                {
                    MaskAdorner.HideMask(content);
                    Logger.Debug($"Refresh [{ReadDocPath}] Data end {(DateTime.Now - start).TotalMilliseconds}ms.");
                }
            });
        }
@@ -792,7 +686,6 @@
        /// <param name="comp"></param>
        public void OnSwActiveDocChange(ModelDoc2 lastDoc, ModelDoc2 doc, Component2 comp)
        {
            // CurrentChanged();
            RefreshData();
        }
@@ -835,7 +728,7 @@
        /// </summary>
        public void AlertSaveWindow()
        {
            var start = DateTime.Now.Millisecond;
            var start = DateTime.Now;
            Logger.Debug("AlertSaveWindow Start...");
            IPropertyOpt opt = ActiveOpter as IPropertyOpt;
            if (opt != null && opt.GetDocChanged())
@@ -859,7 +752,7 @@
                    opt.ResetProperty(out string errMsg);
                }
            }
            Logger.Debug($"AlertSaveWindow End {activeDoc?.GetPathName()} {DateTime.Now.Millisecond - start}ms.");
            Logger.Debug($"AlertSaveWindow End {activeDoc?.GetPathName()} {(DateTime.Now - start).TotalMilliseconds}ms.");
        }
        public void OnTabControlSelectionChange(object sender, SelectionChangedEventArgs e)
@@ -893,5 +786,13 @@
                this.Error($"{ex.Message}");
            }
        }
        public void DisabledHandler()
        {
            if (activeDoc != null)
            {
                DetachDocNotify(activeDoc);
            }
        }
    }
}