From 3ff51e43eece65ec41adc08bb678b304ca1601a1 Mon Sep 17 00:00:00 2001
From: C3031 <shitiangui@lanpucloud.cn>
Date: 星期三, 24 十二月 2025 13:34:45 +0800
Subject: [PATCH] 新增了运动控制界面

---
 LB_SmartVision/Forms/Pages/MotionControlPage/MotionControlForm.cs |  369 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 369 insertions(+), 0 deletions(-)

diff --git a/LB_SmartVision/Forms/Pages/MotionControlPage/MotionControlForm.cs b/LB_SmartVision/Forms/Pages/MotionControlPage/MotionControlForm.cs
new file mode 100644
index 0000000..7440df4
--- /dev/null
+++ b/LB_SmartVision/Forms/Pages/MotionControlPage/MotionControlForm.cs
@@ -0,0 +1,369 @@
+锘縰sing LB_VisionControl;
+using LB_VisionProcesses.Cameras;
+using System;
+using System.Collections.Generic;
+using System.ComponentModel;
+using System.Data;
+using System.Diagnostics;
+using System.Drawing;
+using System.Linq;
+using System.Text;
+using System.Text.RegularExpressions;
+using System.Threading.Tasks;
+using System.Windows;
+using System.Windows.Forms;
+using static ZXing.QrCode.Internal.Mode;
+
+namespace LB_SmartVision.Forms.Pages.MotionControlPage
+{
+    public partial class MotionControlForm : Form
+    {
+        BaseCamera camera { get; set; }
+        UserPictureBox onlinePictureBox { get; set; }
+        public MotionControlForm()
+        {
+            InitializeComponent();
+        }
+
+        public MotionControlForm(BaseCamera camera)
+        {
+            InitializeComponent();
+
+            if (camera == null)
+                return;
+
+            this.camera = camera;
+            this.Text = camera.SN;
+            textBox1.Text = camera.SN;
+
+            onlinePictureBox = new UserPictureBox(uiGroupBoxImage);
+            this.uiGroupBoxImage.Controls.Clear();
+            this.uiGroupBoxImage.Controls.Add(onlinePictureBox);
+            onlinePictureBox.Dock = DockStyle.Fill;
+
+            //鍔犺浇鍥炶皟鍑芥暟
+            Subscribe();
+        }
+
+        private void CameraForm_FormClosing(object sender, FormClosingEventArgs e)
+        {
+            if (camera == null)
+                return;
+
+            camera.StopGrabbing();
+
+            camera.StartGrabbing();
+
+            Unsubscribe();
+        }
+
+        public void Unsubscribe()
+        {
+            try
+            {
+                if (camera == null)
+                    return;
+                //鍙栨秷鍥炶皟鍑芥暟
+                camera.ImageGrabbed -= GetImageBllComplete;
+            }
+            catch (Exception ex)
+            {
+                // 璁板綍閿欒淇℃伅
+                Debug.WriteLine($"閿欒: {ex.Message}");
+            }
+
+        }
+
+        public void Subscribe()
+        {
+            try
+            {
+                if (camera == null)
+                    return;
+
+                //鍙栨秷鍥炶皟鍑芥暟
+                camera.ImageGrabbed -= GetImageBllComplete;
+                //鍔犺浇鍥炶皟鍑芥暟
+                camera.ImageGrabbed += GetImageBllComplete;
+            }
+            catch (Exception ex)
+            {
+                // 璁板綍閿欒淇℃伅
+                Debug.WriteLine($"閿欒: {ex.Message}");
+            }
+        }
+
+        /// <summary>
+        /// 鐩告満鍥炶皟杩愯
+        /// </summary>
+        /// <param name="CCDName"></param>
+        /// <param name="image"></param>
+        private void GetImageBllComplete(object sender, CameraEventArgs e)
+        {
+            if (e.Bitmap == null)
+                return;
+
+            lock (e.Bitmap)
+            {
+                if (this.InvokeRequired) // 妫�鏌ユ槸鍚﹂渶瑕佸湪UI绾跨▼涓婅皟鐢�
+                {
+                    this.Invoke(new Action(() =>
+                    {
+                        onlinePictureBox.Image = e.Bitmap;
+                    })); // 閫掑綊璋冪敤鑷韩锛屼絾杩欐鍦║I绾跨▼涓�
+                }
+                else
+                {
+                    onlinePictureBox.Image = e.Bitmap;
+                }
+            }
+        }
+
+        private void uiButtonGrabOnce_Click(object sender, EventArgs e)
+        {
+            if (camera == null)
+                return;
+
+            Task.Factory.StartNew(() =>
+            {
+                camera.SetTriggerMode(TriggerMode.On, TriggerSource.Software);
+                DateTime startTime = DateTime.Now; // 褰撳墠鏃堕棿
+                camera.GetImageWithSoftTrigger(out Bitmap bitmap);
+            });
+        }
+
+        private void uiButtonCloseSoftGrab_Click(object sender, EventArgs e)
+        {
+            if (camera == null)
+                return;
+
+            Task.Factory.StartNew(() =>
+            {
+                camera.StopGrabbing();
+                camera.SetTriggerMode(TriggerMode.On, TriggerSource.Software);
+                camera.StartGrabbing();
+            });
+        }
+
+        private void uiButtonHardGrabWithPLC_Click(object sender, EventArgs e)
+        {
+            if (camera == null)
+                return;
+
+            Task.Factory.StartNew(() =>
+            {
+                camera.SetTriggerMode(TriggerMode.On, TriggerSource.Line0);
+                camera.StopGrabbing();
+                camera.StartWith_HardTriggerModel();
+            });
+        }
+
+        private void uiButtonDecreaseX_Click(object sender, EventArgs e)
+        {
+            float xPosition = 0;
+            xPosition = float.Parse(textBoxXPosition.Text);
+            MovePosition(-xPosition, 0, 0, 0);
+            PositionNow();
+        }
+
+        private void uiButtonIncreaseX_Click(object sender, EventArgs e)
+        {
+            float xPosition = 0;
+            xPosition = float.Parse(textBoxXPosition.Text);
+            MovePosition(xPosition, 0, 0, 0);
+            PositionNow();
+        }
+
+        private void uiButtonDecreaseY_Click(object sender, EventArgs e)
+        {
+            float yPosition = 0;
+            yPosition = float.Parse(textBoxYPosition.Text);
+            MovePosition(0, -yPosition, 0, 0);
+            PositionNow();
+        }
+
+        private void uiButtonIncreaseY_Click(object sender, EventArgs e)
+        {
+            float yPosition = 0;
+            yPosition = float.Parse(textBoxYPosition.Text);
+            MovePosition(0, yPosition, 0, 0);
+            PositionNow();
+        }
+
+        private void uiButtonDecreaseZ_Click(object sender, EventArgs e)
+        {
+            float zPosition = 0;
+            zPosition = float.Parse(textBoxZPosition.Text);
+            MovePosition(0, 0, -zPosition, 0);
+            PositionNow();
+        }
+
+        private void uiButtonIncreaseZ_Click(object sender, EventArgs e)
+        {
+            float zPosition = 0;
+            zPosition = float.Parse(textBoxZPosition.Text);
+            MovePosition(0, 0, zPosition, 0);
+            PositionNow();
+        }
+
+        private void uiButtonDecreasePitchAngle_Click(object sender, EventArgs e)
+        {
+            float pitchanglePosition = 0;
+            pitchanglePosition = float.Parse(textBoxPitchAnglePosition.Text);
+            MovePosition(0, 0, 0, -pitchanglePosition);
+            AngNow();
+        }
+
+        private void uiButtonIncreasePitchAngle_Click(object sender, EventArgs e)
+        {
+            float pitchanglePosition = 0;
+            pitchanglePosition = float.Parse(textBoxPitchAnglePosition.Text);
+            MovePosition(0, 0, 0, pitchanglePosition);
+            AngNow();
+        }
+
+        public float XPositionDt;
+        public float YPositionDt;
+        public float ZPositionDt;
+        public float AnglePositionDt;
+        public void MovePosition(float fDeltaX, float fDeltaY, float fDeltaZ, float fDeltAngle)
+        {
+            XPositionDt += fDeltaX;
+            YPositionDt += fDeltaY;
+            ZPositionDt += fDeltaZ;
+            AnglePositionDt += fDeltAngle;
+        }
+
+        private void textBoxPointSpeed_TextChanged(object sender, EventArgs e)
+        {
+            float pointspeed = 0;
+            pointspeed = float.Parse(textBoxPointSpeed.Text);
+        }
+
+        private void textBoxAngSpeed_TextChanged(object sender, EventArgs e)
+        {
+            float angspeed = 0;
+            angspeed = float.Parse(textBoxAngSpeed.Text);
+        }
+
+        private void textBoxPositionNow_TextChanged(object sender, EventArgs e)
+        {
+            textBoxPositionNow.Enabled = false;
+
+            string x;
+            string y;
+            string z;
+            x = Convert.ToString(Math.Round(XPositionDt, 3));
+            y = Convert.ToString(Math.Round(YPositionDt, 3));
+            z = Convert.ToString(Math.Round(ZPositionDt, 3));
+
+            textBoxPositionNow.Text = "(" + x + "," + y + "," + z + ")";
+        }
+
+        private void textBoxAngleNow_TextChanged(object sender, EventArgs e)
+        {
+            textBoxAngleNow.Enabled = false;
+
+            string ang;
+            ang = Convert.ToString(Math.Round(AnglePositionDt, 3));
+
+            textBoxAngleNow.Text = ang;
+        }
+
+        private void PositionNow()
+        {
+            string x;
+            string y;
+            string z;
+            x = Convert.ToString(Math.Round(XPositionDt, 3));
+            y = Convert.ToString(Math.Round(YPositionDt, 3));
+            z = Convert.ToString(Math.Round(ZPositionDt, 3));
+
+            textBoxPositionNow.Text = "(" + x + y + z + ")";
+        }
+        private void AngNow()
+        {
+            string ang;
+            ang = Convert.ToString(Math.Round(AnglePositionDt, 3));
+
+            textBoxAngleNow.Text = "(" + ang + ")";
+        }
+
+        private void uiGroupBoxMotionControl_Enter(object sender, EventArgs e)
+        {
+
+        }
+
+        private void label1_Click(object sender, EventArgs e)
+        {
+
+        }
+
+        private void tableLayoutPanel3_Paint(object sender, PaintEventArgs e)
+        {
+
+        }
+
+        private void uiSplitContainer1_Panel1_Paint(object sender, PaintEventArgs e)
+        {
+
+        }
+
+        // 浣跨敤KeyDown浜嬩欢鏇撮�傚悎澶勭悊鍥炶溅閿�
+        private void textBoxXPosition_KeyDown(object sender, KeyEventArgs e)
+        {
+            if (e.KeyCode == Keys.Enter)
+            {
+                //// 楠岃瘉杈撳叆
+                //if (ValidateXPosition())
+                //{
+                //    // 鎵ц纭鎿嶄綔
+                //    ProcessXPosition();
+                //}
+
+                // 闃绘绯荤粺澹伴煶骞惰鏂囨湰妗嗗け鍘荤劍鐐�
+                e.SuppressKeyPress = true;
+                e.Handled = true;
+
+                this.ActiveControl = null;
+            }
+        }
+
+        // 浣跨敤KeyPress楠岃瘉瀹炴椂杈撳叆
+        private void textBoxXPosition_KeyPress(object sender, KeyPressEventArgs e)
+        {
+            // 鍏佽鍥炶溅閿紙铏界劧KeyDown浼氬鐞嗗畠锛�
+            if (e.KeyChar == (char)Keys.Enter)
+                return;
+
+            // 鍏佽閫�鏍奸敭
+            if (e.KeyChar == (char)Keys.Back)
+                return;
+
+            // 鍙厑璁告暟瀛�
+            if (!char.IsDigit(e.KeyChar))
+            {
+                e.Handled = true;  // 闃绘杈撳叆
+            }
+        }
+
+        // 楠岃瘉鏂规硶
+        private bool ValidateXPosition()
+        {
+            if (string.IsNullOrWhiteSpace(textBoxXPosition.Text))
+                return false;
+
+            return Regex.IsMatch(textBoxXPosition.Text, @"^\d+$");
+        }
+
+        // 澶勭悊纭鐨勬柟娉�
+        private void ProcessXPosition()
+        {
+            if (int.TryParse(textBoxXPosition.Text, out int xPosition))
+            {
+                // 鎵ц閫昏緫
+                //LogInfo?.Invoke($"X浣嶇疆宸叉洿鏂颁负: {xPosition}", LogInfoType.INFO);
+            }
+        }
+    }
+}

--
Gitblit v1.9.3