diff --git a/DHSoftware/Views/VisualLocalizationWindow.Designer.cs b/DHSoftware/Views/VisualLocalizationWindow.Designer.cs index ce11b66..50360a5 100644 --- a/DHSoftware/Views/VisualLocalizationWindow.Designer.cs +++ b/DHSoftware/Views/VisualLocalizationWindow.Designer.cs @@ -62,10 +62,19 @@ btnMotorForward = new AntdUI.Button(); iptMotorSpeed = new AntdUI.InputNumber(); label8 = new AntdUI.Label(); + panel3 = new AntdUI.Panel(); + btnSaveBarrierPos = new AntdUI.Button(); + iptBarrierPos = new AntdUI.InputNumber(); + label10 = new AntdUI.Label(); + btnBarrierZero = new AntdUI.Button(); + btnBarrier = new AntdUI.Button(); + iptBarrierSpeed = new AntdUI.InputNumber(); + label11 = new AntdUI.Label(); imageViewerControl1 = new ImageViewerControl(); panel1.SuspendLayout(); panel2.SuspendLayout(); panelMotor.SuspendLayout(); + panel3.SuspendLayout(); SuspendLayout(); // // titlebar @@ -77,12 +86,11 @@ titlebar.Font = new Font("Microsoft YaHei UI", 12F, FontStyle.Regular, GraphicsUnit.Point, 134); titlebar.ForeColor = Color.White; titlebar.Location = new Point(0, 0); - titlebar.Margin = new Padding(4, 4, 4, 4); titlebar.Mode = AntdUI.TAMode.Dark; titlebar.Name = "titlebar"; titlebar.ShowButton = true; titlebar.ShowIcon = true; - titlebar.Size = new Size(1551, 44); + titlebar.Size = new Size(1210, 37); titlebar.SubText = "视觉定位系统"; titlebar.TabIndex = 1; titlebar.Text = "山东迭慧智能科技有限公司"; @@ -100,39 +108,35 @@ panel1.Controls.Add(sltCameraName); panel1.Controls.Add(label1); panel1.Dock = DockStyle.Top; - panel1.Location = new Point(0, 44); - panel1.Margin = new Padding(4, 4, 4, 4); + panel1.Location = new Point(0, 37); panel1.Name = "panel1"; - panel1.Size = new Size(1551, 66); + panel1.Size = new Size(1210, 56); panel1.TabIndex = 2; panel1.Text = "panel1"; // // iptThreshold // - iptThreshold.Location = new Point(1407, 7); - iptThreshold.Margin = new Padding(4, 4, 4, 4); + iptThreshold.Location = new Point(1094, 6); iptThreshold.Name = "iptThreshold"; - iptThreshold.Size = new Size(138, 51); + iptThreshold.Size = new Size(107, 43); iptThreshold.TabIndex = 15; iptThreshold.Text = "0"; // // label6 // label6.BackColor = SystemColors.Window; - label6.Location = new Point(1342, 21); - label6.Margin = new Padding(4, 4, 4, 4); + label6.Location = new Point(1044, 18); label6.Name = "label6"; - label6.Size = new Size(73, 27); + label6.Size = new Size(57, 23); label6.TabIndex = 14; label6.Text = "定位阈值"; // // btnSelectBackImg // btnSelectBackImg.Font = new Font("Microsoft YaHei UI", 9F, FontStyle.Regular, GraphicsUnit.Point, 134); - btnSelectBackImg.Location = new Point(1238, 13); - btnSelectBackImg.Margin = new Padding(4, 4, 4, 4); + btnSelectBackImg.Location = new Point(963, 11); btnSelectBackImg.Name = "btnSelectBackImg"; - btnSelectBackImg.Size = new Size(96, 45); + btnSelectBackImg.Size = new Size(75, 38); btnSelectBackImg.TabIndex = 13; btnSelectBackImg.Text = "打开"; btnSelectBackImg.Type = AntdUI.TTypeMini.Primary; @@ -140,70 +144,63 @@ // btnSelectModel // btnSelectModel.Font = new Font("Microsoft YaHei UI", 9F, FontStyle.Regular, GraphicsUnit.Point, 134); - btnSelectModel.Location = new Point(675, 11); - btnSelectModel.Margin = new Padding(4, 4, 4, 4); + btnSelectModel.Location = new Point(525, 9); btnSelectModel.Name = "btnSelectModel"; - btnSelectModel.Size = new Size(96, 45); + btnSelectModel.Size = new Size(75, 38); btnSelectModel.TabIndex = 12; btnSelectModel.Text = "打开"; btnSelectModel.Type = AntdUI.TTypeMini.Primary; // // iptBackImg // - iptBackImg.Location = new Point(892, 7); - iptBackImg.Margin = new Padding(4, 4, 4, 4); + iptBackImg.Location = new Point(694, 6); iptBackImg.Name = "iptBackImg"; iptBackImg.PlaceholderText = "请选择背景图片"; - iptBackImg.Size = new Size(338, 51); + iptBackImg.Size = new Size(263, 43); iptBackImg.TabIndex = 10; // // label3 // label3.BackColor = SystemColors.Window; - label3.Location = new Point(779, 21); - label3.Margin = new Padding(4, 4, 4, 4); + label3.Location = new Point(606, 18); label3.Name = "label3"; - label3.Size = new Size(105, 27); + label3.Size = new Size(82, 23); label3.TabIndex = 9; label3.Text = "背景图片路径"; // // iptModel // - iptModel.Location = new Point(329, 7); - iptModel.Margin = new Padding(4, 4, 4, 4); + iptModel.Location = new Point(256, 6); iptModel.Name = "iptModel"; iptModel.PlaceholderText = "请选择算法"; - iptModel.Size = new Size(338, 51); + iptModel.Size = new Size(263, 43); iptModel.TabIndex = 3; // // label2 // label2.BackColor = SystemColors.Window; - label2.Location = new Point(248, 21); - label2.Margin = new Padding(4, 4, 4, 4); + label2.Location = new Point(193, 18); label2.Name = "label2"; - label2.Size = new Size(73, 27); + label2.Size = new Size(57, 23); label2.TabIndex = 2; label2.Text = "算法路径"; // // sltCameraName // sltCameraName.List = true; - sltCameraName.Location = new Point(93, 8); - sltCameraName.Margin = new Padding(4, 4, 4, 4); + sltCameraName.Location = new Point(72, 7); sltCameraName.MaxCount = 10; sltCameraName.Name = "sltCameraName"; sltCameraName.PlaceholderText = "请选择相机"; - sltCameraName.Size = new Size(148, 51); + sltCameraName.Size = new Size(115, 43); sltCameraName.TabIndex = 1; // // label1 // label1.BackColor = SystemColors.Window; - label1.Location = new Point(12, 21); - label1.Margin = new Padding(4, 4, 4, 4); + label1.Location = new Point(9, 18); label1.Name = "label1"; - label1.Size = new Size(73, 27); + label1.Size = new Size(57, 23); label1.TabIndex = 0; label1.Text = "相机名称"; // @@ -222,39 +219,35 @@ panel2.Controls.Add(sltDirection); panel2.Controls.Add(label4); panel2.Dock = DockStyle.Top; - panel2.Location = new Point(0, 110); - panel2.Margin = new Padding(4, 4, 4, 4); + panel2.Location = new Point(0, 93); panel2.Name = "panel2"; - panel2.Size = new Size(1551, 66); + panel2.Size = new Size(1210, 56); panel2.TabIndex = 3; panel2.Text = "panel2"; // // iptSpeed // - iptSpeed.Location = new Point(329, 7); - iptSpeed.Margin = new Padding(4, 4, 4, 4); + iptSpeed.Location = new Point(256, 6); iptSpeed.Name = "iptSpeed"; - iptSpeed.Size = new Size(138, 51); + iptSpeed.Size = new Size(107, 43); iptSpeed.TabIndex = 20; iptSpeed.Text = "0"; // // label7 // label7.BackColor = SystemColors.Window; - label7.Location = new Point(248, 21); - label7.Margin = new Padding(4, 4, 4, 4); + label7.Location = new Point(193, 18); label7.Name = "label7"; - label7.Size = new Size(73, 27); + label7.Size = new Size(57, 23); label7.TabIndex = 19; label7.Text = "转盘速度"; // // btnSaveImg // btnSaveImg.Font = new Font("Microsoft YaHei UI", 9F, FontStyle.Regular, GraphicsUnit.Point, 134); - btnSaveImg.Location = new Point(1448, 13); - btnSaveImg.Margin = new Padding(4, 4, 4, 4); + btnSaveImg.Location = new Point(1126, 11); btnSaveImg.Name = "btnSaveImg"; - btnSaveImg.Size = new Size(96, 45); + btnSaveImg.Size = new Size(75, 38); btnSaveImg.TabIndex = 18; btnSaveImg.Text = "保存图像"; btnSaveImg.Type = AntdUI.TTypeMini.Primary; @@ -262,10 +255,9 @@ // btnSavePos // btnSavePos.Font = new Font("Microsoft YaHei UI", 9F, FontStyle.Regular, GraphicsUnit.Point, 134); - btnSavePos.Location = new Point(1302, 13); - btnSavePos.Margin = new Padding(4, 4, 4, 4); + btnSavePos.Location = new Point(1013, 11); btnSavePos.Name = "btnSavePos"; - btnSavePos.Size = new Size(96, 45); + btnSavePos.Size = new Size(75, 38); btnSavePos.TabIndex = 17; btnSavePos.Text = "保存定位"; btnSavePos.Type = AntdUI.TTypeMini.Primary; @@ -273,10 +265,9 @@ // btnReverse // btnReverse.Font = new Font("Microsoft YaHei UI", 9F, FontStyle.Regular, GraphicsUnit.Point, 134); - btnReverse.Location = new Point(1162, 13); - btnReverse.Margin = new Padding(4, 4, 4, 4); + btnReverse.Location = new Point(904, 11); btnReverse.Name = "btnReverse"; - btnReverse.Size = new Size(96, 45); + btnReverse.Size = new Size(75, 38); btnReverse.TabIndex = 16; btnReverse.Text = "转盘反转"; btnReverse.Type = AntdUI.TTypeMini.Primary; @@ -284,10 +275,9 @@ // btnForward // btnForward.Font = new Font("Microsoft YaHei UI", 9F, FontStyle.Regular, GraphicsUnit.Point, 134); - btnForward.Location = new Point(1034, 13); - btnForward.Margin = new Padding(4, 4, 4, 4); + btnForward.Location = new Point(804, 11); btnForward.Name = "btnForward"; - btnForward.Size = new Size(96, 45); + btnForward.Size = new Size(75, 38); btnForward.TabIndex = 15; btnForward.Text = "转盘正转"; btnForward.Type = AntdUI.TTypeMini.Primary; @@ -295,10 +285,9 @@ // btnLocalization // btnLocalization.Font = new Font("Microsoft YaHei UI", 9F, FontStyle.Regular, GraphicsUnit.Point, 134); - btnLocalization.Location = new Point(905, 13); - btnLocalization.Margin = new Padding(4, 4, 4, 4); + btnLocalization.Location = new Point(704, 11); btnLocalization.Name = "btnLocalization"; - btnLocalization.Size = new Size(96, 45); + btnLocalization.Size = new Size(75, 38); btnLocalization.TabIndex = 14; btnLocalization.Text = "开始定位"; btnLocalization.Type = AntdUI.TTypeMini.Primary; @@ -306,31 +295,28 @@ // btnAcquisition // btnAcquisition.Font = new Font("Microsoft YaHei UI", 9F, FontStyle.Regular, GraphicsUnit.Point, 134); - btnAcquisition.Location = new Point(779, 13); - btnAcquisition.Margin = new Padding(4, 4, 4, 4); + btnAcquisition.Location = new Point(606, 11); btnAcquisition.Name = "btnAcquisition"; - btnAcquisition.Size = new Size(96, 45); + btnAcquisition.Size = new Size(75, 38); btnAcquisition.TabIndex = 13; btnAcquisition.Text = "开始采集"; btnAcquisition.Type = AntdUI.TTypeMini.Primary; // // iptPosition // - iptPosition.Location = new Point(546, 7); - iptPosition.Margin = new Padding(4, 4, 4, 4); + iptPosition.Location = new Point(425, 6); iptPosition.Name = "iptPosition"; iptPosition.ReadOnly = true; - iptPosition.Size = new Size(225, 51); + iptPosition.Size = new Size(175, 43); iptPosition.TabIndex = 7; iptPosition.Text = "0"; // // label5 // label5.BackColor = SystemColors.Window; - label5.Location = new Point(474, 21); - label5.Margin = new Padding(4, 4, 4, 4); + label5.Location = new Point(369, 18); label5.Name = "label5"; - label5.Size = new Size(76, 27); + label5.Size = new Size(59, 23); label5.TabIndex = 6; label5.Text = "当前位置"; // @@ -338,21 +324,19 @@ // sltDirection.Items.AddRange(new object[] { "正方向", "反方向" }); sltDirection.List = true; - sltDirection.Location = new Point(93, 7); - sltDirection.Margin = new Padding(4, 4, 4, 4); + sltDirection.Location = new Point(72, 6); sltDirection.MaxCount = 10; sltDirection.Name = "sltDirection"; sltDirection.PlaceholderText = "请选择方向"; - sltDirection.Size = new Size(148, 51); + sltDirection.Size = new Size(115, 43); sltDirection.TabIndex = 5; // // label4 // label4.BackColor = SystemColors.Window; - label4.Location = new Point(12, 21); - label4.Margin = new Padding(4, 4, 4, 4); + label4.Location = new Point(9, 18); label4.Name = "label4"; - label4.Size = new Size(73, 27); + label4.Size = new Size(57, 23); label4.TabIndex = 4; label4.Text = "转盘方向"; // @@ -367,51 +351,46 @@ panelMotor.Controls.Add(iptMotorSpeed); panelMotor.Controls.Add(label8); panelMotor.Dock = DockStyle.Top; - panelMotor.Location = new Point(0, 176); - panelMotor.Margin = new Padding(4, 4, 4, 4); + panelMotor.Location = new Point(0, 149); panelMotor.Name = "panelMotor"; - panelMotor.Size = new Size(1551, 54); + panelMotor.Size = new Size(1210, 46); panelMotor.TabIndex = 4; panelMotor.Text = "panel3"; // // btnSaveMotorPos // btnSaveMotorPos.Font = new Font("Microsoft YaHei UI", 9F, FontStyle.Regular, GraphicsUnit.Point, 134); - btnSaveMotorPos.Location = new Point(966, 6); - btnSaveMotorPos.Margin = new Padding(4, 4, 4, 4); + btnSaveMotorPos.Location = new Point(751, 5); btnSaveMotorPos.Name = "btnSaveMotorPos"; - btnSaveMotorPos.Size = new Size(111, 45); + btnSaveMotorPos.Size = new Size(109, 38); btnSaveMotorPos.TabIndex = 21; - btnSaveMotorPos.Text = "保存电机位置"; + btnSaveMotorPos.Text = "保存相机电机位置"; btnSaveMotorPos.Type = AntdUI.TTypeMini.Primary; // // iptMotorPos // - iptMotorPos.Location = new Point(365, 0); - iptMotorPos.Margin = new Padding(4, 4, 4, 4); + iptMotorPos.Location = new Point(284, 0); iptMotorPos.Name = "iptMotorPos"; iptMotorPos.ReadOnly = true; - iptMotorPos.Size = new Size(225, 51); + iptMotorPos.Size = new Size(175, 43); iptMotorPos.TabIndex = 20; iptMotorPos.Text = "0"; // // label9 // label9.BackColor = SystemColors.Window; - label9.Location = new Point(261, 12); - label9.Margin = new Padding(4, 4, 4, 4); + label9.Location = new Point(203, 10); label9.Name = "label9"; - label9.Size = new Size(104, 27); + label9.Size = new Size(81, 23); label9.TabIndex = 19; label9.Text = "变焦电机位置"; // // btnMotorZero // btnMotorZero.Font = new Font("Microsoft YaHei UI", 9F, FontStyle.Regular, GraphicsUnit.Point, 134); - btnMotorZero.Location = new Point(598, 6); - btnMotorZero.Margin = new Padding(4, 4, 4, 4); + btnMotorZero.Location = new Point(465, 5); btnMotorZero.Name = "btnMotorZero"; - btnMotorZero.Size = new Size(96, 45); + btnMotorZero.Size = new Size(75, 38); btnMotorZero.TabIndex = 18; btnMotorZero.Text = "电机回原点"; btnMotorZero.Type = AntdUI.TTypeMini.Primary; @@ -419,10 +398,9 @@ // btnMotorReverse // btnMotorReverse.Font = new Font("Microsoft YaHei UI", 9F, FontStyle.Regular, GraphicsUnit.Point, 134); - btnMotorReverse.Location = new Point(847, 6); - btnMotorReverse.Margin = new Padding(4, 4, 4, 4); + btnMotorReverse.Location = new Point(659, 5); btnMotorReverse.Name = "btnMotorReverse"; - btnMotorReverse.Size = new Size(96, 45); + btnMotorReverse.Size = new Size(75, 38); btnMotorReverse.TabIndex = 17; btnMotorReverse.Text = "电机反转"; btnMotorReverse.Type = AntdUI.TTypeMini.Primary; @@ -430,60 +408,138 @@ // btnMotorForward // btnMotorForward.Font = new Font("Microsoft YaHei UI", 9F, FontStyle.Regular, GraphicsUnit.Point, 134); - btnMotorForward.Location = new Point(724, 6); - btnMotorForward.Margin = new Padding(4, 4, 4, 4); + btnMotorForward.Location = new Point(563, 5); btnMotorForward.Name = "btnMotorForward"; - btnMotorForward.Size = new Size(96, 45); + btnMotorForward.Size = new Size(75, 38); btnMotorForward.TabIndex = 16; btnMotorForward.Text = "电机正转"; btnMotorForward.Type = AntdUI.TTypeMini.Primary; // // iptMotorSpeed // - iptMotorSpeed.Location = new Point(116, 0); - iptMotorSpeed.Margin = new Padding(4, 4, 4, 4); + iptMotorSpeed.Location = new Point(90, 0); iptMotorSpeed.Name = "iptMotorSpeed"; - iptMotorSpeed.Size = new Size(138, 51); + iptMotorSpeed.Size = new Size(107, 43); iptMotorSpeed.TabIndex = 8; iptMotorSpeed.Text = "0"; // // label8 // label8.BackColor = SystemColors.Window; - label8.Location = new Point(12, 12); - label8.Margin = new Padding(4, 4, 4, 4); + label8.Location = new Point(9, 10); label8.Name = "label8"; - label8.Size = new Size(104, 27); + label8.Size = new Size(81, 23); label8.TabIndex = 1; label8.Text = "变焦电机速度"; // + // panel3 + // + panel3.Controls.Add(btnSaveBarrierPos); + panel3.Controls.Add(iptBarrierPos); + panel3.Controls.Add(label10); + panel3.Controls.Add(btnBarrierZero); + panel3.Controls.Add(btnBarrier); + panel3.Controls.Add(iptBarrierSpeed); + panel3.Controls.Add(label11); + panel3.Dock = DockStyle.Top; + panel3.Location = new Point(0, 195); + panel3.Name = "panel3"; + panel3.Size = new Size(1210, 46); + panel3.TabIndex = 6; + panel3.Text = "panel3"; + // + // btnSaveBarrierPos + // + btnSaveBarrierPos.Font = new Font("Microsoft YaHei UI", 9F, FontStyle.Regular, GraphicsUnit.Point, 134); + btnSaveBarrierPos.Location = new Point(681, 5); + btnSaveBarrierPos.Name = "btnSaveBarrierPos"; + btnSaveBarrierPos.Size = new Size(109, 38); + btnSaveBarrierPos.TabIndex = 21; + btnSaveBarrierPos.Text = "保存挡料电机位置"; + btnSaveBarrierPos.Type = AntdUI.TTypeMini.Primary; + // + // iptBarrierPos + // + iptBarrierPos.Location = new Point(284, 0); + iptBarrierPos.Name = "iptBarrierPos"; + iptBarrierPos.ReadOnly = true; + iptBarrierPos.Size = new Size(175, 43); + iptBarrierPos.TabIndex = 20; + iptBarrierPos.Text = "0"; + // + // label10 + // + label10.BackColor = SystemColors.Window; + label10.Location = new Point(203, 10); + label10.Name = "label10"; + label10.Size = new Size(81, 23); + label10.TabIndex = 19; + label10.Text = "挡料电机位置"; + // + // btnBarrierZero + // + btnBarrierZero.Font = new Font("Microsoft YaHei UI", 9F, FontStyle.Regular, GraphicsUnit.Point, 134); + btnBarrierZero.Location = new Point(465, 5); + btnBarrierZero.Name = "btnBarrierZero"; + btnBarrierZero.Size = new Size(75, 38); + btnBarrierZero.TabIndex = 18; + btnBarrierZero.Text = "电机回原点"; + btnBarrierZero.Type = AntdUI.TTypeMini.Primary; + // + // btnBarrier + // + btnBarrier.Font = new Font("Microsoft YaHei UI", 9F, FontStyle.Regular, GraphicsUnit.Point, 134); + btnBarrier.Location = new Point(563, 5); + btnBarrier.Name = "btnBarrier"; + btnBarrier.Size = new Size(101, 38); + btnBarrier.TabIndex = 16; + btnBarrier.Text = "挡料电机转动"; + btnBarrier.Type = AntdUI.TTypeMini.Primary; + // + // iptBarrierSpeed + // + iptBarrierSpeed.Location = new Point(90, 0); + iptBarrierSpeed.Name = "iptBarrierSpeed"; + iptBarrierSpeed.Size = new Size(107, 43); + iptBarrierSpeed.TabIndex = 8; + iptBarrierSpeed.Text = "0"; + // + // label11 + // + label11.BackColor = SystemColors.Window; + label11.Location = new Point(9, 10); + label11.Name = "label11"; + label11.Size = new Size(81, 23); + label11.TabIndex = 1; + label11.Text = "挡料电机速度"; + // // imageViewerControl1 // imageViewerControl1.Dock = DockStyle.Fill; imageViewerControl1.Image = null; - imageViewerControl1.Location = new Point(0, 230); - imageViewerControl1.Margin = new Padding(4, 4, 4, 4); + imageViewerControl1.Location = new Point(0, 241); imageViewerControl1.Name = "imageViewerControl1"; - imageViewerControl1.Size = new Size(1551, 593); - imageViewerControl1.TabIndex = 5; + imageViewerControl1.Size = new Size(1210, 464); + imageViewerControl1.TabIndex = 7; // // VisualLocalizationWindow // - AutoScaleDimensions = new SizeF(9F, 20F); + AutoScaleDimensions = new SizeF(7F, 17F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(1551, 823); + ClientSize = new Size(1210, 705); Controls.Add(imageViewerControl1); + Controls.Add(panel3); Controls.Add(panelMotor); Controls.Add(panel2); Controls.Add(panel1); Controls.Add(titlebar); - Margin = new Padding(4, 4, 4, 4); Name = "VisualLocalizationWindow"; Text = "CCD光学筛选定位系统"; WindowState = FormWindowState.Maximized; panel1.ResumeLayout(false); panel2.ResumeLayout(false); panelMotor.ResumeLayout(false); + panel3.ResumeLayout(false); ResumeLayout(false); } @@ -517,12 +573,20 @@ private AntdUI.Panel panelMotor; private AntdUI.InputNumber iptMotorSpeed; private AntdUI.Label label8; - private ImageViewerControl imageViewerControl1; private AntdUI.Button btnMotorReverse; private AntdUI.Button btnMotorForward; private AntdUI.InputNumber iptMotorPos; private AntdUI.Label label9; private AntdUI.Button btnMotorZero; private AntdUI.Button btnSaveMotorPos; + private AntdUI.Panel panel3; + private AntdUI.Button btnSaveBarrierPos; + private AntdUI.InputNumber iptBarrierPos; + private AntdUI.Label label10; + private AntdUI.Button btnBarrierZero; + private AntdUI.Button btnBarrier; + private AntdUI.InputNumber iptBarrierSpeed; + private AntdUI.Label label11; + private ImageViewerControl imageViewerControl1; } } diff --git a/DHSoftware/Views/VisualLocalizationWindow.cs b/DHSoftware/Views/VisualLocalizationWindow.cs index 63a8bc4..bc6d3b7 100644 --- a/DHSoftware/Views/VisualLocalizationWindow.cs +++ b/DHSoftware/Views/VisualLocalizationWindow.cs @@ -31,8 +31,54 @@ namespace DHSoftware.Views private System.Threading.Timer Timer; List localizations = new List(); + #region 控件大小随窗体大小等比例缩放 + + private readonly float x; //定义当前窗体的宽度 + private readonly float y; //定义当前窗体的高度 + + private void setTag(Control cons) + { + foreach (Control con in cons.Controls) + { + con.Tag = con.Width + ";" + con.Height + ";" + con.Left + ";" + con.Top + ";" + con.Font.Size; + if (con.Controls.Count > 0) setTag(con); + } + } + + private void setControls(float newx, float newy, Control cons) + { + //遍历窗体中的控件,重新设置控件的值 + foreach (Control con in cons.Controls) + //获取控件的Tag属性值,并分割后存储字符串数组 + if (con.Tag != null) + { + var mytag = con.Tag.ToString().Split(';'); + //根据窗体缩放的比例确定控件的值 + con.Width = Convert.ToInt32(Convert.ToSingle(mytag[0]) * newx); //宽度 + con.Height = Convert.ToInt32(Convert.ToSingle(mytag[1]) * newy); //高度 + con.Left = Convert.ToInt32(Convert.ToSingle(mytag[2]) * newx); //左边距 + con.Top = Convert.ToInt32(Convert.ToSingle(mytag[3]) * newy); //顶边距 + var currentSize = Convert.ToSingle(mytag[4]) * newy; //字体大小 + if (currentSize > 0) con.Font = new Font(con.Font.Name, currentSize, con.Font.Style, con.Font.Unit); + con.Focus(); + if (con.Controls.Count > 0) setControls(newx, newy, con); + } + } + + + /// + /// 重置窗体布局 + /// + private void ReWinformLayout() + { + var newx = Width / x; + var newy = Height / y; + setControls(newx, newy, this); + + } + + #endregion - public VisualLocalizationWindow() { InitializeComponent(); @@ -55,51 +101,200 @@ namespace DHSoftware.Views btnMotorZero.Click += BtnMotorZero_Click; btnSaveMotorPos.Click += BtnSaveMotorPos_Click; panelMotor.Visible = false; - + Resize += VisualLocalizationWindow_Resize; + btnBarrierZero.Click += BtnBarrierZero_Click; + btnBarrier.Click += BtnBarrier_Click; + btnSaveBarrierPos.Click += BtnSaveBarrierPos_Click; + iptMotorSpeed.Value =1200; + iptBarrierSpeed.Value = 500; + AutoDpi(this); + x = Width; + y = Height; + setTag(this); + + + } + + private void BtnSaveBarrierPos_Click(object? sender, EventArgs e) + { + try + { + //根据工位查找点位 + PLCItem? pLCItem = ConfigModel.PLCBaseList? + .FirstOrDefault()? + .PLCItemList? + .Where(it => it.Name == "挡料电机位置").FirstOrDefault(); + if (pLCItem == null) + { + AntdUI.Message.warn(this, $"未找到挡料电机位置地址,请检查该地址是否存在于点位表!", autoClose: 3); + return; + } + + PLCItem? pLCItem1 = ConfigModel.GlobalList? + .FirstOrDefault()? + .StartProcessList? + .Where(it => it.Name == "挡料电机位置").FirstOrDefault(); + + if (pLCItem1 == null) + { + pLCItem1 = new PLCItem(); + pLCItem1.Name = pLCItem.Name; + pLCItem1.Address = pLCItem.Address; + pLCItem1.Value = iptBarrierPos.Text; + pLCItem1.Type = pLCItem.Type; + pLCItem1.StartExecute = true; + ConfigModel.GlobalList? + .FirstOrDefault()? + .StartProcessList?.Add(pLCItem1); + } + else + { + pLCItem1.Value = iptBarrierPos.Text; + } + + ConfigHelper.SaveConfig(); + AntdUI.Message.success(this, "保存成功!", autoClose: 3); + } + catch (Exception ex) + { + AntdUI.Message.error(this,ex.Message, autoClose: 3); + } + } + + private async void BtnBarrier_Click(object? sender, EventArgs e) + { + if (_isOperating) return; + _isOperating = true; + + try + { + // 获取并处理速度值 + if (!int.TryParse(iptBarrierSpeed.Text, out int speed) || speed == 0) + { + AntdUI.Message.warn(this, "挡料电机速度值无效"); + return; + } + if (!int.TryParse(iptBarrierPos.Text, out int barrierPos) || speed == 0) + { + AntdUI.Message.warn(this, "挡料电机位置值无效"); + return; + } + btnBarrierZero.Enabled= false; + int actualSpeed = Math.Abs(speed); // 正转取绝对值 + + + MainWindow.Instance.PLC.BarrierSpeed(actualSpeed); + MainWindow.Instance.PLC.BarrierPos(barrierPos); + MainWindow.Instance.PLC.BarrierClockwise(true); + + // 实时更新循环 + while (barrierPos!=MainWindow.Instance.PLC.ReadBarrierRealPos()) + { + await Task.Delay(50); + } + } + catch (Exception ex) + { + _isOperating = false; + btnBarrierZero.Enabled = true; + AntdUI.Message.error(this, $"挡料电机转动异常: {ex.Message}"); + } + finally + { + MainWindow.Instance.PLC.BarrierClockwise(false); + _isOperating = false; + btnBarrierZero.Enabled = true; + } + } + + private CancellationTokenSource? _bCts; + private bool _isOperating; + + private async void BtnBarrierZero_Click(object? sender, EventArgs e) + { + if (_isOperating) return; + _isOperating = true; + btnBarrier.Enabled = false; + + try + { + + // 执行回零 + await Task.Run(() => MainWindow.Instance.PLC.BarrierToZero(true)); + + // 等待回零完成 + while (!MainWindow.Instance.PLC.ReadBarrierToZero()) + { + await Task.Delay(50); + } + + await Task.Run(() => MainWindow.Instance.PLC.BarrierToZero(false)); + } + catch (Exception ex) + { + btnBarrier.Enabled = true; + _isOperating = false; + AntdUI.Message.error(this, $"回零失败: {ex.Message}"); + } + finally + { + btnBarrier.Enabled = true; + _isOperating = false; + } + } + + private void VisualLocalizationWindow_Resize(object? sender, EventArgs e) + { + ReWinformLayout(); } private void BtnSaveMotorPos_Click(object? sender, EventArgs e) { - //根据工位查找点位 - PLCItem? pLCItem = ConfigModel.PLCBaseList? - .FirstOrDefault()? - .PLCItemList? - .Where(it => it.Name == "相机步进位置").FirstOrDefault(); - if (pLCItem == null) + try { - AntdUI.Message.warn(this, $"未找到相机步进位置地址,请检查该地址是否存在于点位表!", autoClose: 3); - return; - } - - PLCItem? pLCItem1 = ConfigModel.GlobalList? - .FirstOrDefault()? - .StartProcessList? - .Where(it => it.Name == "相机步进位置").FirstOrDefault(); - - if (pLCItem1 == null) - { - pLCItem1 = new PLCItem(); - pLCItem1.Name = pLCItem.Name; - pLCItem1.Address = pLCItem.Address; - pLCItem1.Value = iptMotorPos.Text; - pLCItem1.Type = pLCItem.Type; - pLCItem1.StartExecute = true; - ConfigModel.GlobalList? + //根据工位查找点位 + PLCItem? pLCItem = ConfigModel.PLCBaseList? .FirstOrDefault()? - .StartProcessList?.Add(pLCItem1); - } - else - { - pLCItem1.Value = iptMotorPos.Text; - } + .PLCItemList? + .Where(it => it.Name == "相机步进位置").FirstOrDefault(); + if (pLCItem == null) + { + AntdUI.Message.warn(this, $"未找到相机步进位置地址,请检查该地址是否存在于点位表!", autoClose: 3); + return; + } - ConfigHelper.SaveConfig(); - AntdUI.Message.success(this, "保存成功!", autoClose: 3); + PLCItem? pLCItem1 = ConfigModel.GlobalList? + .FirstOrDefault()? + .StartProcessList? + .Where(it => it.Name == "相机步进位置").FirstOrDefault(); + + if (pLCItem1 == null) + { + pLCItem1 = new PLCItem(); + pLCItem1.Name = pLCItem.Name; + pLCItem1.Address = pLCItem.Address; + pLCItem1.Value = iptMotorPos.Text; + pLCItem1.Type = pLCItem.Type; + pLCItem1.StartExecute = true; + ConfigModel.GlobalList? + .FirstOrDefault()? + .StartProcessList?.Add(pLCItem1); + } + else + { + pLCItem1.Value = iptMotorPos.Text; + } + + ConfigHelper.SaveConfig(); + AntdUI.Message.success(this, "保存成功!", autoClose: 3); + } + catch (Exception ex) + { + AntdUI.Message.error(this, ex.Message, autoClose: 3); + } } - private CancellationTokenSource? _motorCts; - private bool _isMotorOperating; - private bool _isHoming; + @@ -138,6 +333,7 @@ namespace DHSoftware.Views } catch (Exception ex) { + _isBusy = false; AntdUI.Message.error(this, $"正转异常: {ex.Message}"); } finally @@ -201,6 +397,7 @@ namespace DHSoftware.Views } catch (Exception ex) { + _isBusy = false; AntdUI.Message.error(this, $"反转异常: {ex.Message}"); } finally @@ -247,6 +444,8 @@ namespace DHSoftware.Views } catch (Exception ex) { + btnMotorForward.Enabled = btnMotorReverse.Enabled = true; + _isBusy = false; AntdUI.Message.error(this, $"回零失败: {ex.Message}"); } finally @@ -261,54 +460,68 @@ namespace DHSoftware.Views { try { - localizations = VisualLocalization.LoadAll(); + try + { + localizations = VisualLocalization.LoadAll(); - } - catch - { + } + catch + { + } + string cameraName = sltCameraName.Text; + Do3ThinkCamera = MainWindow.Instance.Cameras.Where(it => it.CameraName == cameraName).FirstOrDefault() ?? new Do3ThinkCamera(); + VisualLocalization? visual = localizations.Where(it => it.CameraName == cameraName).FirstOrDefault(); + if (visual != null) + { + iptModel.Text = visual.ModelPath; + iptBackImg.Text = visual.ImgPath; + iptThreshold.Text = visual.Threshold; + sltDirection.SelectedIndex = visual.Direction == "正方向" ? 0 : 1; + iptSpeed.Text = visual.Speed; + } + if (Do3ThinkCamera.IsZoomCamera) + { + panelMotor.Visible = true; + } + else + { + panelMotor.Visible = false; + } } - string cameraName= sltCameraName.Text; - Do3ThinkCamera = MainWindow.Instance.Cameras.Where(it => it.CameraName == cameraName).FirstOrDefault() ?? new Do3ThinkCamera(); - VisualLocalization? visual= localizations.Where(it=>it.CameraName==cameraName).FirstOrDefault(); - if (visual != null) + catch (Exception ex) { - iptModel.Text = visual.ModelPath; - iptBackImg.Text = visual.ImgPath; - iptThreshold.Text = visual.Threshold; - sltDirection.SelectedIndex = visual.Direction == "正方向" ? 0 : 1; - iptSpeed.Text = visual.Speed; - } - if (Do3ThinkCamera.IsZoomCamera) - { - panelMotor.Visible=true; - } - else - { - panelMotor.Visible = false; + AntdUI.Message.error(this, ex.Message, autoClose: 3); } } private void BtnSavePos_Click(object? sender, EventArgs e) { - var form = new SavePositionControl(this,Convert.ToInt32(iptPosition.Text)) { Size = new Size(300, 300) }; - AntdUI.Modal.open(new AntdUI.Modal.Config(this, "", form, TType.None) + try { - BtnHeight = 0, - }); - if (form.submit) - { - VisualLocalization visualLocalization = new VisualLocalization(); - //保存用户操作到文件 - visualLocalization.CameraName = sltCameraName.Text; - visualLocalization.ModelPath=iptModel.Text; - visualLocalization.ImgPath=iptBackImg.Text; - visualLocalization.Threshold=iptThreshold.Text; - visualLocalization.Direction=sltDirection.Text; - visualLocalization.Speed=iptSpeed.Text; - visualLocalization.Save(); + var form = new SavePositionControl(this, Convert.ToInt32(iptPosition.Text)) { Size = new Size(300, 300) }; + AntdUI.Modal.open(new AntdUI.Modal.Config(this, "", form, TType.None) + { + BtnHeight = 0, + }); + if (form.submit) + { + VisualLocalization visualLocalization = new VisualLocalization(); + //保存用户操作到文件 + visualLocalization.CameraName = sltCameraName.Text; + visualLocalization.ModelPath = iptModel.Text; + visualLocalization.ImgPath = iptBackImg.Text; + visualLocalization.Threshold = iptThreshold.Text; + visualLocalization.Direction = sltDirection.Text; + visualLocalization.Speed = iptSpeed.Text; + visualLocalization.Save(); + } } - } + catch (Exception ex) + { + AntdUI.Message.error(this, ex.Message, autoClose: 3); + } + } /// /// 保存图像 @@ -317,29 +530,37 @@ namespace DHSoftware.Views /// private void BtnSaveImg_Click(object? sender, EventArgs e) { - if (!isCapturing) + try { - AntdUI.Message.warn(this, $"未开始采集,无法保存图像!", autoClose: 3); - return; - } - Bitmap bitmap =imageViewerControl1.GetCurrentSnapshot(); - using (SaveFileDialog saveDialog = new SaveFileDialog()) - { - saveDialog.Title = "保存图像文件"; - saveDialog.Filter = "JPEG 图像|*.jpg"; - saveDialog.FilterIndex = 0; - saveDialog.AddExtension = true; - saveDialog.OverwritePrompt = true; + if (!isCapturing) + { + AntdUI.Message.warn(this, $"未开始采集,无法保存图像!", autoClose: 3); + return; + } + Bitmap bitmap = imageViewerControl1.GetCurrentSnapshot(); + using (SaveFileDialog saveDialog = new SaveFileDialog()) + { + saveDialog.Title = "保存图像文件"; + saveDialog.Filter = "JPEG 图像|*.jpg"; + saveDialog.FilterIndex = 0; + saveDialog.AddExtension = true; + saveDialog.OverwritePrompt = true; - if (saveDialog.ShowDialog() == DialogResult.OK) - { - bitmap.Save(saveDialog.FileName, ImageFormat.Jpeg); - AntdUI.Message.success(this, $"图像保存成功!", autoClose: 3); - } - else - { - AntdUI.Message.warn(this, $"取消图像保存操作!", autoClose: 3); + if (saveDialog.ShowDialog() == DialogResult.OK) + { + bitmap.Save(saveDialog.FileName, ImageFormat.Jpeg); + AntdUI.Message.success(this, $"图像保存成功!", autoClose: 3); + } + else + { + AntdUI.Message.warn(this, $"取消图像保存操作!", autoClose: 3); + } } + + } + catch (Exception ex) + { + AntdUI.Message.error(this, ex.Message, autoClose: 3); } } @@ -350,45 +571,52 @@ namespace DHSoftware.Views /// private void BtnLocalization_Click(object? sender, EventArgs e) { - if (!isCapturing) + try { - AntdUI.Message.warn(this, $"未开始采集,无法开始定位!", autoClose: 3); - return; - } - if (!isLocationing) - { - bool direction =sltDirection.SelectedIndex==0?true:false; - if (string.IsNullOrEmpty(iptSpeed.Text)) + if (!isCapturing) { - AntdUI.Message.warn(this, $"请输入速度!", autoClose: 3); + AntdUI.Message.warn(this, $"未开始采集,无法开始定位!", autoClose: 3); return; } - int speed = 0; - - try + if (!isLocationing) { - bool isValid = int.TryParse(iptSpeed.Text, out speed); - if (!isValid) + bool direction = sltDirection.SelectedIndex == 0 ? true : false; + if (string.IsNullOrEmpty(iptSpeed.Text)) { - AntdUI.Message.warn(this, $"输入的速度不是有效值!", autoClose: 3); + AntdUI.Message.warn(this, $"请输入速度!", autoClose: 3); return; } + int speed = 0; + + try + { + bool isValid = int.TryParse(iptSpeed.Text, out speed); + if (!isValid) + { + AntdUI.Message.warn(this, $"输入的速度不是有效值!", autoClose: 3); + return; + } + } + catch (Exception ex) { } + MainWindow.Instance.PLC.TurnSpeed(speed); + MainWindow.Instance.PLC.TurnDirection(direction); + MainWindow.Instance.PLC.TurnStart(true); + isLocationing = true; + btnLocalization.Text = "结束定位"; + btnLocalization.Type = TTypeMini.Warn; + } + else + { + MainWindow.Instance.PLC.TurnStart(false); + iptPosition.Text = MainWindow.Instance.PLC.ReadVisionPos().ToString(); + isLocationing = false; + btnLocalization.Text = "开始定位"; + btnLocalization.Type = TTypeMini.Primary; } - catch (Exception ex) { } - MainWindow.Instance.PLC.TurnSpeed(speed); - MainWindow.Instance.PLC.TurnDirection(direction); - MainWindow.Instance.PLC.TurnStart(true); - isLocationing = true; - btnLocalization.Text = "结束定位"; - btnLocalization.Type = TTypeMini.Warn; } - else + catch (Exception ex) { - MainWindow.Instance.PLC.TurnStart(false); - iptPosition.Text= MainWindow.Instance.PLC.ReadVisionPos().ToString(); - isLocationing = false; - btnLocalization.Text = "开始定位"; - btnLocalization.Type = TTypeMini.Primary; + AntdUI.Message.error(this, ex.Message, autoClose: 3); } } @@ -400,81 +628,90 @@ namespace DHSoftware.Views /// private void BtnAcquisition_Click(object? sender, EventArgs e) { - if (!isCapturing) + try { - if (string.IsNullOrWhiteSpace(sltCameraName.Text)) + if (!isCapturing) { - AntdUI.Message.warn(this, $"请选择相机!", autoClose: 3); - return; - } - if (string.IsNullOrWhiteSpace(iptModel.Text)) - { - AntdUI.Message.warn(this, $"请选择算法!", autoClose: 3); - return; - } - if (string.IsNullOrWhiteSpace(iptBackImg.Text)) - { - AntdUI.Message.warn(this, $"请选择背景图片!", autoClose: 3); - return; - } + if (string.IsNullOrWhiteSpace(sltCameraName.Text)) + { + AntdUI.Message.warn(this, $"请选择相机!", autoClose: 3); + return; + } + if (string.IsNullOrWhiteSpace(iptModel.Text)) + { + AntdUI.Message.warn(this, $"请选择算法!", autoClose: 3); + return; + } + if (string.IsNullOrWhiteSpace(iptBackImg.Text)) + { + AntdUI.Message.warn(this, $"请选择背景图片!", autoClose: 3); + return; + } - // 加载HALCON模型 - if (!File.Exists(iptModel.Text)) - { - AntdUI.Message.warn(this, $"算法文件不存在!", autoClose: 3); - return; - } - if (!File.Exists(iptBackImg.Text)) - { - AntdUI.Message.warn(this, $"图片文件不存在!", autoClose: 3); - return; - } + // 加载HALCON模型 + if (!File.Exists(iptModel.Text)) + { + AntdUI.Message.warn(this, $"算法文件不存在!", autoClose: 3); + return; + } + if (!File.Exists(iptBackImg.Text)) + { + AntdUI.Message.warn(this, $"图片文件不存在!", autoClose: 3); + return; + } - //获取背景图 - backImage = new HImage(); - backImage.ReadImage(iptBackImg.Text); - // 从完整路径获取过程名称 - string procedureName = Path.GetFileNameWithoutExtension(iptModel.Text); - string procedureDir = Path.GetDirectoryName(iptModel.Text); + //获取背景图 + backImage = new HImage(); + backImage.ReadImage(iptBackImg.Text); + // 从完整路径获取过程名称 + string procedureName = Path.GetFileNameWithoutExtension(iptModel.Text); + string procedureDir = Path.GetDirectoryName(iptModel.Text); - // 重新初始化HALCON引擎 - MyEngine.SetProcedurePath(procedureDir); - Procedure = new HDevProcedure(procedureName); - ProcCall = new HDevProcedureCall(Procedure); + // 重新初始化HALCON引擎 + MyEngine.SetProcedurePath(procedureDir); + Procedure = new HDevProcedure(procedureName); + ProcCall = new HDevProcedureCall(Procedure); - if (MainWindow.Instance.PLC.Connected) - { - //启用视觉定位 - MainWindow.Instance.PLC.VisionPos(true); + if (MainWindow.Instance.PLC.Connected) + { + //启用视觉定位 + MainWindow.Instance.PLC.VisionPos(true); + } + else + { + AntdUI.Message.warn(this, $"未连接PLC,无法视觉定位!", autoClose: 3); + return; + } + Do3ThinkCamera = MainWindow.Instance.Cameras.Where(it => it.CameraName == sltCameraName.Text).FirstOrDefault() ?? new Do3ThinkCamera(); + Do3ThinkCamera.OnHImageOutput += OnCameraHImageOutput; + Timer = new System.Threading.Timer(CaptureLoop, null, 0, 50); + isCapturing = true; + btnAcquisition.Text = "结束采集"; + btnAcquisition.Type = TTypeMini.Warn; } else { - AntdUI.Message.warn(this, $"未连接PLC,无法视觉定位!", autoClose: 3); - return; + if (isLocationing) + { + AntdUI.Message.warn(this, $"定位未结束,不能结束采集!", autoClose: 3); + return; + } + + MainWindow.Instance.PLC.VisionPos(false); + Do3ThinkCamera.OnHImageOutput -= OnCameraHImageOutput; + Timer?.Dispose(); + isCapturing = false; + btnAcquisition.Text = "开始采集"; + btnAcquisition.Type = TTypeMini.Primary; } - Do3ThinkCamera=MainWindow.Instance.Cameras.Where(it=>it.CameraName==sltCameraName.Text).FirstOrDefault()??new Do3ThinkCamera(); - Do3ThinkCamera.OnHImageOutput += OnCameraHImageOutput; - Timer = new System.Threading.Timer(CaptureLoop, null, 0, 50); - isCapturing = true; - btnAcquisition.Text = "结束采集"; - btnAcquisition.Type = TTypeMini.Warn; + } - else + catch (Exception ex) { - if (isLocationing) - { - AntdUI.Message.warn(this, $"定位未结束,不能结束采集!", autoClose: 3); - return; - } - - MainWindow.Instance.PLC.VisionPos(false); - Do3ThinkCamera.OnHImageOutput -= OnCameraHImageOutput; - Timer?.Dispose(); - isCapturing = false; - btnAcquisition.Text = "开始采集"; - btnAcquisition.Type = TTypeMini.Primary; + + AntdUI.Message.error(this, ex.Message, autoClose: 3); } - + } @@ -485,7 +722,14 @@ namespace DHSoftware.Views /// private void CaptureLoop(object? state) { - Do3ThinkCamera.Snapshot(); + try + { + Do3ThinkCamera.Snapshot(); + } + catch(Exception ex) + { + AntdUI.Message.error(this, ex.Message, autoClose: 3); + } } private CancellationTokenSource? _rotateCts; @@ -495,68 +739,75 @@ namespace DHSoftware.Views /// private async Task RotateControlAsync(bool direction) { - // 防止重复启动 - if (_isRotating) return; - _isRotating = true; - try { - if (!MainWindow.Instance.PLC.Connected) + // 防止重复启动 + if (_isRotating) return; + _isRotating = true; + + try { - this.Invoke(() => AntdUI.Message.warn(this, "未连接PLC!", autoClose: 3)); - return; + if (!MainWindow.Instance.PLC.Connected) + { + this.Invoke(() => AntdUI.Message.warn(this, "未连接PLC!", autoClose: 3)); + return; + } + + // 输入验证 + if (string.IsNullOrEmpty(iptSpeed.Text)) + { + this.Invoke(() => AntdUI.Message.warn(this, "请输入速度!", autoClose: 3)); + return; + } + + if (!int.TryParse(iptSpeed.Text, out int speed) || speed <= 0) + { + this.Invoke(() => AntdUI.Message.warn(this, "速度必须为正整数!", autoClose: 3)); + return; + } + + // 初始化取消令牌 + _rotateCts?.Dispose(); + _rotateCts = new CancellationTokenSource(); + + // 设置PLC参数 + MainWindow.Instance.PLC.TurnSpeed(speed); + MainWindow.Instance.PLC.TurnDirection(direction); + MainWindow.Instance.PLC.TurnStart(true); + + // 异步更新循环 + while (!_rotateCts.IsCancellationRequested) + { + var position = MainWindow.Instance.PLC.ReadVisionPos(); + Debug.WriteLine(position.ToString()); + this.BeginInvoke(() => iptPosition.Text = position.ToString()); + await Task.Delay(100, _rotateCts.Token); + } } - - // 输入验证 - if (string.IsNullOrEmpty(iptSpeed.Text)) + catch (OperationCanceledException) { - this.Invoke(() => AntdUI.Message.warn(this, "请输入速度!", autoClose: 3)); - return; + // 正常取消不处理 } - - if (!int.TryParse(iptSpeed.Text, out int speed) || speed <= 0) + catch (Exception ex) { - this.Invoke(() => AntdUI.Message.warn(this, "速度必须为正整数!", autoClose: 3)); - return; + this.Invoke(() => AntdUI.Message.error(this, $"操作异常:{ex.Message}")); } - - // 初始化取消令牌 - _rotateCts?.Dispose(); - _rotateCts = new CancellationTokenSource(); - - // 设置PLC参数 - MainWindow.Instance.PLC.TurnSpeed(speed); - MainWindow.Instance.PLC.TurnDirection(direction); - MainWindow.Instance.PLC.TurnStart(true); - - // 异步更新循环 - while (!_rotateCts.IsCancellationRequested) + finally { - var position = MainWindow.Instance.PLC.ReadVisionPos(); - Debug.WriteLine(position.ToString()); - this.BeginInvoke(() => iptPosition.Text = position.ToString()); - await Task.Delay(100, _rotateCts.Token); + // 确保停止转动 + MainWindow.Instance.PLC.TurnStart(false); + var finalPos = MainWindow.Instance.PLC.ReadVisionPos(); + this.Invoke(() => iptPosition.Text = finalPos.ToString()); + + _isRotating = false; + _rotateCts?.Dispose(); + _rotateCts = null; } } - catch (OperationCanceledException) - { - // 正常取消不处理 - } - catch (Exception ex) - { - this.Invoke(() => AntdUI.Message.error(this, $"操作异常:{ex.Message}")); - } - finally - { - // 确保停止转动 - MainWindow.Instance.PLC.TurnStart(false); - var finalPos = MainWindow.Instance.PLC.ReadVisionPos(); - this.Invoke(() => iptPosition.Text = finalPos.ToString()); - - _isRotating = false; - _rotateCts?.Dispose(); - _rotateCts = null; + catch (Exception ex) { + AntdUI.Message.error(this, ex.Message, autoClose: 3); } + } @@ -607,24 +858,32 @@ namespace DHSoftware.Views /// private void BtnSelectBackImg_Click(object? sender, EventArgs e) { - using (OpenFileDialog openFileDialog = new OpenFileDialog()) + try { - // 设置对话框标题 - openFileDialog.Title = "选择背景图片"; - // 限制文件后缀为 .hdvp - openFileDialog.Filter = "图片文件|*.jpg;*.jpeg;*.png;*.bmp"; - // 禁止多选 - openFileDialog.Multiselect = false; - - // 显示对话框并等待用户操作 - if (openFileDialog.ShowDialog() == DialogResult.OK) + using (OpenFileDialog openFileDialog = new OpenFileDialog()) { - string filePath = openFileDialog.FileName; + // 设置对话框标题 + openFileDialog.Title = "选择背景图片"; + // 限制文件后缀为 .hdvp + openFileDialog.Filter = "图片文件|*.jpg;*.jpeg;*.png;*.bmp"; + // 禁止多选 + openFileDialog.Multiselect = false; - iptBackImg.Text = filePath; + // 显示对话框并等待用户操作 + if (openFileDialog.ShowDialog() == DialogResult.OK) + { + string filePath = openFileDialog.FileName; + iptBackImg.Text = filePath; + + } } } + catch (Exception ex) + { + AntdUI.Message.error(this, ex.Message, autoClose: 3); + } + } /// @@ -634,24 +893,31 @@ namespace DHSoftware.Views /// private void BtnSelectModel_Click(object? sender, EventArgs e) { - using (OpenFileDialog openFileDialog = new OpenFileDialog()) + try { - // 设置对话框标题 - openFileDialog.Title = "选择算法文件"; - // 限制文件后缀为 .hdvp - openFileDialog.Filter = "算法文件 (*.hdvp)|*.hdvp"; - // 禁止多选 - openFileDialog.Multiselect = false; - - // 显示对话框并等待用户操作 - if (openFileDialog.ShowDialog() == DialogResult.OK) + using (OpenFileDialog openFileDialog = new OpenFileDialog()) { - string filePath = openFileDialog.FileName; + // 设置对话框标题 + openFileDialog.Title = "选择算法文件"; + // 限制文件后缀为 .hdvp + openFileDialog.Filter = "算法文件 (*.hdvp)|*.hdvp"; + // 禁止多选 + openFileDialog.Multiselect = false; - iptModel.Text = filePath; + // 显示对话框并等待用户操作 + if (openFileDialog.ShowDialog() == DialogResult.OK) + { + string filePath = openFileDialog.FileName; + iptModel.Text = filePath; + + } } } + catch (Exception ex) + { + AntdUI.Message.error(this, ex.Message, autoClose: 3); + } } /// @@ -661,20 +927,29 @@ namespace DHSoftware.Views /// private void VisualLocalizationWindow_Load(object? sender, EventArgs e) { - sltDirection.SelectedIndex = 0; - sltCameraName.Items.Clear(); - if (MainWindow.Instance.Cameras?.Count > 0) + try { - foreach(var cam in MainWindow.Instance.Cameras) + sltDirection.SelectedIndex = 0; + sltCameraName.Items.Clear(); + if (MainWindow.Instance.Cameras?.Count > 0) { - sltCameraName.Items.Add(cam.CameraName); + foreach (var cam in MainWindow.Instance.Cameras) + { + sltCameraName.Items.Add(cam.CameraName); + } } + else + { + AntdUI.Message.warn(this, $"未找到启用相机!", autoClose: 3); + } + } - else + catch (Exception ex) { - AntdUI.Message.warn(this, $"未找到启用相机!", autoClose: 3); + AntdUI.Message.error(this, ex.Message, autoClose: 3); + } - + } @@ -702,36 +977,43 @@ namespace DHSoftware.Views private void OnCameraHImageOutput(DateTime dt, CameraBase camera, MatSet imageSet) { - - - this.BeginInvoke(new MethodInvoker(delegate () + try + { + + this.BeginInvoke(new MethodInvoker(delegate () + { + + imageViewerControl1.Image = imageSet._mat.ToBitmap(); + if (isLocationing) { - - imageViewerControl1.Image = imageSet._mat.ToBitmap(); - if (isLocationing) + HObject obj = OpenCVHelper.MatToHImage(imageSet._mat); + HImage hImage = HalconHelper.ConvertHObjectToHImage(obj); + // 调用 ProcCall 的方法 + ProcCall.SetInputIconicParamObject("INPUT_Image", hImage); // 将图像输入Proc + + ProcCall.SetInputIconicParamObject("BackGroundPic", backImage); + + ProcCall.SetInputCtrlParamTuple("DistThreshold", Convert.ToInt32(iptThreshold.Text)); + ProcCall.Execute(); + double nNUm = ProcCall.GetOutputCtrlParamTuple("OUTPUT_Flag"); + + if (nNUm == 0) { - HObject obj = OpenCVHelper.MatToHImage(imageSet._mat); - HImage hImage = HalconHelper.ConvertHObjectToHImage(obj); - // 调用 ProcCall 的方法 - ProcCall.SetInputIconicParamObject("INPUT_Image", hImage); // 将图像输入Proc - - ProcCall.SetInputIconicParamObject("BackGroundPic", backImage); - - ProcCall.SetInputCtrlParamTuple("DistThreshold", Convert.ToInt32(iptThreshold.Text)); - ProcCall.Execute(); - double nNUm = ProcCall.GetOutputCtrlParamTuple("OUTPUT_Flag"); - - if (nNUm == 0) - { - MainWindow.Instance.PLC.TurnStart(false); - iptPosition.Text = MainWindow.Instance.PLC.ReadVisionPos().ToString(); - isLocationing = false; - btnLocalization.Text = "开始定位"; - btnLocalization.Type = TTypeMini.Primary; - } + MainWindow.Instance.PLC.TurnStart(false); + iptPosition.Text = MainWindow.Instance.PLC.ReadVisionPos().ToString(); + isLocationing = false; + btnLocalization.Text = "开始定位"; + btnLocalization.Type = TTypeMini.Primary; } - })); - + } + })); + + } + catch (Exception ex) + { + AntdUI.Message.error(this, ex.Message, autoClose: 3); + } + } }