UsrKeywordlabeling.cs 16.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
using Asa.FaceControl;
using BLL;
using DeviceLibrary.AutoScanAndLabel;
using DocumentFormat.OpenXml.Drawing;
using DocumentFormat.OpenXml.EMMA;
using DocumentFormat.OpenXml.Wordprocessing;
using log4net.Repository.Hierarchy;
using MemoryRead;
using Model;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;
using Point = System.Drawing.Point;

namespace SmartScan.PlusSettingFrm
{
    public partial class UsrKeywordlabeling : UserControl, ISetMenu
    {
        static List<keylabel> labels = new List<keylabel>();
        string flie = Application.StartupPath + "\\Config\\LabelingData.txt";
        public UsrKeywordlabeling()
        {
            InitializeComponent();
            InitializedData();
            Language.SetLanguage(this);

            
            ClockwiseAngle.Items.Add(0.ToString());
            ClockwiseAngle.Items.Add(83.ToString());
            ClockwiseAngle.Items.Add(90.ToString());
            ClockwiseAngle.Items.Add(180.ToString());
            ClockwiseAngle.Items.Add(270.ToString());
            ClockwiseAngle.Items.Add(340.ToString());

            LabelAngle.Items.Add(0.ToString());
            LabelAngle.Items.Add(90.ToString());
            LabelAngle.Items.Add(180.ToString());
            LabelAngle.Items.Add(270.ToString());
            LabelAngle.Items.Add(340.ToString());
        }

        public FacePanel GetPanel()
        {
            tableLayoutPanel1.Width = facePanel1.Width - 10;
            tableLayoutPanel1.Height = facePanel1.Height - 10;
            return facePanel1;
        }

        public void InitializedData()
        {
            if (!File.Exists(flie))
            {
                File.Create(flie).Close();
                int[] ints = { 7, 13, 15 };
                foreach (var item in ints)
                {
                    keylabel label = new keylabel();
                    label.Primarykey = true;
                    label.PlateW = item;
                    label.key = $"通用-{item}寸(Common-{item} inches)";
                    label.value = $"通用-{item}寸(Common-{item} inches)";
                    label.ClockwiseAngle = 0;
                    label.X = 0;
                    label.Y = 0;
                    label.LabelAngle = 0;
                    labels.Add(label);
                }                                           
                string json = JsonConvert.SerializeObject(labels);
                File.WriteAllText(flie, json);
            }
            else
            {
                string fileContent = File.ReadAllText(flie);
                labels = JsonConvert.DeserializeObject<List<keylabel>>(fileContent);
            }
            ListData.Items.AddRange(labels.Select(a=>a.value).ToArray());
        }

        public void Save()
        {
            try
            {
                string json = JsonConvert.SerializeObject(labels);
                if (!File.Exists(flie))
                {
                    File.Create(flie).Close();
                }
                File.WriteAllText(flie, json);
                ListData.Items.Clear();
                ListData.Items.AddRange(labels.Select(a => a.value).ToArray());
                var data = labels.Where(a => a.Primarykey == false).FirstOrDefault();
                if (data!=null)
                {
                    ConfigHelper.Config.Set("Label_Key", data.key);
                }
               
            }
            catch (Exception ex)
            {
                LogUtil.error($"保存关键字贴标数据出错{ex.Message}");
            }
            
        }

