M182轴承端盖外观缺陷AOI
kingno
2025-05-26 5a405c7dce20d8c79a733c9c786cc42eb59fe81c
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
using Autofac;
using Bro.Common.Interface;
using Bro.Common.Model;
using Bro.M135.Common;
using Bro.M135.DBManager;
using Bro.UI.Model.Winform;
using Sunny.UI;
using System.Data;
 
namespace Bro.M141.Process.UI
{
    //[MenuNode("产品检测记录", "产品检测记录", 3, Bro.Common.Helper.EnumHelper.TopMenu.SystemInfo, MenuNodeType.Form)]
    public partial class FrmProductSpec : MenuFormBase
    {
        M141Process Process_M141 => Process as M141Process;
        M141Config Config_M141 => Process.IConfig as M141Config;
 
        public FrmProductSpec()
        {
            InitializeComponent();
            dgvProductSpecs.AutoGenerateColumns = false;
            dgvProductSpecs.AllowUserToAddRows = false;
            dgvProductSpecs.AllowUserToDeleteRows = false;
            dgvProductSpecs.RowHeadersVisible = false;
            dgvProductSpecs.ShowEditingIcon = false;
 
            this.Load += (s, e) =>
            {
                chkAutoRefresh.Checked = true;
            };
        }
 
        List<ISpec> _defineSpecs = new List<ISpec>();
        public override void OnProcessUpdated()
        {
            base.OnProcessUpdated();
 
            dgvProductSpecs.Columns.Clear();
 
            var specList = new List<ISpec>
            {
                new Spec { Code = "NO"},
                new Spec { Code = "PID"},
                new Spec { Code = "SN" }
            };
 
            // 从 检测标准集合 中获取 缺陷列表
            //var temps = Config_M084.SpecCollection.Where(x => x.IsEnabled).Select(x => x as Spec);
            using (var scope = GlobalVar.Container.BeginLifetimeScope())
            {
                var config = scope.Resolve<IProcessConfig>();
                if (config is ISpecCollector sc)
                {
                    _defineSpecs = sc.GetSpecList();
                }
            }
 
            if (_defineSpecs.Any())
            {
                specList.AddRange(_defineSpecs.OrderBy(x => x.Code));
            }
 
            specList.ForEach(x =>
            {
                var col = new DataGridViewColumn(new DataGridViewTextBoxCell())
                {
                    HeaderText = x.Code,
                    Name = x.Code,
                    AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells,
                    MinimumWidth = x.Code == "NO" ? 60 : 100,
                    //DataPropertyName = nameof(x.ActualValue),
                };
                dgvProductSpecs.Columns.Add(col);
            });
        }
 
        //每隔4秒自动刷新一次
        int _refreshTimes = 0;
        public override void RefreshUIIn100()
        {
            if (!chkAutoRefresh.Checked)
                return;
 
            _refreshTimes++;
            if (_refreshTimes == 40)
            {
                _refreshTimes = 0;
                RefreshProductView();
            }
        }
 
        static object _dataViewLock = new object();
        private void RefreshProductView()
        {
            lock (_dataViewLock)
            {
                this.Invoke(() =>
                {
                    SetDgvProductSpecsData();
                    //dgvProductSpecs.DataSource = GetSource();
                });
            }
        }
 
