FrmProgramList.cs 13.5 KB
using LineSoldering.Common;
using LineSoldering.DeviceLibrary;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using LineSoldering.LoadCSVLibrary;

namespace LineSoldering.Client
{
    public partial class FrmProgramList : FrmBase
    {
        public FrmProgramList()
        {
            InitializeComponent();
        }

        private void FrmPointType_Load(object sender, EventArgs e)
        {
            LoadCom();
            LoadList();
            loadFixtureType();
        }
        List<string> FileList = new List<string>();
        private void LoadCom()
        {
            try
            {
                cmbAoiFile.Items.Clear();
                FileList = new List<string>();

                string appPath = Application.StartupPath;
                string pathName = ConfigAppSettings.GetValue(Setting_Init.AOIFileConfig);
                string filePath = appPath + pathName;
                string path = Path.GetDirectoryName(filePath);
                string suffix = Path.GetExtension(filePath);
                string[] files = Directory.GetFiles(path);
                foreach (string f in files)
                {
                    if (Path.GetExtension(f).Equals(suffix))
                    {
                        string fileName = Path.GetFileName(f);
                        FileList.Add(fileName);
                        cmbAoiFile.Items.Add(fileName);
                    }
                }
                if (FileList.Count > 0)
                {
                    cmbAoiFile.SelectedIndex = 0;
                }
            }
            catch (Exception ex)
            {
                LogUtil.error("加载出错:" + ex.ToString());
            }
        }
        private void LoadList()
        {
            cmbFixtureType.SelectedIndex = 0;
            dgvList.Rows.Clear();
            foreach (SProgramInfo obj in SProgramManager.programList)
            { 
                dgvList.Rows.Add(SetRowInfo(null, obj));
            }
          
        }
        private void loadFixtureType()
        {
            List<SProgramInfo> point = new List<SProgramInfo>();
            point.Add(new SProgramInfo(1));
            point.Add(new SProgramInfo(2));
            point.Add(new SProgramInfo(3));
            this.Column_FixtureType.DataSource = point;
            this.Column_FixtureType.ValueMember = "FixtureType";
            this.Column_FixtureType.DisplayMember = "FixtureName";
        }
        private DataGridViewRow SetRowInfo(DataGridViewRow view, SProgramInfo com)
        {
            if (view == null)
            {
                view = new DataGridViewRow();
                view.CreateCells(dgvList);
            }
            view.Cells[Column_Name.Index].Value = com.PartNumber.ToString(); 
            view.Cells[Column_boardCode.Index].Value = com.Code.ToString();
            view.Cells[Column_FixtureType.Index].Value = com.FixtureType; 
            view.Cells[this.Column_Id.Index].Value = com.Id;
            view.Cells[Column_Width.Index].Value = com.Width;
            view.Cells[Column_Length.Index].Value = com.Length;

            view.Cells[Column_CoverPlate.Index].Value = com.IsCoverPlate;
            view.Cells[Column_Check1.Index].Value = com.IsMaterialCheck1;
            view.Cells[Column_Check2.Index].Value = com.IsMaterialCheck2;
            view.Cells[Column_Station2Check1.Index].Value = com.IsStation2Check1;
            view.Cells[Column_Station2Check2.Index].Value = com.IsStation2Check2;
            view.Cells[Column_AoiFile.Index].Value = com.AoiFile;

            return view;
        }
         
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (groupInfo.Text.StartsWith("新增"))
            {
                AddProgram();
                LoadList();
            }
            else
            {
                if (dgvList.SelectedRows != null && dgvList.SelectedRows.Count > 0)
                {
                    int rowIndex = dgvList.SelectedRows[0].Index;
               
                    DataGridViewRow row = dgvList.Rows[rowIndex];
                    SProgramInfo obj = getRowPointInfo(row);
                    if (obj == null)
                    {
                        MessageBox.Show( "请选择程序!");
                        return;
                    }
                    obj.PartNumber = FormUtil.getValue(txtPartNumber);
                    //obj.ProgramDetial = FormUtil.getValue(txtDes);
                    obj.Length = FormUtil.GetIntValue(txtLength);
                    obj.Width = FormUtil.GetIntValue(txtWidth); 
                    obj.FixtureType = cmbFixtureType.SelectedIndex + 1;
                    obj.Code = FormUtil.getValue(txtWareCode);
                    obj.ProgramDetial = FormUtil.getValue(txtDetial);
                    obj.IsCoverPlate = chbCoverPlate.Checked;
                    obj.IsMaterialCheck1 = chbMaterialCheck1.Checked;
                    obj.IsMaterialCheck2 = chbMaterialCheck2.Checked;
                    obj.IsStation2Check1 = chbStation2Check1.Checked;
                    obj.IsStation2Check2 = chbStation2Check2.Checked;

                    obj.AoiFile = cmbAoiFile.Text;
                    if (obj.PartNumber.Equals(""))
                    {
                        MessageBox.Show("请输入程序名称!");
                        txtPartNumber.Focus();
                        return;
                    }
                    if (obj.Length <= 0)
                    {
                        MessageBox.Show("请输入程序长度!");
                        txtLength.Focus();
                        return;
                    }
                    if (obj.Width <= 0)
                    {
                        MessageBox.Show("请输入程序宽度!");
                        txtWidth.Focus();
                        return;
                    }
                    if (obj.AoiFile.Equals(""))
                    {
                        MessageBox.Show("请选择AOI程序!");
                        txtWidth.Focus();
                        return;
                    }
                    SProgramInfo o = SProgramManager.getByPartNum(obj.PartNumber);
                    if (o != null&&(!obj.Id.Equals(o.Id)))
                    {
                        MessageBox.Show("程序名称【" + obj.PartNumber + "】已存在!");
                        txtPartNumber.Focus();
                        return;
                    }
                    SProgramManager.Update(obj); 
                    SetRowInfo(dgvList.Rows[rowIndex], obj);
                    MessageBox.Show("程序【"+obj.PartNumber+"】保存成功!");
                    groupInfo.Text = "程序【" + obj.PartNumber + "】的基本信息";

                    LoadList();
                }
            }
        }