        private void AddLabel_Click(object sender, EventArgs e)
        {
            try
            {
                string key = ConfigHelper.Config.Get("Label_Key", "");
                if (string.IsNullOrWhiteSpace(key))
                {
                    FrmKeyCollection frmKey = new FrmKeyCollection();
                    frmKey.ShowDialog();
                    if (frmKey.DialogResult == DialogResult.OK)
                    {
                        string name = frmKey.keyname;
                        ConfigHelper.Config.Set("Label_Key", name);
                        keyname.Text = name;
                    }
                }
                else
                {
                    keyname.Text = key;
                    Keyvalue.Text = null;
                    ClockwiseAngle.Text = null;
                    Xaxisoffset.Text = null;
                    Yaxisoffset.Text = null;
                    LabelAngle.Text = null;
                }
                keyname.Visible = true;
                Keyvalue.Visible = true;    
            }
            catch (Exception ex)
            {
                LogUtil.error($"添加时出错{ex.Message}");
            }         
        }
        string name = null;
        private void ListData_SelectedIndexChanged(object sender, EventArgs e)
        {
            Keyvalue.Text = null;
            keyname.Text = null;
            ClockwiseAngle.Text = null;
            Xaxisoffset.Text = null;
            Yaxisoffset.Text = null;
            LabelAngle.Text = null;
            int i = ListData.SelectedIndex;
            if (i<0||labels.Count<1)
            {
                return;
            }
            var model = labels[i];
            if (model == null) 
            {
                return;
            }
            else
            {
                if (model.Primarykey)
                {
                    keyname.Visible = false;
                    Keyvalue.Visible = false;
                }
                else
                {
                    keyname.Visible = true;
                    Keyvalue.Visible = true;
                }
                lab_PlateWs.Text = model.PlateW.ToString();
                name = model.value;
                Keyvalue.Text = model.value;
                keyname.Text = model.key;
                ClockwiseAngle.Text = model.ClockwiseAngle.ToString();
                Xaxisoffset.Text = model.X.ToString();
                Yaxisoffset.Text = model.Y.ToString();
                LabelAngle.Text = model.LabelAngle.ToString();
            }
        }
        private void UpdateLabel_Click(object sender, EventArgs e)
        {
            FrmKeyCollection frmKey = new FrmKeyCollection();
            frmKey.ShowDialog();
            if (frmKey.DialogResult == DialogResult.OK)
            {
                string name = frmKey.keyname;
                ConfigHelper.Config.Set("Label_Key", name);
                if (keyname.Text!= Language.Dialog("Res001"))
                {
                    keyname.Text = name;
                }
                
                var list = labels.Where(a => a.Primarykey==false);
                foreach (var item in list)
                {
                    item.key = name;
                }
            }
        }
        private void SaveLabel_Click(object sender, EventArgs e)
        {
            //string a = " True;匹配成功条码xy:X=1498;Y=1934,角度=120";
            //string names = "579";
            //AMatch aMatch = new AMatch();
            //aMatch.Angle = 120;
            //aMatch.Points = new Point(1498, 1934);
            //Point cenl = new Point(2000, 1800);
            //LabelingAngle(names, aMatch, cenl, true, 15, out int angle);
            ////var i = labels.RemoveAll(A => A.value == Keyvalue.Text);           
            if (string.IsNullOrWhiteSpace(keyname.Text)||string.IsNullOrWhiteSpace(Keyvalue.Text))
            {
                return;
            }

            bool Primarykey = false;
            if (keyname.Text.Contains("通用"))
            {
                Primarykey = true;
            }
            var acd= ClockwiseAngle.Text;
            labels.RemoveAll(A => A.value == Keyvalue.Text);
            keylabel model = new keylabel();
            int.TryParse(lab_PlateWs.Text, out int PlateW);
            model.PlateW = PlateW;
            model.Primarykey = Primarykey;
            model.value = Keyvalue.Text;
            model.key = keyname.Text;
            int.TryParse(ClockwiseAngle.Text, out int Cloc);
            model.ClockwiseAngle = Cloc;
            int.TryParse(Xaxisoffset.Text, out int X);
            model.X = X;
            int.TryParse(Yaxisoffset.Text, out int Y);
            model.Y = Y;
            int.TryParse(LabelAngle.Text, out int CLABANGloc);
            model.LabelAngle = CLABANGloc;
            labels.Add(model);
            Save();
        }

        public static void Updatekeyname(string name)
        {
           string keybname= ConfigHelper.Config.Get("Label_Key");
           var list= labels.Where(a => a.Primarykey == false).ToList();
            foreach (var item in list) 
            {
                item.key = name;
            }            
        }

