ExportMEPSystem.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /* ==============================================================================
  2. * 功能描述:导出所有的岗位
  3. * 创 建 者:Garrett
  4. * 创建日期:2018/8/10 16:36:26
  5. * ==============================================================================*/
  6. using System;
  7. using System.Collections.Generic;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using Autodesk.Revit.DB;
  13. using Autodesk.Revit.DB.Mechanical;
  14. using FWindSoft.Tools;
  15. using Microsoft.Win32;
  16. using Newtonsoft.Json.Linq;
  17. using NPOI.SS.UserModel;
  18. using NPOI.XSSF.UserModel;
  19. using SAGA.DotNetUtils;
  20. using SAGA.DotNetUtils.Logger;
  21. using SAGA.DotNetUtils.Others;
  22. using SAGA.MBI.Calc;
  23. using SAGA.MBI.Common;
  24. using SAGA.MBI.DataArrange;
  25. using SAGA.MBI.Model;
  26. using SAGA.MBI.RequestData;
  27. using SAGA.MBI.Tools;
  28. using SAGA.MBI.WinView.Upload;
  29. using SAGA.RevitUtils.Extends;
  30. using SAGA.RevitUtils.MEP;
  31. using CellType = Autodesk.Revit.DB.CellType;
  32. namespace SAGA.MBI.ToolsData
  33. {
  34. /// <summary>
  35. /// UpdateRelationEquipinFloor
  36. /// </summary>
  37. public class ExportMEPSystem
  38. {
  39. /// <summary>
  40. /// 检查并处理所有楼层
  41. /// </summary>
  42. public static void OperateAll()
  43. {
  44. var floors = DalProjectTree.GetAllFloors(false);
  45. List<ExportItemGroup> contexts = new List<ExportItemGroup>();
  46. foreach (var floor in floors)
  47. {
  48. contexts.Add(Operate(floor));
  49. }
  50. ExportToExcel(contexts);
  51. }
  52. /// <summary>
  53. /// 只处理当前楼层
  54. /// </summary>
  55. public static void OperateCurFloor()
  56. {
  57. MFloor floor = ExternalDataWrapper.Current.Doc.GetCurMFloor();
  58. if (floor != null)
  59. {
  60. var context = Operate(floor);
  61. ExportToExcel(new List<ExportItemGroup>() { context });
  62. }
  63. }
  64. /// <summary>
  65. /// 检查并处理
  66. /// </summary>
  67. /// <param name="floor"></param>
  68. /// <returns></returns>
  69. private static ExportItemGroup Operate(MFloor floor)
  70. {
  71. string fullPah = floor.FullPath;
  72. Document doc = ExternalDataWrapper.Current.UiApp.Application.OpenDocumentFile(fullPah);
  73. ExportItemGroup group =new ExportItemGroup(floor.ToString());
  74. var systems = doc.GetElements<MEPSystem>();
  75. foreach (var mepSystem in systems)
  76. {
  77. string name = mepSystem.Name;
  78. string systemType = doc.GetElement(mepSystem.GetTypeId()).Name;
  79. string type = mepSystem is MechanicalSystem ? "风管" : "水管";
  80. group.Items.Add(new ExportItem(){SystemName = name,SystemType = systemType,Type = type});
  81. }
  82. return group;
  83. }
  84. /// <summary>
  85. /// 导出数据
  86. /// </summary>
  87. /// <param name="xyzsList"></param>
  88. static void ExportToExcel(List<ExportItemGroup> contexts)
  89. {
  90. SaveFileDialog sflg = new SaveFileDialog();
  91. sflg.Filter = "Excel(*.xlsx)|*.xlsx";
  92. sflg.FileName = $"{MBIControl.ProjectCur}系统汇总表";
  93. if (sflg.ShowDialog() != true) return;
  94. ExportToExcel(contexts, sflg.FileName, sflg.FilterIndex);
  95. }
  96. /// <summary>
  97. /// 导出数据到指定目录
  98. /// </summary>
  99. /// <param name="xyzsList"></param>
  100. /// <param name="fileName"></param>
  101. private static void ExportToExcel(List<ExportItemGroup> contexts, string fileName, int filterIndex = 0)
  102. {
  103. try
  104. {
  105. IWorkbook book = new XSSFWorkbook();
  106. #region 添加数据
  107. foreach (var context in contexts)
  108. {
  109. int index = 0;
  110. var floorName = context.FloorName;
  111. ISheet sheet = book.CreateSheet(floorName);
  112. #region 添加表头
  113. IRow row = sheet.CreateRow(0);
  114. NPOI.SS.UserModel.ICell cell0 = row.CreateCell(0);
  115. cell0.SetCellType(NPOI.SS.UserModel.CellType.String);
  116. cell0.SetCellValue("楼层名称");
  117. ICell cell1 = row.CreateCell(1);
  118. cell1.SetCellType(NPOI.SS.UserModel.CellType.String);
  119. cell1.SetCellValue("类型");
  120. ICell cell2 = row.CreateCell(2);
  121. cell2.SetCellType(NPOI.SS.UserModel.CellType.String);
  122. cell2.SetCellValue("系统名称");
  123. ICell cell3 = row.CreateCell(3);
  124. cell3.SetCellType(NPOI.SS.UserModel.CellType.String);
  125. cell3.SetCellValue("系统类型名称");
  126. #endregion
  127. foreach (var item in context.Items)
  128. {
  129. string type = item.Type;
  130. string name = item.SystemName;
  131. string sytemName = item.SystemType;
  132. index++;
  133. row = sheet.CreateRow(index);
  134. ICell cell = row.CreateCell(0, NPOI.SS.UserModel.CellType.String);
  135. cell.SetCellValue(floorName);
  136. cell = row.CreateCell(1, NPOI.SS.UserModel.CellType.String);
  137. cell.SetCellValue(type);
  138. cell = row.CreateCell(2, NPOI.SS.UserModel.CellType.String);
  139. cell.SetCellValue(name);
  140. cell = row.CreateCell(3, NPOI.SS.UserModel.CellType.String);
  141. cell.SetCellValue(sytemName);
  142. }
  143. }
  144. #endregion
  145. #region 写入
  146. MemoryStream ms = new MemoryStream();
  147. book.Write(ms);
  148. book = null;
  149. using (System.IO.FileStream fs = new System.IO.FileStream(fileName, FileMode.Create, FileAccess.Write))
  150. {
  151. byte[] data = ms.ToArray();
  152. fs.Write(data, 0, data.Length);
  153. fs.Flush();
  154. }
  155. ms.Close();
  156. ms.Dispose();
  157. #endregion
  158. }
  159. catch (Exception e)
  160. {
  161. MessageShowBase.Show(e);
  162. }
  163. }
  164. /// <summary>
  165. /// 导出数据到指定目录
  166. /// </summary>
  167. /// <param name="xyzsList"></param>
  168. /// <param name="fileName"></param>
  169. private static void ExportToExcel2(List<CalcContext> contexts, string fileName, int filterIndex = 0)
  170. {
  171. try
  172. {
  173. IWorkbook book = new XSSFWorkbook();
  174. #region 添加数据
  175. ISheet sheet = book.CreateSheet("Summary");
  176. #region 添加表头
  177. IRow row = sheet.CreateRow(0);
  178. NPOI.SS.UserModel.ICell cell0 = row.CreateCell(0);
  179. cell0.SetCellType(NPOI.SS.UserModel.CellType.String);
  180. cell0.SetCellValue("楼层名称");
  181. ICell cell1 = row.CreateCell(1);
  182. cell1.SetCellType(NPOI.SS.UserModel.CellType.String);
  183. cell1.SetCellValue("类型");
  184. ICell cell2 = row.CreateCell(2);
  185. cell2.SetCellType(NPOI.SS.UserModel.CellType.String);
  186. cell2.SetCellValue("总数");
  187. List<string> list = new List<string>() { "FASE-光电感烟探测器", "FASE-智能感温探测器", "FSCP-消火栓起泵按钮" };
  188. var list2 = list.Select(tt => tt.Substring(0, 4)).ToList();
  189. int index = 0;
  190. foreach (var context in contexts)
  191. {
  192. var floorName = context.MFloor.ToString();
  193. #endregion
  194. Action<int> action = (count) =>
  195. {
  196. index++;
  197. row = sheet.CreateRow(index);
  198. ICell cell = row.CreateCell(0, NPOI.SS.UserModel.CellType.String);
  199. cell.SetCellValue(floorName);
  200. cell = row.CreateCell(1, NPOI.SS.UserModel.CellType.String);
  201. cell.SetCellValue(string.Join(",", list));
  202. cell = row.CreateCell(2, NPOI.SS.UserModel.CellType.String);
  203. cell.SetCellValue(count);
  204. };
  205. action(context.MEquipments.Where(t => list2.Contains(t.EquipClassCode)).Count());
  206. }
  207. #endregion
  208. #region 写入
  209. MemoryStream ms = new MemoryStream();
  210. book.Write(ms);
  211. book = null;
  212. using (System.IO.FileStream fs = new System.IO.FileStream(fileName, FileMode.Create, FileAccess.Write))
  213. {
  214. byte[] data = ms.ToArray();
  215. fs.Write(data, 0, data.Length);
  216. fs.Flush();
  217. }
  218. ms.Close();
  219. ms.Dispose();
  220. #endregion
  221. }
  222. catch (Exception e)
  223. {
  224. MessageShowBase.Show(e);
  225. }
  226. }
  227. class ExportItemGroup
  228. {
  229. public ExportItemGroup(string floorName)
  230. {
  231. FloorName = floorName;
  232. Items = new List<ExportItem>();
  233. }
  234. public List<ExportItem> Items { get; set; }
  235. public string FloorName { get; set; }
  236. }
  237. class ExportItem
  238. {
  239. public string SystemName { get; set; }
  240. public string SystemType { get; set; }
  241. public string Type { get; set; }
  242. }
  243. }
  244. }