import re file_path = r'd:\CodeProject\1440\JY.Inspection\HomeForm.cs' with open(file_path, 'r', encoding='utf-8-sig') as f: content = f.read() # Define the old method block (from doc comment to closing brace) old_pattern = r''' /// /// 结果判断 /// /// /// /// private short GetResult\(BlankingData m, ref string strErr,ref List lstError\) \{ short res = 0; strErr = ""; #region 判断各项参数 //入站条码判断 bool IsBarInOK = !m\.ArrivalBarCode\.Contains\("ERROR"\); //出站条码判断 bool IsBarOK = !m\.DepartureBarCode\.Contains\("ERROR"\); //判断条码是否一致 bool bBar = true; if \(m\.ArrivalBarCode\.Trim\(\) != m\.DepartureBarCode\.Trim\(\)\) bBar = false; m\.TMDB = bBar \? "OK" : "NG"; // CCD判断 bool isAllCCD = true; foreach \(var propName in ConstValue\.CCD_SDict\.Keys\) \{ if \(BlankingDataProps\.TryGetValue\(propName, out var prop\)\) \{ string result = prop\.GetValue\(m\)\.ToString\(\); if \(result != "OK"\) \{ isAllCCD = false; \} \} \} bool\[\] b0 = new bool\[16\] \{ true, true, true, true, true, true, true, true, true, true, true, true, true, true,true,true \}; b0\[0\] = m\.CCD1 == "OK" \? true : false; b0\[1\] = m\.CCD2 == "OK" \? true : false; b0\[2\] = m\.CCD3 == "OK" \? true : false; b0\[3\] = m\.CCD4 == "OK" \? true : false; b0\[4\] = m\.CCD5 == "OK" \? true : false; b0\[5\] = m\.CCD6 == "OK" \? true : false; b0\[6\] = m\.CCD7 == "OK" \? true : false; b0\[7\] = m\.CCD8 == "OK" \? true : false; b0\[8\] = m\.CCD9 == "OK" \? true : false; b0\[9\] = m\.CCD10 == "OK" \? true : false; b0\[10\] = m\.CCD11 == "OK" \? true : false; b0\[11\] = m\.CCD12 == "OK" \? true : false; b0\[12\] = m\.CCD13 == "OK" \? true : false; b0\[13\] = m\.CCD14 == "OK" \? true : false; b0\[14\] = m\.CCD15 == "OK" \? true : false; b0\[15\] = m\.CCD16 == "OK" \? true : false; #endregion #region if \( IsBarOK && bBar && b0\[0\] && b0\[1\] && b0\[2\] && b0\[3\] && b0\[4\] && b0\[5\] && b0\[6\] && b0\[7\] && b0\[8\] && b0\[9\] && b0\[10\] && b0\[11\] && b0\[12\] && b0\[13\] && b0\[14\] && b0\[15\]\) \{ strErr = "良品"; res = 1; lstError\.Add\(res\); return 1; \}.*?return 0; #endregion \}''' new_method = ''' /// /// 检验结果错误类型 /// private enum InspectionErrorType { ScanError, BarcodeMismatch, CcdError } /// /// 检验错误项 /// private class InspectionError { public InspectionErrorType Type { get; set; } public string Description { get; set; } } /// /// CCD检测项名称(索引0-15对应CCD1-CCD16) /// private static readonly string[] CcdDefectNames = { "正面(2D/3D)不良", "反面(2D/3D)不良", "左侧面(2D/3D)不良", "右侧面(2D/3D)不良", "顶面(2D/3D)不良", "底面(2D/3D)不良", "底WE1不良", "底WE2不良", "底WE3不良", "底WE4不良", "中ME1不良", "中ME2不良", "中ME3不良", "中ME4不良", "极柱(POS/NEG)不良", "防爆阀(PRO)不良" }; /// /// 结果判断:检查条码和CCD结果,返回错误列表(空列表=良品) /// /// 出站数据实体 /// 错误列表,空列表表示良品 private List GetResult(BlankingData m, ref string strErr) { var errors = new List(); strErr = ""; // 1. 条码检查 bool isBarOK = !m.DepartureBarCode.Contains("ERROR"); bool isBarcodeMatch = m.ArrivalBarCode.Trim() == m.DepartureBarCode.Trim(); m.TMDB = isBarcodeMatch ? "OK" : "NG"; // 2. CCD结果检查 var ccdValues = new[] { m.CCD1, m.CCD2, m.CCD3, m.CCD4, m.CCD5, m.CCD6, m.CCD7, m.CCD8, m.CCD9, m.CCD10, m.CCD11, m.CCD12, m.CCD13, m.CCD14, m.CCD15, m.CCD16 }; bool allCcdOk = Array.TrueForAll(ccdValues, v => v == "OK"); // 3. 全部通过 = 良品 if (isBarOK && isBarcodeMatch && allCcdOk) { strErr = "良品"; return errors; } // 4. 分类收集错误 if (!isBarOK) { errors.Add(new InspectionError { Type = InspectionErrorType.ScanError, Description = $"出站扫码不良,出站条码:{m.DepartureBarCode}" }); } if (!isBarcodeMatch) { errors.Add(new InspectionError { Type = InspectionErrorType.BarcodeMismatch, Description = "入站与出站条码不一致" }); } for (int i = 0; i < ccdValues.Length; i++) { if (ccdValues[i] != "OK") { errors.Add(new InspectionError { Type = InspectionErrorType.CcdError, Description = CcdDefectNames[i] }); } } strErr = string.Join(";", errors.Select(e => e.Description)); return errors; }''' # Use DOTALL to match across lines new_content = re.sub(old_pattern, new_method, content, flags=re.DOTALL) if new_content == content: print("ERROR: Pattern not found!") # Try to find partial matches if '结果判断' in content: print("Found '结果判断' in content") if 'private short GetResult' in content: print("Found 'private short GetResult' in content") if '防爆阀(PRO)不良' in content: print("Found '防爆阀(PRO)不良' in content") # Check for the exact method signature idx = content.find('private short GetResult') if idx >= 0: print(f"Found at index {idx}") print(f"Context: {repr(content[idx:idx+100])}") else: with open(file_path, 'w', encoding='utf-8-sig') as f: f.write(new_content) print("SUCCESS: Method replaced!") print(f"Old content length: {len(content)}") print(f"New content length: {len(new_content)}")