schangxiang@126.com
2025-11-04 f5ed29dc26c7cd952d56ec5721a2efc43cd25992
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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
using ControlzEx.Standard;
using DataEntity;
using DataEntity.Share;
using DataRWDAL;
using HandyControl.Data;
using NPOI.POIFS.Properties;
using NPOI.SS.Formula.Functions;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;
using XCommon.Image;
using XCommon.Log;
using XCommon.Tip;
using XCoreBLL;
using XHandler.Class;
using XHandler.Class.DataEx;
using XHandler.Controls;
using XHandler.View.MethodProperty;
using XImagingXhandler.XDAL;
 
namespace XHandler.View.Tabletop
{
    /// <summary>
    /// 台面模板管理 的交互逻辑
    /// </summary>
    public partial class TabletopManagement : UserControl
    {
        ObservableCollection<TabletopTemplate> tabletopTemplates;          //系统中可用的台面模板数据集
        OperateAuditLogBll operateAuditLogBll = new OperateAuditLogBll();  //向系统中提交操作时数据的类对象
        ObservableCollection<Lattice> latticesList;                        //台面内获取到的所有板位数据集
        public MainWindow mainWindow = null;//主窗体
 
        #region 构造函数
        public TabletopManagement()
        {
            InitializeComponent();
 
            this.DataContext = tabletopTemplates;
        }
        #endregion
 
        #region 加载绑定已创建的台面模板列表
        public void BindingTabletopListOnListbox()
        {
            try
            {
                tabletopTemplates = TabletopTemplateDB.GetTabletopTemplateCollectionFromdb();
                lbxTabletopList.ItemsSource = tabletopTemplates;
                if (tabletopTemplates != null && tabletopTemplates.Count > 0)
                {
                    lbxTabletopList.SelectedIndex = 0;
                }
            }
            catch (Exception ex)
            {
                LoggerHelper.ErrorLog("BindingTabletopListOnListbox Error ", ex);
            }
        }
        #endregion
 
        #region 加载绑定已创建的载架视图列表
        public void BindingTubeLabwareOnListView()
        {
            try
            {
                ObservableCollection<Labware> labwares = LabwareDB.GetSpecialShelfLabware();
                //调用比例进行板材的像素转换
                foreach (var item in labwares)
                {
                    item.labware_length = ConvertScale.GetPixelDistanceByConvertScale(item.labware_length);
                    item.labware_width = ConvertScale.GetPixelDistanceByConvertScale(item.labware_width);
                }
                lvConsumable.ItemsSource = labwares;
            }
            catch (Exception ex)
            {
 
            }
        }
        #endregion
 
        #region 选定台面模板后,处理更新台面板位数据
        private void lbxTabletopList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (lbxTabletopList.SelectedIndex < 0)
                return;
 
            gridTable.Children.Clear();
 
            TabletopTemplate tabletopTemplate = lbxTabletopList.SelectedItem as TabletopTemplate;
 
            if (tabletopTemplate != null)
            {
                //加载该台面模板
                latticesList = TabletopTemplateDB.GetLatticesByTabletopTemplateId(tabletopTemplate.tabletopid);
                foreach(Lattice s in latticesList)
                {
                    Labware labware = LabwareDB.GetLabware(s.labware_id);
                    double widthPixel = labware.labware_width / 2;
                    double lengthPixel = labware.labware_length / 2;
 
                    ShelfLabware shelfLabware = new ShelfLabware()
                    {
                        ShelfWidth = widthPixel,
                        ShelfLength = lengthPixel,
                        ShelfName = s.lattice_num.ToString(),
                        ShelfDBId = s.lattice_id,
                        ShelfTrash = s.is_trash == 1 ? true : false
                    };
 
                    shelfLabware.AllowDrop = false;
                    labware.lattice_pixel_x = s.lattice_pixel_x;
                    labware.lattice_pixel_y = s.lattice_pixel_y;
                    shelfLabware.DataContext = (Labware)labware;
                    shelfLabware.MouseLeftButtonDown += ShelfLabware_MouseLeftButtonDown;
                    shelfLabware.PreviewMouseLeftButtonUp += ShelfLabware_MouseLeftButtonUp;
                    QueryContinueDrag += DoQueryContinueDrag;
                    shelfLabware.Loaded += ShelfLabwareInitial_Loaded;
                    shelfLabware.PreviewMouseMove += ShelfLabware_PreviewMouseMove;
                    shelfLabware.PreviewMouseRightButtonDown += ShelfLabware_PreviewMouseRightButtonDown;
                    shelfLabware.ContextMenu = FindResource("tableLatticeMenu") as ContextMenu;
                    gridTable.Children.Add(shelfLabware);
                    System.Threading.Thread.Sleep(10);
                }
            }
        }
        #endregion
 
        #region 选定的台面,加载板位完成后,对板位属性的处理
        private void ShelfLabwareInitial_Loaded(object sender, RoutedEventArgs e)
        {
            ShelfLabware shelfLabware = sender as ShelfLabware;
            shelfLabware.HorizontalAlignment = HorizontalAlignment.Left;
            shelfLabware.VerticalAlignment = VerticalAlignment.Top;
            if(shelfLabware.ShelfTrash)
            {
                shelfLabware.ShelfTrash = false;
                shelfLabware.ShelfTrash = true;
            }
            //Canvas.SetLeft(shelfLabware, (shelfLabware.DataContext as Labware).lattice_pixel_x);
            //Canvas.SetTop(shelfLabware, (shelfLabware.DataContext as Labware).lattice_pixel_y);
            shelfLabware.Margin = new Thickness((shelfLabware.DataContext as Labware).lattice_pixel_x, (shelfLabware.DataContext as Labware).lattice_pixel_y, 0, 0);
        }
        #endregion
 
