bool FindRepeatPlans(ITable PlansTable, out string[] RepeatPlans, out string[] SummaryNames, out int RepeatCnt) { IFIDSet pNonEmptyPlansFIDSet = new FIDSetClass(); ICursor pPlansCur = null; SummaryNames = null; RepeatPlans = null; RepeatCnt = 0; try { pPlansCur = PlansTable.Search(null, false); Int32 iPlanNameIDX = pPlansCur.Fields.FindField("NAME"); IRow pPlanRow = pPlansCur.NextRow(); //Create a collection of plan names if (m_pStepProgressor.Position < m_pStepProgressor.MaxRange) m_pStepProgressor.Step(); string[] sPlanNames = new string[0]; //define as dynamic array int iCount = 0; bool bCont = true; Utils FabricUTILS = new Utils(); while (pPlanRow != null) { //Check if the cancel button was pressed. If so, stop process bCont = m_pTrackCancel.Continue(); if (!bCont) { RepeatCnt++; return false; } string sPlanNm = (string)pPlanRow.get_Value(iPlanNameIDX); if (sPlanNm.Trim() == "") sPlanNm = "<No Name>"; FabricUTILS.RedimPreserveString(ref sPlanNames, 1); sPlanNames[iCount++] = sPlanNm.Trim() + ", OID:" + pPlanRow.OID; Marshal.ReleaseComObject(pPlanRow); pPlanRow = pPlansCur.NextRow(); if (m_pStepProgressor.Position < m_pStepProgressor.MaxRange) m_pStepProgressor.Step(); } if (pPlanRow!=null) Marshal.ReleaseComObject(pPlanRow); if (pPlansCur != null) Marshal.ReleaseComObject(pPlansCur); System.Array.Sort<string>(sPlanNames); int m = -1; string sName_m; int k = -1; string sName_k; string sThisRepeat = ""; bool bIsNewGroup = false; Dictionary<int, string> dictNameFromID = new Dictionary<int,string>(); int iCnt = sPlanNames.GetLength(0) - 1; //Create a collection of repeat plan names string[] sRepeatPlans = new string[0]; //define as dynamic array FabricUTILS.RedimPreserveString(ref sRepeatPlans, 1); sRepeatPlans[0] = ""; int idx = 0; // used with sRepeatPlans string array int iOID =-1; RepeatCnt = 0; for (int j = iCnt; j >= 0; j--) { //Check if the cancel button was pressed. If so, stop process bCont = m_pTrackCancel.Continue(); if (!bCont) return false; sName_m = ""; sName_k = ""; int l = sPlanNames[j].LastIndexOf(", OID:"); string sName_l = sPlanNames[j].Trim().Substring(0, l); if (sName_l.ToUpper().Trim() == sThisRepeat.ToUpper().Trim()) bIsNewGroup = false; else if (j < iCnt - 1) bIsNewGroup = true; if (j > 0) { k = sPlanNames[j - 1].LastIndexOf(", OID:"); sName_k = sPlanNames[j - 1].Trim().Substring(0, k); } if (j < sPlanNames.GetLength(0)-1) { m = sPlanNames[j + 1].LastIndexOf(", OID:"); sName_m = sPlanNames[j + 1].Trim().Substring(0, m); } sThisRepeat = sName_l; if (sName_l.ToUpper() == sName_k.ToUpper() || sName_l.ToUpper() == sName_m.ToUpper()) //If true then this is a repeated Plan { RepeatCnt++; int i = sPlanNames[j].LastIndexOf(", OID:"); string sOID = sPlanNames[j].Trim().Substring(i + 6); iOID = Convert.ToInt32(sOID); try { dictNameFromID.Add(iOID, sName_l); } catch { } //test if we're in a new group and increment the array if (bIsNewGroup) { FabricUTILS.RedimPreserveString(ref sRepeatPlans, 1); idx++; sRepeatPlans[idx] = ""; } if (!sRepeatPlans[idx].Contains("," + sOID + " ")) sRepeatPlans[idx] += "," + sOID + " "; if (sName_l.ToUpper() == sName_k.ToUpper()) { i = sPlanNames[j - 1].LastIndexOf(", OID:"); sOID = sPlanNames[j - 1].Trim().Substring(i + 6); if (!sRepeatPlans[idx].Contains("," + sOID + " ")) sRepeatPlans[idx] += "," + sOID + " "; } if (sName_l.ToUpper() == sName_m.ToUpper()) { i = sPlanNames[j + 1].LastIndexOf(", OID:"); sOID = sPlanNames[j + 1].Trim().Substring(i + 6); if (!sRepeatPlans[idx].Contains("," + sOID + " ")) sRepeatPlans[idx] += "," + sOID + " "; } } else FabricUTILS.RemoveAt(ref sPlanNames, j); if (m_pStepProgressor.Position < m_pStepProgressor.MaxRange) m_pStepProgressor.Step(); } string[] sTemp = new string[sRepeatPlans.GetLength(0)]; System.Array.Copy(sRepeatPlans, sTemp, sRepeatPlans.GetLength(0)); if (sRepeatPlans.GetLength(0) ==1) //if there is only one repeat plan group { if (sRepeatPlans[0] == null) //test if there are no repeat plans return false; if (sRepeatPlans[0].Trim() == "") //test if there are no repeat plans return false; } //Define the summary array string[] sSummaryNames = new string[0];//create a string array for the summary list int z = 0; int h = 0; foreach (string s in sRepeatPlans) { if (s == null) continue; string sT2 = s.Replace(",", "").Trim(); string[] sOID = sT2.Split(' '); int[] iOIDArr = new int[sOID.GetLength(0)]; int zz = 0; foreach (string xx in sOID) { if (xx.Trim()!="") iOIDArr[zz++] = Convert.ToInt32(xx); else iOIDArr[zz++] = -1; } System.Array.Sort<int>(iOIDArr); zz = 0; foreach (int yy in iOIDArr) sOID[zz++] = Convert.ToString(yy); string sFirstNameFound = ""; int q =0; sTemp[z] = ""; int iOID2 = Convert.ToInt32(sOID[0]); string ThisPlanName = ""; if (iOID2 >= 0) { if (!dictNameFromID.TryGetValue(iOID2, out ThisPlanName))//if this is the first, it is the target plan return false; } sFirstNameFound = "!_!==>" + ThisPlanName + " (id:" + sOID[0] + ")"; foreach (string s2 in sOID) { if (s2 == null) continue; if (s2.Trim() == "-1") continue; if (q > 0) {//if this is not the first plan then FabricUTILS.RedimPreserveString(ref sSummaryNames, 1); //makes space in the array if ( q < sOID.GetLength(0)-1) sSummaryNames[h] += ThisPlanName + " (id:" + s2 + ")" + "!_!"; else sSummaryNames[h] += ThisPlanName + " (id:" + s2 + ")" + sFirstNameFound; h++; } sTemp[z] += "," + s2.Trim(); q++; } FabricUTILS.RedimPreserveString(ref sSummaryNames, 1); //makes space in the array sSummaryNames[sSummaryNames.GetLength(0) - 1] = " !_! ";//add empty row as separator h++; sTemp[z] = sTemp[z].Replace(",", " ").Trim(); sTemp[z] = sTemp[z].Replace(" ", ",").Trim(); z++; sRepeatPlans = sTemp; } SummaryNames = sSummaryNames; RepeatPlans = sRepeatPlans; sTemp = null; FabricUTILS = null; dictNameFromID.Clear(); dictNameFromID = null; if (pNonEmptyPlansFIDSet != null) Marshal.ReleaseComObject(pNonEmptyPlansFIDSet); return true; } catch (COMException ex) { if (pPlansCur != null) Marshal.ReleaseComObject(pPlansCur); if (pNonEmptyPlansFIDSet != null) Marshal.ReleaseComObject(pNonEmptyPlansFIDSet); MessageBox.Show(Convert.ToString(ex.ErrorCode)); return false; } }