Просмотр исходного кода

Merge branch 'GSP' of http://36.129.169.60:30080/gogsadmin/OTS into GSP

zhangjiaxin 1 месяц назад
Родитель
Сommit
f267810fd3

+ 1 - 0
OTSCPP/OTSImagePro/OTSImageProcess.cpp

@@ -2341,6 +2341,7 @@ namespace OTSIMGPROC
 		xray->SetElementQuantifyData(newCheList);
 		partTagId++;
 		xray->SetIndex(partTagId);
+		xray->SetScanFieldId(p->GetFieldId());
 		newPart->SetXrayInfo(xray);
 		newPart->SetConnectedParticlesSequentialString(partsStr);
 		newPart->SetSubParticles(allSubParts);

+ 131 - 63
OTSIncAReportApp/1-UI/Control_Grids/ParticlesGridDevidePage/ParticlesGridDevidePage.cs

@@ -296,6 +296,22 @@ namespace OTSIncAReportGrids
 
 
         #region 自定义方法
+
+        // 新增:为 userSTDDbData 建立索引以加速查找
+        private Dictionary<string, DataRow> BuildUserStdLookup()
+        {
+            var dict = new Dictionary<string, DataRow>();
+            if (userSTDDbData == null)
+                return dict;
+            foreach (DataRow r in userSTDDbData.Rows)
+            {
+                var key = Convert.ToString(r["STDId"]);
+                if (!dict.ContainsKey(key))
+                    dict.Add(key, r);
+            }
+            return dict;
+        }
+
         bool UpdateDataGrid()
         {
             int sel = m_ReportApp.m_conditionChoose.m_conditionData.GetComboDownListIndexByItemName(OTSIncAReportApp.OTSSampleReportInfo.OTS_REPORT_PROP_GRID_ITEMS.PARTICE_LIST);
@@ -320,16 +336,20 @@ namespace OTSIncAReportGrids
             particlesAll.Columns.Add("Hardness");
             particlesAll.Columns.Add("Density");
             particlesAll.Columns.Add("Electrical_conductivity");
+
+            // 使用字典索引 userSTDDbData,避免对每行使用 DataTable.Select(性能瓶颈)
+            var userStdLookup = BuildUserStdLookup();
+
             if (userSTDDbData != null)
             {
                 for (int i = 0; i < particlesAll.Rows.Count; i++)
                 {
-                    DataRow[] dr = userSTDDbData.Select("STDId=" + particlesAll.Rows[i]["TypeId"].ToString());
-                    if (dr.Length > 0)
+                    string key = Convert.ToString(particlesAll.Rows[i]["TypeId"]);
+                    if (userStdLookup.TryGetValue(key, out var dr))
                     {
-                        particlesAll.Rows[i]["Hardness"] = dr[0]["Hardness"].ToString();
-                        particlesAll.Rows[i]["Density"] = dr[0]["Density"].ToString();
-                        particlesAll.Rows[i]["Electrical_conductivity"] = dr[0]["Electrical_conductivity"].ToString();
+                        particlesAll.Rows[i]["Hardness"] = dr["Hardness"].ToString();
+                        particlesAll.Rows[i]["Density"] = dr["Density"].ToString();
+                        particlesAll.Rows[i]["Electrical_conductivity"] = dr["Electrical_conductivity"].ToString();
                     }
                     else
                     {
@@ -356,49 +376,73 @@ namespace OTSIncAReportGrids
                 log.Error("There is an exception in the data of the database!");
                 #region 加载进度条进度部份结束
                 this.Cursor = Cursors.Default;
-                m_frm_userprogress.Close();
+                if (m_frm_userprogress != null && !m_frm_userprogress.IsDisposed)
+                    m_frm_userprogress.Close();
                 #endregion
                 return false;
             }
 
             DataTable elementchemistry = Particledata.GetElementChemistry();
+
+            // 将 elementchemistry 按 key 索引,避免多次 Select
+            var elemIndex = new Dictionary<string, List<DataRow>>();
+            foreach (DataRow er in elementchemistry.Rows)
+            {
+                string key = "XRayId = " + er["XRayId"].ToString() + " and fieldid = " + er["fieldid"].ToString();
+                if (!elemIndex.TryGetValue(key, out var list))
+                {
+                    list = new List<DataRow>();
+                    elemIndex[key] = list;
+                }
+                list.Add(er);
+            }
+
             for (int i = 0; i < particlesAll.Rows.Count; i++)
             {
-                string str = "XRayId = " + particlesAll.Rows[i]["particleId"].ToString() + " and fieldid = " + particlesAll.Rows[i]["fieldid"].ToString();
-                DataRow[] drs = elementchemistry.Select(str);
+                string key = "XRayId = " + particlesAll.Rows[i]["particleId"].ToString() + " and fieldid = " + particlesAll.Rows[i]["fieldid"].ToString();
                 string ConcatenatedString = "";
-                for (int j = 0; j < drs.Length; j++)
+                if (elemIndex.TryGetValue(key, out var drList))
                 {
-                    ConcatenatedString += drs[j]["name"] + "-" + drs[j]["Percentage"] + ';';
+                    for (int j = 0; j < drList.Count; j++)
+                    {
+                        ConcatenatedString += drList[j]["name"] + "-" + drList[j]["Percentage"] + ';';
+                    }
                 }
                 particlesAll.Rows[i]["Element"] = ConcatenatedString;
             }
             particlesAll.Columns.Add("XRayDataCount");
             DataTable XRayData = Particledata.GetXRayData();
+
+            // 建立 XRayData 索引(key: " XrayIndex= <id> and fieldid = <id>")
+            var xrayIndex = new Dictionary<string, byte[]>();
+            foreach (DataRow xr in XRayData.Rows)
+            {
+                if (xr.ItemArray.Length >= 3)
+                {
+                    string key = " XrayIndex= " + xr[0].ToString() + " and fieldid = " + xr[1].ToString();
+                    if (!xrayIndex.ContainsKey(key) && xr[2] is byte[] b)
+                    {
+                        xrayIndex[key] = b;
+                    }
+                }
+            }
+
             for (int i = 0; i < particlesAll.Rows.Count; i++)
             {
-                string subParticleString = particlesAll.Rows[i]["SubParticles"].ToString();
-                if (subParticleString =="")
+                string subParticleString = Convert.ToString(particlesAll.Rows[i]["SubParticles"]);
+                if (string.IsNullOrEmpty(subParticleString))
                 {
                     particlesAll.Rows[i]["XRayDataCount"] = 0;
                     string str = " XrayIndex= " + particlesAll.Rows[i]["particleId"].ToString() + " and fieldid = " + particlesAll.Rows[i]["fieldid"].ToString();
-                    DataRow[] drs = XRayData.Select(str);
-                    uint xraycount = 0;
-                    if (drs != null)
+                    if (xrayIndex.TryGetValue(str, out var bytes))
                     {
-                        if (drs.Length > 0)
+                        uint xraycount = 0;
+                        int len = bytes.Length / 4;
+                        for (int j = 0; j < len; j++)
                         {
-                            uint[] Analysis_xray = new uint[2000];
-                            for (int j = 0; j < 2000; j++)
-                            {
-                                Analysis_xray[j] = BitConverter.ToUInt32((byte[])drs[0][2], j * 4);
-                            }
-                            for (int j = 0; j < 2000; j++)
-                            {
-                                xraycount += Analysis_xray[j];
-                            }
-                            particlesAll.Rows[i]["XRayDataCount"] = xraycount;
+                            xraycount += BitConverter.ToUInt32(bytes, j * 4);
                         }
+                        particlesAll.Rows[i]["XRayDataCount"] = xraycount;
                     }
                 }
                 else
@@ -409,7 +453,7 @@ namespace OTSIncAReportGrids
                     uint[] Analysis_xray = new uint[2000];
                     Particledata.GetXrayByParticleTagIDAndFieldID_ForMergeParticle(Convert.ToInt32(dgvr["particleId"]), Convert.ToInt32(dgvr["fieldid"]), out Analysis_xray);
                     uint xraycount = 0;
-                    for (int j = 0; j < 2000; j++)
+                    for (int j = 0; j < Analysis_xray.Length; j++)
                     {
                         xraycount += Analysis_xray[j];
                     }
@@ -422,7 +466,8 @@ namespace OTSIncAReportGrids
         private void RefreshGridView()
         {
             dgV_ParticlesDevidePage.Visible = false;
-            if (m_frm_userprogress.IsDisposed)
+
+            if (m_frm_userprogress == null || m_frm_userprogress.IsDisposed)
             {
                 ProgressStart();
             }
@@ -430,6 +475,10 @@ namespace OTSIncAReportGrids
             string str_ElementsColName = null;
             Dictionary<string, string> keyValues = InitializeTable(ref str_ElementsColName);
             Dictionary<string, string>.Enumerator en = keyValues.GetEnumerator();
+
+            // 减少 UI 重绘开销
+            dgV_ParticlesDevidePage.SuspendLayout();
+
             for (int irow = 0; irow < keyValues.Count; irow++)
             {
                 if (en.MoveNext())
@@ -439,7 +488,8 @@ namespace OTSIncAReportGrids
                         DataGridViewImageColumn iconColumn = new DataGridViewImageColumn();
                         iconColumn.Name = en.Current.Key;
                         iconColumn.HeaderText = en.Current.Value;
-                        iconColumn.ImageLayout = DataGridViewImageCellLayout.Zoom;
+                        iconColumn.ImageLayout = DataGridViewImageCellLayout.Stretch;
+                        iconColumn.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
                         dgV_ParticlesDevidePage.Columns.Add(iconColumn);
                     }
                     else if (en.Current.Key == "Element")
@@ -464,6 +514,7 @@ namespace OTSIncAReportGrids
 
             if (particlesAll == null)
             {
+                dgV_ParticlesDevidePage.ResumeLayout();
                 return;
             }
             RecordCount = particlesAll.Rows.Count;
@@ -477,50 +528,49 @@ namespace OTSIncAReportGrids
                 particlesFilter.ImportRow(particlesAll.Rows[fi]);
             }
 
-            List<string> ElementTypeSort = new List<string>(str_ElementsColName.Split(',').ToList());//去重
+            List<string> ElementTypeSort = new List<string>((str_ElementsColName ?? "").Split(',').ToList());//去重
+            if (ElementTypeSort.Count > 0 && ElementTypeSort[0] == "")
+            {
+                ElementTypeSort.RemoveAt(0);
+            }
             for (int i = 0; i < ElementTypeSort.Count; i++)
             {
-                if (ElementTypeSort[0] == "")
-                {
-                    
-                    
-                    ElementTypeSort.RemoveAt(0);
-                    break;
-                }
                 dgV_ParticlesDevidePage.Columns.Add(ElementTypeSort[i], ElementTypeSort[i]);
                 int id = dgV_ParticlesDevidePage.Columns.Count;
                 dgV_ParticlesDevidePage.Columns[id - 1].Tag = "NumericType";
             }
-            double jd = 95f / (double)particlesFilter.Rows.Count;
+            double jd = particlesFilter.Rows.Count > 0 ? 95f / (double)particlesFilter.Rows.Count : 95f;
             string filePath = result.FilePath + "\\FIELD_FILES\\";
-            
+
+            // 把 fieldpics 缓存提升到循环外,避免对同一 field 多次磁盘读取
+            Dictionary<string, Bitmap> fieldpics = new Dictionary<string, Bitmap>(StringComparer.OrdinalIgnoreCase);
+
             for (int i = 0; i < particlesFilter.Rows.Count; i++)
             {
-                if (i % 10 == 0)
+                if (i % 10 == 0 && m_frm_userprogress != null && !m_frm_userprogress.IsDisposed)
                     m_frm_userprogress.SetProgressValueAndText((int)(jd * i), "loading..");
 
                 Dictionary<string, string>.Enumerator enl = keyValues.GetEnumerator();
 
                 int add_rowindex = dgV_ParticlesDevidePage.Rows.Add();
                 dgV_ParticlesDevidePage.Rows[add_rowindex].Cells[0].Value = (pageSize * (PageIndex - 1) + add_rowindex + 1).ToString();
-                Dictionary<string, Bitmap> fieldpics=new Dictionary<string, Bitmap>();
                 for (int k = 0; k < keyValues.Count; k++)
                 {
                     if (enl.MoveNext())
                     {
                         if (enl.Current.Key == "ParticleImage")
                         {
-                            string subParticleString = particlesFilter.Rows[i]["SubParticles"].ToString();
+                            string subParticleString = Convert.ToString(particlesFilter.Rows[i]["SubParticles"]);
                             dgV_ParticlesDevidePage.Rows[add_rowindex].Height = 150;
-                            
-                            
+
+
                             dgV_ParticlesDevidePage.Rows[add_rowindex].Cells[k].Style.BackColor = Color.Azure;
 
                             Bitmap bmap = null;
-                            if (subParticleString != null && subParticleString != "" && subParticleString != "IsSubParticle")
+                            if (!string.IsNullOrEmpty(subParticleString) && subParticleString != "IsSubParticle")
                             {
                                 double pix =  result.GetPixelSize();
-                                int width = result.GetImageWidth(); 
+                                int width = result.GetImageWidth();
                                 int height = result.GetImageHeight();
                                 string vs = "," + subParticleString.Replace(':', '-') + ",";
                                 DataTable dataTable = Particledata.GetParticleAllForBig(vs);//组成拼接颗粒的子颗粒
@@ -528,20 +578,30 @@ namespace OTSIncAReportGrids
                                 if (bmap != null)
                                 {
                                     string[] str = subParticleString.Split(',');
-                                    bmap.Tag = new List<string>() { str[0].Split(':')[0], str[0].Split(':')[1] };
+                                    if (str.Length > 0)
+                                    {
+                                        var pair = str[0].Split(':');
+                                        if (pair.Length >= 2)
+                                            bmap.Tag = new List<string>() { pair[0], pair[1] };
+                                    }
                                 }
                             }
                             else if (subParticleString != "IsSubParticle")
                             {
-                                if (!fieldpics.Keys.Contains(particlesFilter.Rows[i]["fieldid"].ToString()))
+                                string fid = Convert.ToString(particlesFilter.Rows[i]["fieldid"]);
+                                if (!fieldpics.ContainsKey(fid))
                                 {
-                                    string imagePath = "Field" + particlesFilter.Rows[i]["fieldid"].ToString() + ".bmp";
-                                    fieldpics.Add(particlesFilter.Rows[i]["fieldid"].ToString(), fldImgAccess.ReadImageFile(imagePath));
+                                    string imagePath = "Field" + fid + ".bmp";
+                                    fieldpics[fid] = fldImgAccess.ReadImageFile(imagePath);
                                 }
                                 Rectangle rectangle = new Rectangle() { X = Convert.ToInt32(particlesFilter.Rows[i]["RectLeft"]), Y = Convert.ToInt32(particlesFilter.Rows[i]["RectTop"]), Width = Convert.ToInt32(particlesFilter.Rows[i]["RectWidth"]), Height = Convert.ToInt32(particlesFilter.Rows[i]["RectHeight"]) };
-                                bmap = fldImgAccess.CapturePic(fieldpics[particlesFilter.Rows[i]["fieldid"].ToString()], rectangle);
-                                bmap.Tag = new List<string>() { particlesFilter.Rows[i]["FieldId"].ToString(), particlesFilter.Rows[i]["ParticleId"].ToString()};
-                                dgV_ParticlesDevidePage.Rows[add_rowindex].Height = bmap.Height + 20;
+                                var src = fieldpics[fid];
+                                if (src != null)
+                                {
+                                    bmap = fldImgAccess.CapturePic(src, rectangle);
+                                    bmap.Tag = new List<string>() { particlesFilter.Rows[i]["FieldId"].ToString(), particlesFilter.Rows[i]["ParticleId"].ToString()};
+                                    dgV_ParticlesDevidePage.Rows[add_rowindex].Height = bmap.Height + 20;
+                                }
                             }
                             dgV_ParticlesDevidePage.Rows[add_rowindex].Cells[k].Value = bmap;
                         }
@@ -558,14 +618,14 @@ namespace OTSIncAReportGrids
                         if (particlesFilter.Columns.Contains(enl.Current.Key))
                         {
                             double num = 0;
-                            if (double.TryParse(particlesFilter.Rows[i][enl.Current.Key].ToString(), out num))
+                            if (double.TryParse(Convert.ToString(particlesFilter.Rows[i][enl.Current.Key]), out num))
                             {
                                 dgV_ParticlesDevidePage.Rows[add_rowindex].Cells[k].Value = Math.Round(num, 2);
                             }
                             else if (enl.Current.Key == "Element")
                             {
                                 List<string> elementtemp = new List<string>(ElementTypeSort);
-                                string[] strcbo = particlesFilter.Rows[i][enl.Current.Key].ToString().Split(';');
+                                string[] strcbo = Convert.ToString(particlesFilter.Rows[i][enl.Current.Key]).Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                                 for (int j = 0; j < strcbo.Length; j++)
                                 {
                                     if (ElementTypeSort.Count == 0)
@@ -573,8 +633,13 @@ namespace OTSIncAReportGrids
                                         break;
                                     }
                                     string[] str = strcbo[j].Split('-');
-                                    if (ElementTypeSort.Contains(str[0]))
-                                    { dgV_ParticlesDevidePage.Rows[add_rowindex].Cells[str[0].ToString()].Value = Math.Round(double.Parse(str[1]), 2).ToString(); }
+                                    if (str.Length >= 2 && ElementTypeSort.Contains(str[0]))
+                                    { 
+                                        if (double.TryParse(str[1], out double parsed))
+                                            dgV_ParticlesDevidePage.Rows[add_rowindex].Cells[str[0].ToString()].Value = Math.Round(parsed, 2).ToString();
+                                        else
+                                            dgV_ParticlesDevidePage.Rows[add_rowindex].Cells[str[0].ToString()].Value = "0";
+                                    }
                                     elementtemp.Remove(str[0].ToString());
                                 }
                                 foreach (var ele in elementtemp)
@@ -611,7 +676,7 @@ namespace OTSIncAReportGrids
                         }
                         if (enl.Current.Key == "Hardness")
                         {
-                            string str = ChangeHardnessColor(particlesFilter.Rows[i]["Hardness"].ToString());
+                            string str = ChangeHardnessColor(Convert.ToString(particlesFilter.Rows[i]["Hardness"]));
                             {
                                 if (str == "#FF0000")
                                 {
@@ -633,10 +698,13 @@ namespace OTSIncAReportGrids
             //加载完成设置鼠标为默认
             this.Cursor = Cursors.Default;
             string str8 = table["str8"].ToString();
-            m_frm_userprogress.SetProgressValueAndText(100, str8);
+            if (m_frm_userprogress != null && !m_frm_userprogress.IsDisposed)
+                m_frm_userprogress.SetProgressValueAndText(100, str8);
             //加载完成,关闭进度条
-            m_frm_userprogress.Close();
+            if (m_frm_userprogress != null && !m_frm_userprogress.IsDisposed)
+                m_frm_userprogress.Close();
             #endregion
+            dgV_ParticlesDevidePage.ResumeLayout();
             dgV_ParticlesDevidePage.Visible = true;
         }
         Dictionary<string, string> InitializeTable(ref string str_ElementsColName)
@@ -1291,7 +1359,7 @@ namespace OTSIncAReportGrids
             #region 加载显示进度条部份
             this.Cursor = Cursors.WaitCursor;
             m_frm_userprogress = new Frm_UserProgress();
-            Form ls_main_form = this.ParentForm.ParentForm;//取出父窗体
+            Form ls_main_form = this.ParentForm?.ParentForm;//取出父窗体
             if (ls_main_form == null)
             {
                 m_frm_userprogress.Visible = false;
@@ -1620,7 +1688,8 @@ namespace OTSIncAReportGrids
 
                 m_frm_userprogress.SetProgressValueAndText(100, "export:" + dgV_ParticlesDevidePage.Rows.Count + "/total:" + dgV_ParticlesDevidePage.Rows.Count);
                 //加载完成,关闭进度条
-                m_frm_userprogress.Close();
+                if (m_frm_userprogress != null && !m_frm_userprogress.IsDisposed)
+                    m_frm_userprogress.Close();
                 Cursor = Cursors.Default;
                 //导出完成后,打开Excel文件
                 if (File.Exists(sfd.FileName))
@@ -2461,7 +2530,6 @@ namespace OTSIncAReportGrids
                     string subParticleString = row["SubParticles"].ToString();
                     particle =Particledata.GetXrayByParticleIDAndFieldID(subParticleString, Convert.ToInt32(dgvr.Cells["particleId"].Value), Convert.ToInt32(dgvr.Cells["fieldid"].Value), out Analysis_xray);
 
-
                     List<ShowElementInfo> list_showelementinfo = Particledata.GetShowElementInfos(particle.ElementList);
                     string str_IncAName = "";
                     str_IncAName = Convert.ToString(dgvr.Cells[1].Value);