        #region 页面加载事件,绑定数据处理
        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            BindingTabletopListOnListbox();
            BindingTubeLabwareOnListView();
            //CalculationPixels();
            SetTabletopGraduated();
        }
        #endregion
 
        #region 重置台面
        private void btnResetTabletop_Click(object sender, RoutedEventArgs e)
        {
            //判断已加的板位是否保存过,保存过则更新数据库
            List<ShelfLabware> shelfLabwares = Utilities.FindAllVisualChild<ShelfLabware>(gridTable);
            foreach (ShelfLabware s in shelfLabwares)
            {
                if (!string.IsNullOrEmpty(s.ShelfDBId))
                {
                    Lattice lattice = LatticeDB.GetLatticeDataByIdFromdb(s.ShelfDBId);
                    lattice.lattice_state = 0;
                    TabletopRelLatticeDB.DeleteLatticeFromdb(lattice);
                }
                else
                {
 
                }
            }
 
            gridTable.Children.Clear();
        }
        #endregion
 
        #region 新加一个台面模板
        /// <summary>
        /// 新加一个台面模板
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public TabletopTemplate AddNewTabletopTemplate(string name)
        {
            TabletopTemplate newTabletopTemplate = new TabletopTemplate();
            newTabletopTemplate.tabletopname = name;
            newTabletopTemplate.tabletopstate = 1;
            newTabletopTemplate.usestate = 0;
            newTabletopTemplate.tabletopid = Guid.NewGuid().ToString();
 
            int ret = TabletopTemplateDB.AddTabletopTemplateIntodb(newTabletopTemplate);
            if (ret > 0)
            {
                tabletopTemplates.Add(newTabletopTemplate);
                PlsSetProperty plsSetProperty = new PlsSetProperty($"台面模板:{name} 添加成功", false);
                plsSetProperty.closeEvent += PlsSetProperty_closeEvent;
                System.Windows.Window wnd = Application.Current.MainWindow;
                Grid parent = Utilities.FindVisualChild<Grid>(wnd);
                parent.Children.Add(plsSetProperty);
            }
            else
            {
                newTabletopTemplate = null;
                PlsSetProperty plsSetProperty = new PlsSetProperty($"台面模板:{name} 添加失败", false);
                plsSetProperty.closeEvent += PlsSetProperty_closeEvent;
                System.Windows.Window wnd = Application.Current.MainWindow;
                Grid parent = Utilities.FindVisualChild<Grid>(wnd);
                parent.Children.Add(plsSetProperty);
            }
 
            return newTabletopTemplate;
        }
        #endregion
 
        #region 关闭新建时弹出的窗口
        private void PlsSetProperty_closeEvent(object sender, EventArgs e)
        {
            System.Windows.Window wnd = Application.Current.MainWindow;
            Grid grid = Utilities.FindVisualChild<Grid>(wnd);
 
            UIElement element = sender as UIElement;
            grid.Children.Remove(element);
        }
        #endregion
 
        #region 布局当前台面的刻度尺
        /// <summary>
        /// 布局当前台面的刻度尺
        /// </summary>
        public void SetTabletopGraduated()
        {
            double length = Shared.SoftwareInformation.realtabletoplength;//mm
            double width = Shared.SoftwareInformation.realtabletopwidth;//mm
 
            gridTable.Width = gridBaseTabletop.Width = length / 2;//统一缩小2倍,即1个像素代表2mm
            gridTable.Height = gridBaseTabletop.Height = width / 2;
        }
        #endregion
 
        #region 获取当前给定显示台面区域的像素长度,映射出的实际长度
        /// <summary>
        /// 获取当前给定显示台面区域的像素长度,映射出的实际长度
        /// </summary>
        public void CalculationPixels()
        {
            double length = 150d;//Shared.SoftwareInformation.realtabletoplength;//mm
            double width = 125d;//Shared.SoftwareInformation.realtabletopwidth;//mm
            double lengthPixelVal = MillimetersToPixelsWidth(length);
            double widthPixedVal = MillimetersToPixelsWidth(width);
 
            //计算像素比例,然后设置控件长宽
            double ratioOfTabletop = widthPixedVal / lengthPixelVal;
 
            int zoomVal = 1, zoomBaseVal = 1;
            int i = 0;
            double calLengthVal = lengthPixelVal / zoomVal;
            while (calLengthVal > 1000)
            {
                i++;
                zoomVal = zoomBaseVal * i;
                calLengthVal = lengthPixelVal / zoomVal;
            }
            Shared.tabletopReduceTimes = zoomVal;
 
 
            //设置台面控件像素长宽
            double tabletopPixedXVal = lengthPixelVal / zoomVal;
            double tabletopPixedYVal = tabletopPixedXVal * ratioOfTabletop;
 
            gridBaseTabletop.Width = tabletopPixedXVal;
            gridBaseTabletop.Height = tabletopPixedYVal;
 
            //计算每个像素对应实际台面多少mm
            double disX = 1 * zoomVal;
            double disY = 1 * zoomVal;
 
            // 假设1像素等于2.54毫米(约等于一英寸)
            disX = PixelsCalculationHelper.PixelsToInches(1 * zoomVal, PixelsCalculationHelper.DpiX) * 2.54;
            disY = PixelsCalculationHelper.PixelsToInches(1 * zoomVal, PixelsCalculationHelper.DpiY) * 2.54;
 
            //50mm占用多少个像素
            double countPixelX = 50d / disX;
            double countPixelY = 50d / disY;
        }
        #endregion
 
        #region 毫米转换为像素
        /// <summary>
        /// 毫米转换为像素
        /// </summary>
        /// <param name="length">length是毫米</param>
        /// <returns>像素值</returns>
        public static double MillimetersToPixelsWidth(double length) //length是毫米,1厘米=10毫米
        {
            System.Windows.Forms.Panel p = new System.Windows.Forms.Panel();
            System.Drawing.Graphics g = System.Drawing.Graphics.FromHwnd(p.Handle);
            IntPtr hdc = g.GetHdc();
            int width = GetDeviceCaps(hdc, 4);     // HORZRES
            int pixels = GetDeviceCaps(hdc, 8);     // BITSPIXEL
            g.ReleaseHdc(hdc);
            return (((double)pixels / (double)width) * (double)length);
        }
 
        [DllImport("gdi32.dll")]
        private static extern int GetDeviceCaps(IntPtr hdc, int Index);     //导入系统方法,获取当前分辨率下的像素和距离值
        #endregion
 
        TextBlock toolTip = null;    //鼠标在台面内移动过程中弹出的提示对象
        System.Windows.Point mouseMovePos = new System.Windows.Point(0, 0);  //鼠标在台面内移动时,实时存储鼠标点的坐标数据
 
        #region 鼠标在台面上移动事件,移动过程中显示坐标
        private void gridTable_MouseMove(object sender, MouseEventArgs e)
        {
            // 获取鼠标相对于Grid的位置
            mouseMovePos = e.GetPosition(gridTable);
 
            //像素坐标值转换为cm值
            double length = (double)mouseMovePos.X * 2 / 10d;
            double width = (double)mouseMovePos.Y * 2 / 10d;
 
            // 显示坐标值
            if (toolTip == null)
            {
                toolTip = new TextBlock();
                toolTip.Text = "(x:" + length.ToString() + ",y:" + width.ToString() + ")";
                toolTip.Background = Brushes.Transparent;
                toolTip.Foreground = Brushes.Black;
                toolTip.Loaded += ToolTip_Loaded;
                gridTable.Children.Add(toolTip);
            }
            else
            {
                toolTip.Margin = new Thickness(mouseMovePos.X, mouseMovePos.Y, 0, 0);
                toolTip.Text = "(x:" + length.ToString() + ",y:" + width.ToString() + ")";
            }
 
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                System.Windows.Point currentPosition = e.GetPosition(gridTable);
 
                var currentPoint = e.GetPosition(gridTable);
 
                Console.WriteLine("currentPoint x:{0},y:{1}", currentPoint.X, currentPoint.Y);
                double left = Math.Min(startPoint.X, currentPoint.X);
                double top = Math.Min(startPoint.Y, currentPoint.Y);
                double gwidth = Math.Abs(startPoint.X - currentPoint.X);
                double height = Math.Abs(startPoint.Y - currentPoint.Y);
 
                //Grid.SetLeft(selectionRectangle, left);
                //Grid.SetTop(selectionRectangle, top);
                //gridTable.Children.Add(selectionRectangle);
                //Grid.SetRow(selectionRectangle, 0);
                //Grid.SetColumn(selectionRectangle, 0);
                if (selectionRectangle != null)
                {
                    selectionRectangle.Width = gwidth;
                    selectionRectangle.Height = height;
                    selectionRectangle.Margin = new Thickness(left, top, 0, 0);
                }
 
                if ((Math.Abs(currentPosition.X - _lastMouseDown.X) > SystemParameters.MinimumHorizontalDragDistance)
                    || (Math.Abs(currentPosition.Y - _lastMouseDown.Y) > SystemParameters.MinimumVerticalDragDistance))
                {
                    if (currentSelectedShelf == null)
                    {
                        return;
                    }
 
                    DragDropAdorner adorner = new DragDropAdorner((UIElement)currentSelectedShelf);
                    mAdornerLayer = AdornerLayer.GetAdornerLayer(gridTable);
                    mAdornerLayer.Add(adorner);
                    Labware lb = currentSelectedShelf.DataContext as Labware;
                    LabwareEx labwareEx = new LabwareEx(lb);
                    DataObject data = new DataObject("LabwareEx", labwareEx);
                    DragDrop.DoDragDrop(currentSelectedShelf, data, DragDropEffects.Move);
 
                    mAdornerLayer.Remove(adorner);
                    mAdornerLayer = null;
                }
 
                if(!isDragging)
                {
                    isDragging= true;
                }
 
                if (!isPressing)
                {
                    isPressing = true;
                }
            }
        }
        #endregion
 
        #region 提示框加载显示完成后的处理
        private void ToolTip_Loaded(object sender, RoutedEventArgs e)
        {
            toolTip.HorizontalAlignment = HorizontalAlignment.Left;
            toolTip.VerticalAlignment= VerticalAlignment.Top;
            toolTip.Margin = new Thickness(mouseMovePos.X, mouseMovePos.Y, 0, 0);
            //mAdornerLayerTool.Remove(adornerTool);
            //mAdornerLayerTool = null;
        }
        #endregion
 
        private System.Windows.Point _lastMouseDown;       //鼠标左键按下选中板位列表中某个板位时,鼠标点的位置
        private AdornerLayer mAdornerLayer = null;         //拖动选中的板位耗材时,控制跟随显示板位的对象
 
        #region 鼠标左键按下选中板位列表中某个板位的事件
        private void lvConsumable_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.ChangedButton == MouseButton.Left)
            {
                _lastMouseDown = e.GetPosition(lvConsumable);
            }
        }
        #endregion
 
        #region 鼠标从选中某个列表中的板位后,开始移动时的处理
        private void lvConsumable_PreviewMouseMove(object sender, MouseEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                System.Windows.Point currentPosition = e.GetPosition(lvConsumable);
                if ((Math.Abs(currentPosition.X - _lastMouseDown.X) > SystemParameters.MinimumHorizontalDragDistance)
                    || (Math.Abs(currentPosition.Y - _lastMouseDown.Y) > SystemParameters.MinimumVerticalDragDistance))
                {
                    HitTestResult result = VisualTreeHelper.HitTest(lvConsumable, currentPosition);
                    if (result == null)
                        return;
 
                    ListViewItem lvi = Utilities.FindVisualParent<ListViewItem>(result.VisualHit);
                    if (lvi == null)
                        return;
                    if (lvConsumable.SelectedItem == null)
                        return;
                    DragDropAdorner adorner = new DragDropAdorner((UIElement)lvi);
                    mAdornerLayer = AdornerLayer.GetAdornerLayer(lvConsumable);
                    mAdornerLayer.Add(adorner);
 
                    Labware lb = lvConsumable.SelectedItem as Labware;
                    LabwareEx labwareEx = new LabwareEx(lb);
                    DataObject data = new DataObject("LabwareEx", labwareEx);
                    ShelfLabware slabware= Utilities.FindLastVisualChild<ShelfLabware>(lvi);
                    DragDrop.DoDragDrop(slabware, data, DragDropEffects.Copy);
                    if (mAdornerLayer == null)
                        mAdornerLayer = AdornerLayer.GetAdornerLayer(lvConsumable);
                    {
                        mAdornerLayer.Remove(adorner);
                        mAdornerLayer = null;
                    }
                }
                isDragging = true;
            }
        }
        #endregion
 
        #region 继续拖动控制,实时更新拖动对象的显示UI
        private void DoQueryContinueDrag(object sender, QueryContinueDragEventArgs e)
        {
            if (mAdornerLayer != null)
            {
                mAdornerLayer.Update();
            }
        }
        #endregion
 
        #region 从板位耗材列表中拖放板位后或从台面内拖放某个板位后,放置板位的处理
        System.Windows.Point mouseDropPos=new System.Windows.Point(0,0);   //存储某个板位被放置后的时刻,鼠标点的位置数据
 
        private void gridTable_Drop(object sender, DragEventArgs e)
        {
            //再次取得目标gridTable内鼠标的坐标位置
            mouseDropPos = e.GetPosition(gridTable);
 
            if (e.Effects == DragDropEffects.Move)
            {
 
            }
 
            LabwareEx lb = e.Data.GetData("LabwareEx") as LabwareEx;
            if (e.Effects == DragDropEffects.Copy ||
                e.Effects == DragDropEffects.Move)
            {
                //lb.label = canvas.Name;
            }
 
            if (isDragging)
            {
                isDragging = false;
                if (!isPressing)
                {
                    //构建一个Shelf对象
                    //根据两1个像素,计算出板位的款和高;
                    double widthPixel = lb.labware_width / 2;
                    double lengthPixel = lb.labware_length / 2;
 
                    int countOfShelfInGD = Utilities.FindAllVisualChild<ShelfLabware>(gridTable).Count;
 
                    ShelfLabware shelfLabware = new ShelfLabware()
                    {
                        ShelfWidth = widthPixel,
                        ShelfLength = lengthPixel,
                        ShelfName = "P" + (countOfShelfInGD + 1).ToString(),
                        //Width= lengthPixel,
                        //Height= widthPixel
                    };
 
                    shelfLabware.AllowDrop = false;
                    shelfLabware.DataContext = (Labware)lb;
                    shelfLabware.MouseLeftButtonDown += ShelfLabware_MouseLeftButtonDown;
                    shelfLabware.PreviewMouseLeftButtonUp += ShelfLabware_MouseLeftButtonUp;
                    QueryContinueDrag += DoQueryContinueDrag;
                    shelfLabware.Loaded += ShelfLabware_Loaded;
                    shelfLabware.PreviewMouseMove += ShelfLabware_PreviewMouseMove;
                    shelfLabware.PreviewMouseRightButtonDown += ShelfLabware_PreviewMouseRightButtonDown;
                    shelfLabware.ContextMenu = FindResource("tableLatticeMenu") as ContextMenu;
                    gridTable.Children.Add(shelfLabware);
                    
                }
                else
                {
                    double left = currentSelectedShelf.Margin.Left;
                    double top = currentSelectedShelf.Margin.Top;
 
                    //Canvas.SetLeft(currentSelectedShelf, left + e.GetPosition(gridTable).X - dragStartPoint.X);
                    //Canvas.SetTop(currentSelectedShelf, top + e.GetPosition(gridTable).Y - dragStartPoint.Y);
                    //原始位置
                    //新位置
                    double currentPixelXVal = e.GetPosition(gridTable).X;
                    double currentPixelYVal = e.GetPosition(gridTable).Y;
                    //变化
                    double increaseXVal = dragStartPoint.X - currentPixelXVal;
                    double increaseYVal = dragStartPoint.Y - currentPixelYVal;
                    if (increaseXVal > 0)
                    {
                        left = left - increaseXVal;
                    }
                    else
                    {
                        left = left + increaseXVal;
                    }
                    if (increaseYVal > 0)
                    {
                        top = top - increaseYVal;
                    }
                    else
                    {
                        top = top + increaseYVal;
                    }
 
                    currentSelectedShelf.Margin = new Thickness(currentPixelXVal, currentPixelYVal, 0, 0);
                    currentSelectedShelf.ReleaseMouseCapture();
                    currentSelectedShelf = null;
                    isPressing = false;
                }
            }
            else
            {
                if (isPressing)
                {
                    if (currentSelectedShelf != null)
                    {
                        currentSelectedShelf.ReleaseMouseCapture();
                        currentSelectedShelf = null;
                    }
                    isPressing = false;
                }
                else
                {
                    //构建一个Shelf对象
                    //根据两1个像素,计算出板位的款和高;
                    double widthPixel = lb.labware_width / 2;
                    double lengthPixel = lb.labware_length / 2;
 
                    int countOfShelfInGD = Utilities.FindAllVisualChild<ShelfLabware>(gridTable).Count;
 
                    ShelfLabware shelfLabware = new ShelfLabware()
                    {
                        ShelfWidth = widthPixel,
                        ShelfLength = lengthPixel,
                        ShelfName = "P" + (countOfShelfInGD + 1).ToString(),
                        //Width= lengthPixel,
                        //Height= widthPixel
                    };
 
                    shelfLabware.AllowDrop = false;
                    shelfLabware.DataContext = (Labware)lb;
                    shelfLabware.MouseLeftButtonDown += ShelfLabware_MouseLeftButtonDown;
                    shelfLabware.PreviewMouseLeftButtonUp += ShelfLabware_MouseLeftButtonUp;
                    QueryContinueDrag += DoQueryContinueDrag;
                    shelfLabware.Loaded += ShelfLabware_Loaded;
                    shelfLabware.PreviewMouseMove += ShelfLabware_PreviewMouseMove;
                    shelfLabware.PreviewMouseRightButtonDown += ShelfLabware_PreviewMouseRightButtonDown;
                    shelfLabware.ContextMenu = FindResource("tableLatticeMenu") as ContextMenu;
                    gridTable.Children.Add(shelfLabware);
                }
            }
        }
        #endregion
 
        #region 鼠标左键点击台面上某个板位后抬起左键事件
        private void ShelfLabware_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            isPressing= false;
            Console.WriteLine("si1");
        }
        #endregion
 
        #region 鼠标右键点击台面上某个板位后,创建右键菜单
        private void ShelfLabware_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            ShelfLabware selectedShelfLabware = sender as ShelfLabware;
 
            if (selectedShelfLabware == null)
                return;
            if (selectedShelfLabware != null)
            {
                ContextMenu menu = FindResource("tableLatticeMenu") as ContextMenu;
                MenuItem miTrash = menu.Items[0] as MenuItem;
                MenuItem miUnTrash = menu.Items[1] as MenuItem;
                if (selectedShelfLabware.ShelfTrash)
                {
                    miTrash.IsEnabled = false;
                    miUnTrash.IsEnabled = true;
                }
                else
                {
                    miTrash.IsEnabled = true;
                    miUnTrash.IsEnabled = false;
                }
            }
        }
        #endregion
 
        #region 鼠标点移动到某个台面板位上时,阻止坐标提示显示
        private void ShelfLabware_PreviewMouseMove(object sender, MouseEventArgs e)
        {
            if (toolTip != null)
            {
                gridTable.Children.Remove(toolTip);
                toolTip = null;
            }
            //e.Handled = true;
        }
        #endregion
 
        #region 台面上的板位完成加载显示后需要处理的内容
        private void ShelfLabware_Loaded(object sender, RoutedEventArgs e)
        {
            ShelfLabware shelfLabware=sender as ShelfLabware;
            shelfLabware.HorizontalAlignment = HorizontalAlignment.Left;
            shelfLabware.VerticalAlignment = VerticalAlignment.Top;
            shelfLabware.Margin = new Thickness(mouseDropPos.X, mouseDropPos.Y, 0, 0);
        }
        #endregion
 
        private bool isPressing = false;                    //鼠标左键是否按住台面上的某个板位:true,false
        private bool isDragging = false;                    //鼠标左键按住后是否拖动,true,false
        private System.Windows.Point dragStartPoint;        //鼠标左键按住台面上的某个板位位置后,获取该位置坐标点
        private ShelfLabware currentSelectedShelf = null;   //鼠标左键按住台面上的某个板位,该板位对象
        #region 鼠标左键在台面显示区域内按下事件
        private void ShelfLabware_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            isPressing = true;
            dragStartPoint = e.GetPosition(gridTable);
            if (e.ChangedButton == MouseButton.Left)
            {
                //_lastMouseDown = e.GetPosition(sender as ShelfLabware);
                currentSelectedShelf = sender as ShelfLabware;
                //currentSelectedShelf.CaptureMouse();
                double widthPixel = (currentSelectedShelf.DataContext as Labware).labware_width / 2;
                double lengthPixel = (currentSelectedShelf.DataContext as Labware).labware_length / 2;
                currentSelectedShelf.Width = lengthPixel;
                currentSelectedShelf.Height = widthPixel;
            }
        }
        #endregion
        
 
        #region 鼠标左键从台面显示区域内抬起事件
        private void gridTable_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (isDragging)
            {
                isDragging = false;
                if (currentSelectedShelf != null)
                {
                    currentSelectedShelf.ReleaseMouseCapture();
                }
            }
 
            if(isPressing)
            {
                isPressing = false;
            }
            if (selectionRectangle != null)
            {
                gridTable.Children.Remove(selectionRectangle);
            }
 
            //ShelfLabware shelfLabware = parentVisual as ShelfLabware;
            //循环构建板位对象,添加到集合中
            foreach (ShelfLabware item in Utilities.FindAllVisualChild<ShelfLabware>(gridTable))
            {
                Point endPoint = e.GetPosition(gridTable);
                Rect rect = new Rect(startPoint, endPoint);
 
                // 遍历canvas内的所有控件,根据控件的位置和选择框位置判断是否选中
                foreach (ShelfLabware element in Utilities.FindAllVisualChild<ShelfLabware>(gridTable))//UIElement element in canvas.Children)
                {
                    //VisualTreeHelper.get
                    if (rect.Contains(new Point(item.Margin.Left,item.Margin.Top)))
                    {
                        // 选中控件的逻辑
                        // 例如: element.IsSelected = true;
                        //if (item.ShelfName.Equals(shelfLabware.ShelfName))
                        //{
                            item.IsSelected = true;
                        //}
                    }
                }
                
            }
        }
        #endregion
 
        #region 鼠标从台面模板内移出事件
        private void gridTable_MouseLeave(object sender, MouseEventArgs e)
        {
            if (toolTip != null)
            {
                gridTable.Children.Remove(toolTip);
                toolTip = null;
            }
        }
        #endregion
 
        #region 台面模板内板位右键移除选中的板位
        private void tableLatticeMenuDelete_Click(object sender, RoutedEventArgs e)
        {
            MenuItem mi = e.Source as MenuItem;
            ContextMenu menu = mi.Parent as ContextMenu;
            ShelfLabware selectedShelfLabware = menu.PlacementTarget as ShelfLabware;
            
            if (selectedShelfLabware == null )
                return;
            if (selectedShelfLabware != null)
            {
                int deletedIndex= Convert.ToInt32(selectedShelfLabware.ShelfName.Substring(1, selectedShelfLabware.ShelfName.Length - 1));
                gridTable.Children.Remove(selectedShelfLabware);
                //latticesList.Remove(latticesList.FirstOrDefault(x => x.lattice_num.Equals(selectedShelfLabware.ShelfName)));//集合中移除
                if(!string.IsNullOrEmpty(selectedShelfLabware.ShelfDBId))
                {
                    Lattice lattice = LatticeDB.GetLatticeDataByIdFromdb(selectedShelfLabware.ShelfDBId);
                    lattice.lattice_state = 0;
                    TabletopRelLatticeDB.DeleteLatticeFromdb(lattice);
                }
                else
                {
 
                }
                //TabletopRelLatticeDB.DeleteLatticeFromdb(LatticeDB.GetLatticeDataByIdFromdb(selectedShelfLabware.ShelfName,Shared.DeviceArmList.FirstOrDefault(x=>x.arm_type.Equals(1)).device_arm_id,Shared.SoftwareInformation.software_device_number));
 
                //所有label向前移动一位
                List<ShelfLabware> shelfLabwares = Utilities.FindAllVisualChild<ShelfLabware>(gridTable);
                foreach(var item in shelfLabwares)
                {
                    int index = Convert.ToInt32(item.ShelfName.Substring(1, item.ShelfName.Length - 1));
                    if(deletedIndex<index)
                    {
                        item.ShelfName = "P" + (index - 1).ToString();
 
                        if (!string.IsNullOrEmpty(item.ShelfDBId))
                        {
                            Lattice lattice = LatticeDB.GetLatticeDataByIdFromdb(item.ShelfDBId);
                            lattice.lattice_num = item.ShelfName;
                            TabletopRelLatticeDB.UpdateLatticeFromdb(lattice);
                        }
                    }
                }
            }
        }
        #endregion
 
        #region 保存当前选中的台面模板
        /// <summary>
        /// 保存当前选中的台面模板
        /// </summary>
        public void SaveTabletopTemplate()
        {
            if (lbxTabletopList.SelectedIndex < 0)
            {
                ShowTip.ShowNotice(string.Format("{0}失败{1}{2}", "保存", Environment.NewLine, "请选中一个台面模板再保存!"), InfoType.Warning);
                return;
            }
 
            TabletopTemplate tabletopTemplate = lbxTabletopList.SelectedItem as TabletopTemplate;
 
            if (tabletopTemplate != null)
            {
                List<Lattice> lattices = new List<Lattice>();
                
 
                string operateContent = string.Empty;
                int sum = 0;
                //循环构建板位对象,添加到集合中
                foreach (ShelfLabware item in Utilities.FindAllVisualChild<ShelfLabware>(gridTable))
                {
                    Lattice lattice = new Lattice();
                    lattice.software_information_id = Shared.SoftwareInformation.software_information_id;
                    lattice.lattice_num = item.ShelfName;
                    operateContent += "板位:" + item.ShelfName;
                    lattice.lattice_X = 0d;
                    lattice.lattice_Y = 0d;
                    lattice.lattice_Z = 0d;
                    lattice.lattice_length = item.ShelfLength*2;
                    lattice.lattice_width= item.ShelfWidth*2;
                    lattice.lattice_lp_X = 0d;
                    lattice.lattice_lp_Y = 0d;
                    lattice.lattice_lp_Z = 0d;
                    lattice.gripper_rotational = 0d;
                    lattice.gripper_over_rotational = 0d;
                    lattice.device_arm_id = Shared.DeviceArmList.FirstOrDefault(x => x.arm_type.Equals(1)).device_arm_id;
                    lattice.is_trash = 0;
                    lattice.lattice_ch = 0;
                    lattice.lattice_pixel_x = item.Margin.Left;
                    lattice.lattice_pixel_y = item.Margin.Top;
                    lattice.labware_id = (item.DataContext as Labware).labware_id;
                    lattice.lattice_state = 1;
                    lattice.is_trash = item.ShelfTrash ? 1 : 0;
                    lattices.Add(lattice);
                    if(!string.IsNullOrEmpty(item.ShelfDBId))
                    {
                        lattice.lattice_id= item.ShelfDBId;
                        int ret = TabletopRelLatticeDB.UpdateLatticeFromdb(lattice);  //更新lattice
 
                        TabletopRelLattice tabletopRelLattice = new TabletopRelLattice()
                        {
                            lattice_id = Convert.ToInt32(lattice.lattice_id),
                            tabletopid = tabletopTemplate.tabletopid,
                            lattice_pixel_x = lattice.lattice_pixel_x,
                            lattice_pixel_y = lattice.lattice_pixel_y,
                            labware_id = lattice.labware_id,
                            isbaselattice = 1
                        };
                        int ret1 = TabletopRelLatticeDB.UpdateTabletopRelLatticeFromdb(tabletopRelLattice);//更新关系坐标
                        if (ret>0&&ret1>0)
                        {
                            sum++;
                        }
                    }
                    else
                    {
                        int index = TabletopRelLatticeDB.GetMaxLattceId();//maxDBid
                        int ret = TabletopRelLatticeDB.AddLatticeIntodb(lattice, index + 1, tabletopTemplate.tabletopid);
                        if (ret > 0)
                        {
                            sum++;
                            item.ShelfDBId = (index + 1).ToString();
                        }
                    }
                }
                
                //板位添加进入数据库
                //int result=TabletopRelLatticeDB.AddLatticeIntodb(lattices, index+1, tabletopTemplate.tabletopid);
 
                if(sum == Utilities.FindAllVisualChild<ShelfLabware>(gridTable).Count)//成功
                {
                    PlsSetProperty plsSetProperty = new PlsSetProperty($"台面模板:{tabletopTemplate.tabletopname} 保存成功", false);
                    plsSetProperty.closeEvent += PlsSetProperty_closeEvent;
                    System.Windows.Window wnd = Application.Current.MainWindow;
                    Grid parent = Utilities.FindVisualChild<Grid>(wnd);
                    parent.Children.Add(plsSetProperty);
                    OperateAuditLogDB.AddOperateAuditLogIntodb(operateAuditLogBll.GenerateOperateAuditLog("修改", operateContent, Shared.User.username, "台面模板", "", "", "台面模板[id:" + tabletopTemplate.tabletopid + "]", "成功"));
                }
                else
                {
                    PlsSetProperty plsSetProperty = new PlsSetProperty($"台面模板:{tabletopTemplate.tabletopname} 保存失败", false);
                    plsSetProperty.closeEvent += PlsSetProperty_closeEvent;
                    System.Windows.Window wnd = Application.Current.MainWindow;
                    Grid parent = Utilities.FindVisualChild<Grid>(wnd);
                    parent.Children.Add(plsSetProperty);
                    OperateAuditLogDB.AddOperateAuditLogIntodb(operateAuditLogBll.GenerateOperateAuditLog("修改", operateContent, Shared.User.username, "台面模板", "", "", "台面模板[id:" + tabletopTemplate.tabletopid + "]", "失败"));
                }
            }
        }
        #endregion
 
        #region 设置为垃圾桶菜单
        private void tableMenuSetTrash_Click(object sender, RoutedEventArgs e)
        {
            MenuItem mi = e.Source as MenuItem;
            ContextMenu menu = mi.Parent as ContextMenu;
            ShelfLabware selectedShelfLabware = menu.PlacementTarget as ShelfLabware;
 
            if (selectedShelfLabware == null)
                return;
            if (selectedShelfLabware != null)
            {
                //设置垃圾桶
                if (string.IsNullOrEmpty(selectedShelfLabware.ShelfDBId))
                {
                    selectedShelfLabware.ShelfTrash = true;
                }
                else
                {
                    selectedShelfLabware.ShelfTrash = true;
                    Lattice lattice = LatticeDB.GetLatticeDataByIdFromdb(selectedShelfLabware.ShelfDBId);
                    lattice.is_trash = 1;
                    int ret=LatticeDB.UpdateLatticeIntodb(lattice);
                }
                
            }
        }
        #endregion
 
        #region 取消垃圾桶
        private void tableMenuSetUnTrash_Click(object sender, RoutedEventArgs e)
        {
            MenuItem mi = e.Source as MenuItem;
            ContextMenu menu = mi.Parent as ContextMenu;
            ShelfLabware selectedShelfLabware = menu.PlacementTarget as ShelfLabware;
 
            if (selectedShelfLabware == null)
                return;
            if (selectedShelfLabware != null)
            {
                //取消垃圾桶
                if (string.IsNullOrEmpty(selectedShelfLabware.ShelfDBId))
                {
                    selectedShelfLabware.ShelfTrash = false;
                }
                else
                {
                    selectedShelfLabware.ShelfTrash = false;
                    Lattice lattice = LatticeDB.GetLatticeDataByIdFromdb(selectedShelfLabware.ShelfDBId);
                    lattice.is_trash = 0;
                    int ret = LatticeDB.UpdateLatticeIntodb(lattice);
                }
 
            }
        }
        #endregion
 
        #region 台面模板右键设置应用到此设备菜单事件
        private void tableMenuSetSystemAppTemplate_Click(object sender, RoutedEventArgs e)
        {
            MenuItem mi = e.Source as MenuItem;
            ContextMenu menu = mi.Parent as ContextMenu;
            var selectedTabletopTemplateListItem = menu.PlacementTarget as ListBox;
            PlsToolTipWin plsToolTipWin = null;
            plsToolTipWin = new PlsToolTipWin(Properties.SettingsTextResource.strTabletopManageSetApplicationTipInfo);
            plsToolTipWin.ShowDialog();
            // 确认卸载
            if (plsToolTipWin.DialogResult == true)
            {
 
                TabletopTemplate selectedTabletopTemplate = selectedTabletopTemplateListItem.SelectedItem as TabletopTemplate;
                if (selectedTabletopTemplate == null)
                    return;
                if (selectedTabletopTemplate != null)
                {
                    ObservableCollection<TabletopTemplate> tabletopTemplates = TabletopTemplateDB.GetTabletopTemplateCollectionFromdb();
                    int sum = 0;
                    //设置所有模板
                    foreach (TabletopTemplate t in tabletopTemplates)
                    {
                        if (t.tabletopid.Equals(selectedTabletopTemplate.tabletopid))
                        {
                            t.usestate = 1;
                        }
                        else
                        {
                            t.usestate = 0;
                        }
 
                        int ret = TabletopTemplateDB.UpdateTabletopTemplateIntodb(t);
                        if (ret > 0)
                        {
                            sum++;
                        }
                    }
                    if (sum == tabletopTemplates.Count)
                    {
                        ShowTip.ShowNotice(string.Format("{0}成功{1}{2}", "设置", Environment.NewLine, "设置应用到此设备成功!"), InfoType.Warning);
                    }
                    else
                    {
                        ShowTip.ShowNotice(string.Format("{0}失败{1}{2}", "设置", Environment.NewLine, "设置应用到此设备失败!"), InfoType.Warning);
                    }
                    BindingTabletopListOnListbox();
 
                    //刷新系统台面,刷新前
                    if(mainWindow!=null)
                    {
                        mainWindow.testDesign = null;
 
                        if(RunWnd.hadRun)
                        {
                            mainWindow.runWnd= null;
                        }
                    }
                }
            }
        }
        #endregion
 
        #region 台面模板列表右键删除菜单事件
        private void tableMenuDeleteTabletopTemplate_Click(object sender, RoutedEventArgs e)
        {
            MenuItem mi = e.Source as MenuItem;
            ContextMenu menu = mi.Parent as ContextMenu;
            var selectedTabletopTemplateListItem = menu.PlacementTarget as ListBox;
            TabletopTemplate selectedTabletopTemplate =  selectedTabletopTemplateListItem.SelectedItem as TabletopTemplate;
            if (selectedTabletopTemplate == null)
                return;
            if (selectedTabletopTemplate != null)
            {
                selectedTabletopTemplate.tabletopstate = 0;
                int ret = TabletopTemplateDB.UpdateTabletopTemplateIntodb(selectedTabletopTemplate);
 
                if (ret >0)
                {
                    ShowTip.ShowNotice(string.Format("{0}成功{1}{2}", "删除", Environment.NewLine, "删除台面模板成功!"), InfoType.Warning);
                }
                else
                {
                    ShowTip.ShowNotice(string.Format("{0}失败{1}{2}", "删除", Environment.NewLine, "删除台面模板失败!"), InfoType.Warning);
                }
 
                //刷新list
                gridTable.Children.Clear();
                BindingTabletopListOnListbox();
            }
        }
        #endregion
 
        #region 台面模板列表右键,使用出厂台面
        private void tableMenuSetSystemDefaultAppTemplate_Click(object sender, RoutedEventArgs e)
        {
            PlsToolTipWin plsToolTipWin = null;
            plsToolTipWin = new PlsToolTipWin(Properties.SettingsTextResource.strTabletopManageSetApplicationTipInfo);
            plsToolTipWin.ShowDialog();
            // 确认卸载
            if (plsToolTipWin.DialogResult == true)
            {
                ObservableCollection<TabletopTemplate> tabletopTemplates = TabletopTemplateDB.GetTabletopTemplateCollectionFromdb();
                int sum = 0;
                //设置所有模板
                foreach (TabletopTemplate t in tabletopTemplates)
                {
                    t.usestate = 0;
 
                    int ret = TabletopTemplateDB.UpdateTabletopTemplateIntodb(t);
                    if (ret > 0)
                    {
                        sum++;
                    }
                }
 
                if (sum == tabletopTemplates.Count)
                {
                    ShowTip.ShowNotice(string.Format("{0}成功{1}{2}", "设置", Environment.NewLine, "设置出厂台面成功!"), InfoType.Warning);
                }
                else
                {
                    if (tabletopTemplates.Count == 0)
                    {
                        ShowTip.ShowNotice(string.Format("{0}成功{1}{2}", "设置", Environment.NewLine, "设置出厂台面成功!"), InfoType.Warning);
                    }
                    else
                    {
                        ShowTip.ShowNotice(string.Format("{0}失败{1}{2}", "设置", Environment.NewLine, "设置出厂台面失败!"), InfoType.Warning);
                    }
                }
 
                BindingTabletopListOnListbox();
 
                //刷新系统台面,刷新前
                if (mainWindow != null)
                {
                    mainWindow.testDesign = null;
                    if (RunWnd.hadRun)
                    {
                        mainWindow.runWnd = null;
                    }
                }
            }
        }
        #endregion
 
        #region 多选板位 可顶部对齐/左对齐
        public void AssignmentShelfLabware()
        {
            //if(Keyboard.)
        }
        #endregion
 
        #region 开启选中范围检查处理
        private System.Windows.Point startPoint;
        private Rectangle selectionRectangle;
        private void gridTable_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            try
            {
                startPoint = e.GetPosition(gridTable);
                Console.WriteLine("startPoint x:{0},y:{1}",startPoint.X,startPoint.Y);
 
                //循环清除所有选中状态
                foreach (ShelfLabware item in Utilities.FindAllVisualChild<ShelfLabware>(gridTable))
                {
                    item.IsSelected = false;
                }
 
                //selectionRectangle = new Rectangle();
                //gridTable.Children.Add(selectionRectangle);
                //Canvas.SetLeft(selectionRectangle, startPoint.X);
                //Canvas.SetTop(selectionRectangle, startPoint.Y);
                //selectionRectangle.Fill = Brushes.Transparent;
                //selectionRectangle.Stroke = Brushes.Blue;
                //selectionRectangle.StrokeDashArray = new DoubleCollection() { 2, 2 };1232
 
                HitTestResult result = VisualTreeHelper.HitTest(gridTable, startPoint);
                var parentVisual = VisualTreeHelper.GetParent(result.VisualHit);
                if (parentVisual is ShelfLabware)
                {
                    ShelfLabware shelfLabware = parentVisual as ShelfLabware;
                    //循环构建板位对象,添加到集合中
                    foreach (ShelfLabware item in Utilities.FindAllVisualChild<ShelfLabware>(gridTable))
                    {
                        if (item.ShelfName.Equals(shelfLabware.ShelfName))
                        {
                            item.IsSelected = true;
                        }
                    }
                }
            }
            catch(Exception ex)
            {
                LoggerHelper.ErrorLog("gridTable_MouseLeftButtonDown error", ex);
            }
        }
        #endregion
 
        #region 选中的板位,顶部对其
        private void btnTop_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                ShelfLabware minNumShelfLabware = new ShelfLabware();
                List<ShelfLabware> selectedShelfLabwares = GetAllSelectedShelfLabware(out minNumShelfLabware);
                foreach (ShelfLabware shelf in selectedShelfLabwares)
                {
                    shelf.Margin = new Thickness(shelf.Margin.Left, minNumShelfLabware.Margin.Top, 0, 0);
                }
            }
            catch (Exception ex)
            {
                LoggerHelper.ErrorLog("btnTop_Click error", ex);
            }
        }
        #endregion
 
        #region 选中的板位,左侧对其
        private void btnLeft_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                ShelfLabware minNumShelfLabware = new ShelfLabware();
                List<ShelfLabware> selectedShelfLabwares = GetAllSelectedShelfLabware(out minNumShelfLabware);
                foreach (ShelfLabware shelf in selectedShelfLabwares)
                {
                    shelf.Margin = new Thickness(minNumShelfLabware.Margin.Left, shelf.Margin.Top, 0, 0);
                }
            }
            catch(Exception ex)
            {
                LoggerHelper.ErrorLog("btnLeft_Click error", ex);
            }
        }
        #endregion
 
        #region 获取所有当前选中的板位对象,按小编号在前排序
        private List<ShelfLabware> GetAllSelectedShelfLabware(out ShelfLabware minNumShelfLabware)
        {
            List<ShelfLabware> shelfLabwaresResult = new List<ShelfLabware>();
            List<ShelfLabware> shelfLabwares = new List<ShelfLabware>();
            minNumShelfLabware = null;
 
            //循环获取所有选中状态的板位
            foreach (ShelfLabware item in Utilities.FindAllVisualChild<ShelfLabware>(gridTable))
            {
                if(item.IsSelected == true)
                {
                    shelfLabwares.Add(item);
                }
            }
 
            int currentTotal = shelfLabwares.Count;
            for (int i=0;i< currentTotal; i++)
            {
                ShelfLabware shelfLabware = GetMinIdShelfLabware(shelfLabwares);
                if(i==0)
                {
                    minNumShelfLabware = shelfLabware;
                }
 
                shelfLabwaresResult.Add(shelfLabware);
                shelfLabwares.Remove(shelfLabware);
            }
                
 
            return shelfLabwaresResult;
        }
        #endregion
 
        #region 求出最小的板位对象
        private ShelfLabware GetMinIdShelfLabware(List<ShelfLabware> shelfLabwares)
        {
            ShelfLabware shelfLabware = null;
            ShelfLabware firstShelfLabware = shelfLabwares[0];
            shelfLabware= firstShelfLabware;
            int num = Convert.ToInt32(firstShelfLabware.ShelfName.Substring(1));
            for (int i = 1; i < shelfLabwares.Count; i++)
            {
                if(num> Convert.ToInt32(shelfLabwares[i].ShelfName.Substring(1)))
                {
                    shelfLabware = shelfLabwares[i];
                    num = Convert.ToInt32(shelfLabwares[i].ShelfName.Substring(1));
                }
            }
 
            return shelfLabware;
        }
        #endregion
    }
}