        private void AddProgram()
        {
            SProgramInfo obj = new SProgramInfo();
           
            obj.PartNumber = FormUtil.getValue(txtPartNumber);
            //obj.ProgramDetial = FormUtil.getValue(txtDes);
            obj.Length = FormUtil.GetIntValue(txtLength);
            obj.Width = FormUtil.GetIntValue(txtWidth);
            obj.Id = SProgramManager.GetNextId();
            obj.FixtureType = cmbFixtureType.SelectedIndex + 1; 
            obj.Code = FormUtil.getValue(txtWareCode);
            obj.ProgramDetial = FormUtil.getValue(txtDetial);
            obj.IsCoverPlate = chbCoverPlate.Checked;
            obj.IsMaterialCheck1 = chbMaterialCheck1.Checked;
            obj.IsMaterialCheck2 = chbMaterialCheck2.Checked;
            obj.IsStation2Check1 = chbStation2Check1.Checked;
            obj.IsStation2Check2 = chbStation2Check2.Checked;
            obj.AoiFile = cmbAoiFile.Text;

            if (obj.PartNumber.Equals(""))
            {
                MessageBox.Show("请输入程序名称!");
                txtPartNumber.Focus();
                return;
            }
            if (obj.Length <= 0)
            {
                MessageBox.Show("请输入程序长度!");
                txtLength.Focus();
                return;
            }
            if (obj.Width <= 0)
            {
                MessageBox.Show("请输入程序宽度!");
                txtWidth.Focus();
                return;
            }
            if (obj.AoiFile.Equals(""))
            {
                MessageBox.Show("请选择AOI程序!");
                txtWidth.Focus();
                return;
            }
            if (SProgramManager.getByPartNum(obj.PartNumber) != null)
            {
                MessageBox.Show("程序名称【"+obj.PartNumber+"】已存在!");
                txtPartNumber.Focus();
                return;
            }
            SProgramManager.Add(obj); 
            MessageBox.Show("程序【" + obj.PartNumber + "】保存成功!");
            groupInfo.Text = "程序【" + obj.PartNumber + "】的基本信息";
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            if (dgvList.SelectedRows != null && dgvList.SelectedRows.Count > 0)
            {
                int rowIndex = dgvList.SelectedRows[0].Index;
                DeleteCom(rowIndex);
            }
        }

