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
using Bro.Common.Base;
using Bro.Common.Helper;
using Bro.Common.Interface;
using Bro.UI.Model.Winform;
using HalconDotNet;
using Sunny.UI;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
 
namespace Bro.M141.Process.UI
{
    //[MenuNode("点检配置", "点检配置", 8, Common.Helper.EnumHelper.TopMenu.SystemInfo, MenuNodeType.Form)]
    public partial class FormInspectionSetting : MenuFormBase
    {
        M141Process prM141Process => Process as M141Process;
        M141Config M141Config => Process.IConfig as M141Config;
        InspectionConfig InspectionConfig => prM141Process.InspectionConfig;
 
        List<string> _allDefects = new List<string>();
 
        public FormInspectionSetting()
        {
            InitializeComponent();
 
            flpDefects.Padding = new System.Windows.Forms.Padding(10, 10, 0, 0);
            flpDefects.AutoScroll = true;
            flpDefects.VerticalScroll.Visible = true;
            flpDefects.SizeChanged += FlpDefects_SizeChanged;
        }
 
 
 
 
        public override void OnProcessUpdated()
        {
            base.OnProcessUpdated();
 
            this.Invoke(() =>
            {
                flpDefects.Controls.Clear();
                var allSpecs = M141Config.GetSpecList();
 
                _allDefects = M141Config.DefectSwitchCollection.Where(u => u.IsEnable && allSpecs.All(s => s.Code != u.DefectName)).Select(u => u.DefectName).OrderBy(u => u).ToList();
                //var allPositionSet = M084Config.PositionSetCollection.Select(u => u.PositionNum).ToList();
                //var allPositionSet2 = M084Config.PositionSetCollection.Select(u => u.PositionNum2).ToList();
 
                LoadPositionSetCtrls(_allDefects, M141Config.WorkPositionCollection);
 
                //M084Config.DefectSwitchCollection.Where(u => u.IsEnable && allSpecs.All(s => s.Code != u.DefectName)).ForEach(u =>
                //{
                //    var existed = InspectionConfig.CheckDefectNames.FirstOrDefault(t => t.DefectName == u.DefectName);
                //    bool isChecked = existed != null;
                //    if (existed == null)
                //    {
                //        existed = new DefectPositionSet()
                //        {
                //            PositionNum = 1,
                //            DefectName = u.DefectName,
                //        };
                //    }
 
                //    CtrlDefectPositionSet ctrlPset = new CtrlDefectPositionSet(existed, isChecked, allPositionSet);
                //    flpDefects.Controls.Add(ctrlPset);
                //});
 
                lblLastSpotCheck.Text = InspectionConfig.LastInspectTime == null ? "无历史点检记录" : $"上次点检时间:{InspectionConfig.LastInspectTime.Value.ToString("yyyy-MM-dd HH:mm:ss")}";
 
                pgInspectionConfig.SelectedObject = InspectionConfig;
            });
        }
 
        private void LoadPositionSetCtrls(List<string> allDefects, List<WorkPositionSet> allPositionSet)
        {
            flpDefects.Controls.Clear();
 
            allPositionSet.ForEach(u =>
            {
                for (int i = 1; i <= 2; i++)
                {
                    var positionDefects = InspectionConfig.CheckDefectNames.Where(d => d.PositionNum == u.PositionName && d.ProduceIndex == i.ToString()).ToList();
                    CtrlDefectsInPosition ctrl = new CtrlDefectsInPosition(_allDefects, u.PositionName, positionDefects, i.ToString());
                    flpDefects.Controls.Add(ctrl);
                }
            });
 
            FlpDefects_SizeChanged(null, null);
        }
 
 
        private void FlpDefects_SizeChanged(object? sender, EventArgs e)
        {
            foreach (Control c in flpDefects.Controls)
            {
                c.Width = flpDefects.Width - 30;
            }
        }
 
 
        public HImage ConvertHObjectToHImage(HObject obj)
        {
            HOperatorSet.CountChannels(obj, out HTuple channels);
 
            HImage img = new HImage();
            if (channels.I == 1)
            {
                HTuple pointer, type, width, height;
                HOperatorSet.GetImagePointer1(obj, out pointer, out type, out width, out height);
 
                //img.GenImage1Extern(type, width, height, pointer, IntPtr.Zero);
                img.GenImage1(type, width, height, pointer);
            }
            else
            {
                HTuple pRed, pGreen, pBlue, type, width, height;
                HOperatorSet.GetImagePointer3(obj, out pRed, out pGreen, out pBlue, out type, out width, out height);
 
                img.GenImage3(type, width, height, pRed, pGreen, pBlue);
            }
 
            return img;
        }
 
        private void btnSave_Click(object sender, EventArgs e)
        {
 
            //HObject image = new HObject();
            //HObject zoomImage = new HObject();
 
            //// 读取图像
            //HOperatorSet.ReadImage(out image, "C:\\Users\\饶俊\\Desktop\\1111.png");
 
            //// 调用zoom_image_factor算子
            //HOperatorSet.ZoomImageFactor(image, out zoomImage, 0.5, 0.5, "constant");
 
            ////HOperatorSet.Threshold(zoomImage, out HObject Regions, 0, 255);
            ////HOperatorSet.GrayFeatures(Regions, image, "mean", out HTuple grayValue);
 
 
            //// 打印灰度值
            ////MessageBox.Show( grayValue.D.ToString());
            //HImage a=  ConvertHObjectToHImage(zoomImage);
            //var bitmap = a.ConvertHImageToBitmap();
            //bitmap.Save("C:\\Users\\饶俊\\Desktop\\222.png", ImageFormat.Png);
 
            //return;
 
 
 
 
            List<DefectPositionSet> defectNames = new List<DefectPositionSet>();
 
            foreach (Control c in flpDefects.Controls)
            {
                if (c is CtrlDefectsInPosition cc)
                {
                    var defects = cc.GetPositionDefects();
                    defectNames.AddRange(defects);
                }
            }
 
 
 
 
 
            //if (defectNames.Count == 0)
            //{
            //    MessageBox.Show("未选择点检缺陷信息", "保存点检配置提示");
            //    return;
            //}
 
            InspectionConfig.CheckDefectNames = defectNames;
 
            if (prM141Process.SaveInspectionConfig(out string error))
            {
                MessageBox.Show("点检配置信息保存成功!");
            }
            else
            {
                MessageBox.Show($"点检配置信息保存失败\r\n{error}");
            }
        }
 
        //private void chkSelectAll_CheckedChanged(object sender, EventArgs e)
        //{
        //    foreach (Control c in flpDefects.Controls)
        //    {
        //        if (c is CtrlDefectPositionSet chk)
        //        {
        //            chk.SetChecked(chkSelectAll.Checked);
        //        }
        //    }
        //}
    }
}