        public static void Deletskeyname()
        {
            var list = labels.Where(a => a.Primarykey == false).ToList();
            foreach (var item in list)
            {
                item.key = "";
            }
        }
        public static Point LabelingAngle(string keyvalue, AMatch aMatch, System.Drawing.Point centrality, bool isvaor, int PlateW, out int angle,string codeType)
        {


            Point lable = aMatch.Points;
            Point newpoint = aMatch.Points;
            var Data = labels.FirstOrDefault(a => a.value == keyvalue);           
            int RotateAngle = 0;
            //int X = 0, Y = 0;
            if (Data==null&&labels.Count>0)
            {
                Data = labels.FirstOrDefault(a=>a.Primarykey&&a.PlateW==PlateW);
            }
            if (Data!=null)
            {
                RotateAngle = Data.ClockwiseAngle;
                //X += Data.X; Y += Data.Y;                
            }
            LogUtil.info($"关键字:{Data.key}={Data.value}; 标签随盘心角度={RotateAngle}; xy偏移{Data.X}; {Data.Y}; 标签本体旋转角度:{Data.LabelAngle}");
            newpoint = Overwritelabel(centrality, aMatch.Points, isvaor, PlateW, aMatch.Angle, RotateAngle, Data.LabelAngle, out angle, codeType);
            //if (Data != null&&Data.LabelAngle > 0)
            //{            
            //    angle = Data.LabelAngle;
            //}
            if (Data.X != 0) {
                newpoint= MaterialEdit.PointWithAngle(newpoint, angle - 90, Data.X);
            }
            if (Data.Y != 0)
            {
                newpoint = MaterialEdit.PointWithAngle(newpoint, angle, Data.Y);
            }
            LogUtil.info($"处理后角度={angle};处理后贴标位置:{newpoint};");
            return new Point(newpoint.X, newpoint.Y);
        }
        //
        //    270° (上)
        //      ↑
        //180° ←┼→ 0° (右)
        //      ↓
        //     90° (下)
        public static Point Overwritelabel(Point centrality, Point lable, bool isvaor, int PlateW, int Angle,int RotateAngle,int labelangle, out int angle,string codeType)
        {


            if (isvaor)
            {
                bool isbarcode = true;
                if (codeType.StartsWith("QR Code"))
                {
                    int deg = Angle;
                    deg += 90;
                    if (deg > 360)
                        deg = deg - 360;
                    angle = ScanningCameraAngle(deg,0);
                    isbarcode = false;
                    LogUtil.info($"QR Code转换后角度,Angle:{Angle}, deg:{angle}");
                    Angle = angle;
                }
                else if (codeType.StartsWith("Data Matrix"))
                {
                    int deg = Angle;
                    angle = ScanningCameraAngle(deg, 0);
                    isbarcode = false;
                    LogUtil.info($"Data Matrix转换后角度,Angle:{Angle}, deg:{angle}");
                    Angle = angle;
                }


                //Angle =  Angle + 270;
                if (isbarcode)
                {
                    var dc1 = LabelingPosition.distance(LabelingPosition.PointWithAngle(lable, Angle, 50), centrality);
                    var dc2 = LabelingPosition.distance(LabelingPosition.PointWithAngle(lable, Angle + 180, 50), centrality);
                    if (dc2 > dc1)
                        Angle = Angle + 180;
                    if (Angle > 360)
                        Angle = Angle - 360;
                    LogUtil.info($"结合盘心计算后, dc1:{dc1}, dc2:{dc2}, deg:{Angle}");
                }
                else
                {
                    LogUtil.info($"二维码转换后角度, deg:{Angle}");
                }


                    angle = Angle + 83 + labelangle;
                if (angle > 360)
                    angle = angle - 360;
                if (angle > 360)
                    angle = angle - 360;
            }
            else
            {
                angle = OldAngleAlgo(centrality, lable, PlateW);
            }

            Point Points = LabelingPosition.ClockwiseRotation(lable, centrality, 83 + RotateAngle);

            LogNet.log.Info($"是否id相机={isvaor};新标签位置:{Points};输入角度:{Angle},设定盘心偏转:{RotateAngle},设定标签旋转:{labelangle},转换后角度:{angle}");
            return Points;
        }
        public static int ScanningCameraAngle(int angles, int RotateAngle)
        {
            //ID MVS相机, 右侧为正角度, 左侧为负角度
            int angle;
            int rotationAngle = RotateAngle;// 83;
            if (angles < 0)
            {
                angle = -1 * angles + rotationAngle;
            }
            else
            {
                angle = 360 - angles + rotationAngle;
            }
            angle -= 90;
            if (angle >= 360)
            {
                angle -= 360;
            }
            else if (angle < 0)
            {
                angle = 360 + angle;
            }
            return angle;
        }

        /// <summary>
        /// 获取以点为中心的角度,-180~180
        /// </summary>
        /// <param name="pt1"></param>
        /// <param name="pt2"></param>
        /// <returns></returns>
        public static double getAngle(Point pt1, Point pt2)
        {
            double mb_x = pt2.X - pt1.X;
            double mb_y = pt2.Y - pt1.Y;
            double rotation = Math.Atan2(mb_y, mb_x);


            rotation = rotation / Math.PI * 180;
            return rotation;
            //double angle = 360 - rotation + 90;

            //angle = angle < 0 ? 360 + angle : angle;
            //angle = angle % 360;
            //return Math.Floor(angle / 360 * 24);
        }

        public static int OldAngleAlgo(Point lable, Point center, int PlateW)
        {
            int labelAngle = 0;
            var a = (int)getAngle(lable, center);
            int angle = a < 0 ? 360 + a : a;
            if (PlateW == 15)
            {
                labelAngle = angle + 120;
            }
            else if (PlateW == 13)
            {
                labelAngle = angle + 180;
            }
            else
            {
                labelAngle = angle + 180;
            }
            labelAngle += 83;
            labelAngle = labelAngle > 360 ? labelAngle - 360 : labelAngle;
            return labelAngle;
            //if (labelAngle <= 180)
            //{
            //    labelAngle = Label_R_Angle_Diff - (180 - labelAngle);
            //}
            //else
            //{
            //    labelAngle = (labelAngle - 180) + Label_R_Angle_Diff;
            //}
        }
    }
    public class keylabel
    {
        public bool Primarykey { get; set; } = false;
        /// <summary>
        /// 料盘尺寸
        /// </summary>
        public int PlateW { get; set; }
        /// <summary>
        /// 关键字
        /// </summary>
        public string key { get; set; }
        /// <summary>
        /// 关键字内容
        /// </summary>
        public string value { get; set; }
        /// <summary>
        /// 顺特征码旋转多少度
        /// </summary>
        public int ClockwiseAngle { get; set; } = 0;
        /// <summary>
        /// x轴偏移量
        /// </summary>
        public int X { get; set; } = 0;
        /// <summary>
        /// y轴偏移量
        /// </summary>
        public int Y { get; set; } = 0;
        /// <summary>
        /// 标签旋转角度
        /// </summary>
        public int LabelAngle { get; set; } = 0;
    }
}