// 載入 Grade 的 xml 內容到全域變數
        internal static void LoadGradeConfig()
        {
            //Debug
            DebugTool.WriteLog("SystemVariable.cs", "LoadGradeConfig()");

            XDocument XGrade = GetGradeConfXDoc();
            // 載入 Roi 模式
            XElement XRoiMode = XGrade.Element("GradeConfig").Element("Roi").Element("RoiMode");
            XElement XRoiColumns = XGrade.Element("GradeConfig").Element("Roi").Element("RoiColumns");
            XElement XRoiRows = XGrade.Element("GradeConfig").Element("Roi").Element("RoiRows");

            try
            {
                GradeVariable.RoiMode = int.TryParse(XRoiMode.Value, out GradeVariable.RoiMode) ? GradeVariable.RoiMode : 0;
                GradeVariable.RoiGradeColumns = int.TryParse(XRoiColumns.Value, out GradeVariable.RoiGradeColumns) ? GradeVariable.RoiGradeColumns : 0;
                GradeVariable.RoiGradeRows = int.TryParse(XRoiRows.Value, out GradeVariable.RoiGradeRows) ? GradeVariable.RoiGradeRows : 0;
            }
            catch (Exception ex)
            {
                DebugTool.WriteLog("SystemVariable.cs", "*** Load Roi mode Error:" + ex.Message, MapWindowVariable.FlawPieces.Count.ToString(), MapWindowVariable.CurrentPiece.ToString());
            }

            // 載入 Roi 欄位 start, end 座標資料
            try
            {
                IEnumerable<XElement> XColumns = XGrade.Element("GradeConfig").Element("Roi").Elements("Column");
                GradeVariable.RoiColumnsGrid.Clear();
                foreach (XElement c in XColumns)
                {
                    RoiGrade tmpRoiColumn = new RoiGrade();
                    tmpRoiColumn.Name = c.Attribute("Name").Value;
                    tmpRoiColumn.Start = double.Parse(c.Element("Start").Value);
                    tmpRoiColumn.End = double.Parse(c.Element("End").Value);
                    GradeVariable.RoiColumnsGrid.Add(tmpRoiColumn);
                }

                IEnumerable<XElement> XRows = XGrade.Element("GradeConfig").Element("Roi").Elements("Row");
                GradeVariable.RoiRowsGrid.Clear();
                foreach (XElement r in XRows)
                {
                    RoiGrade tmpRoiRow = new RoiGrade();
                    tmpRoiRow.Name = r.Attribute("Name").Value;
                    tmpRoiRow.Start = double.Parse(r.Element("Start").Value);
                    tmpRoiRow.End = double.Parse(r.Element("End").Value);
                    GradeVariable.RoiRowsGrid.Add(tmpRoiRow);
                }
            }
            catch (Exception ex)
            {
                DebugTool.WriteLog("SystemVariable.cs", "*** Load Roi columns Error:" + ex.Message, MapWindowVariable.FlawPieces.Count.ToString(), MapWindowVariable.CurrentPiece.ToString());
            }

            // 載入 Grade setting > Point xml default value
            try
            {
                XElement XPointEnable = XGrade.Element("GradeConfig").Element("Grade").Element("PointGrade").Element("Enable");
                GradeVariable.IsPointEnable = (int.Parse(XPointEnable.Value) == 1) ? true : false;

                IEnumerable<XElement> XPointSubPieces = XGrade.Element("GradeConfig").Element("Grade").Element("PointGrade").Elements("SubPiece");
                GradeVariable.PointSubPieces.Clear();

                // 1. 先加入一筆 ALL 的 SubPiece 指全部使用相同設定
                PointSubPiece allsame = new PointSubPiece();
                allsame.Name = "ALL";
                allsame.Grades = new List<PointGrade>();
                foreach (FlawTypeNameExtend i in PxPVariable.FlawTypeName)
                {
                    PointGrade tmpPG = new PointGrade();
                    tmpPG.ClassId = i.FlawType;
                    tmpPG.ClassName = i.Name;
                    tmpPG.Score = 0;

                    allsame.Grades.Add(tmpPG);
                }
                GradeVariable.PointSubPieces.Add(allsame);

                // 2. 用 Rows 和 Columns 造出所有欄位再去 xml 補資料
                foreach (RoiGrade r in GradeVariable.RoiRowsGrid)
                {
                    foreach (RoiGrade c in GradeVariable.RoiColumnsGrid)
                    {
                        PointSubPiece tmpPointSubPiece = new PointSubPiece();
                        tmpPointSubPiece.Name = "ROI-" + r.Name + c.Name;
                        tmpPointSubPiece.Grades = new List<PointGrade>();
                        foreach (FlawTypeNameExtend i in PxPVariable.FlawTypeName)
                        {
                            PointGrade tmpPG = new PointGrade();
                            tmpPG.ClassId = i.FlawType;
                            tmpPG.ClassName = i.Name;
                            tmpPG.Score = 0;
                            tmpPointSubPiece.Grades.Add(tmpPG);
                        }
                        GradeVariable.PointSubPieces.Add(tmpPointSubPiece);
                    }
                }
                // 3. 補上 XML 有記錄的資料到 GradeVariable.PointSubPieses
                foreach (XElement xsp in XPointSubPieces)
                {
                    foreach (PointSubPiece gsp in GradeVariable.PointSubPieces)
                    {
                        if (gsp.Name == xsp.Attribute("Name").Value)
                        {
                            foreach (PointGrade g in gsp.Grades)
                            {
                                foreach (XElement ft in xsp.Elements("FlawType"))
                                {
                                    if(g.ClassId == int.Parse(ft.Attribute("Id").Value))
                                        g.Score = int.Parse(ft.Value);
                                }
                            }
                        }
                    }

                }

            }
            catch (Exception ex)
            {
                DebugTool.WriteLog("SystemVariable.cs", "*** Set Grade setting > Point xml default value errors:" + ex.Message, MapWindowVariable.FlawPieces.Count.ToString(), MapWindowVariable.CurrentPiece.ToString());
            }

            // 載入 Grade setting > Mark grade xml default
            try
            {
                XElement XMarkGradetEnable = XGrade.Element("GradeConfig").Element("Grade").Element("MarkGrade").Element("Enable");
                GradeVariable.IsMarkGradeEnable = (int.Parse(XMarkGradetEnable.Value) == 1) ? true : false;

                // Set gridview of markgrade value
                IEnumerable<XElement> XMarkGradeSubPieces = XGrade.Element("GradeConfig").Element("Grade").Element("MarkGrade").Elements("SubPiece");
                GradeVariable.MarkGradeSubPieces.Clear();

                // 1. 先加入一筆 ALL 的 SubPiece 指全部使用相同設定
                MarkSubPiece allsame = new MarkSubPiece();
                allsame.Name = "ALL";
                allsame.Grades = new List<MarkGrade>();
                foreach (XElement i in XMarkGradeSubPieces)
                {
                    int asc = 65;
                    Regex rgx = new Regex("^[A-Z]*$");
                    if (i.Attribute("Name").Value == allsame.Name)
                    {
                        foreach (XElement j in i.Elements("GradeRow"))
                        {
                            MarkGrade tmp = new MarkGrade();
                            tmp.GradeName = rgx.IsMatch(j.Attribute("Id").Value.ToString()) ? j.Attribute("Id").Value : Chr(asc).ToString();
                            asc++;
                            tmp.Score = int.Parse(j.Value);
                            allsame.Grades.Add(tmp);
                        }

                    }
                }
                if (allsame.Grades.Count <= 0)
                {
                    MarkGrade tmpMG = new MarkGrade();
                    tmpMG.GradeName = Chr(65).ToString();
                    allsame.Grades.Add(tmpMG);
                }
                GradeVariable.MarkGradeSubPieces.Add(allsame);

                // 2. 用 Rows 和 Columns 造出所有欄位再去 xml 補資料
                foreach (RoiGrade r in GradeVariable.RoiRowsGrid)
                {
                    foreach (RoiGrade c in GradeVariable.RoiColumnsGrid)
                    {
                        MarkSubPiece tmpMarkSubPiece = new MarkSubPiece();
                        tmpMarkSubPiece.Name = "ROI-" + r.Name + c.Name;
                        tmpMarkSubPiece.Grades = new List<MarkGrade>();
                        foreach (XElement i in XMarkGradeSubPieces)
                        {
                            if (i.Attribute("Name").Value == tmpMarkSubPiece.Name)
                            {
                                // 3. 補上 XML 有記錄的資料
                                int asc = 65;
                                Regex rgx = new Regex("^[A-Z]*$");
                                foreach (XElement j in i.Elements("GradeRow"))
                                {
                                    MarkGrade tmp = new MarkGrade();

                                    tmp.GradeName = rgx.IsMatch(j.Attribute("Id").Value.ToString()) ? j.Attribute("Id").Value : Chr(asc).ToString();
                                    asc++;
                                    tmp.Score = int.Parse(j.Value);
                                    tmpMarkSubPiece.Grades.Add(tmp);
                                }
                                // 4. 如果沒有新增一筆 A
                                if (tmpMarkSubPiece.Grades.Count <= 0)
                                {
                                    MarkGrade tmg = new MarkGrade();
                                    tmg.GradeName = Chr(65).ToString();
                                    tmpMarkSubPiece.Grades.Add(tmg);
                                }
                            }
                        }
                        GradeVariable.MarkGradeSubPieces.Add(tmpMarkSubPiece);
                    }
                }
            }
            catch (Exception ex)
            {
                DebugTool.WriteLog("SystemVariable.cs", "*** Grade setting > Mark grade xml default errors:" + ex.Message, MapWindowVariable.FlawPieces.Count.ToString(), MapWindowVariable.CurrentPiece.ToString());
            }

            // 載入 Grade setting > Pass or fail filter socre xml default
            try
            {
                XElement XPFEnable = XGrade.Element("GradeConfig").Element("Grade").Element("PassFail").Element("Enable");
                GradeVariable.IsPassOrFailScoreEnable = (int.Parse(XPFEnable.Value) == 1) ? true : false;
                XElement XScore = XGrade.Element("GradeConfig").Element("Grade").Element("PassFail").Element("Score");
                GradeVariable.PassOrFileScore = int.Parse(XScore.Value);
            }
            catch (Exception ex)
            {
                DebugTool.WriteLog("SystemVariable.cs", "*** Grade setting > Pass or fail filter socre xml default errors:" + ex.Message, MapWindowVariable.FlawPieces.Count.ToString(), MapWindowVariable.CurrentPiece.ToString());

            }

            //Debug
            DebugTool.WriteLog("SystemVariable.cs", "LoadGradeConfig() # Complete");
        }
        private void btnCreateGrid_Click(object sender, EventArgs e)
        {
            int x = int.TryParse(tboxColumns.Text, out x) ? x : 1;
            int y = int.TryParse(tboxRows.Text, out y) ? y : 1;
            TmpColumnsGrid.Clear();
            for (var i = 0; i < x; i++)
            {
                RoiGrade tmp = new RoiGrade();
                tmp.Name = (i + 1).ToString();
                tmp.Start = 0;
                tmp.End = 0;
                TmpColumnsGrid.Add(tmp);
            }
            TmpRowsGrid.Clear();
            for (var i = 0; i < y; i++)
            {
                RoiGrade tmp = new RoiGrade();
                tmp.Name = (i + 1).ToString();
                tmp.Start = 0;
                tmp.End = 0;
                TmpRowsGrid.Add(tmp);
            }

            gvColumns.DataSource = typeof(List<RoiGrade>);
            gvColumns.DataSource = bsColumns.DataSource;
            gvRows.DataSource = typeof(List<RoiGrade>);
            gvRows.DataSource = bsRows.DataSource;

            // Clear and create list of ROI-Pieces like ROI-11, ROI-12 etc...
            bsRoiList.DataSource = GetSubPieceList();
            cboxSubPieceOfPoint.DataSource = bsRoiList.DataSource;
            cboxSubPieceOfGrade.DataSource = bsRoiList.DataSource;

            // Create new TmpPointSubPieces
            // 1. 先加入一筆 ALL 的 SubPiece 指全部使用相同設定
            TmpPointSubPieces.Clear();
            PointSubPiece allsame = new PointSubPiece();
            allsame.Name = "ALL";
            allsame.Grades = new List<PointGrade>();
            foreach (FlawTypeNameExtend i in PxPVariable.FlawTypeName)
            {
                PointGrade tmpPG = new PointGrade();
                tmpPG.ClassId = i.FlawType;
                tmpPG.ClassName = i.Name;
                tmpPG.Score = 0;

                allsame.Grades.Add(tmpPG);
            }
            TmpPointSubPieces.Add(allsame);

            // 2. 用 Rows 和 Columns 造出所有欄位
            foreach (RoiGrade r in TmpRowsGrid)
            {
                foreach (RoiGrade c in TmpColumnsGrid)
                {
                    PointSubPiece tmpPointSubPiece = new PointSubPiece();
                    tmpPointSubPiece.Name = "ROI-" + r.Name + c.Name;
                    tmpPointSubPiece.Grades = new List<PointGrade>();
                    foreach (FlawTypeNameExtend i in PxPVariable.FlawTypeName)
                    {
                        PointGrade tmpPG = new PointGrade();
                        tmpPG.ClassId = i.FlawType;
                        tmpPG.ClassName = i.Name;
                        tmpPG.Score = 0;
                        tmpPointSubPiece.Grades.Add(tmpPG);
                    }
                    TmpPointSubPieces.Add(tmpPointSubPiece);
                }
            }

            // Create new TmpMarkSubPieces
            // 1. 先加入一筆 ALL 的 SubPiece 指全部使用相同設定
            TmpMarkSubPieces.Clear();
            MarkSubPiece msp = new MarkSubPiece();
            msp.Name = "ALL";
            msp.Grades = new List<MarkGrade>();

            MarkGrade tmpMG = new MarkGrade();
            tmpMG.GradeName = Chr(65).ToString();
            tmpMG.Score = 0;
            msp.Grades.Add(tmpMG);
            TmpMarkSubPieces.Add(msp);

            // 2. 用 Rows 和 Columns 造出所有欄位
            foreach (RoiGrade r in TmpRowsGrid)
            {
                foreach (RoiGrade c in TmpColumnsGrid)
                {
                    MarkSubPiece tmpMarkSubPiece = new MarkSubPiece();
                    tmpMarkSubPiece.Name = "ROI-" + r.Name + c.Name;
                    tmpMarkSubPiece.Grades = new List<MarkGrade>();

                    MarkGrade tmpMG2 = new MarkGrade();
                    tmpMG2.GradeName = Chr(65).ToString();
                    tmpMG2.Score = 0;
                    tmpMarkSubPiece.Grades.Add(tmpMG2);
                    TmpMarkSubPieces.Add(tmpMarkSubPiece);
                }
            }
            cboxSubPieceOfPoint_SelectedIndexChanged(cboxSubPieceOfPoint,e);
            cboxSubPieceOfGrade_SelectedIndexChanged(cboxSubPieceOfGrade,e);
        }