XYConvertManager.cs 19.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 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484
using System;
using System.CodeDom;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TSA_V.Common;

namespace TSA_V.DeviceLibrary
{
    public class XYConvertManager
    {
        public static int BoundsWidth = 1920;
        public static int BoundsHeight = 1080;
        private static List<ConBaseInfo> conList = null;
        public static List<ConBaseInfo> ConList
        {
            get
            {
                if (conList == null)
                {
                    try
                    {
                        conList = JsonHelper.DeserializeJsonToObject<List<ConBaseInfo>>(Setting_NInit.Data_LastCalibrateInfo);
                    }
                    catch (Exception ex)
                    {
                        LogUtil.error("转换lastCalibrateBean出错:" + ex.ToString());
                    }
                }
                return conList;

            }
            set
            {
                conList = value;
                Setting_NInit.Data_LastCalibrateInfo = JsonHelper.SerializeObject(conList);
            }
        }

        public static void SaveData(BoardInfo board)
        {
            List<ConBaseInfo> data = new List<ConBaseInfo>() {
                new ConBaseInfo(new Point(0,0),board.calInfo.leftUpPoint),
                new ConBaseInfo(new Point(board.boardWidth,board.boardLength),board.calInfo.rightBottomPoint),
                };
        }



        public static SMTPointInfo CalPointNodePosition(SMTPointInfo smtPoint, List<SMTPointInfo> checkOKList)
        {
            try
            {
                if (smtPoint.CheckOK && Setting_NInit.Work_UseCheckOK)
                {
                    //校准点不再自动计算
                    return smtPoint;
                }
                if (checkOKList.Count < 2)
                {
                    return smtPoint;
                }
                double oldX = smtPoint.NodePositionX;
                double oldY = smtPoint.NodePositionY;
                //SMTPointInfo APoint = GetPoint(smtPoint, checkOKList);

                //SMTPointInfo BPoint = GetPoint(APoint, checkOKList);
                SMTPointInfo APoint = checkOKList[0];
                SMTPointInfo BPoint = checkOKList[1];



                //计算坐标    
                if (Math.Abs(smtPoint.PositionX - APoint.PositionX) < 0.01)
                {
                    smtPoint.NodePositionX = APoint.NodePositionX;
                }
                else
                {
                    smtPoint.NodePositionX = APoint.NodePositionX + (BPoint.NodePositionX - APoint.NodePositionX) * (smtPoint.PositionX - APoint.PositionX) / (BPoint.PositionX - APoint.PositionX);
                    if (double.IsNaN(smtPoint.NodePositionX) || double.IsInfinity(smtPoint.NodePositionX))
                    {
                        smtPoint.NodePositionX = APoint.NodePositionX;
                    }
                }
                if (Math.Abs(smtPoint.PositionY - APoint.PositionY) < 0.01)
                {
                    smtPoint.NodePositionY = APoint.NodePositionY;
                }
                else
                {
                    smtPoint.NodePositionY = APoint.NodePositionY + (BPoint.NodePositionY - APoint.NodePositionY) * (smtPoint.PositionY - APoint.PositionY) / (BPoint.PositionY - APoint.PositionY);
                    if (double.IsNaN(smtPoint.NodePositionY) || double.IsInfinity(smtPoint.NodePositionY))
                    {
                        smtPoint.NodePositionY = APoint.NodePositionY;
                    }
                }
                if (smtPoint.NodePositionX <= 0)
                {
                    smtPoint.NodePositionX = 1;
                }
                else if (smtPoint.NodePositionX > BoundsWidth)
                {
                    smtPoint.NodePositionX = BoundsWidth;
                }
                if (smtPoint.NodePositionY <= 0)
                {
                    smtPoint.NodePositionY = 1;
                }
                else if (smtPoint.NodePositionY > BoundsHeight)
                {
                    smtPoint.NodePositionY = BoundsHeight;
                }

                if (smtPoint.PointSizeX < 1 || smtPoint.PointSizeX > 255)
                {
                    smtPoint.PointSizeX = Setting_NInit.Device_DefaultPointSize;
                }
                if (smtPoint.PointSizeY < 1 || smtPoint.PointSizeY > 255)
                {
                    smtPoint.PointSizeY = Setting_NInit.Device_DefaultPointSize;
                }


                double xXishu = (BPoint.NodePositionX - APoint.NodePositionX) / (BPoint.PositionX - APoint.PositionX);
                double yXishu = (BPoint.NodePositionY - APoint.NodePositionY) / (BPoint.PositionY - APoint.PositionY);
                //计算点大小
                if (smtPoint.imgP?.uTime > smtPoint.pUTime)
                {
                    smtPoint.PointSizeX = (int)((BPoint.NodePositionX - APoint.NodePositionX) * smtPoint.imgP.SizeX / (BPoint.PositionX - APoint.PositionX));
                    smtPoint.PointSizeY = (int)((BPoint.NodePositionY - APoint.NodePositionY) * smtPoint.imgP.SizeY / (BPoint.PositionY - APoint.PositionY));
                    smtPoint.PenWidth = (int)((BPoint.NodePositionX - APoint.NodePositionX) * smtPoint.imgP.PWidth / (BPoint.PositionX - APoint.PositionX));

                }

                //smtPoint.PointSize = 4;
                //smtPoint.NodePositionX = BPoint.NodePositionX - (BPoint.NodePositionX - APoint.NodePositionX) * (BPoint.PositionX - smtPoint.PositionX) / (BPoint.PositionX - APoint.PositionX);
                //smtPoint.NodePositionY = BPoint.NodePositionY - (BPoint.NodePositionY - APoint.NodePositionY) * (BPoint.PositionY - smtPoint.PositionY) / (BPoint.PositionY - APoint.PositionY);
                LogUtil.info($" 【{smtPoint.PN}】图片坐标【{smtPoint.PositionX},{smtPoint.PositionY}】旧坐标【{oldX},{oldY}】新坐标【{smtPoint.NodePositionX},{smtPoint.NodePositionY}】" +
                    $"点尺寸({smtPoint.PointSizeX},{smtPoint.PointSizeY}),画笔宽{smtPoint.PenWidth}");
                return smtPoint;

            }
            catch (Exception ex)
            {
                LogUtil.error(" 【" + smtPoint.PN + "】图片坐标【" + smtPoint.PositionX + "," + smtPoint.PositionY + "】校准坐标出错:" + ex.ToString());

            }
            return smtPoint;
        }


