CheckDuplicationBIMId.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /* ==============================================================================
  2. * 功能描述:检查BimID是否重复,多个岗位对应一个模型
  3. * 创 建 者:Garrett
  4. * 创建日期:2018/12/13 17:25:22
  5. * ==============================================================================*/
  6. using SAGA.MBI.Calc;
  7. using SAGA.MBI.DataArrange;
  8. using SAGA.MBI.Model;
  9. using SAGA.MBI.RequestData;
  10. using SAGA.MBI.WinView.Upload;
  11. using SAGA.RevitUtils.Extends;
  12. using System;
  13. using System.Collections.Generic;
  14. using System.IO;
  15. using System.Linq;
  16. using System.Text;
  17. using Microsoft.Win32;
  18. using NPOI.SS.UserModel;
  19. using NPOI.XSSF.UserModel;
  20. using SAGA.DotNetUtils.Others;
  21. using SAGA.MBI.Common;
  22. using SAGA.MBI.Tools;
  23. namespace SAGA.MBI.ToolsData
  24. {
  25. /// <summary>
  26. /// CheckDuplicationBIMId
  27. /// </summary>
  28. class CheckDuplicationBIMId
  29. {
  30. /// <summary>
  31. /// 检查BIMId是否重复
  32. /// </summary>
  33. public static void CorrectAll()
  34. {
  35. List<IEnumerable<IGrouping<string, MRevitEquipBase>>> groupss=new List<IEnumerable<IGrouping<string, MRevitEquipBase>>>();
  36. foreach (TreeNodeItem buildingItem in MBIControl.ProjectTree.Children)
  37. {
  38. string buildingId = buildingItem.Item.Id;
  39. var groups=(Check(buildingId));
  40. if (groups == null) continue;
  41. Correct(groups);
  42. }
  43. }
  44. /// <summary>
  45. /// 检查BIMId是否重复
  46. /// </summary>
  47. private static IEnumerable<IGrouping<string, MRevitEquipBase>> Check(string buildingId)
  48. {
  49. var dutys = DalCommon.GetAllDutys(buildingId);
  50. var bybimidGroups = dutys.GroupBy(t => t.BimID);
  51. var exceptionGroups = bybimidGroups.Where(t => t.Count() >= 2 && !t.Key.IsOnlyDutyNoModelBIMID());
  52. return exceptionGroups;
  53. }
  54. /// <summary>
  55. /// 修正Bimdid重复数据,保留信息点最多的
  56. /// </summary>
  57. /// <param name="groups"></param>
  58. private static void Correct(IEnumerable<IGrouping<string, MRevitEquipBase>> groups)
  59. {
  60. foreach (IGrouping<string, MRevitEquipBase> equipBases in groups)
  61. {
  62. var list=equipBases.ToList();
  63. list.Sort((x, y) => { return x.CloundJObject.Count.CompareTo(y.CloundJObject.Count);});
  64. for (int i = 0; i < list.Count-1; i++)
  65. {
  66. var item = list[i];
  67. item.DelObject();
  68. }
  69. }
  70. }
  71. /// <summary>
  72. /// 导出数据
  73. /// </summary>
  74. /// <param name="xyzsList"></param>
  75. private static void ExportToExcel(List<IEnumerable<IGrouping<string, MRevitEquipBase>>> groupss)
  76. {
  77. SaveFileDialog sflg = new SaveFileDialog();
  78. sflg.Filter = "Excel(*.xlsx)|*.xlsx";
  79. if (sflg.ShowDialog() != true) return;
  80. string fileName = sflg.FileName;
  81. IWorkbook book = new XSSFWorkbook();
  82. ISheet sheet = book.CreateSheet("Summary");
  83. foreach (IEnumerable<IGrouping<string, MRevitEquipBase>> groups in groupss)
  84. {
  85. ExportToExcel(sheet, groups, fileName);
  86. }
  87. Save(book, fileName);
  88. }
  89. /// <summary>
  90. /// 导出数据到指定目录
  91. /// </summary>
  92. /// <param name="xyzsList"></param>
  93. /// <param name="fileName"></param>
  94. private static void ExportToExcel(ISheet sheet, IEnumerable<IGrouping<string, MRevitEquipBase>> groups, string fileName)
  95. {
  96. try
  97. {
  98. #region 添加表头
  99. IRow row = sheet.CreateRow(0);
  100. NPOI.SS.UserModel.ICell cell0 = row.CreateCell(0);
  101. cell0.SetCellType(NPOI.SS.UserModel.CellType.String);
  102. cell0.SetCellValue("BIMID");
  103. ICell cell1 = row.CreateCell(1);
  104. cell1.SetCellType(CellType.String);
  105. cell1.SetCellValue("DutyId");
  106. #endregion
  107. #region 添加数据
  108. int index = 1;
  109. foreach (var group in groups)
  110. {
  111. var bimid = group.Key;
  112. var dutys = "";
  113. foreach (var equip in group)
  114. {
  115. dutys += equip.Id + ";";
  116. }
  117. IRow rowN = sheet.CreateRow(index);
  118. NPOI.SS.UserModel.ICell cellN0 = rowN.CreateCell(0);
  119. cellN0.SetCellType(NPOI.SS.UserModel.CellType.String);
  120. cellN0.SetCellValue(bimid);
  121. ICell cellN1 = rowN.CreateCell(1);
  122. cellN1.SetCellType(CellType.String);
  123. cellN1.SetCellValue(dutys);
  124. index++;
  125. }
  126. #endregion
  127. }
  128. catch (Exception e)
  129. {
  130. MessageShowBase.Show(e);
  131. }
  132. }
  133. private static void Save(IWorkbook book, string fileName)
  134. {
  135. #region 写入
  136. MemoryStream ms = new MemoryStream();
  137. book.Write(ms);
  138. using (System.IO.FileStream fs = new System.IO.FileStream(fileName, FileMode.Create, FileAccess.Write))
  139. {
  140. byte[] data = ms.ToArray();
  141. fs.Write(data, 0, data.Length);
  142. fs.Flush();
  143. }
  144. ms.Close();
  145. ms.Dispose();
  146. #endregion
  147. }
  148. }
  149. }