        /// <summary>
        /// 填充产品检测列表记录
        /// </summary>
        private void SetDgvProductSpecsData()
        {
            dgvProductSpecs.Rows.Clear();
            var productModels = Process_M141.ProductSpecResultList;
            //var productModels = GetProductModelTest();
            var rowNo = 0;
            foreach (var product in productModels)
            {
                //var detectResultLists = product.Details.SelectMany(u => u.Value.DetectResults.Values).ToList();
                //List<DetectResult> defectList = detectResultLists.SelectMany(u => u.SelectMany(t => t)).ToList();
 
                int rowIndex = dgvProductSpecs.Rows.Add(); // 添加新行并获取其索引
                dgvProductSpecs.Rows[rowIndex].Cells["NO"].Value = ++rowNo;
                dgvProductSpecs.Rows[rowIndex].Cells["PID"].Value = product.PID;
                dgvProductSpecs.Rows[rowIndex].Cells["SN"].Value = product.SN;
 
                //var specs = defectList.SelectMany(x => x.Specs).ToList();
                List<ISpec> specs = new List<ISpec>();
                specs.AddRange(product.Details.SelectMany(u => u.SpecList).ToList().ConvertAll(u => (ISpec)u));
                specs.AddRange(product.Details.SelectMany(u => u.ResultList.SelectMany(d => d.Specs)));
 
                foreach (var defineSpec in _defineSpecs)
                {
                    var curCell = dgvProductSpecs.Rows[rowIndex].Cells[defineSpec.Code];
                    var actualSpec = specs.FirstOrDefault(x => x.Code == defineSpec.Code);
                    if (actualSpec == null)
                    {
                        curCell.Value = "";
                        continue;
                    }
 
                    //dr[i++] = temp.GetMeasureValueStr;
 
                    // 自行计算
                    if (actualSpec.ActualValue == null)
                    {
                        curCell.Value = "--";
                        curCell.Style.ForeColor = Color.Yellow; // TBD
                    }
                    else
                    {
                        curCell.Value = actualSpec.ActualValue;
                        if (defineSpec.StandardValue - defineSpec.Tolrenance_Negative <= actualSpec.ActualValue &&
                            actualSpec.ActualValue <= defineSpec.StandardValue + defineSpec.Tolrenance_Positive)
                        {
                            curCell.Style.ForeColor = Color.Green;// OK
                        }
                        else
                        {
                            curCell.Style.ForeColor = Color.Red;// NG
                        }
                    }
 
                    //// 另一种处理方式,不确定MeasureResult 业务是否已进行处理
                    //if (actualSpec.MeasureResult == null)
                    //{
                    //    curCell.Value = "--";
                    //    curCell.Style.ForeColor = Color.Yellow;
                    //}
                    //else
                    //{
                    //    curCell.Value = actualSpec.ActualValue;
                    //    curCell.Style.ForeColor = actualSpec.MeasureResult.Value ? Color.Green : Color.Red;
                    //}
                }
            }
 
            // DataGridView 自动滚动到未行
            if (dgvProductSpecs.RowCount >= 1)
            {
                dgvProductSpecs.FirstDisplayedScrollingRowIndex = dgvProductSpecs.RowCount - 1;
            }
        }
 
        /// <summary>
        /// 测试记录
        /// </summary>
        /// <returns></returns>
        private static List<ProductModel> GetProductModelTest()
        {
            var productSpecResultList = new List<ProductModel>();
            productSpecResultList.Add(new ProductModel
            {
                PID = "2024081117100955",
                SN = "SN1",
                Details = new List<P_PRODUCT_DETAIL>()
                {
                    { new P_PRODUCT_DETAIL()
                        {
                            SpecList = new List<Spec>
                            {
 
                                new Spec {  Code="FAI91A_X", ActualValue=1, MeasureResult = true, IsEnabled=true},
                                new Spec {  Code="FAI91A_Y", ActualValue=2},
                                new Spec {  Code="FAI91B_X", ActualValue=3},
                                new Spec {  Code="FAI91B_Y", ActualValue=4},
                                new Spec {  Code="FAI91_A", ActualValue=5},
                                new Spec {  Code="FAI91_B", ActualValue=6}
                            }
                        }
                    }
                }
            });
 
            productSpecResultList.Add(new ProductModel
            {
                PID = "2024081117100958",
                SN = "SN2",
                Details = new List<P_PRODUCT_DETAIL>()
                {
                    { new P_PRODUCT_DETAIL()
                        {
                            SpecList = new List<Spec>
                            {
 
                                new Spec {  Code="FAI91A_X", ActualValue=1, MeasureResult = true, IsEnabled=true},
                                new Spec {  Code="FAI91A_Y", ActualValue=2},
                                new Spec {  Code="FAI91B_X", ActualValue=3},
                                new Spec {  Code="FAI91B_Y", ActualValue=4},
                                new Spec {  Code="FAI91_A", ActualValue=5},
                                new Spec {  Code="FAI91_B", ActualValue=6}
                            }
                        }
                    }
                }
            });
            return productSpecResultList;
        }
    }
}