        public static Point CalPCBAreaNodePosition(Point position, List<SMTPointInfo> checkOKList, int type)
        {
            Point nodePosition = new Point(0,0);
            try
            {
               
                if (checkOKList.Count < 2)
                {
                    return nodePosition;
                }  
                SMTPointInfo APoint = checkOKList[0];
                SMTPointInfo BPoint = checkOKList[1];
                 
                //计算坐标    
                if (Math.Abs(position.X - APoint.PositionX) < 0.01)
                {
                    nodePosition.X =(int) APoint.NodePositionX;
                }
                else
                {
                    nodePosition.X =(int)( APoint.NodePositionX + (BPoint.NodePositionX - APoint.NodePositionX) * (position.X - APoint.PositionX) / (BPoint.PositionX - APoint.PositionX));
                    if (double.IsNaN(nodePosition.X) || double.IsInfinity(nodePosition.X))
                    {
                        nodePosition.X =(int) APoint.NodePositionX;
                    }
                }
                if (Math.Abs(position.Y - APoint.PositionY) < 0.01)
                {
                    nodePosition.Y = (int)APoint.NodePositionY;
                }
                else
                {
                    nodePosition.Y =(int)( APoint.NodePositionY + (BPoint.NodePositionY - APoint.NodePositionY) * (position.Y - APoint.PositionY) / (BPoint.PositionY - APoint.PositionY));
                    if (double.IsNaN(nodePosition.Y) || double.IsInfinity(nodePosition.Y))
                    {
                        nodePosition.Y =(int) APoint.NodePositionY;
                    }
                }
                if (nodePosition.X <= 0)
                {
                    nodePosition.X = 1;
                }
                else if (nodePosition.X > BoundsWidth)
                {
                    nodePosition.X = BoundsWidth;
                }
                if (nodePosition.Y <= 0)
                {
                    nodePosition.Y = 1;
                }
                else if (nodePosition.Y > BoundsHeight)
                {
                    nodePosition.Y = BoundsHeight;
                }
                  
                //LogUtil.info($" 【{smtPoint.PN}】图片坐标【{smtPoint.PositionX},{smtPoint.PositionY}】旧坐标【{oldX},{oldY}】新坐标【{smtPoint.NodePositionX},{smtPoint.NodePositionY}】" +
                //    $"点尺寸({smtPoint.PointSizeX},{smtPoint.PointSizeY}),画笔宽{smtPoint.PenWidth}");
                //return smtPoint;

            }
            catch (Exception ex)
            {
                LogUtil.error(" CalPointNodePosition 多PCB基准点计算错误【 图片坐标【" + position.X + "," + position.Y + "】校准坐标出错:" + ex.ToString());

            }
            return nodePosition;
        }

