private void SaveImage(HTuple handle, bool result)
        {
            try
            {
                Task.Run(() =>
                {
                    string imageName = $"{DateTime.Now:HHmmss}.tiff";

                    string res        = result ? "OK" : "NG";
                    string pathSource = $@"{ProductMgr.GetInstance().ProductPath}Images\{DateTime.Now:yyyyMMdd}\{res}\Robot\Source\";
                    string pathWindow = $@"{ProductMgr.GetInstance().ProductPath}Images\{DateTime.Now:yyyyMMdd}\{res}\Robot\Window\";

                    if (!System.IO.Directory.Exists(pathSource))
                    {
                        System.IO.Directory.CreateDirectory(pathSource);
                    }

                    if (!System.IO.Directory.Exists(pathWindow))
                    {
                        System.IO.Directory.CreateDirectory(pathWindow);
                    }

                    string fileNameSource = $"{pathSource}{imageName}";
                    string fileNameWindow = $"{pathWindow}{imageName}";

                    HDevelopExport.WriteImage(imgSrc, fileNameSource);
                    HDevelopExport.DumpWindow(handle, fileNameWindow);
                });
            }
            catch (Exception)
            {
            }
        }
コード例 #2
0
        private void ShowProductAll()
        {
            string[] productCollection = ProductMgr.GetInstance().GetProductList();
            lstProduct.Items.Clear();
            lstProduct.Items.AddRange(productCollection);
            lstProduct.SelectedIndex = 0;

            grpProductManage.Text = $@"当前产品-{ProductMgr.GetInstance().ProductName}";
        }
コード例 #3
0
        private void btnAddProduct_Click(object sender, EventArgs e)
        {
            bool result = ProductMgr.GetInstance().Add(txtProduct.Text);

            if (!result)
            {
                MessageBox.Show($"产品{txtProduct.Text}已存在");
            }
            else
            {
                ShowProductAll();
            }
        }
コード例 #4
0
        private void btnSelectProduct_Click(object sender, EventArgs e)
        {
            bool result = ProductMgr.GetInstance().ChangeProduct(txtProduct.Text);

            if (!result)
            {
                MessageBox.Show($"没有{txtProduct.Text}产品");
            }
            else
            {
                grpProductManage.Text = $@"当前产品-{ProductMgr.GetInstance().ProductName}";
            }
        }
コード例 #5
0
        public void SaveParam()
        {
            Param.ProductName = ProductMgr.GetInstance().ProductName;

            //序列化SystemParam类实例到文件
            XmlSerializer xs = new XmlSerializer(typeof(SystemParam));

            string path = $@"{Environment.CurrentDirectory}\Product\SystemParam.xml";

            System.IO.FileStream fs = new System.IO.FileStream(path, System.IO.FileMode.Create);

            xs.Serialize(fs, Param);
            fs.Close();
        }
