Ejemplo n.º 1
0
 /// <summary>
 /// 控件缩放时自动调整halcon窗口的大小
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void VisionControl_SizeChanged(object sender, EventArgs e)
 {
     if (m_windowHandle != null)
     {
         bool bUpdate = false;
         LockDisplay();
         try
         {
             HOperatorSet.SetWindowExtents(m_windowHandle, this.ClientRectangle.Y, this.ClientRectangle.X, this.ClientRectangle.Width, this.ClientRectangle.Height);
             bUpdate = true;
         }
         catch (HalconException HDevExpDefaultException1)
         {
             System.Diagnostics.Debug.WriteLine(HDevExpDefaultException1.ToString());
         }
         catch (Exception exp)
         {
             System.Diagnostics.Debug.WriteLine(exp.ToString());
         }
         finally { UnlockDisplay(); }
         if (bUpdate && m_IVisionControlUpdate != null)
         {
             m_IVisionControlUpdate.UpdateVisionControl(this);
         }
     }
 }
Ejemplo n.º 2
0
        /// <summary>
        /// 打开相机
        /// </summary>
        /// <returns></returns>
        public override bool Open()
        {
            if (m_hAcqHandle == null)
            {
                try
                {
                    //HOperatorSet.OpenFramegrabber("GigEVision", 0, 0, 0, 0, 0, 0, "default",
                    //    -1, "default", -1, "false", "default", this.Name, 0, -1, out m_hAcqHandle);
                    //HOperatorSet.OpenFramegrabber("DirectShow", 1, 1, 0, 0, 0, 0, "default", 8, "rgb",
                    //    -1, "false", "default", "[0] Integrated Camera", 0, -1, out m_hAcqHandle);

                    HOperatorSet.OpenFramegrabber("MVision", 0, 0, 0, 0, 0, 0, "default", 8,
                                                  "default", -1, "false", "auto", this.Name, 0, -1, out m_hAcqHandle);

                    if (m_hAcqHandle == null || m_hAcqHandle.Length == 0)
                    {
                        m_hAcqHandle = null;
                        return(false);
                    }
                    else
                    {
                        return(true);
                    }
                }
                catch (HalconException HDevExpDefaultException1)
                {
                    System.Diagnostics.Debug.WriteLine(HDevExpDefaultException1.ToString());
                    m_hAcqHandle = null;
                    return(false);
                }
            }
            return(true);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 鼠标右键按下时返回全屏显示
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void VisionControl_MouseUp(object sender, MouseEventArgs e)
        {
            if (MouseMode == WindowMouseMode.Move && e.Button == MouseButtons.Middle)
            {
                if (isOpen())
                {
                    //Action<object> action = (object obj) =>
                    //{
                    bool   bUpdate = false;
                    HTuple row0, col0, row1, col1;
                    LockDisplay();
                    try
                    {
                        HOperatorSet.GetPart(m_windowHandle, out row0, out col0, out row1, out col1);

                        if (row0 != 0 || col0 != 0 || col1 - col0 != ImageWidth || row1 - row0 != ImageHeight)
                        {
                            HOperatorSet.SetPart(m_windowHandle, 0, 0, ImageHeight, ImageWidth);
                            bUpdate = true;
                        }
                    }
                    catch (HalconException HDevExpDefaultException1)
                    {
                        System.Diagnostics.Debug.WriteLine(HDevExpDefaultException1.ToString());
                    }
                    catch (Exception exp)
                    {
                        System.Diagnostics.Debug.WriteLine(exp.ToString());
                    }
                    finally
                    {
                        UnlockDisplay();
                    }

                    if (bUpdate && m_IVisionControlUpdate != null)
                    {
                        m_IVisionControlUpdate.UpdateVisionControl(this);
                    }
                    //};
                    //Task t1 = new Task(action, "");
                    //t1.Start();
                    //t1.Wait();
                }
            }
            else if (e.Button == MouseButtons.Left)
            {
                if (this.Cursor == Cursors.Hand)
                {
                    this.Cursor = Cursors.Arrow;
                }
            }
            else if (e.Button == MouseButtons.Right)
            {
                if (this.Cursor == Cursors.Hand)
                {
                    this.Cursor = Cursors.Arrow;
                }
            }
        }
        public override void UpdateVisionControl(VisionControl ctl)
        {
            ctl.LockDisplay();
            try
            {
                if (imgSrc != null && imgSrc.IsInitialized() && imgSrc.Key != IntPtr.Zero)
                {
                    if (imgSrc != null)
                    {
                        HOperatorSet.DispObj(imgSrc, ctl.GetHalconWindow());
                    }

                    while (ctl.getStack().Count > 0)
                    {
                        HOperatorSet.DispObj(ctl.getStack().Pop(), ctl.GetHalconWindow());
                    }

                    foreach (var mea in MeasureMgr.GetInstance().MeasureList)
                    {
                        HOperatorSet.SetColor(ctl.GetHalconWindow(), "blue");
                        HOperatorSet.DispObj(mea.LineEdge, ctl.GetHalconWindow());
                        HOperatorSet.SetColor(ctl.GetHalconWindow(), "green");
                        HOperatorSet.DispObj(mea.ContourOk, ctl.GetHalconWindow());
                        HOperatorSet.SetColor(ctl.GetHalconWindow(), "red");
                        HOperatorSet.DispObj(mea.ContourAreaNG, ctl.GetHalconWindow());
                        HOperatorSet.SetColor(ctl.GetHalconWindow(), "magenta");
                        HOperatorSet.DispObj(mea.ContourPosNG, ctl.GetHalconWindow());
                    }

                    HOperatorSet.SetColor(ctl.GetHalconWindow(), "blue");

                    //HTuple hv_Width, hv_Height;
                    //HOperatorSet.GetImageSize(m_image, out hv_Width, out hv_Height);
                    //HObject ht_CrossLineV, ht_CrossLineH;
                    //HOperatorSet.SetColor(ctl.GetHalconWindow(), "red");
                    //HOperatorSet.GenRegionLine(out ht_CrossLineV, 0, hv_Width / 2, hv_Height, hv_Width / 2);
                    //HOperatorSet.GenRegionLine(out ht_CrossLineH, hv_Height / 2, 0, hv_Height / 2, hv_Width);
                    //HOperatorSet.DispObj(ht_CrossLineV, ctl.GetHalconWindow());
                    //HOperatorSet.DispObj(ht_CrossLineH, ctl.GetHalconWindow());
                }
            }
            catch (HalconException HDevExpDefaultException1)
            {
                System.Diagnostics.Debug.WriteLine(HDevExpDefaultException1.ToString());
            }
            catch (AccessViolationException e)
            {
                System.Diagnostics.Debug.WriteLine(e.ToString());
            }
            catch (Exception e)
            {
                System.Diagnostics.Debug.WriteLine(e.ToString());
            }
            finally
            {
                ctl.UnlockDisplay();
            }
        }
        /// <summary>
        /// 更新显示控件
        /// </summary>
        /// <param name="ctl"></param>
        public override void UpdateVisionControl(VisionControl ctl)
        {
            ctl.LockDisplay();
            try
            {
                if (imgSrc != null && imgSrc.IsInitialized() && imgSrc.Key != IntPtr.Zero)
                {
                    HTuple num = 0;
                    HOperatorSet.CountObj(imgSrc, out num);
                    if (num > 0)//&& m_image.IsInitialized() && m_image.Key != IntPtr.Zero)
                    {
                        HOperatorSet.DispObj(imgSrc, ctl.GetHalconWindow());
                    }
                    //    HOperatorSet.DispObj(ModelContour, ctl.GetHalconWindow());

                    //if (MeasureRect != null && MeasureRect.IsInitialized())
                    //{
                    //    HOperatorSet.SetColor(m_visionControl.GetHalconWindow(), "red");
                    //    HOperatorSet.DispObj(MeasureRect, ctl.GetHalconWindow());
                    //}

                    if (Lines != null && Lines.IsInitialized())
                    {
                        HOperatorSet.SetColor(m_visionControl.GetHalconWindow(), "blue");
                        HOperatorSet.DispObj(Lines, ctl.GetHalconWindow());
                    }

                    if (IntraDistance != null && IntraDistance.Length > 0)
                    {
                        if (IntraDistance.Type == HTupleType.STRING)
                        {
                            HDevelopExport.disp_message(m_visionControl.GetHalconWindow(),
                                                        IntraDistance, "window", 20, -1, "green", "false");
                        }
                        else
                        {
                            var value = SideCameraCalibData.PixelToMm(IntraDistance);
                            HDevelopExport.disp_message(m_visionControl.GetHalconWindow(),
                                                        $"{value:F2}", "window", 20, -1, "green", "false");
                        }
                    }
                }
            }
            catch (HalconException HDevExpDefaultException1)
            {
                System.Diagnostics.Debug.WriteLine(HDevExpDefaultException1.ToString());
            }
            catch (Exception exp)
            {
                System.Diagnostics.Debug.WriteLine(exp.ToString());
            }
            finally
            {
                ctl.UnlockDisplay();
            }
        }
Ejemplo n.º 6
0
 /// <summary>
 /// 初始化halcon窗口,分辨率为2592 * 1944
 /// </summary>
 ///
 //     [DllImport("user32.dll")]static extern IntPtr GetWindowDC(IntPtr hWnd);
 public void InitWindow()
 {
     try
     {
         //          HOperatorSet.NewExternWindow(this.Handle, 0, 0, this.Width, this.Height, out m_windowHandle);
         //          HOperatorSet.SetWindowDc(m_windowHandle, GetWindowDC(this.Handle));
         HOperatorSet.OpenWindow(0, 0, this.Width, this.Height, this.Handle, "", "", out m_windowHandle);
         HOperatorSet.SetWindowParam(m_windowHandle, "background_color", "#000040");
         HOperatorSet.ClearWindow(m_windowHandle);
         HOperatorSet.SetPart(m_windowHandle, 0, 0, ImageHeight - 1, ImageWidth - 1);
     }
     catch (HalconException HDevExpDefaultException1)
     {
         System.Diagnostics.Debug.WriteLine(HDevExpDefaultException1.ToString());
     }
 }
Ejemplo n.º 7
0
        /// <summary>
        /// 关闭相机
        /// </summary>
        /// <returns></returns>
        public override bool Close()
        {
            if (m_hAcqHandle != null)
            {
                try
                {
                    HOperatorSet.CloseFramegrabber(m_hAcqHandle);
                }
                catch (HalconException HDevExpDefaultException1)
                {
                    System.Diagnostics.Debug.WriteLine(HDevExpDefaultException1.ToString());
                    return(false);
                }
                m_hAcqHandle = null;
            }

            m_image = null;
            return(true);
        }
Ejemplo n.º 8
0
 /// <summary>
 /// 软件触发一次同步采集
 /// </summary>
 /// <returns></returns>
 public override int Snap()
 {
     if (m_hAcqHandle == null)
     {
         Open();
     }
     if (m_hAcqHandle != null)
     {
         try
         {
             m_image?.Dispose();
             HOperatorSet.GrabImage(out m_image, m_hAcqHandle);
         }
         catch (HalconException HDevExpDefaultException1)
         {
             System.Diagnostics.Debug.WriteLine(HDevExpDefaultException1.ToString());
             return(0);
         }
         return(1);
     }
     return(0);
 }
Ejemplo n.º 9
0
        /// <summary>
        /// 平移图像
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void VisionControl_MouseMove(object sender, MouseEventArgs e)
        {
            if (MouseMode == WindowMouseMode.Move && isOpen())
            {
                if (this.Cursor == Cursors.Hand)
                {
                    int x = e.X - ptMouse.X;
                    int y = e.Y - ptMouse.Y;

                    if (Math.Abs(x) > 2 || Math.Abs(y) > 2)
                    {
                        ptMouse.X = e.X;
                        ptMouse.Y = e.Y;

                        HTuple row0, col0, row1, col1;
                        bool   bUpdate = false;

                        LockDisplay();

                        try
                        {
                            HOperatorSet.GetPart(m_windowHandle, out row0, out col0, out row1, out col1);
                            double zoom = 1.0 * (row1 - row0) / this.Height;
                            x = (int)(x * zoom);
                            y = (int)(y * zoom);

                            HOperatorSet.SetPart(m_windowHandle, row0 - y, col0 - x, row1 - y, col1 - x);

                            //清理窗口前关闭刷新,以免闪烁
                            HOperatorSet.SetSystem("flush_graphic", "false");
                            HOperatorSet.ClearWindow(m_windowHandle);
                            HOperatorSet.SetSystem("flush_graphic", "true");

                            bUpdate = true;
                        }
                        catch (HalconException HDevExpDefaultException1)
                        {
                            System.Diagnostics.Debug.WriteLine(HDevExpDefaultException1.ToString());
                        }
                        catch (Exception exp)
                        {
                            System.Diagnostics.Debug.WriteLine(exp.ToString());
                        }
                        finally
                        {
                            UnlockDisplay();
                        }

                        if (bUpdate && m_IVisionControlUpdate != null)
                        {
                            Action <object> action = (object obj) =>
                            {
                                m_IVisionControlUpdate.UpdateVisionControl(this);
                            };
                            Task t1 = new Task(action, "");
                            t1.Start();
                            t1.Wait();
                        }
                    }
                }
            }
        }
Ejemplo n.º 10
0
        /// <summary>
        /// 鼠标滚动时缩放图片大小
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void VisionControl_MouseWheel(object sender, MouseEventArgs e)
        {
            if (MouseMode == WindowMouseMode.Move && isOpen())
            {
                HTuple row, col, button;
                HTuple row0, col0, row1, col1;
                bool   bUpdate = false;

                LockDisplay();
                try
                {
                    HOperatorSet.GetMposition(m_windowHandle, out row, out col, out button);
                    HOperatorSet.GetPart(m_windowHandle, out row0, out col0, out row1, out col1);

                    HTuple width  = col1 - col0;
                    HTuple height = row1 - row0;
                    float  k      = (float)width / ImageWidth;

                    if ((k < 50 && e.Delta < 0) || (k > 0.02 && e.Delta > 0))
                    {
                        HTuple Zoom;
                        if (e.Delta > 0)
                        {
                            Zoom = 1.3;
                        }
                        else
                        {
                            Zoom = 1 / 1.3;
                        }

                        HTuple r1 = (row0 + ((1 - (1.0 / Zoom)) * (row - row0)));
                        HTuple c1 = (col0 + ((1 - (1.0 / Zoom)) * (col - col0)));
                        HTuple r2 = r1 + (height / Zoom);
                        HTuple c2 = c1 + (width / Zoom);

                        HOperatorSet.SetPart(m_windowHandle, r1, c1, r2, c2);

                        //清理窗口前关闭刷新,以免闪烁
                        HOperatorSet.SetSystem("flush_graphic", "false");
                        HOperatorSet.ClearWindow(m_windowHandle);
                        HOperatorSet.SetSystem("flush_graphic", "true");

                        bUpdate = true;
                    }
                }
                catch (HalconException HDevExpDefaultException1)
                {
                    System.Diagnostics.Debug.WriteLine(HDevExpDefaultException1.ToString());
                }
                catch (Exception exp)
                {
                    System.Diagnostics.Debug.WriteLine(exp.ToString());
                }
                finally
                {
                    UnlockDisplay();
                }
                if (bUpdate && m_IVisionControlUpdate != null)
                {
                    Action <object> action = (object obj) =>
                    {
                        m_IVisionControlUpdate.UpdateVisionControl(this);
                    };
                    Task t1 = new Task(action, "");
                    t1.Start();
                    t1.Wait();
                }
            }
        }