chr
2024-08-07 22beee93f14d042aa184148c53efb79e23416526
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
using PdmSwPlugin.Common.CustomHandler;
using PdmSwPlugin.Common.Util;
using PdmSwPlugin.PropertySetting.Interface;
using PdmSwPlugin.PropertySetting.Panel.Attr;
using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
using System.Collections.Generic;
using System.ComponentModel;
using System.Reflection;
using System.Runtime.CompilerServices;
 
namespace PdmSwPlugin.PropertySetting.Panel.Model
{
    public class BasePanelModel : INotifyPropertyChanged, IPropertyOpt
    {
        
        #region 不能公用的东西,真有你的啊C#
        public event PropertyChangedEventHandler PropertyChanged;
        public void RaisePropertyChanged(string name)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
        }
 
        public void RaiseAndSetIfChanged<T>(ref T old, T @new, [CallerMemberName] string propertyName = null)
        {
            old = @new;
            if (propertyName != null)
            {
                RaisePropertyChanged(propertyName);
                var attr = this.GetType().GetProperty(propertyName)?.GetCustomAttribute<PropertySettingAttr>();
                if (attr == null)
                {
                    return;
                }
                if (attr.NeedSave)
                {
                    UpdateProperty?.Invoke(this, attr.Name, @new);
                }
            }
        }
        #endregion
        protected bool changed;
        public event PanelPropertyChanged UpdateProperty;
        protected ModelDoc2 doc;
 
        private Dictionary<string, string> InitData = new Dictionary<string, string>();
 
        public bool SetProperties(ModelDoc2 doc, object properties, bool docChange, out string errMsg)
        {
            this.doc = doc;
            var data = docChange ? InitData : null;
            return NameConstant.SetProperties(this, properties, data, out errMsg);
        }
 
        public bool SaveDoc(ref int err, ref int warn)
        {
            Dictionary<string, string> props = GetAllProperties();
            CustomPropertyUtil.SetCustomProperties(doc, props);
            if (doc.Save3((int)swSaveAsOptions_e.swSaveAsOptions_AvoidRebuildOnSave,
                    ref err, ref warn))
            {
                InitData = props;
                return true;
            }
            return false;
        }
 
        public virtual bool SetSettings(object settings)
        {
            return true;
        }
 
        public Dictionary<string, string> GetAllProperties()
        {
            return NameConstant.GetAllProperties(this);
        }
 
        public bool ClearAllProperties(out string ErrMsg)
        {
            ErrMsg = null;
            
            return true;
        }
 
        public bool UpdateSingleProperty(string name, string value)
        {
            changed = NameConstant.UpdateSingleProperty(this, name, value);
            return changed;
        }
 
        public bool GetDocChanged()
        {
            return NameConstant.CompareWithInit(this, InitData);
        }
 
        public bool ResetProperty(out string errMsg)
        {
            throw new System.NotImplementedException();
        }
    }
}