        private SProgramInfo getRowPointInfo(DataGridViewRow row)
        {
            SProgramInfo point = new SProgramInfo();
            try
            {
                if (row.Cells[Column_Name.Name].Value == null)
                {
                    return null;
                } 
                int id = Convert.ToInt32( row.Cells[this.Column_Id.Name].Value.ToString());
                point = SProgramManager.getById(id);
            }
            catch (Exception ex)
            {
                LogUtil.error( "保存数据出错:" + ex.ToString());
                MessageBox.Show("请检查程序数据是否正确!");
                return null;
            }
            return point;
        }
        private void showDetail(int rowIndex)
        {
            DataGridViewRow row = dgvList.Rows[rowIndex];
            SProgramInfo obj = getRowPointInfo(row);
            if (obj == null)
            {
                MessageBox.Show("请选择程序!");
                return;
            }
            txtId.Tag = obj;
            txtPartNumber.Text = obj.PartNumber;
            txtWareCode.Text = obj.Code;
            txtLength.Text = obj.Length.ToString();
            txtWidth.Text = obj.Width.ToString();
            chbCoverPlate.Checked = obj.IsCoverPlate;
            chbMaterialCheck1.Checked = obj.IsMaterialCheck1;
            chbMaterialCheck2.Checked = obj.IsMaterialCheck2;
            chbStation2Check1.Checked = obj.IsStation2Check1;
            chbStation2Check2.Checked = obj.IsStation2Check2;
            cmbFixtureType.SelectedIndex = obj.FixtureType - 1;
            txtId.Text = obj.Id.ToString();
            //判断索引
            int index = FileList.IndexOf(obj.AoiFile);
            if (index >= 0)
            {
                cmbAoiFile.SelectedIndex = index;
            }
           
            groupInfo.Text = "程序【" + obj.PartNumber + "】的基本信息";
        }
        private void DeleteCom(int rowIndex)
        {
            DataGridViewRow row = dgvList.Rows[rowIndex];
            SProgramInfo obj = getRowPointInfo(row);
            if (obj.GetBoardMap().Count > 0)
            {
                MessageBox.Show("程序【"+obj.PartNumber+"】已经配置了产品,无法删除");
                return;
            }
            if (MessageBox.Show(
                  "确认要删除程序【" + obj.PartNumber + "】吗?","提示",MessageBoxButtons.OKCancel,
                MessageBoxIcon.Question) != DialogResult.OK)
            {
                return;
            }
            else
            {
                SProgramManager.Delete(obj); 
                this.dgvList.Rows.RemoveAt(rowIndex);
                MessageBox.Show("程序【" + obj.PartNumber + "】删除成功!");
                LoadList();
            }
        }

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

        private void dgvList_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex != -1 && e.ColumnIndex >= 0)
            {
                string name = this.dgvList.Columns[e.ColumnIndex].Name;
                if (name.Equals(this.Column_Del.Name))
                {
                    DeleteCom(e.RowIndex);
                }  
            }
        }

        private void dgvList_SelectionChanged(object sender, EventArgs e)
        {
            if (dgvList.SelectedRows != null && dgvList.SelectedRows.Count > 0)
            {
                int rowIndex = dgvList.SelectedRows[0].Index;
               
                showDetail(rowIndex); 
            }
        }

        private void FrmComponentList_FormClosing(object sender, FormClosingEventArgs e)
        {
             
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            groupInfo.Text = "新增";
            txtId.Text = "";
            txtPartNumber.Text = "";
            txtLength.Text = "";
            txtWidth.Text = "";
            txtDetial.Text = "";
            txtWareCode.Text = "";
            chbCoverPlate.Checked = false;
            chbMaterialCheck1.Checked = false;
            chbMaterialCheck2.Checked = false;
            chbStation2Check1.Checked = false;
            chbStation2Check2.Checked = false;
        }
         
    }
}