FrmBoardList.cs 15.4 KB
using log4net;
using MetroFramework.Forms;
using TSA_V.Common; 
using TSA_V.DeviceLibrary;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using TSA_V.LoadCSVLibrary;

namespace TSA_V
{
    public partial class FrmBoardList : FrmBase
    {
        public static FrmBoardList instance = new FrmBoardList(); 
        public static readonly ILog LOGGER = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        private bool LoadOk = false;
        private FrmBoardList( )
        {
            InitializeComponent();
        }
        PointDisplay display = new PointDisplay();
        private void FrmBoard_Load(object sender, EventArgs e)
        {
            display.SetPic(picBoard, panBoard);
            LanguageProcess();
            LoadCom(); 
        }
        public void LoadCom()
        {
            LoadOk = false;
            cmbBoardList.DataSource = null;
            cmbBoardList.DataSource = BoardManager.boardList;
            cmbBoardList.DisplayMember = "boardName";
            cmbBoardList.ValueMember = "boardId";
            if (BoardManager.boardList.Count > 0)
            {
                cmbBoardList.SelectedIndex = 0;
            }
            this.listPoint.Columns.Clear();
            ColumnHeader preSendwire = new ColumnHeader();
            preSendwire.Text =ResourceCulture.GetString(ResourceCulture.Col_Num, "位号");   //设置列标题
            preSendwire.Width = 120;    //设置列宽度
            preSendwire.TextAlign = HorizontalAlignment.Left;   //设置列的对齐方式
            this.listPoint.Columns.Add(preSendwire);    //将列头添加到ListView控件。
            ColumnHeader chName = new ColumnHeader();
            chName.Text = ResourceCulture.GetString(ResourceCulture.Col_Name, "物料编码");   //设置列标题
            chName.Width = 100;    //设置列宽度
            chName.TextAlign = HorizontalAlignment.Left;   //设置列的对齐方式
            this.listPoint.Columns.Add(chName);    //将列头添加到ListView控件。
           
            ColumnHeader position = new ColumnHeader();
            position.Text = ResourceCulture.GetString(ResourceCulture.Col_Position, "料盘位置");   //设置列标题
            position.Width = 80;    //设置列宽度
            position.TextAlign = HorizontalAlignment.Left;   //设置列的对齐方式
            this.listPoint.Columns.Add(position);    //将列头添加到ListView控件。
            ColumnHeader leftCount = new ColumnHeader();
            leftCount.Text = ResourceCulture.GetString(ResourceCulture.Col_KuCun, "库存");   //设置列标题
            leftCount.Width = 100;    //设置列宽度
            leftCount.TextAlign = HorizontalAlignment.Left;   //设置列的对齐方式
            this.listPoint.Columns.Add(leftCount);    //将列头添加到ListView控件。

            LoadOk = true;
            LoadBoard();
        }

        private void UpdateCol()
        {
            if (this.listPoint.Columns.Count >= 4)
            {
                this.listPoint.Columns[0].Text = ResourceCulture.GetString(ResourceCulture.Col_Num, "编号");
                this.listPoint.Columns[1].Text = ResourceCulture.GetString(ResourceCulture.Col_Name, "名称");
                this.listPoint.Columns[2].Text = ResourceCulture.GetString(ResourceCulture.Col_Position, "位置");
                this.listPoint.Columns[3].Text = ResourceCulture.GetString(ResourceCulture.Col_KuCun, "库存");
            }
        }
        //FrmBoardInfo frm = null;
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (cmbBoardList.Text != "" && cmbBoardList.SelectedIndex >= 0)
            {
                BoardInfo board = (BoardInfo)cmbBoardList.SelectedItem;
                this.Visible = false;
                FrmBoardInfo.instance.Show(); 
                FrmBoardInfo.instance.SetBoard(board);
                FrmBoardInfo.instance.LoadForm();
          
            }
            else
            {
                MessageBox.Show(ResourceCulture.GetString(ResourceCulture.SelectP,"请选择程序!"));
            }
        }
        private int pointHight = 14;
        //private void btnLook_Click(object sender, EventArgs e)
        //{
        //    LoadBoard();

        //    this.panel1.Refresh();
        //}

        private void LoadBoard()
        {
            if (!LoadOk)
            {
                return;
            }
            try
            {
              
                LoadOk = false;
                if (cmbBoardList.Text != "" && cmbBoardList.SelectedIndex >= 0)
                {
                    BoardInfo board = (BoardInfo)cmbBoardList.SelectedItem;
                    groupBox2.Text = "【" + board.boardName + "】";
                    txtBomName.Text = board.bomName;
                    txtLength.Text = board.boardLength.ToString();
                    txtWidth.Text = board.boardWidth.ToString();
                    txtPointCount.Text = board.smtList.Count.ToString();
                    txtMianji.Text = (board.boardLength * board.boardWidth).ToString();
                    LoadPoint(board);

                    loadPictureBoxSize(board);
                    FrmProjectorScreen.instance.ShowPoint(false, board.smtList.ToArray());
                }

            }
            catch (Exception ex)
            {

            }
            finally
            {
                LoadOk = true;
            } 
            
        }
        int preId = -1;
        private void LoadPoint(BoardInfo board)
        {
            if (preId >= 0 && preId.Equals(board.boardId))
            {
                return;
            }
            preId = board.boardId;
            this.listPoint.Items.Clear();
            int i = 0;
            foreach (SMTPointInfo point in board.smtList)
            {
                ListViewItem lvi = new ListViewItem();
                lvi.ImageIndex = i;

                lvi.Text = point.PartNum.ToString();
                lvi.SubItems.Add(point.pointName);

                ComponetInfo com = CSVBomManager.GetCom(board.bomName, point.PartNum);
                if (com != null)
                {
                    lvi.SubItems.Add(com.PositionNum);
                    lvi.SubItems.Add(com.ComCount.ToString());
                }
                else
                {
                    lvi.SubItems.Add(point.PositionNum);
                    lvi.SubItems.Add(0.ToString());
                }
                //lvi.SubItems.Add(point.NodePositionX.ToString());
                //lvi.SubItems.Add(point.NodePositionY.ToString());
                this.listPoint.Items.Add(lvi);
                i++;
            }
            
             
        }
     
        private void btnNew_Click(object sender, EventArgs e)
        {
            //FrmBoardInfo info = new FrmBoardInfo(null);
            FrmAddBoard info = new FrmAddBoard();
            this.Visible = false;
            info.ShowDialog();
            if (FrmBoardInfo.instance.Visible.Equals(false))
            {
                this.Visible = true;
                LoadCom();
                cmbBoardList_SelectedIndexChanged(null, null);
                preId = -1;
            }
        }

