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
using DataEntity.Share;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Configuration;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Xml;
using XCommon.Log;
using XCore;
using XHandler.Class;
using XHandler.Class.DataEx;
using XHandler.View.ManualCoating;
using XImagingXhandler.XDAL;
using DataRWDAL;
using XHandler.View.MethodProperty;
using XHandler.Controls.Run.Com;
using XCoreBLL;
namespace XHandler.View.ManualPick
{
    /// <summary>
    /// 切胶转移孔位设置 的交互逻辑
    /// </summary>
    public partial class CutSetting : Window
    {
        // 挑菌的板位号
        private string choiceLatticeId = string.Empty;
        // 本画面的数据
        private ObservableCollection<ManualChoiceData> choiceDatas = new ObservableCollection<ManualChoiceData>();
        private CoatingFileBll coatingFileBll = new CoatingFileBll();
        CutGumFileBll cutGumFileBll = new CutGumFileBll();
        // 涂布耗材
        private List<string> lstCoatingLabwareText = new List<string>();
        private List<string> lstCoatingLabwareValue = new List<string>();
 
        public event EventHandler okEvent;
        public RunWnd launchView = null;    // 运行界面
        public XmlNode methodNode = null;
        public DataTable dtChoiceParams;
        bool bGroup = false;
 
        // 前画面传过来数据
        public ObservableCollection<BacteriaCoordinateEx> dgBacteriaCoordinates = null;
 
        public CutSetting()
        {
            InitializeComponent();
            this.Owner = (Window)Shared.Main;
        }
 
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                // 加载挑菌数据
                InitializeChoiceData();
 