        private SMTPointInfo GetPoint(SMTPointInfo smtPoint, List<SMTPointInfo> checkOKList)
        {
            SMTPointInfo p = null;
            double maxPingFang = 0;
            foreach (SMTPointInfo sm in checkOKList)
            {
                if (sm.pointNum.Equals(smtPoint.pointNum))
                {
                    continue;
                }
                double xjuli = sm.PositionX - smtPoint.PositionX;
                double yjuli = sm.PositionY - smtPoint.PositionY;
                double cping = xjuli * xjuli + yjuli * yjuli;
                if (cping > maxPingFang)
                {
                    p = sm;
                    maxPingFang = cping;
                    continue;
                }
            }
            if (p != null)
            {
                return p;
            }
            return null;
        }


        public static List<ProjectorPInfo> GetCopyProjectPointCR(BoardInfo board, SMTPointInfo sMTPoint = null)
        {
            List<SMTPointInfo> pointList = GetCopyPointCR(board, sMTPoint);
            List<ProjectorPInfo> projectorPInfoList = new List<ProjectorPInfo>();
            foreach (SMTPointInfo point in pointList)
            {
                ProjectorPInfo projectorPInfo = point.GetScreenShowPInfo();
                projectorPInfoList.Add(projectorPInfo);
            }
            return projectorPInfoList;
        }

