FrmTool.cs 15.1 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405

using OnlineStore.Common;
using OnlineStore.DeviceLibrary;
using OnlineStore.LoadCSVLibrary;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace OnlineStore.ACSingleStore
{
    public partial class FrmTool : FrmBase
    {
        private BoxAutoPoint autoP;
        private string LogName="自动对点位";
        private string DefaultfilePath = "";
        private bool IsStart = false;
        
        private BoxBean Box = null;
        //private PToolInfo workInfo = new PToolInfo();
        public FrmTool(BoxBean box, string ioIp, int ioIndex, string name)
        {
            InitializeComponent();
            this.Box = box;
            autoP = new BoxAutoPoint(box);
            DefaultfilePath = Application.StartupPath + @"\" + box.Name + @"position\";
            if (!Directory.Exists(DefaultfilePath))
            {
                Directory.CreateDirectory(DefaultfilePath);
                LogUtil.info(LogName + "创建位置保存文件夹:" + DefaultfilePath);
            }
            lblFileP.Text = DefaultfilePath;
            this.Text = name + "_自动对点位 ";
            LogName = this.Text;
            txtUpdownStart.Text = 0.ToString();
            FrmTool.CheckForIllegalCrossThreadCalls = false;
        }
        private int P3Offset = 0;
        private int P4Offset = 0;
        private int P5Offset = 0;
        private int P6Offset = 0;
        private void Form1_Load(object sender, EventArgs e)
        {
            AxisManager.instance.IsShowMsg = false;

            CheckForIllegalCrossThreadCalls = false;
           

            if (IOManager.instance == null)
            {
                IOManager.Init();
                IOManager.instance.ConnectionIOList(new List<string> { /*IoIp*/ });
            }

            P3Offset = ConfigAppSettings.GetIntValue(Setting_Init.Tool_P3_Offset);
            P4Offset = ConfigAppSettings.GetIntValue(Setting_Init.Tool_P4_Offset);
            P5Offset = ConfigAppSettings.GetIntValue(Setting_Init.Tool_P5_Offset);
            P6Offset = ConfigAppSettings.GetIntValue(Setting_Init.Tool_P6_Offset);
            int tSpeed = ConfigAppSettings.GetIntValue(Setting_Init.Tool_TargetSpeed);
            int tPosition = ConfigAppSettings.GetIntValue(Setting_Init.Tool_TargetPosition);

            if (P3Offset.Equals(0)) P3Offset = 60;
            if (P4Offset.Equals(0)) P4Offset = -60;
            if (P5Offset.Equals(0)) P5Offset = -60;
            if (P6Offset.Equals(0)) P6Offset = 60;
            if (tSpeed.Equals(0)) tSpeed = 20;
            if (tPosition.Equals(0)) tPosition = 60;

            txtP3Offset.Text = P3Offset.ToString();
            txtP4Offset.Text = P4Offset.ToString();
            txtP5Offset.Text = P5Offset.ToString();
            txtP6Offset.Text = P6Offset.ToString();
            txtSpeed.Text = tSpeed.ToString();
            txtTargetPosition.Text = tPosition.ToString();

            SaveConfig(tSpeed, tPosition);


            axisJogControl1.SetBoxBean(Box);
            int p = AxisManager.instance.GetActualtPosition(Box.Config.InOut_Axis.DeviceName, Box.Config.InOut_Axis.GetAxisValue());
            if (p > 0)
            {
                txtInout.Text = p.ToString();
            }

            p = AxisManager.instance.GetActualtPosition(Box.Config.Middle_Axis.DeviceName, Box.Config.Middle_Axis.GetAxisValue());
            if (p > 0)
            {
                DataGridViewRow view = new DataGridViewRow();
                view.CreateCells(dataGridView1);

                view.Cells[0].Value = p;
                dataGridView1.Rows.Add(view);
            } 
            timer1.Start();
            ioSingle.IOName = ResourceCulture.GetString("激光检测信号");
        }
         
        private void SaveConfig(int speed, int position)
        {
            //保存配置
            ConfigAppSettings.SaveValue(Setting_Init.Tool_P3_Offset, P3Offset);
            ConfigAppSettings.SaveValue(Setting_Init.Tool_P4_Offset, P4Offset);
            ConfigAppSettings.SaveValue(Setting_Init.Tool_P5_Offset, P5Offset);
            ConfigAppSettings.SaveValue(Setting_Init.Tool_P6_Offset, P6Offset);
            ConfigAppSettings.SaveValue(Setting_Init.Tool_TargetSpeed, speed);
            ConfigAppSettings.SaveValue(Setting_Init.Tool_TargetPosition, position);
        }
     
        private void workMoveStatus(bool isStart)
        {
            group1.Enabled = !isStart;
            group2.Enabled = !isStart;
            group3.Enabled = !isStart;
            group4.Enabled = !isStart;
            group5.Enabled = true;
            //group6.Enabled = !isStart ;
            group6.Enabled = true;
            btnStart.Enabled = !isStart;
            btnStop.Enabled = isStart;
            axisJogControl1.Enabled = !isStart;
            txtSpeed.Enabled = !isStart;
            btnExit.Enabled = !isStart;

        }
        private void btnSdStop_Click(object sender, EventArgs e)
        {
            LogUtil.info(LogName + "点击:停止运动");
            autoP.StopMove();
        }

      
        private bool isInProcesss = false;


        private void timer1_Tick(object sender, EventArgs e)
        {
            try
            {
                ioSingle.IOValue = (int)Box.IOValue(IO_Type.CheckPos);
                ioSingle.ShowData();
                if (autoP.IsStop())
                {
                    if (group1.Enabled.Equals(false))
                    {
                        workMoveStatus(false);
                    }
                    if (!IsStart)
                    {
                        lblMoveInfo.Text = "请按步骤确认位置信息,然后点击“开始校对位置”按钮启动位置校准";
                    }
                    else
                    {
                        lblMoveInfo.Text = "点位校准已结束,请打开文件夹查看位置文件";
                    }
                    lblErrorMsg.Text = "";
                }
                else
                {
                    if (group1.Enabled.Equals(true))
                    {
                        workMoveStatus(true);
                    } 

                    lblMoveInfo.Text = autoP.CurrStr();
                    lblErrorMsg.Text = autoP.WarnMsg;
                }
            }
            catch (Exception ex)
            {
            }
        }

        private void FrmPositionTool_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!autoP.IsStop())
            {
                MessageBox.Show("请先停止位置校准,再退出界面","提示",MessageBoxButtons.OK,MessageBoxIcon.Warning);
                e.Cancel = true;
                return;
            }
            timer1.Stop();
         
            LogUtil.logBox = null; 
        }

        private void btnOpenFolder_Click(object sender, EventArgs e)
        {
            if (IsStart)
            {
                System.Diagnostics.Process.Start("Explorer.exe", autoP.paramInfo.FileTargetPath);
            }
            else
            { 
                System.Diagnostics.Process.Start("Explorer.exe", DefaultfilePath);
            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnInoutP_Click(object sender, EventArgs e)
        {
            int p = AxisManager.instance.GetActualtPosition(Box.Config.InOut_Axis.DeviceName, Box.Config.InOut_Axis.GetAxisValue()); 
            txtInout.Text = p.ToString();
        }

        private void btnUpdownP_Click(object sender, EventArgs e)
        {
            int p = AxisManager.instance.GetActualtPosition(Box.Config.UpDown_Axis.DeviceName, Box.Config.UpDown_Axis.GetAxisValue());
            txtTargetPosition.Text = p.ToString();
        }

        private void btnAddMiddleP_Click(object sender, EventArgs e)
        {
            int p = AxisManager.instance.GetActualtPosition(Box.Config.Middle_Axis.DeviceName, Box.Config.Middle_Axis.GetAxisValue());
            
            DataGridViewRow view = new DataGridViewRow();
            view.CreateCells(dataGridView1);
            view.Cells[0].Value = dataGridView1.Rows.Count + 1;
            view.Cells[1].Value = p;
            dataGridView1.Rows.Add(view);

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex != -1 && e.ColumnIndex >= 0)
            {
                string name = this.dataGridView1.Columns[e.ColumnIndex].Name;
                int rowIndex = e.RowIndex;
                if (name.Equals(this.Column_Del.Name))
                {
                    DialogResult dialogResult = MessageBox.Show("确认要删除该行数据吗?", "提示?", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                    if (dialogResult.Equals(DialogResult.OK))
                    {
                        this.dataGridView1.Rows.RemoveAt(rowIndex);
                        for (int index = 0; index < dataGridView1.Rows.Count; index++)
                        {
                            dataGridView1.Rows[index].Cells[0].Value = (index + 1);
                        }
                    }
                }
            }
        }

        private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            if (e.RowIndex > -1 && e.ColumnIndex > -1)
            {
                DataGridView grid = (DataGridView)sender;
                grid.Rows[e.RowIndex].ErrorText = "";
                //这里最好用列名,而不是列索引号做判断
                if (grid.Columns[e.ColumnIndex].Name == Column_position.Name)
                {

                    Int32 newInteger = 0;
                    if (!int.TryParse(e.FormattedValue.ToString(), out newInteger))
                    {
                        e.Cancel = true;
                        grid.Rows[e.RowIndex].ErrorText = "位置只能输入整数,请输入正确的位置 !";
                        MessageBox.Show("位置只能输入整数,请输入正确的位置 !");
                        return;
                    }
                }
            }
        }


        #region 校准位置功能

        private void btnAbsMove_Click(object sender, EventArgs e)
        {
            int inoutP = FormUtil.GetIntValue(txtInout);
            if (inoutP <= 0)
            {
                MessageBox.Show("请输入正确的进出轴前进位置","提示");
                txtInout.Focus();
                return;
            }
            int SPostion = FormUtil.GetIntValue(txtUpdownStart);
            if (SPostion < 0)
            {
                MessageBox.Show("请输入正确的升降轴开始位置", "提示");
                txtUpdownStart.Focus();
                return;
            }
            txtUpdownStart.Text = SPostion.ToString();
            int TPostion = FormUtil.GetIntValue(txtTargetPosition);
            if (TPostion <= 0)
            {
                MessageBox.Show("请输入正确的升降轴最高位置", "提示");
                txtTargetPosition.Focus();
                return;
            }
            int speed = FormUtil.GetIntValue(txtSpeed);
            if (speed <= (0))
            {
                MessageBox.Show(ResourceCulture.GetString("请输入正确的速度"), "提示");
                txtSpeed.Focus();
                return;
            }
            P3Offset = FormUtil.GetIntValue(txtP3Offset);
            P4Offset = FormUtil.GetIntValue(txtP4Offset);
            P5Offset = FormUtil.GetIntValue(txtP5Offset);
            P6Offset = FormUtil.GetIntValue(txtP6Offset);

            SaveConfig(speed, TPostion);

            autoP.paramInfo = new PToolInfo();
            autoP.paramInfo.InoutTargetPosition = inoutP;
            autoP.paramInfo.UpdownStartPosition = SPostion;
            autoP.paramInfo.UpdownTargetPosition = TPostion;
            autoP.paramInfo.UpdownSpeed = speed;
            autoP.paramInfo.MiddlePositionList = new List<int>();
            autoP.paramInfo.PositionList = new List<int>();
            autoP.paramInfo.P3Offset = P3Offset;
            autoP.paramInfo.P4Offset = P4Offset;
            autoP.paramInfo.P5Offset = P5Offset;
            autoP.paramInfo.P6Offset = P6Offset;
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                string p = row.Cells[Column_position.Index].Value.ToString();
                try
                {
                    int mP = Convert.ToInt32(p);
                    autoP.paramInfo.MiddlePositionList.Add(mP);
                }
                catch (Exception ex)
                {
                    LogUtil.error("旋转轴位置列表【" + p + "】不是整数,直接跳过");
                }
            }
            if (autoP.paramInfo.MiddlePositionList.Count <= 0)
            {
                MessageBox.Show("请至少输入一个旋转轴位置","提示");
                dataGridView1.Focus();
                return;
            }

            autoP.paramInfo.LastValue = IO_VALUE.LOW;

            string date = DateTime.Now.ToString("yyyyMMddHHmm");
            string filePath = DefaultfilePath + date + @"\";
            if (Directory.Exists(filePath))
            {
                DialogResult result = MessageBox.Show("文件夹【" + filePath + "】已存在,将覆盖文件夹内容,是否确定继续?",
                    "确认提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                if (!result.Equals(DialogResult.OK))
                {
                    return;
                }
            }
            else
            {
                Directory.CreateDirectory(filePath);
            }
            autoP.paramInfo.FileTargetPath = filePath; 
            string msg = autoP.paramInfo.ParamStr();
            DialogResult result1 = MessageBox.Show(msg, "请确认对点位参数", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (result1.Equals(DialogResult.OK))
            {
                autoP.StopMove();
                LogUtil.info(LogName + "开始校对位置:" + msg + " 启动定时器 "); 
                autoP.Start();
                lblFileP.Text =   filePath;
                IsStart = true;
            }
        }

        #endregion

        private void btnMHome_Click(object sender, EventArgs e)
        {
            LogUtil.info(LogName + "点击:旋转轴回原点");
            ConfigMoveAxis axis = Box.Config.Middle_Axis;
            AxisManager.instance.HomeMove(axis.DeviceName, axis.GetAxisValue(),axis.HomeHighSpeed,axis.HomeLowSpeed,axis.HomeAddSpeed);
        }

        private void btnMStop_Click(object sender, EventArgs e)
        {
            LogUtil.info(LogName + "点击:旋转轴停止");
            ConfigMoveAxis axis = Box.Config.Middle_Axis;
            AxisManager.instance.SuddenStop(axis.DeviceName, axis.GetAxisValue());
        }
    }

    public class ResourceCulture
    {
        internal static string GetString(string str)
        {
            return str;
        }
    }
}