                // 1:人工挑选菌落;0:成像系统自动挑选菌落
                string choicemode = dtChoiceParams==null?"1":dtChoiceParams.Rows[9]["属性值"].ToString();
                if (choicemode.Equals("0"))
                {
                    Console.WriteLine("ManualPickBacteria do");
                    btnOK_Click(null, null);
                }
            }
            catch (Exception ex)
            {
                LoggerHelper.ErrorLog("ERROR:", ex);
            }
        }
 
        /// <summary>
        /// 加载挑菌数据
        /// </summary>
        private void InitializeChoiceData()
        {
            // 挑菌的板位号
            choiceLatticeId = ConfigurationManager.AppSettings["choiceLatticeId"].ToString();
 
            if (methodNode != null)
            {
                // 涂布耗材
                lstCoatingLabwareText = methodNode.SelectSingleNode("labware/text").InnerText.ToString().Split(',').ToList();
                lstCoatingLabwareValue = methodNode.SelectSingleNode("labware/value").InnerText.ToString().Split(',').ToList();
            }
 
            try
            {
                #region
                dgCoatingData.ItemsSource = null;//清空
                choiceDatas.Clear();
 
                // 挑菌数据
                ObservableCollection<BacteriaCoordinateEx> choiceList = dgBacteriaCoordinates;
 
                Labware labwareDes = new Labware();
                int rowNum = 0;
                int columnNum = 0;
                XmlNodeList latticeDesXmlList = null;
 
                if (launchView != null)     // 运行界面
                {
                    XmlNode latticeEnv = launchView.xmlDocument.SelectSingleNode("root/env");
 
                    // 目标板耗材Id
                    string labwareIdDes = lstCoatingLabwareValue[0];
                    // 来源皿barcode
                    string barcodeSrc = string.Empty;
                    if (dtChoiceParams != null)
                    {
                        barcodeSrc = dtChoiceParams.Rows[1][2].ToString();
                    }
 
                    // 目标板耗材信息
                    labwareDes = LabwareDB.GetLabware(labwareIdDes);
                    if (labwareDes != null && labwareDes.labware_id != "")
                    {
                        rowNum = (int)labwareDes.number_row;
                        columnNum = (int)labwareDes.number_column;
                    }
                    else
                    {
                        System.Windows.Application.Current.Dispatcher.Invoke(new Action(() =>
                        {
                            string strMsg = "Please set lattices! Later try again.";
                            PlsToolTipWin plsToolTipWin = new PlsToolTipWin(strMsg);
                            plsToolTipWin.Topmost = true;
                            plsToolTipWin.Height = this.ActualHeight;
                            plsToolTipWin.Width = this.ActualWidth;
 
                            SolidColorBrush mybtn1_Brush = new SolidColorBrush(System.Windows.Media.Color.FromArgb(0, 0, 0, 0));
                            plsToolTipWin.Background = (System.Windows.Media.Brush)mybtn1_Brush;
                            plsToolTipWin.btnCancel.Visibility = Visibility.Hidden;
                            plsToolTipWin.ShowDialog();
                            this.Close();
                            return;
                        }));
                    }
 
                    // 获取涂布数据
                    GetCoatingData(latticeEnv, choiceList, barcodeSrc);
                }
 
                dgCoatingData.DataContext = choiceDatas;
                dgCoatingData.ItemsSource = choiceDatas;
 
                dgCoatingData.Columns[1].IsReadOnly = true;     // 来源位置
                dgCoatingData.Columns[5].IsReadOnly = true;     // 目标板位置
                dgCoatingData.Columns[3].IsReadOnly = true;
                dgCoatingData.Columns[4].IsReadOnly = true;
 
                #endregion
            }
            catch (Exception ex)
            {
                LoggerHelper.ErrorLog("ERROR:", ex);
            }
        }
 
        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                okEvent?.Invoke(this, EventArgs.Empty);
 
                // 将DataTable中数据写入到CSV文件中
                // 把 ObservableCollection<ManualChoiceData> 类型转为DataTable
                DataTable dt = cutGumFileBll.ManualChoiceDataTypeChangeToDataTable(choiceDatas);
                if (dt != null && dt.Rows.Count >= 0)
                {
                    var pickResultPath = ConfigurationManager.AppSettings["PickResultPath"];
                    string directoryName = Path.GetDirectoryName(pickResultPath);
                    if (!Directory.Exists(directoryName))
                    {
                        Directory.CreateDirectory(directoryName);
                    }
 
                    string strPickResultFile = string.Format("{0}{1}", pickResultPath, ConfigurationManager.AppSettings["PickResultFile"]);
                    if (File.Exists(strPickResultFile))
                    {
                        File.Delete(strPickResultFile);
                    }
                    ExcelAndCsvHelper.WriteDataTableToCSV(dt, strPickResultFile);
                }
 
                this.Close();
            }
            catch (Exception ex)
            {
                LoggerHelper.ErrorLog("ERROR:", ex);
            }
        }
 
        private void dgCoatingData_BeginningEdit(object sender, DataGridBeginningEditEventArgs e)
        {
 
        }
 
        private void dgCoatingData_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
        {
            //如果点击了放弃按钮或ESC
            if (e.EditAction == DataGridEditAction.Cancel)
            {
                return;
            }
            //获取编辑模式显示的元素,因为使用了模板,因此这里是ContentPresenter类型
            ContentPresenter cp = e.EditingElement as ContentPresenter;
            if (cp != null && VisualTreeHelper.GetChildrenCount(cp) > 0)
            {
                //找到编辑框
                TextBox textBox = VisualTreeHelper.GetChild(cp, 0) as TextBox;
                //如果找到
                if (textBox != null)
                {
                    var a = choiceDatas;
 
                    var dataGrid = sender as DataGrid;
                    dataGrid.BeginEdit();
 
 
                }
            }
        }
 
        private void dgCoatingData_PreviewMouseMove(object sender, MouseEventArgs e)
        {
 
        }
 
        private void dgCoatingData_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
        {
            if (e.AddedCells.Count > 1)
            {
                int a = 0;
            }
        }
 
        DataFillItems dataFillItems = null;
        private void dgCoatingData_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (bGroup)
            {
                DataFillInfo info = GetSelectedCellData();
 
                dataFillItems = new DataFillItems(info);
                dataFillItems.Height = this.ActualHeight;
                dataFillItems.Width = this.ActualWidth;
                dataFillItems.cutSetting = this;
                SolidColorBrush mybtn1_Brush = new SolidColorBrush(System.Windows.Media.Color.FromArgb(0, 0, 0, 0));
                dataFillItems.Background = (System.Windows.Media.Brush)mybtn1_Brush;
                dataFillItems.ShowDialog();
 
            }
            bGroup = false;
        }
 
        public void DataFillTips_OKEvent()
        {
            if (dataFillItems != null)
            {
                DataFillInfo info = (DataFillInfo)dataFillItems.dataFillInfo;
                if (info.nFrom > 0 && info.nTo > 0 && info.nFrom <= info.nTo)
                {
                    for (int i = info.nFrom - 1; i < info.nTo; i++)
                    {
                        ManualChoiceData data = choiceDatas[i];
                        if (info.column == 1)
                            data.sourceplate = info.columnContent;
                        else if (info.column == 2)
                            data.sourceplate_num = info.columnContent;
                        else if (info.column == 5)
                            data.targetplate = info.columnContent;
                        else if (info.column == 6)
                            data.targetplate_num = info.columnContent;
                        else if (info.column == 7)
                            data.targetplate_well = info.columnContent;
                    }
                }
            }
        }
 
        /// <summary>
        /// 获取选中单元格数据
        /// </summary>
        /// <param name="columnContent">返回第一个有内容的单元格里的内容</param>
        /// <returns>返回选中的行list</returns>
        private DataFillInfo GetSelectedCellData()
        {
            DataFillInfo dataFillInfo = new DataFillInfo();
 
            foreach (var cell in dgCoatingData.SelectedCells)
            {
                ManualChoiceData data = cell.Item as ManualChoiceData;
                dataFillInfo.column = cell.Column.DisplayIndex;
                if (string.IsNullOrEmpty(dataFillInfo.columnContent))
                {
                    if (dataFillInfo.column == 1)
                    {
                        dataFillInfo.columnName = Properties.Resources.strSourceDish;
                        dataFillInfo.columnContent = data.sourceplate;
                    }
                    else if (dataFillInfo.column == 2)
                    {
                        dataFillInfo.columnName = Properties.Resources.strSourceDishBarcode;
                        dataFillInfo.columnContent = data.sourceplate_num;
                    }
                    else if (dataFillInfo.column == 5)
                    {
                        dataFillInfo.columnName = Properties.Resources.strTargetBoard;
                        dataFillInfo.columnContent = data.targetplate;
                    }
                    else if (dataFillInfo.column == 6)
                    {
                        dataFillInfo.columnName = Properties.Resources.strDestBoardBarcode;
                        dataFillInfo.columnContent = data.targetplate_num;
                    }
                    else if (dataFillInfo.column == 7)
                    {
                        dataFillInfo.columnName = Properties.Resources.strTargetWell;
                        dataFillInfo.columnContent = data.targetplate_well;
                    }
                }
 
                DataGridCell dgc = GetDataGridCell(cell);
                DataGridRow dataGridRow = Utilities.FindVisualParent<DataGridRow>(dgc);
 
                int row = FindRowIndex(dataGridRow);
                dataFillInfo.rows.Add(row);
            }
            if (dataFillInfo.rows.Count > 0)
            {
                dataFillInfo.nFrom = dataFillInfo.rows[0] + 1;
                dataFillInfo.nTo = dataFillInfo.rows[dataFillInfo.rows.Count - 1] + 1;
            }
            return dataFillInfo;
        }
 
        private int FindRowIndex(DataGridRow row)
        {
            DataGrid dataGrid = ItemsControl.ItemsControlFromItemContainer(row) as DataGrid;
 
            int index = dataGrid.ItemContainerGenerator.IndexFromContainer(row);
 
            return index;
        }
 
        public DataGridCell GetDataGridCell(DataGridCellInfo cellInfo)
        {
            var cellContent = cellInfo.Column.GetCellContent(cellInfo.Item);
            if (cellContent != null)
                return (DataGridCell)cellContent.Parent;
 
            return null;
        }
 
        /// <summary>
        /// 获取涂布数据
        /// </summary>
        /// <param name="latticeEnv"></param>
        /// <param name="choiceList"></param>
        /// <param name="barcodeSrc"></param>
        private void GetCoatingData(XmlNode latticeEnv, ObservableCollection<BacteriaCoordinateEx> choiceList, string barcodeSrc)
        {
            // 涂布耗材集合:count = 2
            List<CoatingLabware> lstCoatingLabware = new List<CoatingLabware>();
            Labware labwareDes = null;
 
            #region 循环涂布耗材集合,添加耗材信息
            foreach (string coatingLabwareId in lstCoatingLabwareValue)
            {
                if (string.IsNullOrEmpty(coatingLabwareId))
                {
                    continue;
                }
 
                CoatingLabware coatingLab = new CoatingLabware();
                // 获取目标板耗材信息
                labwareDes = LabwareDB.GetLabware(coatingLabwareId);
                if (!string.IsNullOrEmpty(labwareDes.labware_id))
                {
                    coatingLab.RowNum = (int)labwareDes.number_row;
                    coatingLab.ColumnNum = (int)labwareDes.number_column;
                }
 
                // 耗材Id
                coatingLab.LabwareId = coatingLabwareId;
                // 所有涂布耗材台面信息
                
                //string xPath = string.Format("platform", coatingLabwareId);
                //XmlNode axmlNode = latticeEnv.SelectSingleNode(xPath);
                coatingLab.NodeList = latticeEnv.SelectNodes("platform");
                lstCoatingLabware.Add(coatingLab);
            }
            #endregion
 
            #region 获取涂布使用的所有目标板的孔位信息
            if (launchView.lstCoatingLabware == null)
            {
                foreach (CoatingLabware labwareType in lstCoatingLabware)
                {
                    // 拍照节点选了两个涂布耗材,实际台面上没有
                    if (labwareType.NodeList == null || labwareType.NodeList.Count == 0)
                    {
                        continue;
                    }
 
                    labwareType.ListCoatingWell = new List<CoatingWell>();
                    foreach (XmlNode latticeDesXmlItem in labwareType.NodeList)
                    {
                        XmlNode xnodeLabware = latticeDesXmlItem.SelectSingleNode("labware[@id=1]");
                        if (xnodeLabware != null)
                        {
                            string strLabwareTxt = xnodeLabware.SelectSingleNode("labware_id").InnerText;
                            if (strLabwareTxt == labwareType.LabwareId)
                            {
                                // 获取当前板所有孔
                                List<CoatingWell> lstCoatingWell = GetWellsForPlate(labwareType.RowNum, labwareType.ColumnNum, xnodeLabware);
                                labwareType.ListCoatingWell.AddRange(lstCoatingWell);
                            }
                        }
                    }
                }
 
                launchView.lstCoatingLabware = lstCoatingLabware;
            }
            #endregion
 
            // 循环挑菌数据
            for (int choiceCurrentNum = 1; choiceCurrentNum <= choiceList.Count; choiceCurrentNum++)
            {
                // 根据涂布接种次数动态生成数据
                for (int choiceTimeIndex = 0; choiceTimeIndex < launchView.choiceTimes; choiceTimeIndex++)
                {
                    // 涂布数据
                    ManualChoiceData choiceData = new ManualChoiceData();
                    choiceData.is_new_plate_batch = choiceCurrentNum == 1 ? true : false;
 
                    CoatingLabware coatingLabware = launchView.lstCoatingLabware[choiceTimeIndex];
                    if (coatingLabware.ListCoatingWell == null)
                    {
                        continue;
                    }
 
                    // 获取目标板的信息
                    CoatingWell coatingWell = coatingLabware.ListCoatingWell.FirstOrDefault(it => !it.IsUsed);
 
                    // 之前一批的目标板已经被使用完了,需要新一批次的目标板
                    if (coatingWell == null)
                    {
                        foreach (var coatingWellItem in coatingLabware.ListCoatingWell)
                        {
                            coatingWellItem.IsUsed = false;
                            coatingWellItem.Barcode = string.Empty;
                        }
 
                        // 再取一次未使用的有效孔
                        coatingWell = coatingLabware.ListCoatingWell.FirstOrDefault(it => !it.IsUsed);
                        choiceData.is_new_plate_batch = true;
                    }
 
                    // 当前空已被使用
                    coatingWell.IsUsed = true;
 
                    choiceData.choice_id = Convert.ToInt32(choiceList[choiceCurrentNum - 1].bacteriacoordinate_id);
                    launchView.choiceSrcLabwareSname = "P1";//coatingLabware.LabwarePositon;
                    choiceData.sourceplate = choiceTimeIndex == 0 ? launchView.choiceSrcLabwareSname : choiceDatas[choiceDatas.Count - 1].sourceplate;
                    choiceData.sourceplate_num = choiceTimeIndex == 0 ? barcodeSrc : choiceDatas[choiceDatas.Count - 1].sourceplate_num;
                    choiceData.machine_coordinate = choiceTimeIndex == 0 ? choiceList[choiceCurrentNum - 1].coordinate_machine : choiceDatas[choiceDatas.Count - 1].machine_coordinate;
                    choiceData.pixel_coordinate = choiceList[choiceCurrentNum - 1].coordinate_pixel;
                    choiceData.targetplate = coatingWell.LatticeNum;                // 方格编号
                    choiceData.targetplate_num = coatingWell.Barcode;               // 板编号
                    choiceData.targetplate_well = coatingWell.Site;                 // 孔号
                                                                                    // 是否是一个板子最后的一个有效孔:最后一个菌时,一定是板子的最后一个有效孔!
                    choiceData.is_plate_last_well = choiceCurrentNum == choiceList.Count ? true : coatingWell.IsPlateLastWell;
                    choiceDatas.Add(choiceData);
                }
            }
        }
 
        /// <summary>
        /// 获取当前板所有孔
        /// </summary>
        /// <param name="nRow"></param>
        /// <param name="nCol"></param>
        /// <param name="latticeNum"></param>
        /// <param name="latticeDesXmlItem"></param>
        /// <returns></returns>
        private List<CoatingWell> GetWellsForPlate(int nRow, int nCol, XmlNode latticeDesXml)
        {
            // TODO 需要和画面设定的有效孔位进行绑定
            List<CoatingWell> lstCoatingWell = new List<CoatingWell>();
 
            // 所有孔
            int nRowAscii = 65;
            for (int x = 1; x <= nCol; x++)
            {
                nRowAscii = 65;
 
                for (int y = 1; y <= nRow; y++, nRowAscii++)
                {
                    string strWell = string.Format("{0}{1}", Char.ConvertFromUtf32(nRowAscii), x);
                    CoatingWell coatingWell = new CoatingWell();
                    coatingWell.LatticeNum = latticeDesXml.SelectSingleNode("labware_sname").InnerText;
                    coatingWell.RowIndex = nRow;
                    coatingWell.ColIndex = nCol;
                    coatingWell.Site = strWell;
                    coatingWell.IsUsed = false;
                    coatingWell.Barcode = latticeDesXml.SelectSingleNode("labware_barcode").InnerText;
 
                    lstCoatingWell.Add(coatingWell);
                }
            }
 
            // 一个板子最后的一个有效孔
            if (lstCoatingWell.Count > 0)
            {
                lstCoatingWell.Last().IsPlateLastWell = true;
            }
            return lstCoatingWell;
        }
 
        private void DataGridCell_PreviewMouseMove(object sender, MouseEventArgs e)
        {
            if (e.OriginalSource is System.Windows.Shapes.Path)
            {
                Cursor = Cursors.Cross;
            }
            else
                Cursor = Cursors.Arrow;
        }
 
        private void DataGridCell_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            Debug.WriteLine("Button down");
            if (e.OriginalSource is System.Windows.Shapes.Path)
            {
                bGroup = true;
 
            }
        }
 
        private void DataGridCell_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            Debug.WriteLine("Double click");
            DataGridCell cell = sender as DataGridCell;
            if ((cell.Column.DisplayIndex == 1) || (cell.Column.DisplayIndex == 2) || (cell.Column.DisplayIndex == 5)
                || (cell.Column.DisplayIndex == 6) || (cell.Column.DisplayIndex == 7))
            {
                cell.Column.IsReadOnly = false;
            }
            cell.Focus();
            cell.IsSelected = true;
            dgCoatingData.BeginEdit();
 
            e.Handled = true;
        }
    }
}