FrmAddBoard.cs 12.2 KB
using PUSICANLibrary;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using TSA_V.Common; 
using TSA_V.DeviceLibrary;
using TSA_V.LoadCSVLibrary;

namespace TSA_V
{
    public partial class FrmAddBoard : FrmBase
    {
        public FrmAddBoard()
        {
            InitializeComponent();
        }

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

        private void btnSave_Click(object sender, EventArgs e)
        {
            BoardInfo board = SaveBoard();
            if (board != null)
            { 
                MessageBox.Show(ResourceCulture.GetString(ResourceCulture.SaveOk,"保存成功!"));
                this.Close();
            }
        }
        private bool GetFormBoard(BoardInfo board)
        {
            board.originX = xyMoveControl1.XValue;
            board.originY = xyMoveControl1.YValue; 
            board.boardName = FormUtil.getValue(txtBoardName); 
            board.orgType = 4;
            board.orgType = cmbOrgType.SelectedIndex + 1;
            if (board.boardName.Equals(""))
            {
                MessageBox.Show(ResourceCulture.GetString(ResourceCulture.WritePName, "请输入程序名称!"));
                cmbBomList.Focus();
                return false ;
            }
            if (!(BoardManager.getBoardByName(board.boardName) == null))
            {
                MessageBox.Show(ResourceCulture.GetString(ResourceCulture.PNameExist, "程序名【" + board.boardName + "】已存在,请重新输入!"));
                txtBoardName.Focus();
                return false ;
            }
            if (cmbBomList.SelectedIndex < 0)
            {
                MessageBox.Show(ResourceCulture.GetString(ResourceCulture.ChoiceLibrary, "请选择元器件库!"));
                txtBoardName.Focus();
                return false ;
            }
            board.boardCode = FormUtil.getValue(txtCode);
            board.bomName = cmbBomList.Text;
            board.imgName = FormUtil.getValue(txtImagePath);
            if (board.imgName.Equals(""))
            {
                MessageBox.Show(ResourceCulture.GetString(ResourceCulture.ImportPImage, "请导入程序图片!"));
                this.btnOpenFile.Focus();
                return false ;
            }
            board.boardLength = FormUtil.GetIntValue(txtBoardW);
            board.boardWidth = FormUtil.GetIntValue(txtBoardL);

            if (board.boardLength <= 0)
            {
                MessageBox.Show(ResourceCulture.GetString(ResourceCulture.WriteLength, "请输入宽度!"));
                this.txtBoardW.Focus();
                return false ;
            }
            if (board.boardWidth <= 0)
            {
                MessageBox.Show(ResourceCulture.GetString(ResourceCulture.WriteWidth, "请输入长度!"));
                this.txtBoardL.Focus();
                return false ;
            }
            board.LineWidth = FormUtil.GetIntValue(txtLineWidth);
            if (board.LineWidth <= 0)
            {
                MessageBox.Show(ResourceCulture.GetString(ResourceCulture.WriteLineWidth, "请输入线体宽度!"));
                this.txtLineWidth.Focus();
                return false;
            }
            if (!TSAVBean.IsValidPosition(board.originX, board.originY))
            {
                MessageBox.Show(ResourceCulture.GetString(ResourceCulture.WPointP, "请输入有效的右下角坐标!"));
                //this.txtRobotX.Focus();
                return false ;
            } 

            string imagePath = Path.GetFullPath(txtImagePath.Text);
            ProcessPictures.copyImage(Application.StartupPath, board.boardName, imagePath);
            board.imgName = board.boardName + Path.GetExtension(openFileDialog1.FileName);
            return true;
        }
        private BoardInfo SaveBoard()
        {
            try
            {
                BoardInfo board = new BoardInfo();
                bool result = GetFormBoard(board);
                if (!result)
                {
                    return null;
                } 
                List<ComponetInfo> comList = CSVReaderBomManager.GetComList(board.bomName);
                if (chbSort.Checked)
                {
                    comList = (from m in comList orderby m.PositionX ascending, m.PositionY ascending select m).ToList<ComponetInfo>();
                }
                List<SMTPointInfo> smtList = new List<SMTPointInfo>();
                int i = 1;
                double xNodeChange = TSAVBean.X_ChangeValue;
                double yNodeChange = TSAVBean.Y_ChangeValue;
                LogUtil.info("新增【"+board.boardName+"】,根据换算参数计算红外坐标(转换系数:x["+xNodeChange + "]y["+yNodeChange+"])");

                double pianYiX = 0, pianYiY = 0;
              
                 int xFangxiang = 1;
                int yFangxiang = -1;
                int pointSizeX = xyMoveControl1.PointSizeX;
                int pointSizeY = xyMoveControl1.PointSizeY;
                int pointType = xyMoveControl1.PointType;
                int penWidth = xyMoveControl1.PenWidth;
                foreach (ComponetInfo com in comList)
                {
                    double x =com.PositionX+ pianYiX;
                    double y =  com.PositionY+pianYiY;
 
                    if (board.orgType.Equals(2))
                    {
                        x = com.PositionX;
                        y = board.boardLength - y;
                    }
                    else if (board.orgType.Equals(3))
                    {
                        x = board.boardWidth - com.PositionX;
                        y = com.PositionY;
                    }
                    else if (board.orgType.Equals(4))
                    {
                        y = board.boardLength - y;
                        x = board.boardWidth - x;
                    }
                    SMTPointInfo point = new SMTPointInfo(i, com.PartNum, com.ComponentName, x, y, com.PositionNum, false, false, 0, 0,pointType,pointSizeX,pointSizeY,penWidth);


                    y = board.boardLength - y;
                    x = board.boardWidth - x;
                    double nodeXValue = (board.originX + (double)(x * xNodeChange*xFangxiang));
                    double nodeYValue = (board.originY -  (double)(y * yNodeChange*yFangxiang));
                    if (board.orgType.Equals(2))
                    {
                        nodeXValue = (board.originX + (double)(x * xNodeChange * xFangxiang));
                        nodeYValue = (board.originY + (double)(y * yNodeChange * yFangxiang));
                    }else if (board.orgType.Equals(3))
                    {
                        nodeXValue = (board.originX - (double)(x * xNodeChange * xFangxiang));
                        nodeYValue = (board.originY - (double)(y * yNodeChange * yFangxiang));
                    }
                    else if (board.orgType.Equals(4))
                    {
                        nodeXValue = (board.originX - (double)(x * xNodeChange * xFangxiang));
                        nodeYValue = (board.originY + (double)(y * yNodeChange * yFangxiang));
                    }
                
                    if (TSAVBean.IsValidPosition(nodeXValue, nodeYValue))
                    {
                        point.NodePositionX = nodeXValue;
                        point.NodePositionY = nodeYValue;
                    }
                    else
                    {
                        point.NodePositionX = board.originX;
                        point.NodePositionY = board.originY;
                        LogUtil.error("新增【" + board.boardName + "】元器件【" + point.pointName + "】导入【" + com.PositionX + "," + com.PositionY + "】红点【" + nodeXValue + "," + nodeYValue + "】超出移动范围,默认为右下角坐标!");
                    }
                    point.WeldTemp = 360;
                    point.WeldTime = 3;
                    point.NeedCheck = true;
                    point.NeedSoldering = true;
                    LogUtil.info("新增【" + board.boardName + "】元器件【" + point.pointName + "】导入【" + com.PositionX + "," + com.PositionY + "】图片【" + point.PositionX+","+point.PositionY+ "】红点【" + nodeXValue + "," + nodeYValue + "】");
                    smtList.Add(point);
                    i++;

                }
                board.smtList = smtList;
                board.boardId = BoardManager.GetNextId();
                BoardManager.Add(board);
                return board;
            }
            catch (Exception ex)
            {
                MessageBox.Show( ResourceCulture.GetString(ResourceCulture.SaveError, "保存失败!"));
                LogUtil.error("添加程序出错:" + ex.ToString());
                return null;
            }
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            BoardInfo board = SaveBoard();
            if (board!=null)
            {
                this.Visible = false;
                FrmBoardInfo.instance.Show();
                FrmBoardInfo.instance.SetBoard(board);
                FrmBoardInfo.instance.LoadForm();  
                this.Close();
            }
        }

        private void btnOpenFile_Click(object sender, EventArgs e)
        {
            openFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory); 
            openFileDialog1.DefaultExt = "jpg|gif|bmp|png|jpeg";
            openFileDialog1.Filter = "jpg|*.jpg|gif|*.gif|bmp|*.bmp|png|*.png|jpeg|*.jpeg";
            string directory = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);//桌面路径
            openFileDialog1.InitialDirectory = directory;
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                openFileDialog1.Tag = true;
                this.txtImagePath.Text= openFileDialog1.FileName; 
            } 
        }
       
        private void FrmAddBoard_Load(object sender, EventArgs e)
        {
            cmbOrgType.SelectedIndex = 2;
            this.timer1.Enabled = true;
            LoadComList();
            LanguageProcess(); 

            //xyMoveControl1.XValue = ConfigAppSettings.GetDoubleValue(Setting_Init.BOARD_ORIGIN_X);
            //xyMoveControl1.YValue = ConfigAppSettings.GetDoubleValue(Setting_Init.BOARD_ORIGIN_Y);
            this.cmbOrgType.Items.Clear();
            if (ResourceCulture.CurrLanguage.Equals(ResourceCulture.English))
            {
                this.cmbOrgType.Items.AddRange(new object[] {
            "TOP LEFT CORNER",
            "LEFT BOTTOM",
            "TOP RIGHT CORNER",
            "BOTTOM RIGHT CORNER"});
            }
            else if (ResourceCulture.CurrLanguage.Equals(ResourceCulture.China))
            {
                this.cmbOrgType.Items.AddRange(new object[] {
            "左上角",
            "左下角",
            "右上角",
            "右下角"});
            }
            cmbOrgType.SelectedIndex = 2;
            SetScreen();
            xyMoveControl1.ShowPointEvent += XyMoveControl1_ShowPointEvent;
            xyMoveControl1.XValue = TSAVBean.X_Max/2;
            xyMoveControl1.YValue = TSAVBean.Y_Max/2;
            xyMoveControl1.ShowText = "";
           
        }
        private bool XyMoveControl1_ShowPointEvent(ProjectorPInfo p)
        {
            FrmProjectorScreen.instance.ClearPoint();
            FrmProjectorScreen.instance.ShowPoint(p);
            return true;
        }
        private void LoadComList()
        {
            List<string> keyList = new List<string>(LoadCSVLibrary.CSVReaderBomManager.allComMap.Keys);
            this.cmbBomList.Items.Clear();
            foreach (string key in keyList)
            {
                this.cmbBomList.Items.Add(key);
            }

            if (keyList.Count > 0)
            {
                this.cmbBomList.SelectedIndex = 0;
            }
        }
        private void timer1_Tick(object sender, EventArgs e)
        { 
            xyMoveControl1.UpdateStatus();
        } 
    }
}