        /// <summary>
        /// 多板拷贝点
        /// </summary>
        /// <param name="board"></param>
        /// <param name="sMTPoint"></param>
        /// <param name="targetRow"></param>
        /// <param name="targetCol"></param>
        /// <param name="targetRowValue"></param>
        /// <param name="targetColValue"></param>
        /// <returns></returns>
        public static List<List<Point>> GetCopyArea(BoardInfo board, int targetRow = 0, int targetCol = 0, int targetRowValue = 0, int targetColValue = 0)
        {
            List<List<Point>> result = new List<List<Point>>();
            if (board == null || board.calInfo == null || board.calInfo.leftUpPoint == null || board.calInfo.rightBottomPoint == null)
            {
                return result;
            }
            try
            {
                result.Add(new List<Point>() { board.calInfo.leftUpPoint, board.calInfo.rightBottomPoint });
                List<SMTPointInfo> checkOKList = board.getCalPoint();

                if (board == null || checkOKList == null || checkOKList.Count < 2 || board.PCBCount == 1)
                {
                    return result;
                }

                if (board.pcbRow <= 1 && board.pcbCol <= 1)
                {
                    return result;
                }

                int pcbRow = targetRow > 0 ? targetRow : board.pcbRow;
                int pcbCol = targetCol > 0 ? targetCol : board.pcbCol;
                int pcbColValue = targetColValue > 0 ? targetColValue : board.pcbColValue;
                int pcbRowValue = targetRowValue > 0 ? targetRowValue : board.pcbRowValue;
                for (int currR = 1; currR <= pcbRow; currR++)
                {
                    for (int currC = 1; currC <= pcbCol; currC++)
                    {
                        int index = (currR - 1) * pcbCol + currC;
                        if (index == 1) continue; // PCB1 为基准


                        // 默认按均匀间距计算
                        int dx = (currC - 1) * pcbColValue;
                        int dy = (currR - 1) * pcbRowValue;
                        // 若设置了某个PCB相对PCB1的偏移,则覆盖默认值
                        try
                        {
                            if (board.mPCbOffsetMap != null && board.mPCbOffsetMap.ContainsKey(index))
                            {
                                var pOffset = board.mPCbOffsetMap[index];
                                dx = pOffset.X;
                                dy = pOffset.Y;
                            }
                        }
                        catch { }


                        Point leftPoint = new Point(dx, dy);
                        Point rightPoint = new Point(dx + board.boardWidth, dy + board.boardLength);

                        if (checkOKList != null)
                        {
                            Point leftNodePoint = XYConvertManager.CalPCBAreaNodePosition(leftPoint, checkOKList, 1);
                            Point rightNodePoint = XYConvertManager.CalPCBAreaNodePosition(rightPoint, checkOKList, 2);

                            result.Add(new List<Point>() { leftNodePoint, rightNodePoint });
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogUtil.error("获取" + board.boardName + "拷贝点出错:" + ex.ToString());
            }
            finally
            {

            }
            return result;
        }

        /// <summary>
        /// 多板拷贝点
        /// </summary>
        /// <param name="board"></param>
        /// <param name="sMTPoint"></param>
        /// <param name="targetRow"></param>
        /// <param name="targetCol"></param>
        /// <param name="targetRowValue"></param>
        /// <param name="targetColValue"></param>
        /// <returns></returns>
        public static List<SMTPointInfo> GetCopyPointCR(BoardInfo board, SMTPointInfo sMTPoint = null,int targetRow=0,int targetCol=0,int targetRowValue=0,int targetColValue=0)
        {
            List<SMTPointInfo> pointList = new List<SMTPointInfo>();
            try
            {

                List<SMTPointInfo> basePoints = new List<SMTPointInfo>();
                if (sMTPoint == null)
                {
                    if (board != null)
                    {

                        basePoints = new List<SMTPointInfo>(board.GetSmtList());
                    }
                    else
                    {

                        return pointList;
                    }
                }
                else
                {
                    basePoints.Add(sMTPoint);
                }
                List<SMTPointInfo> checkOKList = null;
                try
                {
                    if (board.calInfo != null)
                    {
                        checkOKList = board.getCalPoint();
                    }
                }
                catch (Exception ex)
                {
                    LogUtil.error("获取" + board.boardName + "校准点出错:" + ex.ToString());
                }
                if (board == null || checkOKList == null || checkOKList.Count < 2)
                {
                    return pointList;
                }
                pointList.AddRange(basePoints);
                int baseCount = basePoints.Count;
                string msg = "";

                if (board.pcbRow <= 1 && board.pcbCol <= 1)
                {
                    return pointList;
                }
                int pcbRow =targetRow>0?targetRow: board.pcbRow; 
                int pcbCol = targetCol > 0?targetCol: board.pcbCol; 
                int pcbColValue = targetColValue > 0?targetColValue: board.pcbColValue; 
                int pcbRowValue = targetRowValue > 0 ? targetRowValue: board.pcbRowValue;
          
                for (int currR = 1; currR <= pcbRow; currR++)
                {
                    for (int currC = 1; currC <= pcbCol; currC++)
                    {
                        int index = (currR - 1) * pcbCol + currC;
                        if (index == 1) continue; // PCB1 为基准

                        foreach (SMTPointInfo point in basePoints)
                        {
                            SMTPointInfo newPoint = new SMTPointInfo(point);
                            newPoint.pointNum = baseCount + point.pointNum;
                            // 默认按均匀间距计算
                            int dx = (currC - 1) * pcbColValue;
                            int dy = (currR - 1) * pcbRowValue;
                            // 若设置了某个PCB相对PCB1的偏移,则覆盖默认值
                            try
                            {
                                if (board.mPCbOffsetMap != null && board.mPCbOffsetMap.ContainsKey(index))
                                {
                                    var pOffset = board.mPCbOffsetMap[index];
                                    dx = pOffset.X;
                                    dy = pOffset.Y;
                                }
                            }
                            catch { }
                            newPoint.PositionX = newPoint.PositionX + dx;
                            newPoint.PositionY = newPoint.PositionY + dy;
                            newPoint.Group = index;
                            newPoint.TagNo = point.TagNo + "-" + index;
                            newPoint.PointSizeX = point.PointSizeX;
                            newPoint.CheckOK = false;

                            if (checkOKList != null)
                            {
                                try { newPoint = XYConvertManager.CalPointNodePosition(newPoint, checkOKList); } catch { }
                            }
                            msg += "\r\n" + point.pointName + "_" + point.PN + $",更改组={index} , X={newPoint.PositionX},Y={newPoint.PositionY},投影X={newPoint.NodePositionX},投影Y={newPoint.NodePositionY}";
                            pointList.Add(newPoint);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogUtil.error("获取" + board.boardName + "拷贝点出错:" + ex.ToString());
            }
            finally
            {

            }
            return pointList;
        }
    }

    public class ConBaseInfo
    {
        public ConBaseInfo()
        {

        }
        public ConBaseInfo(Point point1,Point point2)
        {
            this.IPoint = point1;
            this.Point = point2;
        }
        /// <summary>
        /// 板子实际坐标
        /// </summary>
        public Point IPoint = new Point();
        /// <summary>
        /// 投影对应坐标
        /// </summary>
        public Point Point = new Point(); 
    }
}