chr
2024-08-12 e9d7a5ef4c17e4804fb988dd193ff7d1fa36d52b
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Runtime.CompilerServices;
using System.Windows.Controls;
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;
 
namespace PdmSwPlugin.PropertySetting.Panel
{
    public partial class StandardModulePanel : UserControl, 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
 
        public event PanelPropertyChanged UpdateProperty;
 
        private Dictionary<string, string> InitData = new Dictionary<string, string>();
 
        private ModelDoc2 doc;
 
        #region UI属性
        private string _materialCode;
        [PropertySettingAttr(Name = NameConstant.materialCode)]
        public string materialCode
        {
            get => _materialCode;
            set
            {
                RaiseAndSetIfChanged(ref _materialCode, value);
            }
        }
 
        private string _materialName;
        [PropertySettingAttr(Name = NameConstant.materialName)]
        public string materialName
        {
            get => _materialName;
            set
            {
                RaiseAndSetIfChanged(ref _materialName, value);
            }
        }
 
        private string _materialModel;
        [PropertySettingAttr(Name = NameConstant.materialModel)]
        public string materialModel
        {
            get => _materialModel;
            set
            {
                RaiseAndSetIfChanged(ref _materialModel, value);
            }
        }
 
        private string _brand;
        [PropertySettingAttr(Name = NameConstant.brand)]
        public string brand
        {
            get => _brand;
            set
            {
                RaiseAndSetIfChanged(ref _brand, value);
            }
        }
 
        private string _weight = "";
        [PropertySettingAttr(Name = NameConstant.weight, NeedSave = false)]
        public string weight
        {
            get => CustomPropertyUtil.GetMass(doc);
            set
            {
                RaiseAndSetIfChanged(ref _weight, value);
            }
        }
 
        [PropertySettingAttr(Name = NameConstant.weight, NeedInit = false)]
        public string weightEval
        {
            get => doc == null ? null : $"\"SW-质量@{Path.GetFileName(doc.GetPathName())}\"";
            set { }
        }
 
        private string _designer = "";
        [PropertySettingAttr(Name = NameConstant.designer)]
        public string designer
        {
            get => _designer;
            set
            {
                RaiseAndSetIfChanged(ref _designer, value);
            }
        }
 
        private string _version = "";
        [PropertySettingAttr(Name = NameConstant.version)]
        public string version
        {
            get => _version;
            set
            {
                RaiseAndSetIfChanged(ref _version, value);
            }
        }
 
        private string _remark = "";
        [PropertySettingAttr(Name = NameConstant.remark)]
        public string remark
        {
            get => _remark;
            set
            {
                RaiseAndSetIfChanged(ref _remark, value);
            }
        }
        #endregion
 
        public StandardModulePanel()
        {
            InitializeComponent();
            DataContext = this;
        }
 
        /// <summary>
        /// 设置属性
        /// </summary>
        /// <param name="properties"></param>
        /// <param name="errMsg"></param>
        /// <returns></returns>
        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;
        }
 
        /// <summary>
        /// 获取属性
        /// </summary>
        /// <returns></returns>
        public Dictionary<string, string> GetAllProperties()
        {
            return NameConstant.GetAllProperties(this);
        }
 
        public bool UpdateSingleProperty(string name, string value)
        {
            return NameConstant.UpdateSingleProperty(this, name, value);
        }
 
        public bool SetSettings(object settings)
        {
            return true;
        }
 
        public object GetPropertyValue(string propertyName)
        {
            return null;
        }
 
        public bool ClearAllProperties(out string ErrMsg)
        {
            ErrMsg = null;
            return true;
        }
 
        public bool InitProperties(object properties, out string errMsg)
        {
            errMsg = null;
            return true;
        }
 
        public bool GetDocChanged()
        {
            return NameConstant.CompareWithInit(this, InitData);
        }
 
        public bool ResetProperty(out string errMsg)
        {
            return SetProperties(doc, InitData, false, out errMsg);
        }
    }
}