chr
7 天以前 43a0207d207390abdeeb3ab9155eebf03edd7b1a
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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
using log4net;
using PdmSwPlugin.Common;
using PdmSwPlugin.Common.Entity;
using PdmSwPlugin.Common.Util;
using SolidWorks.Interop.sldworks;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Net.Http;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms.Integration;
using System.Windows.Input;
using PdmSwPlugin.Common.Util.Http;
 
namespace PdmSwPlugin.MaterialSelect
{
    /// <summary>
    /// MaterialSelectControl.xaml 的交互逻辑
    /// </summary>
    [PdmSwPlugin(AppId = "", Title = "物料优选", Version = "0.1")]
    public partial class MaterialSelectControl : UserControl, INotifyPropertyChanged
    {
        /// <summary>
        /// 日志处理
        /// </summary>
        private static ILog Logger = LogManager.GetLogger("MaterialSelect");
        #region 字段
        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);
            }
        }
 
 
        public static long GetWindowHandle(SldWorks sld)
        {
            MaterialSelectControl control = new MaterialSelectControl(sld);
            // 只能用winform,代理wpf
            ElementHost host = new ElementHost
            {
                Child = control,
                AutoSize = true,
                Dock = System.Windows.Forms.DockStyle.Fill
            };
            /// 下面这一段不要删,指不定哪天又用到了
            System.Windows.Forms.Form form = new System.Windows.Forms.Form
            {
                ControlBox = false,
                FormBorderStyle = System.Windows.Forms.FormBorderStyle.None,
                KeyPreview = true,
                Dock = System.Windows.Forms.DockStyle.Fill
            };
            form.Controls.Add(host);
            return form.Handle.ToInt64();
        }
 
        private string _partNo;
        public string PartNo
        {
            get => _partNo;
            set => RaiseAndSetIfChanged(ref _partNo, value);
        }
 
        private string _partModel;
        public string PartModel
        {
            get => _partModel;
            set => RaiseAndSetIfChanged(ref _partModel, value);
        }
        private string _partBrandName;
        public string PartBrandName
        {
            get => _partBrandName;
            set => RaiseAndSetIfChanged(ref _partBrandName, value);
        }
 
        private string _partBussinesstypeName;
        public string PartBussinesstypeName
        {
            get => _partBussinesstypeName;
            set
            {
                RaiseAndSetIfChanged(ref _partBussinesstypeName, value);
                SelectBussinessType();
            }
        }
 
        private string _selectedBtName;
        public string SelectedBtName
        {
            get => _selectedBtName;
            set
            {
                RaiseAndSetIfChanged(ref _selectedBtName, value);
                SelectBussinesstypeSpec();
            }
        }
 
        /// <summary>
        /// 底部状态栏内容
        /// </summary>
        private string _statusBarText;
        public string StatusBarText
        {
            get => _statusBarText;
            set => RaiseAndSetIfChanged(ref _statusBarText, value);
        }
 
        /// <summary>
        /// 当前打开的文档路径
        /// </summary>
        private string _activeDocPath;
        public string ActiveDocPath
        {
            get => _activeDocPath;
            set => RaiseAndSetIfChanged(ref _activeDocPath, value);
        }
 
        private string _order;
        public string Order
        {
            get => _order;
            set => RaiseAndSetIfChanged(ref _order, value);
        }
 
        private string _column;
        public string Column
        {
            get => _column;
            set => RaiseAndSetIfChanged(ref _column, value);
        }
 
        private ObservableCollection<string> _btNames;
        public ObservableCollection<string> BtNames
        {
            get => _btNames;
            set => RaiseAndSetIfChanged(ref _btNames, value);
        }
        public Dictionary<string, string> BtCache { get; set; }
 
        #region 分页数据
        private long _currentPage;
        public long CurrentPage
        {
            get => _currentPage;
            set => RaiseAndSetIfChanged(ref _currentPage, value);
        }
 
        private long _totalPage;
        public long TotalPage
        {
            get => _totalPage;
            set => RaiseAndSetIfChanged(ref _totalPage, value);
        }
 
        private long _totalCount;
        public long TotalCount
        {
            get { return _totalCount; }
            set => RaiseAndSetIfChanged(ref _totalCount, value);
        }
 
        private long _size;
        public long Size
        {
            get { return _size; }
            set => RaiseAndSetIfChanged(ref _size, value);
        }
 
        private bool _loading = false;
        public bool Loading
        {
            get => _loading;
            set => RaiseAndSetIfChanged(ref _loading, value);
        }
        #endregion 分页数据
        #endregion 字段
 
        #region 控件
        #endregion
 
        /// <summary>
        /// 按钮查询函数
        /// </summary>
        /// <param name="sender">*</param>
        /// <param name="e">*</param>
        private void SimpleQuery(object sender, RoutedEventArgs e)
        {
            HttpSelectPartWithSpec(1, 10);
        }
 
        /// <summary>
        /// 重置参数并查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ResetQuery(object sender, RoutedEventArgs e)
        {
            ResetQuery();
            SimpleQuery(sender, e);
        }
 
        /// <summary>
        /// 详细查询按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SelectBussinesstypeSpec(object sender, RoutedEventArgs e)
        {
            SelectBussinesstypeSpec();
        }
 
        /// <summary>
        /// 重置规格按钮Click-清空规格参数并查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ResetSpec(object sender, RoutedEventArgs e)
        {
            ResetSpec();
            SimpleQuery(sender, e);
        }
 
        /// <summary>
        /// 换页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ChangePage(object sender, RoutedEventArgs e)
        {
            string type = (string)((Button)sender).Tag;
            long nextPage;
            switch (type)
            {
                case "1":
                    nextPage = CurrentPage + 1;
                    if (nextPage > TotalPage)
                    {
                        return;
                    }
                    HttpSelectPartWithSpec(nextPage, 10);
                    break;
                case "-1":
                    nextPage = CurrentPage - 1;
                    if (nextPage < 1)
                    {
                        return;
                    }
                    HttpSelectPartWithSpec(nextPage, 10);
                    break;
                case "2":
                    HttpSelectPartWithSpec(TotalPage, 10);
                    break;
                case "-2":
                    HttpSelectPartWithSpec(1, 10);
                    break;
                default:
                    break;
            }
        }
 
        /// <summary>
        /// 下载物料图纸
        /// </summary>
        /// <param name="sender">*</param>
        /// <param name="e">*</param>
        private void LoadPartDrawingDoc(object sender, RoutedEventArgs e)
        {
            try
            {
                PdmPart part = table.SelectedItem as PdmPart;
                DoAddCompnent(part);
                e.Handled = true;
            }
            catch (Exception ex)
            {
                this.Error($"加载图纸失败!异常:{ex}");
            }
            // HttpClientCreator.GetClient().GetDownloadWithName("penApi/wpf/bt/eDrawing", "C:\\temp");
        }
 
        /// <summary>
        /// 清空所有参数
        /// </summary>
        private void ResetQuery()
        {
            PartNo = null;
            PartModel = null;
            ResetSpec();
        }
 
        /// <summary>
        /// 清空规格参数
        /// </summary>
        private void ResetSpec()
        {
            //PartBussinesstypeName = "";
            //SelectedBtName = null;
            //if (BtNames != null)
            //{
            //    BtNames.Clear();
            //}
            //if (BtCache != null)
            //{
            //    BtCache.Clear();
            //}
            ObservableCollection<PartSpec> specs = (ObservableCollection<PartSpec>)specTable.ItemsSource;
            if (specs == null || specs.Count <= 0)
            {
                return;
            }
            foreach (PartSpec spec in specs)
            {
                spec.value = null;
            }
        }
 
        /// <summary>
        /// 更新分页相关
        /// </summary>
        /// <param name="page">从web返回的分页数据</param>
        private void UpdatePage(Page<PdmPart> page)
        {
            List<ListSortDirection?> cache = new List<ListSortDirection?>();
            foreach (DataGridColumn col in table.Columns)
            {
                cache.Add(col.SortDirection);
            }
            CurrentPage = page.current;
            TotalCount = page.total;
            Size = page.size;
            TotalPage = (long)Math.Ceiling(TotalCount * 1.0D / Size);
            table.ItemsSource = page.records;
            for (int i = 0; i < table.Columns.Count; i++)
            {
                table.Columns[i].SortDirection = cache[i];
            }
        }
 
        /// <summary>
        /// 物料行选中事件,预览图纸
        /// </summary>
        /// <param name="sender">*</param>
        /// <param name="e">*</param>
        private void table_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            object[] selectedItems = (object[])e.AddedItems;
            if (selectedItems.Length <= 0)
            {
                return;
            }
            PdmPart part = (PdmPart)selectedItems[0];
            if (string.IsNullOrEmpty(part.drawingPath))
            {
                StatusBarText = $"服务器未返回 [{part.partNo}] 图纸路径";
                return;
            }
            string path = GlobalConfig.SwFilePath + Path.DirectorySeparatorChar + part.drawingPath;
            if (!File.Exists(path))
            {
                StatusBarText = $"未找到 [{part.partNo}] 图纸文件:{path}";
                return;
            }
            StatusBarText = "";
            eControl.FilePath = GlobalConfig.SwFilePath + Path.DirectorySeparatorChar + part.drawingPath;
            //eControl.EDrawingHost.Dispose();
        }
 
        private void table_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            try
            {
                PdmPart part = table.SelectedItem as PdmPart;
                DoAddCompnent(part);
            }
            catch (Exception ex)
            {
                Logger.Error("加载图纸失败!异常:{0}", ex);
                this.Error($"加载图纸失败!异常:{ex}");
            }
        }
 
        private void DoAddCompnent(PdmPart part)
        {
            string absolutePath = MoveFileToUserDir(part);
            // string absolutePath = DownloadFileToUserDir(part);
            if (!string.IsNullOrEmpty(absolutePath))
            {
                AddComponent(absolutePath);
            }
        }
 
        /// <summary>
        /// 共享盘的方式
        /// </summary>
        /// <param name="part"></param>
        /// <returns></returns>
        private string MoveFileToUserDir(PdmPart part)
        {
            string newPath = null;
            if (!string.IsNullOrEmpty(part.drawingPath))
            {
                string absolutePath = GlobalConfig.SwFilePath + Path.DirectorySeparatorChar + part.drawingPath;
                newPath = GlobalConfig.UserPartsDir + Path.DirectorySeparatorChar + part.drawingPath;
                if (!File.Exists(newPath))
                {
                    Directory.CreateDirectory(Path.GetDirectoryName(newPath));
                    File.Copy(absolutePath, newPath, true);
                }
                else if (!MD5Util.FileEquals(absolutePath, newPath))
                {
                    // 防止文件只读
                    FileInfo info = new FileInfo(newPath);
                    info.IsReadOnly = false;
                    File.Delete(newPath);
                    File.Copy(absolutePath, newPath, true);
                }
            }
            return newPath;
        }
 
        /// <summary>
        /// 下载的方式
        /// </summary>
        /// <param name="part"></param>
        /// <returns></returns>
        private string DownloadFileToUserDir(PdmPart part)
        {
            string newPath = null;
            if (!string.IsNullOrEmpty(part.drawingPath))
            {
                newPath = GlobalConfig.UserPartsDir + Path.DirectorySeparatorChar + part.drawingPath;
                if (!File.Exists(newPath))
                {
                    Directory.CreateDirectory(Path.GetDirectoryName(newPath));
                    HttpClient c = clientCreator.GetClient();
                    c.GetDownloadSync($"openApi/wpf/downloadCadFile/{part.id}", newPath);
                }
                else if (!MD5Util.IsFile(newPath, part.md5))
                {
                    // 防止本地文件只读
                    FileInfo info = new FileInfo(newPath);
                    info.IsReadOnly = false;
                    File.Delete(newPath);
 
                    HttpClient c = clientCreator.GetClient();
                    c.GetDownloadSync($"openApi/wpf/downloadCadFile/{part.id}", newPath);
                }
            }
            return newPath;
        }
 
 
        private void table_Sorting(object sender, DataGridSortingEventArgs e)
        {
            Column = e.Column.SortMemberPath;
            if (e.Column.SortDirection == null)
            {
                e.Column.SortDirection = ListSortDirection.Ascending;
                Order = "ASC";
            }
            else if (e.Column.SortDirection == ListSortDirection.Ascending)
            {
                e.Column.SortDirection = ListSortDirection.Descending;
                Order = "DESC";
            }
            else
            {
                e.Column.SortDirection = null;
                Column = null;
                Order = "DESC";
            }
            HttpSelectPartWithSpec(1, 10);
            e.Handled = true;
        }
    }
}