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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
using System.Collections.Generic;
using System.Collections.ObjectModel;
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.Entity;
using PdmSwPlugin.PropertySetting.Interface;
using PdmSwPlugin.PropertySetting.Panel.Attr;
using SolidWorks.Interop.sldworks;
using SolidWorks.Interop.swconst;
 
namespace PdmSwPlugin.PropertySetting.Panel
{
    public partial class JgjPartPanel : 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 void ccc<T>(ref T old, T @new, [CallerMemberName] string propertyName = null)
        {
            old = @new;
            if (propertyName != null)
            {
                RaisePropertyChanged(propertyName);
            }
        }
 
        public event PanelPropertyChanged UpdateProperty;
 
        private Dictionary<string, string> InitData = new Dictionary<string, string>();
 
        private Dictionary<string, JgjType> datas;
        private Dictionary<string, ObservableCollection<string>> stuffItemCache;
 
        private Dictionary<string, ObservableCollection<string>> surfaceItemCache;
 
        private Dictionary<string, ObservableCollection<string>> heatItemCache;
 
        private ModelDoc2 doc;
 
        #region 下拉框数据源
        private ObservableCollection<string> _jgjTypeItems;
        public ObservableCollection<string> jgjTypeItems
        {
            get => _jgjTypeItems;
            set => RaiseAndSetIfChanged(ref _jgjTypeItems, value);
        }
 
        private ObservableCollection<string> _stuffItems;
        public ObservableCollection<string> stuffItems
        {
            get => _stuffItems;
            set => RaiseAndSetIfChanged(ref _stuffItems, value);
        }
 
        private ObservableCollection<string> _surfaceItems;
        public ObservableCollection<string> surfaceItems
        {
            get => _surfaceItems;
            set => RaiseAndSetIfChanged(ref _surfaceItems, value);
        }
 
        private ObservableCollection<string> _heatItems;
        public ObservableCollection<string> heatItems
        {
            get => _heatItems;
            set => RaiseAndSetIfChanged(ref _heatItems, value);
        }
 
        private ObservableCollection<string> _materialTypeItems = NameConstant.MaterialTypes;
        public ObservableCollection<string> materialTypeItems
        {
            get => _materialTypeItems;
            set => RaiseAndSetIfChanged(ref _materialTypeItems, value);
        }
 
        private ObservableCollection<string> _docTypeItems = NameConstant.DocTypes;
        public ObservableCollection<string> docTypeItems
        {
            get => _docTypeItems;
            set => RaiseAndSetIfChanged(ref _docTypeItems, value);
        }
        #endregion
 
        private bool _docTypeEnabled = true;
        [PropertySettingAttr(NeedInit = false, NeedSave = false)]
        public bool DocTypeEnabled
        {
            get => _docTypeEnabled;
            set => RaiseAndSetIfChanged(ref _docTypeEnabled, value);
        }
 
        #region UI属性
        private string _materialCode;
        [PropertySettingAttr(Name = NameConstant.materialCode, defaultValue = "$PRP:\"SW-File Name\"")]
        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 _jgjType;
        [PropertySettingAttr(Name = NameConstant.jgjType)]
        public string jgjType
        {
            get => _jgjType;
            set
            {
                string temp = value;// jgjTypeItems.Contains(value) ? value : "无";
                var datas = stuffItemCache.Get(temp, new ObservableCollection<string> { "无" });
                RaiseAndSetIfChanged(ref _stuffItems, datas, "stuffItems");
                RaiseAndSetIfChanged(ref _jgjType, temp);
                UpdateField("jgjType", temp);
            }
        }
 
        private string _stuff;
        [PropertySettingAttr(Name = NameConstant.stuff)]
        public string stuff
        {
            get => _stuff;
            set
            {
                string temp = value;// stuffItems.Contains(value) ? value : "无";
                var sDatas = surfaceItemCache.Get($"{jgjType}_{temp}", new ObservableCollection<string> { "无" });
                var hDatas = heatItemCache.Get($"{jgjType}_{temp}", new ObservableCollection<string> { "无" });
                ccc(ref _surfaceItems, sDatas, "surfaceItems");
                ccc(ref _heatItems, hDatas, "heatItems");
                ccc(ref _stuff, temp);
            }
        }
 
        private string _surface;
        [PropertySettingAttr(Name = NameConstant.surface)]
        public string surface
        {
            get => _surface;
            set
            {
                string temp = value;// surfaceItems.Contains(value) ? value : "无";
                ccc(ref _surface, temp);
            }
        }
 
        private string _heat;
        [PropertySettingAttr(Name = NameConstant.heat)]
        public string heat
        {
            get => _heat;
            set
            {
                string temp = value;// heatItems.Contains(value) ? value : "无";
                ccc(ref _heat, temp);
            }
        }
 
        private string _weight = "";
        [PropertySettingAttr(Name = NameConstant.weight, NeedSave = false)]
        public string weight
        {
            get => CustomPropertyUtil.GetMass(doc);
            set
            {
                ccc(ref _weight, value);
            }
        }
 
        [PropertySettingAttr(Name = NameConstant.weight, NeedInit = false)]
        public string weightEval
        {
            get => doc == null ? null : $"\"SW-质量@{Path.GetFileName(doc.GetPathName())}\"";
            set { }
        }
 
        private string _materialType = "";
        [PropertySettingAttr(Name = NameConstant.materialType, defaultValue = "普通件")]
        public string materialType
        {
            get => _materialType;
            set
            {
                RaiseAndSetIfChanged(ref _materialType, value);
            }
        }
 
        private string _designer = "";
        [PropertySettingAttr(Name = NameConstant.designer)]
        public string designer
        {
            get => _designer;
            set
            {
                RaiseAndSetIfChanged(ref _designer, value);
            }
        }
 
        private string _remark = "";
        [PropertySettingAttr(Name = NameConstant.remark)]
        public string remark
        {
            get => _remark;
            set
            {
                RaiseAndSetIfChanged(ref _remark, value);
            }
        }
 
        private string _docType = "";
        [PropertySettingAttr(Name = NameConstant.docType, defaultValue = "加工件")]
        public string docType
        {
            get => _docType;
            set
            {
                string temp = value;
                if (NameConstant.SpecialJgjType.Contains(jgjType))
                {
                    temp = "加工件";
                }
                ccc(ref _docType, temp);
            }
        }
 
        private string _versionChange = "0";
        [PropertySettingAttr(Name = NameConstant.versionChange, defaultValue = "0")]
        public string versionChange
        {
            get => _versionChange;
            set
            {
                RaiseAndSetIfChanged(ref _versionChange, value);
                RaiseAndSetIfChanged(ref _versionChangeBind, value == "1", "versionChangeBind");
            }
        }
 
        private bool _versionChangeBind = false;
        [PropertySettingAttr(Name = NameConstant.versionChange, NeedSave = false, NeedInit = false)]
        public bool versionChangeBind
        {
            get => _versionChangeBind;
            set
            {
                RaiseAndSetIfChanged(ref _versionChangeBind, value);
                RaiseAndSetIfChanged(ref _versionChange, value ? "1" : "0", "versionChange");
            }
        }
        #endregion
 
        public JgjPartPanel()
        {
            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 void RefreshItem()
        {
            if (datas == null)
            {
                return;
            }
            var finalStuffs = stuffItemCache.Get(jgjType, new ObservableCollection<string> { "无" });
            string key = $"{jgjType}_{stuff}";
            var finalSurfaces = surfaceItemCache.Get(key, new ObservableCollection<string> { "无" });
            var finalHeats = heatItemCache.Get(key, new ObservableCollection<string> { "无" });
 
            Dispatcher.Invoke(() =>
            {
                stuffItems = finalStuffs;
                surfaceItems = finalSurfaces;
                heatItems = finalHeats;
            });
        }
 
        public bool SetSettings(object settings)
        {
            Dictionary<string, JgjType> datas = settings as Dictionary<string, JgjType>;
            this.datas = datas;
 
            stuffItemCache = new Dictionary<string, ObservableCollection<string>>();
 
            surfaceItemCache = new Dictionary<string, ObservableCollection<string>>();
 
            heatItemCache = new Dictionary<string, ObservableCollection<string>>();
 
            ObservableCollection<string> jgjTypes = new ObservableCollection<string> { "无" };
 
            foreach (string key in datas.Keys)
            {
                jgjTypes.Add(key);
                JgjType type = datas[key];
                ObservableCollection<string> stuffs = new ObservableCollection<string> { "无" };
 
                Dictionary<string, StuffType> stuffDatas = type.stuffMap;
                foreach (string stuff in stuffDatas.Keys)
                {
                    if (stuff != "无")
                    {
                        stuffs.Add(stuff);
                    }
                    StuffType stuffType = stuffDatas[stuff];
                    string key2 = key + "_" + stuff;
 
                    ObservableCollection<string> surfaces = new ObservableCollection<string> { "无" };
                    ObservableCollection<string> heats = new ObservableCollection<string> { "无" };
                    foreach (string surface in stuffType.surfaceParam)
                    {
                        if (surface != "无") surfaces.Add(surface);
                    }
                    foreach (string heat in stuffType.heatParam)
                    {
                        if (heat != "无") heats.Add(heat);
                    }
                    surfaceItemCache.Add(key2, surfaces);
                    heatItemCache.Add(key2, heats);
                }
                stuffItemCache.Add(key, stuffs);
                if (!stuffItemCache.ContainsKey("无"))
                {
                    stuffItemCache.Add("无", new ObservableCollection<string> { "无" });
                }
            }
 
            Dispatcher.Invoke(() =>
            {
                jgjTypeItems = jgjTypes;
            });
            return true;
        }
 
        public bool ClearAllProperties(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);
        }
 
        private void stuffInput_DropDownOpened(object sender, System.EventArgs e)
        {
            ComboBox comboBox = sender as ComboBox;
            if (comboBox == null) return;
            if (comboBox.Name == "stuffInput")
            {
                stuffItems = stuffItemCache.Get(jgjType, new ObservableCollection<string> { "无" });
            }
            else if (comboBox.Name == "surfaceInput")
            {
                string key = $"{jgjType}_{stuff}";
                surfaceItems = surfaceItemCache.Get(key, new ObservableCollection<string> { "无" });
            }
            else if (comboBox.Name == "heatInput")
            {
                string key = $"{jgjType}_{stuff}";
                heatItems = heatItemCache.Get(key, new ObservableCollection<string> { "无" });
            }
        }
 
        private void UpdateField(string field, string value)
        {
            if (field == "jgjType")
            {
                if (NameConstant.SpecialJgjType.Contains(value))
                {
                    docType = "加工件";
                    DocTypeEnabled = false;
                }
                else
                {
                    DocTypeEnabled = true;
                }
                return;
            }
        }
    }
}