コード例 #6
0
        private void btnDelProduct_Click(object sender, EventArgs e)
        {
            if (lstProduct.Items.Count <= 1)
            {
                MessageBox.Show($"至少需要保留一个产品");
                return;
            }

            if (txtProduct.Text == ProductMgr.GetInstance().ProductName)
            {
                MessageBox.Show($"{txtProduct.Text}是当前产品,不能删除");
                return;
            }

            DialogResult result = MessageBox.Show($"确认删除{txtProduct.Text}产品?", "删除产品", MessageBoxButtons.OKCancel);

            if (result == DialogResult.OK)
            {
                ProductMgr.GetInstance().Delete(txtProduct.Text);
                ShowProductAll();
            }
        }
        public override bool ProcessImage(VisionControl ctl)
        {
            try
            {
                //清理数据
                foreach (var mea in MeasureMgr.GetInstance().MeasureList)
                {
                    mea.ClearResult();
                }

                ctl.clearObj();
                ctl.DisplayResults();

                //保存图像
                //string imageName = $"{DateTime.Now:HHmmss}.tiff";

                //if (AutoForm._autoForm.Param.IsSaveImageAll)
                //{
                //    //保存原图
                //    string path = $@"{ProductMgr.GetInstance().ProductPath}Images\Robot\{DateTime.Now:yyMMdd}\Source\";
                //    if (!System.IO.Directory.Exists(path))
                //    {
                //        System.IO.Directory.CreateDirectory(path);
                //    }
                //    string fileName = $"{path}{imageName}";
                //    HDevelopExport.WriteImage(imgSrc, fileName);
                //}

                //图像预处理
                HObject image;
                if (ProductMgr.GetInstance().Param.PlatformRegion != null)
                {
                    HOperatorSet.ReduceDomain(imgSrc, ProductMgr.GetInstance().Param.PlatformRegion, out image);

                    //if (ProductMgr.GetInstance().Param.IsPerprocess)
                    //{
                    //    image = HDevelopExport.Preprocess(image, ProductMgr.GetInstance().Param.Emphasize, false);
                    //}
                }
                else
                {
                    image = imgSrc;
                }

                //查找模板
                HTuple row, column, angle, scale, score;
                bool   result = HDevelopExport.FindScaleShapeModel(image, out row, out column, out angle, out scale, out score);

                //显示轮廓
                HDevelopExport.dev_display_shape_matching_results(ctl.GetHalconWindow(),
                                                                  ProductMgr.GetInstance().Param.ModelID, "blue", row, column, angle, scale, scale, 0);

                if (result && ProductMgr.GetInstance().Param.IsSecondPos)
                {
                    HTuple transRow, transColumn, transRadian;
                    result = HDevelopExport.FindPinCenter(imgSrc, row, column, angle, out transRow, out transColumn, out transRadian);

                    if (result)
                    {
                        row    = transRow;
                        column = transColumn;
                        angle  = transRadian;
                    }

                    //ctl.DisplayResults();
                }

                if (!result)
                {
                    Log.Show("查找模板失败");
                    return(false);
                }

                //*************相对位置**************
                //HTuple relRow, relColumn;

                //relRow = PlatformCalibData.MarkRow - row;
                //relColumn = PlatformCalibData.MarkColumn - column;

                //SendData.X = PlatformCalibData.PixelToMm(relColumn);
                //SendData.Y = PlatformCalibData.PixelToMm(relRow);

                //用矩阵获得Mark点的世界坐标和模板的世界坐标,求差值
                HTuple colMark, rowMark, rowTrans, colTrans;
                HOperatorSet.AffineTransPixel(PlatformCalibData.HomMat2D, PlatformCalibData.MarkColumn, PlatformCalibData.MarkRow, out colMark, out rowMark);
                HOperatorSet.AffineTransPixel(PlatformCalibData.HomMat2D, column, row, out colTrans, out rowTrans);
                SendData.X = colMark - colTrans;
                SendData.Y = rowMark - rowTrans;

                Log.Show($"目标位置:X:{SendData.X:F2},Y:{SendData.Y:F2}");

                //***********************测量****************************
                MeasureMgr.GetInstance().MeasureAll(image, row, column, angle);

                //显示数据
                int hasCount = 0, meaCount = 0;
                for (int i = 0; i < MeasureMgr.GetInstance().MeasureList.Count; i++)
                {
                    var mea = MeasureMgr.GetInstance().MeasureList[i];

                    hasCount += mea.PinCount;
                    meaCount += mea.CountOK + mea.CountAreaNG + mea.CountPosNG;

                    //发送的数据
                    SendData.CountAreaNG += mea.CountAreaNG;
                    SendData.CountPosNG  += mea.CountPosNG;

                    if (mea.DiameterMax.Length > 0)
                    {
                        //显示和保存数据
                        Data.Show(i, mea.DiameterMax.ToDArr(), mea.DisLeft.ToDArr(), mea.DisRight.ToDArr(), mea.DisTop.ToDArr());
                    }
                }

                ctl.AddToStack(ProductMgr.GetInstance().Param.ModelContours);
                ctl.AddToStack(ProductMgr.GetInstance().Param.ModelOriginContours);
                ctl.AddToStack(ProductMgr.GetInstance().Param.MarkContours);

                ctl.DisplayResults();

                //显示数据到窗口
                HTuple degree;
                HOperatorSet.TupleDeg(angle, out degree);
                Log.Show($"查找模板:row:{row.D:F2},column:{column.D:F2},degree:{degree.D:F2},分数:{score.D:F2}");

                HDevelopExport.disp_message(ctl.GetHalconWindow(), $"位置:{row.D:F2},{column.D:F2},{degree.D:F2}", "window", 10, -1, "green", "false");
                HDevelopExport.disp_message(ctl.GetHalconWindow(), $"分数:{score.D:F2}", "window", 30, -1, "green", "false");

                HDevelopExport.disp_message(ctl.GetHalconWindow(), $"X:{SendData.X:F2},Y:{SendData.Y:F2}", "window", 150, -1, "green", "false");
                HDevelopExport.disp_message(ctl.GetHalconWindow(), $"{hasCount}-{meaCount}", "window", 170, -1, "green", "false");

                if (SendData.CountAreaNG > 0)
                {
                    HDevelopExport.disp_message(ctl.GetHalconWindow(), $"面积NG:{SendData.CountAreaNG}", "window", 190, -1, "red", "false");
                }

                if (SendData.CountPosNG > 0)
                {
                    HDevelopExport.disp_message(ctl.GetHalconWindow(), $"位置NG:{SendData.CountPosNG}", "window", 210, -1, "magenta", "false");
                }

                //显示日志
                Log.Show($"面积NG:{SendData.CountAreaNG},位置NG:{SendData.CountPosNG}");

                //保存窗口图像和原图
                result = result && SendData.CountAreaNG == 0 && SendData.CountPosNG == 0;
                if (AutoForm._autoForm.Param.IsSaveImageAll)
                {
                    SaveImage(ctl.GetHalconWindow(), result);
                }
                else
                {
                    if (AutoForm._autoForm.Param.IsSaveImageNG && !result)
                    {
                        SaveImage(ctl.GetHalconWindow(), result);
                    }
                }

                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }
        public override bool ProcessImage(VisionControl ctl)
        {
            try
            {
                ctl.clearObj();
                ctl.DisplayResults();

                //保存图像
                //string imageName = $"{DateTime.Now:HHmmss}.tiff";

                //if (AutoForm._autoForm.Param.IsSaveImageAll)
                //{
                //    //保存原图
                //    string path = $@"{ProductMgr.GetInstance().ProductPath}Images\Platform\{DateTime.Now:yyMMdd}\Source\";
                //    if (!System.IO.Directory.Exists(path))
                //    {
                //        System.IO.Directory.CreateDirectory(path);
                //    }
                //    string fileName = $"{path}{imageName}";
                //    HDevelopExport.WriteImage(imgSrc, fileName);
                //}

                //图像预处理
                HObject image;

                if (ProductMgr.GetInstance().Param.PlatformRegion != null)
                {
                    HOperatorSet.ReduceDomain(imgSrc, ProductMgr.GetInstance().Param.PlatformRegion, out image);

                    //if (ProductMgr.GetInstance().Param.IsPerprocess)
                    //{
                    //    image = HDevelopExport.Preprocess(image, ProductMgr.GetInstance().Param.Emphasize, false);
                    //}
                }
                else
                {
                    image = imgSrc;
                }

                //查找模板
                HTuple row, column, angle, scale, score;
                bool   result = HDevelopExport.FindScaleShapeModel(image, out row, out column, out angle, out scale, out score);

                //显示轮廓
                HDevelopExport.dev_display_shape_matching_results(ctl.GetHalconWindow(),
                                                                  ProductMgr.GetInstance().Param.ModelID, "blue", row, column, angle, scale, scale, 0);

                /*
                 * //二次定位
                 * if (result && ProductMgr.GetInstance().Param.IsSecondPos)
                 * {
                 *  HTuple transRow, transColumn, transRadian;
                 *  result = HDevelopExport.FindPinCenter(imgSrc, row, column, angle, out transRow, out transColumn, out transRadian);
                 *
                 *  if (result)
                 *  {
                 *      row = transRow;
                 *      column = transColumn;
                 *      angle = transRadian;
                 *  }
                 *
                 *  //ctl.DisplayResults();
                 * }
                 */

                if (!result)
                {
                    Log.Show("查找模板失败");
                }
                else
                {
                    //弧度差
                    HTuple relRow, relColumn, relRadian, rowTrans, colTrans;
                    //relRadian = PlatformCalibData.MarkRadian - angle;
                    relRadian = 0 - angle;

                    //旋转特征点
                    //HTuple homMat2D;
                    //HOperatorSet.HomMat2dIdentity(out homMat2D);
                    //HOperatorSet.HomMat2dRotate(homMat2D, relRadian, PlatformCalibData.CenterRow, PlatformCalibData.CenterColumn, out homMat2D);
                    //HOperatorSet.AffineTransPixel(homMat2D, row, column, out rowTrans, out colTrans);

                    //以平台Mark点为基准
                    //relRow = PlatformCalibData.MarkRow - rowTrans;
                    //relColumn = PlatformCalibData.MarkColumn - colTrans;

                    //没有旋转,直接计算X和Y方向的差值,下面的角度只用于显示
                    //relRow = PlatformCalibData.MarkRow - row;
                    //relColumn = PlatformCalibData.MarkColumn - column;

                    //弧度转角度
                    HTuple degree;
                    HOperatorSet.TupleDeg(relRadian, out degree);

                    //赋值给需要发送的数据
                    //SendData.X = PlatformCalibData.PixelToMm(relColumn);
                    //SendData.Y = PlatformCalibData.PixelToMm(relRow);
                    //SendData.Angle = degree;


                    //用矩阵获得Mark点的世界坐标和模板的世界坐标,求差值
                    HTuple colMark, rowMark;
                    HOperatorSet.AffineTransPixel(PlatformCalibData.HomMat2D, PlatformCalibData.MarkColumn, PlatformCalibData.MarkRow, out colMark, out rowMark);
                    HOperatorSet.AffineTransPixel(PlatformCalibData.HomMat2D, column, row, out colTrans, out rowTrans);
                    SendData.X = colMark - colTrans;
                    SendData.Y = rowMark - rowTrans;



                    ctl.AddToStack(ProductMgr.GetInstance().Param.ModelContours);
                    ctl.AddToStack(ProductMgr.GetInstance().Param.ModelOriginContours);
                    ctl.AddToStack(ProductMgr.GetInstance().Param.MarkContours);

                    ctl.DisplayResults();

                    //显示数据到窗口
                    HTuple deg;
                    HOperatorSet.TupleDeg(angle, out deg);
                    Log.Show($"查找模板:row:{row.D:F2},column:{column.D:F2},degree:{deg.D:F2},分数:{score.D:F2}");

                    HDevelopExport.disp_message(ctl.GetHalconWindow(), $"位置:{row.D:F2},{column.D:F2},{degree.D:F2}", "window", 10, -1, "green", "false");
                    HDevelopExport.disp_message(ctl.GetHalconWindow(), $"分数:{score.D:F2}", "window", 30, -1, "green", "false");

                    HDevelopExport.disp_message(ctl.GetHalconWindow(), $"X:{SendData.X:F2},Y:{SendData.Y:F2},A:{SendData.Angle:F2}", "window", 150, -1, "green", "false");
                    Log.Show($"目标位置:X:{SendData.X:F2},Y:{SendData.Y:F2},A:{SendData.Angle:F2}");
                }

                //保存查找模板的窗口图片和原图
                if (AutoForm._autoForm.Param.IsSaveImageAll)
                {
                    SaveImage(ctl.GetHalconWindow(), result);
                }
                else
                {
                    if (AutoForm._autoForm.Param.IsSaveImageNG && !result)
                    {
                        SaveImage(ctl.GetHalconWindow(), result);
                    }
                }

                return(result);
            }
            catch (Exception)
            {
                return(false);
            }
        }
コード例 #9
0
        private void AutoForm_Load(object sender, EventArgs e)
        {
            //btnSystemConfig.Enabled = false;
            btnSideCamera.Enabled             = false;
            RoundButton_Communication.Enabled = false;
            btnImageFile.Enabled      = false;
            BtnRun.Enabled            = false;
            RoundButton_Login.Enabled = false;
            //BtnRun.BaseColorEnd = BtnRun.BaseColor = Color.FromArgb(230, 216, 216);

            //btnSystemConfig.BaseColor = Color.DarkGray;
            RoundButton_Communication.BaseColor = Color.DarkGray;
            btnImageFile.BaseColor      = Color.DarkGray;
            BtnRun.BaseColor            = Color.DarkGray;
            RoundButton_Login.BaseColor = Color.DarkGray;

            tlbVer.Alignment = ToolStripItemAlignment.Right;
            tlbVer.Text      = $"V2.1:{System.IO.File.GetLastWriteTime(this.GetType().Assembly.Location)}";

            _autoForm = this;

            //获取相机名称
            string fileName = $@"{System.Environment.CurrentDirectory}\Product\CameraName.ini";

            CameraName.MainCamera = IniTool.GetString(fileName, "CameraName", "MainCamera", "MainCam");
            CameraName.SideCamera = IniTool.GetString(fileName, "CameraName", "SideCamera", "HE012A1GM");

#if JAI
            //添加相机并绑定到窗口
            foreach (var cam in CameraJai.FindCamera())
            {
                if (cam.ModelName == CameraName.MainCamera || cam.ModelName == CameraName.SideCamera)
                {
                    VisionMgr.GetInstance().AddCamera(new CameraJai(cam.ModelName, cam));
                }
            }
#else
            //添加相机并绑定到窗口
            //VisionMgr.GetInstance().AddCamera(new CameraGige(CameraName.MainCamera));
            //VisionMgr.GetInstance().AddCamera(new CameraGige(CameraName.SideCamera));
            VisionMgr.GetInstance().AddCamera(new CameraMVision(CameraName.MainCamera));
            VisionMgr.GetInstance().AddCamera(new CameraMVision(CameraName.SideCamera));
#endif

            //添加视觉步骤
            VisionMgr.GetInstance().AddVisionStep(CameraName.MainCamera, new ProcessMainPos(VisionStepName.MainPos));
            VisionMgr.GetInstance().AddVisionStep(CameraName.MainCamera, new ProcessMainMea(VisionStepName.MainMea));
            VisionMgr.GetInstance().AddVisionStep(CameraName.SideCamera, new ProcessSideMea(VisionStepName.SideMea));

            //加载系统参数
            LoadParam();

            //切换产品
            ProductMgr.GetInstance().ProductChangedMethod += ChangeProduct;
            ProductMgr.GetInstance().ChangeProduct(Param.ProductName);

            //关联页面和按钮
            m_dicForm.Add(btnMainCamera, new MainCameraForm());
            //m_dicForm.Add(RoundButton_Login, new LoginForm());
            m_dicForm.Add(RoundButton_Communication, new CommunicationForm());
            m_dicForm.Add(btnSideCamera, new SideCameraForm());
            m_dicForm.Add(btnSystemConfig, new SystemConfigForm());

            //初始化页面属性
            foreach (KeyValuePair <RoundButton, Form> kp in m_dicForm)
            {
                kp.Value.TopLevel = false;
                kp.Value.Parent   = this.panel_main;
                kp.Value.Dock     = DockStyle.Fill;
            }

            btnMainCamera.PerformClick();

            //切换用户
            //((LoginForm)m_dicForm[RoundButton_Login]).UserChangingMethod = ChangeUser;
            //ChangeUser(UserMode.Operator, Param.OperatorPassword);

            //设置日志显示
            Log.Show  = ((MainCameraForm)m_dicForm[btnMainCamera]).ShowLog;
            Data.Show = ((MainCameraForm)m_dicForm[btnMainCamera]).ShowData;

            //连接服务器
            ((SystemConfigForm)m_dicForm[btnSystemConfig]).SetServerMethod = SetServer;
            ConnectServer();
        }
コード例 #10
0
        public override bool ProcessImage(VisionControl ctl)
        {
            try
            {
                //清理数据
                foreach (var mea in MeasureMgr.GetInstance().MeasureList)
                {
                    mea.ClearResult();
                }

                ctl.clearObj();
                ctl.DisplayResults();

                //图像预处理
                HObject image;
                if (ProductMgr.GetInstance().Param.PlatformRegion != null)
                {
                    HOperatorSet.ReduceDomain(imgSrc, ProductMgr.GetInstance().Param.PlatformRegion, out image);
                }
                else
                {
                    image = imgSrc;
                }

                //查找模板
                HTuple row, column, angle, scale, score;
                bool   result = HDevelopExport.FindScaleShapeModel(image, out row, out column, out angle, out scale, out score);

                //显示轮廓
                HDevelopExport.dev_display_shape_matching_results(ctl.GetHalconWindow(),
                                                                  ProductMgr.GetInstance().Param.ModelID, "blue", row, column, angle, scale, scale, 0);

                if (!result)
                {
                    Log.Show("查找模板失败");
                    return(false);
                }


                //***********************测量****************************
                image = MeasureMgr.GetInstance().ImagePre(image);
                MeasureMgr.GetInstance().MeasureAll(image, row, column, angle);
                //ctl.AddToStack(image);

                //显示数据
                bool resRoi = true;//每个测量区域都做判断
                int  hasCount = 0, meaCount = 0;
                for (int i = 0; i < MeasureMgr.GetInstance().MeasureList.Count; i++)
                {
                    var mea = MeasureMgr.GetInstance().MeasureList[i];

                    if (mea.PinCount != mea.CountPinMea)
                    {
                        resRoi = false;
                    }

                    hasCount += mea.PinCount;
                    //meaCount += mea.CountOK + mea.CountAreaNG + mea.CountPosNG;
                    meaCount += mea.CountPinMea;

                    //发送的数据
                    //SendData.CountAreaNG += mea.CountAreaNG;
                    //SendData.CountPosNG += mea.CountPosNG;

                    //if (mea.DiameterMax.Length > 0)
                    //{
                    //    //显示和保存数据
                    //    Data.Show(i, mea.DiameterMax.ToDArr(), mea.DisLeft.ToDArr(), mea.DisRight.ToDArr(), mea.DisTop.ToDArr());
                    //}
                }

                string color = "green";
                if (resRoi && hasCount == meaCount)
                {
                    color = "green";
                    SendData.PinCountOK = true;
                }
                else
                {
                    color = "red";
                    SendData.PinCountOK = false;
                }

                ctl.AddToStack(ProductMgr.GetInstance().Param.ModelContours);
                ctl.AddToStack(ProductMgr.GetInstance().Param.ModelOriginContours);
                //ctl.AddToStack(ProductMgr.GetInstance().Param.MarkContours);

                ctl.DisplayResults();

                for (int i = 0; i < MeasureMgr.GetInstance().MeasureList.Count; i++)
                {
                    var mea = MeasureMgr.GetInstance().MeasureList[i];

                    if (mea.PinCount == mea.CountPinMea)
                    {
                        HDevelopExport.disp_message(ctl.GetHalconWindow(), $"ROI{i}数量:{mea.PinCount},测量数量:{mea.CountPinMea}", "window", 10 + 20 * i, -1, "green", "false");
                    }
                    else
                    {
                        HDevelopExport.disp_message(ctl.GetHalconWindow(), $"ROI{i}数量:{mea.PinCount},测量数量:{mea.CountPinMea}", "window", 10 + 20 * i, -1, "red", "false");
                    }
                }

                HDevelopExport.disp_message(ctl.GetHalconWindow(), $"总数量:{hasCount},测量数量:{meaCount}", "window", 10 + 20 * MeasureMgr.GetInstance().MeasureList.Count, -1, color, "false");

                //保存窗口图像和原图
                result = result && SendData.CountAreaNG == 0 && SendData.CountPosNG == 0;
                if (AutoForm._autoForm.Param.IsSaveImageAll)
                {
                    SaveImage(ctl.GetHalconWindow(), result);
                }
                else
                {
                    if (AutoForm._autoForm.Param.IsSaveImageNG && !result)
                    {
                        SaveImage(ctl.GetHalconWindow(), result);
                    }
                }

                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }