FrmMPcbConfig.cs 17.4 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 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440


using log4net;
using NPOI.Util;
using System;
using System.Collections.Generic;
using System.Drawing; 
using System.Reflection; 
using System.Windows.Forms;
using TSA_V.Common;
using TSA_V.DeviceLibrary;
using TSA_V.frmBoard;
using TSA_V.LoadCSVLibrary;
using static TSA_V.Common.LogUtil;

namespace TSA_V
{
    public partial class FrmMPcbConfig : FrmBase
    { 
        public BoardInfo currBoardInfo = null; 
 
        public static readonly ILog LOGGER = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

        public FrmMPcbConfig(BoardInfo board)
        {
            currBoardInfo = board;
            InitializeComponent(); 
            CheckForIllegalCrossThreadCalls = false;
            LanguagePro();
            // 确保打开对话框时立即加载并显示正确的网格文本
            //try { LoadBoardInfo(); } catch {}
        } 
        public void LoadForm()
        { 
        }
        private void LanguagePro()
        {
          
            this.dataGridViewImageColumn1.HeaderText = ResourceCulture.GetString(ResourceCulture.ItemText_Up, "上升");
            this.dataGridViewImageColumn2.HeaderText = ResourceCulture.GetString(ResourceCulture.ItemText_Down, "下降");
         
            
        }
        private void FrmBoardInfo_Shown(object sender, EventArgs e)
        { 
            LoadBoardInfo();  
        } 
         

        private void LoadBoardInfo()
        { 
            if (currBoardInfo == null) return;
            try
            {
                // 使用已有配置填充界面
                numRow.Value = (currBoardInfo.pcbRow > 0 ? currBoardInfo.pcbRow : 1);
                numCol.Value = (currBoardInfo.pcbCol > 0 ? currBoardInfo.pcbCol : 1);
                txtRowS.Text = (currBoardInfo.pcbRowValue > 0 ? currBoardInfo.pcbRowValue : 1).ToString();
                txtColS.Text = (currBoardInfo.pcbColValue > 0 ? currBoardInfo.pcbColValue : 1).ToString();

                // 更新提示文案:可单独设置相对偏移
                try { label1.Text = ResourceControl.GetString("MPCBTips" ,"*点击下方PCB块可设置其相对PCB1的偏移(单位:mm)。未设置则按上方行/列间距均匀排布"); } catch { }


                // 打开界面时,根据当前行列值模拟显示
                int rows =(int)numRow.Value;
                int cols = (int)numCol.Value;
                if (rows < 1) rows = 1;
                if (cols < 1) cols = 1;
                //SimulatePcbGrid(rows, cols);
                try
                {
                    if (FrmProjectorScreen.instance != null)
                    {
                        //List<SMTPointInfo> pointList = XYConvertManager.GetCopyPointCR(currBoardInfo);
                        //FrmProjectorScreen.instance.ShowPoint(false, currBoardInfo.PointColor, pointList.ToArray());

                        FrmProjectorScreen.instance.ShowAreaAndPoint(currBoardInfo,false,   currBoardInfo.GetSmtList().ToArray());
                    }
                }
                catch (Exception ex)
                {
                    LogUtil.info("测试投影效果失败: " + ex.Message);
                }

                try
                {
                    SimulatePcbGrid(rows, cols,currBoardInfo.pcbRowValue,currBoardInfo.pcbColValue);
                }
                catch (Exception ex)
                {
                    LogUtil.info("模拟PCB布局失败: " + ex.Message);
                }
            }
            catch (Exception ex)
            {
                LogUtil.error("加载多PCB配置失败:" + ex);
            }
        } 
        private bool isRun = false;
        private bool IsProcess = false;
        private void timer_Elapsed(object sender, EventArgs e)
        {
            if (IsProcess) { return; }
            IsProcess = true;
             
            IsProcess = false;
        }

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


        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex != -1 && e.ColumnIndex >= 0)
            { 
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        { 
            if (currBoardInfo == null)
            {
                MessageBox.Show(ResourceControl.GetString("BoardNull", "板卡信息为空,无法测试。"));
                return;
            }
            
            // 输入校验
            int pcbRow =(int)(numRow.Value);
            int pcbCol = (int)(numCol.Value);
            int pcbRowValue = FormUtil.GetIntValue(txtRowS);
            int pcbColValue = FormUtil.GetIntValue(txtColS);

            if (pcbRow < 1 || pcbCol < 1)
            {
                MessageBox.Show(ResourceCulture.GetString("PcbRowColInvalid", "行列数必须都大于等于1!"),
                    ResourceCulture.GetString("MsgTitle", "提示"), MessageBoxButtons.OK, MessageBoxIcon.Warning);
                if (pcbRow < 1)
                {
                    numRow.Focus();
                }
                if(pcbCol < 1)
                {
                    numCol.Focus();
                }
                return;
            }

            if (pcbRowValue <= 0)
            {
                MessageBox.Show(ResourceCulture.GetString("PcbRowValueInvalid", "行间距必须大于0!"), 
                    ResourceCulture.GetString("MsgTitle", "提示"), MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtRowS.Focus();
                return;
            }

            if (pcbColValue <= 0)
            {
                MessageBox.Show(ResourceCulture.GetString("PcbColValueInvalid", "列间距必须大于0!"), 
                    ResourceCulture.GetString("MsgTitle", "提示"), MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtColS.Focus();
                return;
            }

            // 保存多PCB参数
            currBoardInfo.pcbRow = pcbRow;
            currBoardInfo.pcbCol = pcbCol;
            currBoardInfo.pcbRowValue = pcbRowValue;
            currBoardInfo.pcbColValue = pcbColValue;

            BoardManager.Update(currBoardInfo);

            MessageBox.Show(ResourceCulture.GetString(ResourceCulture.SaveOk, "保存成功!"));
            Close();
        }
         
        private void button1_Click(object sender, EventArgs e)
        {
            Close();
        } 
        private void button1_Click_1(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void btnTest_Click(object sender, EventArgs e)
        {
            // 容错与输入校验
            int pcbRow = (int)numRow.Value;
            int pcbCol = (int)numCol.Value;
            int pcbRowValue = FormUtil.GetIntValue(txtRowS);
            int pcbColValue = FormUtil.GetIntValue(txtColS);

            if (pcbRow < 1 || pcbCol < 1)
            {
                MessageBox.Show(ResourceCulture.GetString("PcbRowColInvalid", "行列数必须都大于等于1!"),
                    ResourceCulture.GetString("MsgTitle", "提示"), MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (pcbRowValue <= 0)
            {
                MessageBox.Show(ResourceCulture.GetString("PcbRowValueInvalid", "行间距必须大于0!"),
                    ResourceCulture.GetString("MsgTitle", "提示"), MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtRowS.Focus();
                return;
            }

            if (pcbColValue <= 0)
            {
                MessageBox.Show(ResourceCulture.GetString("PcbColValueInvalid", "列间距必须大于0!"),
                    ResourceCulture.GetString("MsgTitle", "提示"), MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtColS.Focus();
                return;
            }
            if (currBoardInfo == null)
            {
                MessageBox.Show(ResourceCulture.GetString("BoardNull", "板卡信息为空,无法测试。"));
                LogUtil.info("板卡信息为空,无法测试");
                return;
            }
            if (currBoardInfo.smtList == null || currBoardInfo.smtList.Count == 0)
            {
                MessageBox.Show(ResourceCulture.GetString("NoPoints", "当前程序没有组装点,无法测试。"));
                LogUtil.info("当前程序没有组装点,无法测试");
                return;
            }

            // 安全获取校准点,若未校准则按原点位直接使用
           
           
         
            try
            {
                if (FrmProjectorScreen.instance != null)
                {
                    BoardInfo board = currBoardInfo.Copy();
                    board.pcbRow = pcbRow;
                    board.pcbCol = pcbCol;
                    board.pcbRowValue = pcbRowValue;
                    board.pcbColValue = pcbColValue;
                    FrmProjectorScreen.instance.ShowAreaAndPoint(board, true );
                    //List<SMTPointInfo> pointList = XYConvertManager.GetCopyPointCR(currBoardInfo, null, pcbRow, pcbCol, pcbRowValue, pcbColValue);
                    //FrmProjectorScreen.instance.ShowPoint(false, currBoardInfo.PointColor, pointList.ToArray());
                }
            }
            catch(Exception ex)
            {
                LogUtil.info("测试投影效果失败: " + ex.Message);
            }
             
            try
            {
                SimulatePcbGrid(pcbRow, pcbCol,pcbRowValue,pcbColValue);
            }
            catch (Exception ex)
            {
                LogUtil.info("模拟PCB布局失败: " + ex.Message);
            }
        }

        private void SimulatePcbGrid(int rows, int cols,int pcbRowValue,int pcbColValue)
        {
            if (tableLayoutPanel1 == null) return;
            tableLayoutPanel1.SuspendLayout();
            try
            {
                tableLayoutPanel1.Controls.Clear();
                tableLayoutPanel1.ColumnStyles.Clear();
                tableLayoutPanel1.RowStyles.Clear();

                tableLayoutPanel1.ColumnCount = cols;
                tableLayoutPanel1.RowCount = rows;

                // 均分布局
                for (int c = 0; c < cols; c++)
                {
                    tableLayoutPanel1.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100f / Math.Max(1, cols)));
                }
                for (int r = 0; r < rows; r++)
                {
                    tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.Percent, 100f / Math.Max(1, rows)));
                }

                // 创建标签:PCB1 使用强调色,其他使用普通色
                Color baseBack = Color.FromArgb(255, 220, 240, 255); // 淡蓝
                Color baseFore = Color.FromArgb(60, 60, 60);
                Color refBack = Color.FromArgb(255, 255, 230, 200); // 淡橙(基准)
                Color refFore = Color.FromArgb(120, 60, 0);

                // 读取行/列间距,作为默认偏移计算依据
                //int colGap = FormUtil.GetIntValue(txtColS);
                //if (colGap <= 0) colGap = currBoardInfo != null && currBoardInfo.pcbColValue > 0 ? currBoardInfo.pcbColValue : 1;
                //int rowGap = FormUtil.GetIntValue(txtRowS);
                //if (rowGap <= 0) rowGap = currBoardInfo != null && currBoardInfo.pcbRowValue > 0 ? currBoardInfo.pcbRowValue : 1;

                for (int r = 0; r < rows; r++)
                {
                    for (int c = 0; c < cols; c++)
                    {
                        int index = r * cols + c + 1;
                        // 计算默认偏移(均匀排布)
                        int defDx = c * pcbColValue;
                        int defDy = r  * pcbRowValue;
                        // 如果有单独设置,则使用已设置的值
                        int showDx = defDx;
                        int showDy = defDy;
                        try
                        {
                            if (currBoardInfo != null)
                            {
                                var p = currBoardInfo.GetPCBOffsetPoint(index);
                                if (index == 1)
                                {
                                    showDx = 0; showDy = 0; // PCB1 固定为(0,0)
                                }
                                else    if (p.X != 0 || p.Y != 0)
                                {
                                    showDx = p.X; showDy = p.Y;
                                }
                            }
                        }
                        catch { }

                        Label lblPcbInfo = new Label();
                        lblPcbInfo.Dock = DockStyle.Fill;
                        lblPcbInfo.TextAlign = ContentAlignment.MiddleCenter;
                        lblPcbInfo.Margin = new Padding(6);
                        lblPcbInfo.AutoSize = false;
                        lblPcbInfo.Font = new Font("微软雅黑", 12f, FontStyle.Bold);
                        lblPcbInfo.Text = $"PCB{index}({showDx},{showDy})";
                        lblPcbInfo.Tag = index;
                        lblPcbInfo.Cursor = Cursors.Hand;
                        lblPcbInfo.Click += LabelPcb_Click;

                        if (index == 1)
                        {
                            lblPcbInfo.BackColor = refBack;
                            lblPcbInfo.ForeColor = refFore;
                            lblPcbInfo.BorderStyle = BorderStyle.FixedSingle;
                            lblPcbInfo.Cursor = Cursors.No;
                        }
                        else
                        {
                            lblPcbInfo.BackColor = baseBack;
                            lblPcbInfo.ForeColor = baseFore;
                            lblPcbInfo.BorderStyle = BorderStyle.FixedSingle;
                        }

                        tableLayoutPanel1.Controls.Add(lblPcbInfo, c, r);
                    }
                }
            }
            finally
            {
                tableLayoutPanel1.ResumeLayout();
            }
        }

        private void LabelPcb_Click(object sender, EventArgs e)
        {
            try
            {
                var lbl = sender as Label;
                if (lbl == null) return;
                int index = (int)lbl.Tag;
                if (index == 1)
                {
                    MessageBox.Show(ResourceControl.GetString("Pcb1Fixed", "PCB1作为基准,偏移固定为(0,0)。", ResourceCulture.GetString("MsgTitle", "提示"), MessageBoxButtons.OK, MessageBoxIcon.Information));
                    return;
                }

                // 计算当前默认偏移:若已有单独设置则用已设置值,否则按行列间距均匀排布
                int rows = (int)numRow.Value;
                int cols = (int)numCol.Value;
                int colGap = FormUtil.GetIntValue(txtColS);
                if (colGap <= 0) colGap = currBoardInfo != null && currBoardInfo.pcbColValue > 0 ? currBoardInfo.pcbColValue : 1;
                int rowGap = FormUtil.GetIntValue(txtRowS);
                if (rowGap <= 0) rowGap = currBoardInfo != null && currBoardInfo.pcbRowValue > 0 ? currBoardInfo.pcbRowValue : 1;
                int r0 = (index - 1) / Math.Max(1, cols);
                int c0 = (index - 1) % Math.Max(1, cols);

                int defDx = c0 * colGap;
                int defDy = r0 * rowGap;
                int currDx = defDx;
                int currDy = defDy;
                try
                {
                    var p = currBoardInfo.GetPCBOffsetPoint(index);
                    if (p.X != 0 || p.Y != 0)
                    {
                        currDx = p.X;
                        currDy = p.Y;
                    }
                }
                catch { }

                using (var dlg = new TSA_V.frmBoard.FrmPCbOffsetConfig(index, currDx, currDy))
                {
                    if (dlg.ShowDialog(this) == DialogResult.OK)
                    {
                        int x = dlg.OffsetX;
                        int y = dlg.OffsetY;
                        currBoardInfo.SetPCBOffset(index, new Point(x, y));
                        // 更新标签显示
                        lbl.Text = $"PCB{index}({x},{y})";
                        try
                        {
                            if (FrmProjectorScreen.instance != null)
                            {
                                //List<SMTPointInfo> pointList = XYConvertManager.GetCopyPointCR(currBoardInfo, null, rows, cols, rowGap, colGap);
                                //FrmProjectorScreen.instance.ShowPoint(false, currBoardInfo.PointColor, pointList.ToArray());
                                BoardInfo board = currBoardInfo.Copy() ;
                                board.pcbRow = rows;
                                board.pcbCol = cols;
                                board.pcbRowValue = rowGap;
                                board.pcbColValue = colGap;
                                FrmProjectorScreen.instance.ShowAreaAndPoint(board, true);
                            }
                        }
                        catch (Exception ex)
                        {
                            LogUtil.info("测试投影效果失败: " + ex.Message);
                        }
                    }
                }
           

            }
            catch (Exception ex)
            {
                LogUtil.info("编辑PCB偏移失败:" + ex.Message);
            }
        }
    }


}