        private void cmbBoardList_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadBoard();
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            if (cmbBoardList.Text != "" && cmbBoardList.SelectedIndex >= 0)
            { 
                BoardInfo board = (BoardInfo)cmbBoardList.SelectedItem;
                ////如果是当前默认程序,不能删除
                //if (BoardManager.CurrBoard != null && board.boardId.Equals(BoardManager.CurrBoardId))
                //{
                //    MessageBox.Show(ResourceCulture.GetString(ResourceCulture.CanotDel,"【{0}】是默认工作程序,不能删除!",board.boardName));
                //    return;
                //}
                if (MessageBox.Show(ResourceCulture.GetString(ResourceCulture.SureDeleteP,"确认要删除程序【{0}】吗?",board.boardName),
                 ResourceCulture.GetString(ResourceCulture.MsgTitle,   "提示"),
                //if (MessageBox.Show("确认要删除程序【" + board.boardName + "】吗?", "删除确认",
                  MessageBoxButtons.OKCancel,
                  MessageBoxIcon.Question).Equals(DialogResult.OK))
                {
                    BoardManager.Delete(board);
                    cmbBoardList.DataSource = BoardManager.boardList;
                    MessageBox.Show(ResourceCulture.GetString(ResourceCulture.DeletePOk, "删除程序【{0}】成功!", board.boardName));
                    this.listPoint.Clear();
                    this.txtBomName.Text = "";
                    this.txtLength.Text = "";
                    this.txtMianji.Text = "";
                    this.txtPointCount.Text = "";
                    this.txtWidth.Text = "";
                    this.picBoard.Image = null;

                    LoadCom(); 
                    cmbBoardList_SelectedIndexChanged(null, null);
                    preId = -1;
                }
            }
            else
            {
                MessageBox.Show(ResourceCulture.GetString(ResourceCulture.SelectP,"请选择程序!"));
            }
        }
        private BoardInfo preBoard = null;
        private void loadPictureBoxSize(BoardInfo board = null)
        {
            if (board == null)
            {
                board = preBoard; 
            }
            if (board == null)
            {
                return;
            }
            preBoard = board;
            picBoard.SizeMode = PictureBoxSizeMode.StretchImage;

            List<SMTPointInfo> pointList = board.smtList;
            int width = board.boardWidth;
            int height = board.boardLength;
            display.SetImage(board.GetImage());
            display.ShowName = false ;
            display.ImageNormal = true ;
            display.LoadPoint(width, height, pointList); 
        }

        private void FrmBoardList_Shown(object sender, EventArgs e)
        {
            //SetSkin(this); 
            //LoadBoard();
            if ((BoardManager.boardList.Count <= 0) && (LoadCSVLibrary.CSVBomManager.allComMap.Count <= 0))
            {
                MessageBox.Show(ResourceCulture.GetString(ResourceCulture.ImportComList, "请先导入元器件库!"));
                this.Close();
            }
            display.SetPic(picBoard, panBoard);
        }

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

        private void FrmBoardList_FormClosing(object sender, FormClosingEventArgs e)
        {
            Close();
            e.Cancel = true;
        }
        private void Close()
        {
            FrmProjectorScreen.instance.ClearPoint();
            this.Visible = false;
            FrmMenu.frmMain.Visible = true;
        }

        private void picBoard_VisibleChanged(object sender, EventArgs e)
        {
            if (this.Visible)
            {
                UpdateCol();
            }
        }

        private void btnImport_Click(object sender, EventArgs e)
        {
            openFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
            openFileDialog1.Filter = @"data|*.data";
            DialogResult result = this.openFileDialog1.ShowDialog();
            if (result.Equals(DialogResult.OK))
            {
                string fileName = this.openFileDialog1.FileName;

                string[] lines = File.ReadAllLines(fileName, Encoding.GetEncoding("gbk"));
                if (lines.Length <= 0)
                {
                    MessageBox.Show(ResourceCulture.GetString("解析程序失败,请选择正确文件"), ResourceCulture.GetString("提示"), MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                bool isNeedUpdate = false;
                BoardInfo board = BoardManager.StringToBoard(lines[0], out isNeedUpdate);
                if (board == null)
                {
                    MessageBox.Show(ResourceCulture.GetString("解析程序失败,请选择正确文件"), ResourceCulture.GetString("提示"), MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                if (BoardManager.getBoardByName(board.boardName) != null)
                {
                    MessageBox.Show(ResourceCulture.GetString("导入失败:程序名已存在"), ResourceCulture.GetString("提示"), MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                string oldName = ConfigAppSettings.GetValue(Setting_Init.BOARD_IMAGE_DEFAULT);
                string newName = board.boardName + Path.GetExtension(oldName);
                string configPath = ConfigAppSettings.GetValue(Setting_Init.BOARD_IMAGE_PATH);
                string oldPath = Application.StartupPath + @"\" + configPath +@"\" + oldName;
                string newPath = Application.StartupPath + @"\" + configPath + @"\" + newName;
                if (File.Exists(oldPath))
                {
                    File.Copy(oldPath, newPath, true);
                }
                board.imgName = newName;
                board.boardId = BoardManager.GetNextId();
                BoardManager.Add(board);
                MessageBox.Show(ResourceCulture.GetString("导入成功"), ResourceCulture.GetString("提示"));
                LoadCom();
                preId = -1;
            }
        }

        private void btnExport_Click(object sender, EventArgs e)
        {
            if (cmbBoardList.Text != "" && cmbBoardList.SelectedIndex >= 0)
            {
                BoardInfo board = (BoardInfo)cmbBoardList.SelectedItem;

                SaveFileDialog sfd = new SaveFileDialog();
                sfd.FileName = "";
                sfd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
                sfd.FileName = "PROGRAM-" + board.boardName + "-" + DateTime.Now.ToString("yyyy:MM:dd:HH:mm").Replace(":", "");
                sfd.Filter = @"data|*.data";
                DialogResult result = sfd.ShowDialog();

                if (result.Equals(DialogResult.OK))
                {
                    string filePath = sfd.FileName;
                    {
                        if (File.Exists(filePath))
                        {
                            File.Delete(filePath);
                        }

                        File.WriteAllText(filePath, JsonHelper.SerializeObject(board), Encoding.GetEncoding("gbk"));
                        MessageBox.Show(ResourceCulture.GetString("成功导出程序到文件:")+"\r\n" + filePath, ResourceCulture.GetString("提示"));
                    }
                }
            }
        }

        private void listPoint_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (LoadOk)
            {
                if (listPoint.SelectedItems != null && listPoint.SelectedItems.Count > 0)
                {
                    int index = listPoint.SelectedItems[0].Index;
                    display.selectIndex = index;
                    loadPictureBoxSize(null);
                }
            }
        }
    }
}