BoardInfo.cs 22.6 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 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693
using TSA_V.Common;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using TSA_V.LoadCSVLibrary;
using System.Reflection;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace TSA_V.DeviceLibrary
{
    public class BoardInfo
    {
        public string boardShowName
        {
            get
            {

                if (String.IsNullOrEmpty(boardCode))
                {
                    return boardName;
                }
                return boardName + "(" + boardCode + ")";
            }
            set { }
        }

        /// <summary>
        /// 电路板ID
        /// </summary>
        public int boardId { get; set; }
        /// <summary>
        /// 元器件库名称
        /// </summary>
        public string bomName { get; set; }
        /// <summary>
        /// 电路板名称
        /// </summary>
        public string boardName { get; set; }
        /// <summary>
        /// 电路板条形码
        /// </summary>
        public string boardCode { get; set; }
        /// <summary>
        /// 宽度(显示的长度→)
        /// </summary>
        public int boardWidth { get; set; }
        /// <summary>
        /// 高度(显示的高度↑)
        /// </summary>
        public int boardLength { get; set; }
        /// <summary>
        /// 元器件列表
        /// </summary>
        public List<SMTPointInfo> smtList { get; set; }

        // 新增:多PCB模式配置
        /// <summary>
        /// 多PCB模式-行数
        /// </summary>
        public int pcbRow { get; set; } = 1;
        /// <summary>
        /// 多PCB模式-列数
        /// </summary>
        public int pcbCol { get; set; } = 1;
        /// <summary>
        /// 多PCB模式-行间距(mm)
        /// </summary>
        public double pcbRowValue { get; set; } = 1;
        /// <summary>
        /// 多PCB模式-列间距(mm)
        /// </summary>
        public double pcbColValue { get; set; } = 1;
        /// <summary>
        /// 多点位投射功能,=true所有点位同时显示。
        /// </summary>
        public bool mPointsProjection { get; set; } = false;
        /// <summary>
        /// 多pcb模式,每个PCB针对PCB1的偏移坐标。key=pcb索引号,value    =偏移坐标
        /// </summary>
        public Dictionary<int, PcbOffsetInfo> mPCbOffsetMap = new Dictionary<int, PcbOffsetInfo>();

        public PcbOffsetInfo GetPCBOffsetPoint(int pcbIndex)
        {
            if (mPCbOffsetMap.ContainsKey(pcbIndex))
            {
                return mPCbOffsetMap[pcbIndex];
            }
            else
            {
                return new PcbOffsetInfo(0, 0,0);
            }
        }

        public void SetPCBOffset(int pcbIndex, PcbOffsetInfo value)
        {
            mPCbOffsetMap[pcbIndex] = value;
            LogUtil.info("boardName=" + boardName + ",  SetPCBOffset: pcbIndex=" + pcbIndex + ", value=" + value.ToString());
        }

        public int  PointColor = Color.White.ToArgb();

        public List<SMTPointInfo> GetSmtList(string pn="")
        {
            if (String.IsNullOrEmpty(pn))
            { 
                List<SMTPointInfo> needWorkSmtList = (from m in smtList where m.Disable.Equals(false) select m).ToList<SMTPointInfo>();
                return needWorkSmtList;
            }
            else
            { 
                List<SMTPointInfo> needWorkSmtList = (from m in smtList where m.Disable.Equals(false) &&m.PN.Equals(pn) select m).ToList<SMTPointInfo>();
                return needWorkSmtList;
            }
        }
      
        /// <summary>
        /// 基准点坐标X
        /// </summary>
        public double originX = 800;
        /// <summary>
        /// 基准点坐标Y
        /// </summary>
        public double originY = 800;
        /// <summary>
        /// 图片名称
        /// </summary>
        public string imgName { get; set; }

        /// <summary>
        /// 固定原点的位置,1=左上角,2=左下角,3=右上角,4=右下角
        /// </summary>
        public int orgType { get; set; }

        /// <summary>
        /// 线体宽度
        /// </summary>
        public int LineWidth { get; set; }

        //public double BaseX = 0;
        //public double BaseY = 0;
        //public double BaseImageX = 0;
        //public double BaseImageY = 0;

        public string AOIProName = "";

        public Image myImage = null;

        public List<ComponetInfo> componetList { get; set; } = null;

        public string GetImgPath(bool isCheck = false)
        {
            string path = ConfigAppSettings.GetValue(Setting_Init.BOARD_IMAGE_PATH);
            string imagePath = Application.StartupPath + "/" + path + "/" + imgName;
            if (isCheck)
            {
                if (imgName != null && File.Exists(imagePath))
                { 
                    return imagePath;
                }
                else
                {
                    imagePath = Application.StartupPath + @"/" + path + ConfigAppSettings.GetValue(Setting_Init.BOARD_IMAGE_PATH);
                }
            }
            return imagePath;
        }
        public Image GetImage()
        {
            if (myImage == null)
            { 
                string imagePath = GetImgPath(); 
                if (imgName != null && File.Exists(imagePath))
                {
                    System.Drawing.Image img = System.Drawing.Image.FromFile(imagePath); 
                    myImage = new System.Drawing.Bitmap(img); 
                    img.Dispose();
                }
                else
                {
                    return BoardManager.defaultImage;
                }
            }
            if (myImage == null)
            {
                return BoardManager.defaultImage;
            }
            else
            {
                return myImage;
            }

        }
        public string GetDes()
        {
            //Length: 175mm
            //Width:190mm
            //Component Qty:
            return " " + ResourceControl.GetString(ResourceControl.Length, "长度") + ":" + boardLength +"mm"+
                "\r\n" + ResourceControl.GetString(ResourceControl.Width, "宽度") + ":" + boardWidth +"mm"+
                 //"," + ResourceControl.GetString(ResourceControl.LineWidth, "线体宽度") + ":" + LineWidth +
                "\r\n" + ResourceControl.GetString(ResourceControl.InfoCount, "组件数量") +":" + GetSmtList().Count;
            //return "电路板名称:" + boardName + ",长度:" + boardLength + ",宽度:" + boardWidth + ",组装信息数量:" + pointList.Count;
        }
         
        public string GetAoiFileName()
        {
            string path = Application.StartupPath + ConfigAppSettings.GetValue(Setting_Init.AOIFileConfig)+AOIProName;
            return path;
        }

        public int GetNextPNum()
        {
            int maxNum = 0;
            foreach(SMTPointInfo p in smtList)
            {
                if (p.pointNum > maxNum)
                {
                    maxNum = p.pointNum;
                }
            }
            maxNum++;
            return maxNum;
        }
        /// <summary>
        /// 导出导入程序时使用
        /// </summary>
        public byte[] imageByte { get; set; }

        public string imageBase64Data { get; set; }

         public bool IsValid()
        {
            if (String.IsNullOrEmpty(boardName))
            {
                return false;
            }
            if (this.smtList==null)
            {
                return false;
            }
            if (this.smtList.Count <= 0)
            {
                return false;
            }
            if (this.LineWidth <= 0 && this.boardLength <= 0)
            {
                return false;
            }
            return true;
        }

        public List<SMTPointInfo> getCalPoint()
        {
            try
            {
                SMTPointInfo lefup = new SMTPointInfo();
                lefup.PositionX = 0;
                lefup.PositionY = 0;
                lefup.NodePositionX = calInfo.leftUpPoint.X;
                lefup.NodePositionY = calInfo.leftUpPoint.Y;

                SMTPointInfo right = new SMTPointInfo();
                right.PositionX = boardWidth;
                right.PositionY = boardLength;
                right.NodePositionX = calInfo.rightBottomPoint.X;
                right.NodePositionY = calInfo.rightBottomPoint.Y;
                List<SMTPointInfo> checkOKList = new List<SMTPointInfo>() { lefup, right };


                return checkOKList;
            }
            catch (Exception ex)
            {
                LogUtil.error(boardName + "获取校准点列表出错:" + ex.ToString());
            }
            return new List<SMTPointInfo>();
        }

        public CalibrateBean calInfo = null;

        public int PCBCount
        {
            get
            {
                int count = this.pcbRow * pcbCol;
                if (count <= 0) { count = 1; }
                return count;
            }
        }
    }

    public class CalibrateBean
    {
        public Point leftUpPoint = new Point();

        public Point rightBottomPoint = new Point();

        /// <summary>
        /// 1=已确认左上角位置
        /// 2=已确认右下角位置
        /// 3=正在校准中
        /// </summary>
        public int CurrStep = 0;
    }
    /// <summary>
    ///组装信息信息
    /// </summary>
    public class SMTPointInfo
    {
        public SMTPointInfo()
        {
            PositionNum = "";
            pointNum = 0;
            PN = "";
            PositionX = 0;
            PositionY = 0;
        }
        public SMTPointInfo(int num, string partNum, string name, double positionX, double positionY, string PositionNum,string showText,
            int pointType=1,int pointSizex=1,int pointSizeY=1,int penWidth =2, int PolaritiesType=0,
            bool needs = false, bool needc = false, int sTemp = 0, double sTime = 0)
        {
            this.TagNo = partNum;
            this.pointNum = num;
            this.PN = name;
            this.PositionX = positionX;
            this.PositionY = positionY;
            this.PositionNum = PositionNum;
            this.NeedCheck = needc;
            this.NeedSoldering = needs;
            this.WeldTemp = sTemp;
            this.WeldTime = sTime;
            this.PointType = pointType;
            this.PointSizeX = pointSizex;
            this.PointSizeY = pointSizeY;
            this.PenWidth = penWidth;
            this.ShowText = showText;
            this.PolaritiesType = PolaritiesType;
            this.imgP=new DrawPointInfo(pointSizex, pointSizeY,penWidth);
            //if (String.IsNullOrEmpty(this.ShowText))
            //{
            //    this.ShowText = name;
            //}
        }
        public SMTPointInfo(SMTPointInfo oldPointInfo)
        { 
            // 使用反射获取属性并复制值
            PropertyInfo[] properties = typeof(SMTPointInfo).GetProperties();
            foreach (PropertyInfo property in properties)
            {
                if (property.CanWrite)
                {
                    property.SetValue(this, property.GetValue(oldPointInfo));
                }
            }  
        }
        /// <summary>
        ///组装信息编号
        /// </summary>
        public int pointNum { get; set; }
        /// <summary>
        /// 位号 原来的PartNum
        /// </summary>
        public string TagNo { get; set; }
        /// <summary>
        /// 物料编码 原来的pointName
        /// </summary>
        public string PN { get; set; }



        /// <summary>
        ///已过时。 位号/编号,与元器件库中的编号一致,不可以修改,作为关联关系
        /// </summary> 
        public string PartNum { get; set; }

        /// <summary>
        ///已过时。物料编码/组装信息名字
        /// </summary>
        public string pointName { get; set; }

        /// <summary>
        /// 料格的位置信息
        /// </summary>
        public string PositionNum { get; set; }
        /// <summary>
        /// 图片上显示的x坐标
        /// </summary>
        public double PositionX { get; set; }
        /// <summary>
        ///图片上显示的 y坐标
        /// </summary>
        public double PositionY { get; set; }


        private double nodePositionX;
        private double nodePositionY;
        /// <summary>
        /// X轴的移动坐标
        /// </summary>
        public double NodePositionX
        {
            get { return nodePositionX; }
            set
            {
                nodePositionX = Math.Round(value, 2);
            }
        }
        /// <summary>
        /// Y轴的移动坐标
        /// </summary>
        public double NodePositionY
        {
            get { return nodePositionY; }
            set
            {
                nodePositionY = Math.Round(value, 2);
            }
        }

        /// <summary>
        /// 是否需要焊接
        /// </summary>
        public bool NeedSoldering { get; set; } = false;
         
        /// <summary>
        /// 焊接温度
        /// </summary>
        public int WeldTemp { get; set; } = 0;
        /// <summary>
        /// 焊接时间,秒,可以小数
        /// </summary>
        public double WeldTime { get; set; } = 0;
        /// <summary>
        /// 是否需要检测
        /// </summary>
        public bool NeedCheck { get; set; } = false;

        /// <summary>
        /// 是否已经校准
        /// </summary>
        public bool CheckOK = false;

        /// <summary>
        /// 复制点位时自动设置组
        /// </summary>
        public int Group { get; set; } = 0;

        public long pUTime { get; set; } = 0;
        public string  ToCSVStr()
        {
            string spilt = ",";
            return  PN + spilt + "1" + spilt + "2" + spilt + PositionX + spilt + PositionY + spilt + "CRD" + spilt + NodePositionX + spilt + NodePositionY;
        }

        public string GetDisplayStr()
        {
            string text = PN;
            if (Setting_NInit.Work_PointDisplayType.Equals(1))
            {
                text = TagNo;
            }
            return text;
        }

        public void UpdateImgP(ProjectorPInfo p)
        {
            PositionX = p.PX;
            PositionY = p.PY;
            PointType = p.PType;
            PointSizeX = p.SizeX;
            PointSizeY=p.SizeY;
            PenWidth = p.PenWidth;
            this.ShowText = p.ShowText;
            this.PolaritiesType = p.PolaritiesType;
            this.PolarityAngle= p.PolarityAngle;
        }

        public ProjectorPInfo GetShowPInfo(float imageXiShu=1,float imageYShu=1)
        {
            ProjectorPInfo p = new ProjectorPInfo(PositionX*imageXiShu, PositionY * imageYShu, PointType, PolaritiesType,PolarityAngle, (int)(PointSizeX * imageXiShu), (int)(PointSizeY * imageYShu), (int)(PenWidth * imageXiShu), ShowText);
            return p;
        }

        public ProjectorPInfo GetScreenShowPInfo(float imageXiShu = 1, float imageYShu=1)
        {
            ProjectorPInfo p = new ProjectorPInfo(NodePositionX * imageXiShu, NodePositionY * imageYShu, PointType, PolaritiesType, PolarityAngle, (int)(PointSizeX * imageXiShu), (int)(PointSizeY * imageYShu), (int)(PenWidth * imageXiShu), ShowText);
            return p;
        }

        /// <summary>
        /// 点大小
        /// </summary>
        public int PointSizeX { get; set; } = 1;
        public int PointSizeY { get; set; } = 1;
        public int PenWidth { get; set; } = 2;
        /// <summary>
        /// 图片上的点的信息
        /// </summary>
        public DrawPointInfo imgP { get; set; } = null;

        /// <summary>
        /// 点类型,1=点,2=+,3=|,3=-,4=方形,5=圆圈
        /// </summary>
        public int PointType { get; set; } = 1;
        public string ShowText = "";
        /// <summary>
        /// 是否禁用,默认0
        /// </summary>
        public bool Disable  = false;

        /// <summary>
        /// 是否编辑成功
        /// </summary>
        public bool IsEdit  =false;

        /// <summary>
        /// 极性,
        /// </summary>
        public int PolaritiesType { get; set; } = 0;
        /// <summary>
        /// 极性角度,
        /// </summary>
        public int PolarityAngle { get; set; } = 0;

        /// <summary>
        /// 导出导入程序时使用
        /// </summary>
        public ComponetInfo componet;
      
    }
    public class DrawPointInfo
    {
        public DrawPointInfo()
        {

        }
        public DrawPointInfo(int sizeX, int sizeY, int pWidth )
        {
            SizeX = sizeX;
            SizeY = sizeY;
            PWidth = pWidth;
            this.uTime = DateTime.Now.Ticks;
        }

        public int SizeX = 5;
        public int SizeY = 5;
        public int PWidth = 1;
        /// <summary>
        /// 最后一次更新时间
        /// </summary>
        public long uTime;
    }

    [JsonConverter(typeof(PcbOffsetInfoConverter))]
    public class PcbOffsetInfo
    {

        public PcbOffsetInfo() { }
        public PcbOffsetInfo(double x, double y, double a)
        {
                this.X = x;
                this.Y = y;
                this.A = a;
        }

        public double X { get; set; }

        public double Y { get; set; }

        public double A { get; set; }

        public string getShowText(int index)
        {
            return $"PCB{index}(X={Math.Round(X,1)},Y={Math.Round(Y,1)},A={Math.Round(A, 1)})";
        }
    }
    public class PcbOffsetInfoConverter : JsonConverter<PcbOffsetInfo>
    {
        // 序列化:将 A 保留一位小数输出
        public override void WriteJson(JsonWriter writer, PcbOffsetInfo value, JsonSerializer serializer)
        {
            if (value == null)
            {
                writer.WriteNull();
                return;
            }
            writer.WriteStartObject();
            writer.WritePropertyName("X");
            writer.WriteValue(value.X);
            writer.WritePropertyName("Y");
            writer.WriteValue(value.Y);
            writer.WritePropertyName("A");
            // 关键:保留一位小数(四舍五入)
            writer.WriteValue(Math.Round(value.A, 1));
            writer.WriteEndObject();
        }

        // 反序列化:适配不同格式的 A(支持 int/string/float 转 double)
        public override PcbOffsetInfo ReadJson(JsonReader reader, Type objectType, PcbOffsetInfo existingValue, bool hasExistingValue, JsonSerializer serializer)
        {
            if (reader.TokenType == JsonToken.Null)
            {
                return null;
            }

            try
            {
                switch (reader.TokenType)
                {
                    // 1. 处理字符串格式(如 "x, y, a")
                    case JsonToken.String:
                        {
                            string s = (reader.Value as string) ?? string.Empty;
                            var parts = s.Split(new[] { ',', ';', ' ' }, StringSplitOptions.RemoveEmptyEntries)
                                         .Select(p => p.Trim()).ToArray();
                            // X/Y 仍按 int 解析
                            int x = parts.Length > 0 && int.TryParse(parts[0], out var xi) ? xi : 0;
                            int y = parts.Length > 1 && int.TryParse(parts[1], out var yi) ? yi : 0;
                            // 关键:A 改为 double 解析,保留一位小数
                            double a = parts.Length > 2 && double.TryParse(parts[2], out var ai)
                                       ? Math.Round(ai, 1)
                                       : 0.0;
                            return new PcbOffsetInfo(x, y, a);
                        }

                    // 2. 处理数组格式(如 [x, y, a])
                    case JsonToken.StartArray:
                        {
                            var arr = JArray.Load(reader);
                            double x = arr.Count > 0 ? SafeDouble(arr[0]) : 0.0;
                            double y = arr.Count > 1 ? SafeDouble(arr[1]) : 0.0;
                            // 关键:A 用 SafeDouble 解析,保留一位小数
                            double a = arr.Count > 2 ? SafeDouble(arr[2]) : 0.0;
                            return new PcbOffsetInfo(x, y, Math.Round(a, 1));
                        }

                    // 3. 处理对象格式(如 {X:x, Y:y, A:a})
                    case JsonToken.StartObject:
                        {
                            var obj = JObject.Load(reader);
                            double x = SafeDouble(obj["X"] ?? obj["x"]);
                            double y = SafeDouble(obj["Y"] ?? obj["y"]);
                            // 关键:A 解析支持大小写键,保留一位小数
                            double a = SafeDouble(obj["A"] ?? obj["a"]);
                            return new PcbOffsetInfo(x, y, Math.Round(a, 1));
                        }

                    // 4. 处理单值格式(极少场景,视为 X,Y=0,A=0.0)
                    case JsonToken.Integer:
                    case JsonToken.Float:
                        {
                            int x = Convert.ToInt32(reader.Value);
                            return new PcbOffsetInfo(x, 0, 0.0);
                        }

                    default:
                        return new PcbOffsetInfo(0, 0, 0.0);
                }
            }
            catch
            {
                // 解析失败兜底:A 设为 0.0
                return new PcbOffsetInfo(0, 0, 0.0);
            }
        }

        // 原有:安全解析 int(不变)
        //private static int SafeInt(JToken t)
        //{
        //    if (t == null) return 0;
        //    if (t.Type == JTokenType.Integer) return (int)t;
        //    if (t.Type == JTokenType.Float) return (int)Math.Round((double)t);
        //    if (t.Type == JTokenType.String && int.TryParse((string)t, out var v)) return v;
        //    return 0;
        //}

        // 新增:安全解析 double(适配 A 的 double 类型)
        private static double SafeDouble(JToken t)
        {
            if (t == null) return 0.0;
            if (t.Type == JTokenType.Integer) return (double)t; // int 转 double(如 5 → 5.0)
            if (t.Type == JTokenType.Float) return (double)t;   // 直接取 float/double 值(如 3.14 → 3.14)
            if (t.Type == JTokenType.String && double.TryParse((string)t, out var v)) return v; // 字符串转 double(如 "2.5" → 2.5)
            return 0.0;
        }
    }
}