CheckEquipInSpace.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /* ==============================================================================
  2. * 功能描述:检查模型中那些设备没有在空间中
  3. * 创 建 者:Garrett
  4. * 创建日期:2018/7/11 14:34: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 SAGA.MBI.Calc;
  13. using SAGA.MBI.Model;
  14. using SAGA.MBI.WinView.Upload;
  15. using Autodesk.Revit.DB;
  16. using Microsoft.Win32;
  17. using SAGA.DotNetUtils.Logger;
  18. using SAGA.MBI.DataArrange;
  19. using SAGA.MBI.Tools;
  20. using SAGA.RevitUtils.Extends;
  21. using NPOI.SS.UserModel;
  22. using NPOI.XSSF.UserModel;
  23. using SAGA.DotNetUtils.Extend;
  24. using SAGA.DotNetUtils.Others;
  25. using ICell= NPOI.SS.UserModel.ICell;
  26. using CellType= NPOI.SS.UserModel.CellType;
  27. namespace SAGA.MBI.ToolsData
  28. {
  29. /// <summary>
  30. /// CheckEquipInSpace
  31. /// </summary>
  32. public class CheckEquipInSpace
  33. {
  34. /// <summary>
  35. /// 导出没有在空间中的设备和部件
  36. /// </summary>
  37. public static void Export()
  38. {
  39. var floors = DalUploadFloor.GetHasFileFloors();
  40. List<CalcContext> contexts = new List<CalcContext>();
  41. foreach (UploadFloor floor in floors)
  42. {
  43. var context = GetEquipNotInSpaceCalcContext(floor.MFloor);
  44. contexts.Add(context);
  45. }
  46. ExportToExcel(contexts);
  47. }
  48. /// <summary>
  49. /// 重新上传设备所在空间关系数据
  50. /// </summary>
  51. public static void ReuploadEquipInElementSp()
  52. {
  53. var doc = ExternalDataWrapper.Current.Doc;
  54. var floorid=doc.PathName.GetFileName();
  55. MFloor floor = DalProjectTree.GetFloorByFloorId(floorid);
  56. CalcContext context = DalCommon.DownLoadFloorDataByBIMFloorInfo(floor);
  57. context.OpenDocument();
  58. var document = context.RevitDoc;
  59. var spaces = context.MSpaces;
  60. var rspaces = context.RevitDoc.GetSpaces().Where(t => t.IsValidObject).ToList();
  61. foreach (var equipment in context.MEquipments)
  62. {
  63. var bimId = equipment.BimID;
  64. Element fi=document.GetElement(bimId.GetBIMID());
  65. if (fi != null)
  66. {
  67. var space = fi.GetReferenceSpace(rspaces);
  68. if (space != null)
  69. {
  70. var spaceBimid = space.GetCloudBIMId();
  71. var cspace = spaces.FirstOrDefault(t => spaceBimid == t.BimID);
  72. if (cspace != null)
  73. {
  74. var relation=new RltEquipInSpace(cspace,equipment){Operator = DocumentChangedOperator.Add};
  75. context.UnGroupDatas.Add(relation);
  76. }
  77. }
  78. }
  79. }
  80. DalCommon.BetchUpdateRelation(context.UnGroupDatas);
  81. }
  82. /// <summary>
  83. /// 获取没有在空间中的设备
  84. /// </summary>
  85. /// <param name="floor"></param>
  86. /// <returns></returns>
  87. private static CalcContext GetEquipNotInSpaceCalcContext(MFloor floor)
  88. {
  89. CalcContext context = new CalcContext(floor);
  90. context.OpenDocument();
  91. try
  92. {
  93. CalcEquipPartRlt(context);
  94. }
  95. catch (Exception e)
  96. {
  97. MessageShowBase.Show(e);
  98. }
  99. finally
  100. {
  101. context.RevitDoc.CloseDoc();
  102. }
  103. return context;
  104. }
  105. /// <summary>
  106. /// 读取空间中的设备、部件、设备所在空间的关系
  107. /// </summary>
  108. /// <param name="context"></param>
  109. private static void CalcEquipPartRlt(CalcContext context)
  110. {
  111. var linkElements = context.RevitDoc.GetMbiElements();
  112. var rspaces = context.RevitDoc.GetSpaces();
  113. //过滤出的所有实体
  114. foreach (Element fi in linkElements)
  115. {
  116. if (fi.IsEquipment())
  117. {
  118. MEquipment equipment = DalEquip.GetEquipment(fi);
  119. if (equipment == null) continue;
  120. //识别设备所在空间
  121. var space = fi.GetReferenceSpace(rspaces);
  122. if (space == null)
  123. {
  124. string familyName = fi.GetFamilyName();
  125. var id = fi.Id.ToString();
  126. equipment.EquipClassCode = familyName;
  127. equipment.BimID = id;
  128. context.MEquipments.Add(equipment);
  129. }
  130. }
  131. // 在这里处理部件
  132. if (fi.IsEquipmentPart())
  133. {
  134. MEquipmentPart part = DalEquipPart.GetEquipPart(fi);
  135. if (part == null) continue;
  136. context.MEquipmentParts.Add(part);
  137. //识别设备所在空间
  138. var space = fi.GetReferenceSpace(rspaces);
  139. if (space == null)
  140. {
  141. string familyName = fi.GetFamilyName();
  142. var id = fi.Id.ToString();
  143. part.EquipClassCode = familyName;
  144. part.BimID = id;
  145. context.MEquipmentParts.Add(part);
  146. }
  147. }
  148. }
  149. }
  150. /// <summary>
  151. /// 导出数据
  152. /// </summary>
  153. /// <param name="xyzsList"></param>
  154. public static void ExportToExcel(List<CalcContext> contexts)
  155. {
  156. SaveFileDialog sflg = new SaveFileDialog();
  157. sflg.Filter = "Excel(*.xlsx)|*.xlsx";
  158. if (sflg.ShowDialog() != true) return;
  159. ExportToExcel(contexts, sflg.FileName, sflg.FilterIndex);
  160. }
  161. /// <summary>
  162. /// 导出数据到指定目录
  163. /// </summary>
  164. /// <param name="xyzsList"></param>
  165. /// <param name="fileName"></param>
  166. private static void ExportToExcel(List<CalcContext> contexts, string fileName, int filterIndex = 0)
  167. {
  168. try
  169. {
  170. IWorkbook book = new XSSFWorkbook();
  171. ISheet sheet = book.CreateSheet("Summary");
  172. #region 添加表头
  173. IRow row = sheet.CreateRow(0);
  174. NPOI.SS.UserModel.ICell cell0 = row.CreateCell(0);
  175. cell0.SetCellType(NPOI.SS.UserModel.CellType.String);
  176. cell0.SetCellValue("楼层名称");
  177. ICell cell1 = row.CreateCell(1);
  178. cell1.SetCellType(CellType.String);
  179. cell1.SetCellValue("楼层所在位置");
  180. ICell cell2 = row.CreateCell(2);
  181. cell2.SetCellType(CellType.String);
  182. cell2.SetCellValue("族名称");
  183. ICell cell3 = row.CreateCell(3);
  184. cell3.SetCellType(CellType.String);
  185. cell3.SetCellValue("设备id");
  186. #endregion
  187. #region 添加数据
  188. int index = 0;
  189. foreach (var context in contexts)
  190. {
  191. var floorName = context.MFloor.ToString();
  192. var floorPath = context.MFloor.FullPath;
  193. foreach (var equip in context.MEquipments)
  194. {
  195. var family = equip.EquipClassCode;
  196. var id = equip.BimID;
  197. index++;
  198. row = sheet.CreateRow(index);
  199. ICell cell = row.CreateCell(0, CellType.String);
  200. cell.SetCellValue(floorName);
  201. cell = row.CreateCell(1, CellType.String);
  202. cell.SetCellValue(floorPath);
  203. cell = row.CreateCell(2, CellType.String);
  204. cell.SetCellValue(family);
  205. cell = row.CreateCell(3, CellType.String);
  206. cell.SetCellValue(id);
  207. }
  208. }
  209. #endregion
  210. #region 写入
  211. MemoryStream ms = new MemoryStream();
  212. book.Write(ms);
  213. book = null;
  214. using (System.IO.FileStream fs = new System.IO.FileStream(fileName, FileMode.Create, FileAccess.Write))
  215. {
  216. byte[] data = ms.ToArray();
  217. fs.Write(data, 0, data.Length);
  218. fs.Flush();
  219. }
  220. ms.Close();
  221. ms.Dispose();
  222. #endregion
  223. }
  224. catch (Exception e)
  225. {
  226. MessageShowBase.Show(e);
  227. }
  228. }
  229. }
  230. }