CalcContext.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /* ==============================================================================
  2. * 功能描述:CalcContext
  3. * 创 建 者:Garrett
  4. * 创建日期:2018/4/1 11:19:24
  5. * ==============================================================================*/
  6. using System;
  7. using System.Collections;
  8. using System.Collections.Generic;
  9. using System.Collections.ObjectModel;
  10. using System.Linq;
  11. using System.Reflection;
  12. using System.Text;
  13. using System.Threading.Tasks;
  14. using Autodesk.Revit.DB;
  15. using DevExpress.Mvvm.Native;
  16. using SAGA.DotNetUtils;
  17. using SAGA.DotNetUtils.Data;
  18. using SAGA.DotNetUtils.Extend;
  19. using SAGA.DotNetUtils.Logger;
  20. using SAGA.MBI.DataArrange;
  21. using SAGA.MBI.Model;
  22. using SAGA.MBI.RequestData;
  23. using SAGA.MBI.Tools;
  24. using SAGA.MBI.WinView.PositionBeaconModeling;
  25. using SAGA.MBI.WinView.TraceView;
  26. using SAGA.MBI.WinView.Upload;
  27. using SAGA.RevitUtils;
  28. using SAGA.RevitUtils.Extends;
  29. namespace SAGA.MBI.Calc
  30. {
  31. /// <summary>
  32. /// CalcContext
  33. /// </summary>
  34. public class CalcContext : InitObejct
  35. {
  36. public CalcContext(MFloor mFloor) : base()
  37. {
  38. MFloor = mFloor;
  39. }
  40. public MFloor MFloor { get; set; }
  41. public string FloorId { get { return MFloor.Id; } }
  42. public Document RevitDoc { get; set; }
  43. [InitAttribute]
  44. public ObservableCollection<MISpace> MSpaces { get; set; }
  45. [InitAttribute]
  46. public ObservableCollection<MEquipment> MEquipments { get; set; }
  47. [InitAttribute]
  48. public ObservableCollection<MEquipmentPart> MEquipmentParts { get; set; }
  49. [InitAttribute]
  50. public List<RltEquipInSpace> RltEquipInSpaces { get; set; }
  51. /// <summary>
  52. /// 信标
  53. /// </summary>
  54. [InitAttribute]
  55. public ObservableCollection<MBeacon> MBeacons { get; set; }
  56. [InitAttribute]
  57. public List<RltBeaconinElementSp> RltBeaconinElementSps { get; set; }
  58. /// <summary>
  59. /// 未分组的待更新的关系数据
  60. /// </summary>
  61. [InitAttribute]
  62. public List<MGraphRelationBase> UnGroupDatas { get; set; }
  63. /// <summary>
  64. /// 打开项目
  65. /// </summary>
  66. public Document OpenDocument()
  67. {
  68. string path = MFloor.FullPath;
  69. var uiApp = ExternalDataWrapper.Current.UiApp;
  70. RevitDoc = uiApp.Application.OpenDocumentFile(path);
  71. return RevitDoc;
  72. }
  73. /// <summary>
  74. /// 关闭项目
  75. /// </summary>
  76. public void CloseDocument()
  77. {
  78. RevitDoc?.CloseDoc();
  79. }
  80. public bool Upload()
  81. {
  82. bool result = true;
  83. Log4Net.Debug("更新楼层数据开始");
  84. try
  85. {
  86. //修改空间
  87. DalSpace.BatchUpdate(MSpaces);
  88. //修改设备
  89. DalEquip.BatchUpdate(MEquipments);
  90. //修改部件
  91. DalCommon.BatchUpdate(MEquipmentParts);
  92. //修改设备所在空间关系
  93. DalCommon.BetchUpdateRelation(RltEquipInSpaces);
  94. //修改信标
  95. DalCommon.BatchUpdate(MBeacons);
  96. //修改信标所在元空间
  97. DalCommon.BetchUpdateRelation(RltBeaconinElementSps);
  98. //更新未分组的数据
  99. DalCommon.BetchUpdateRelation(UnGroupDatas);
  100. }
  101. catch (Exception e)
  102. {
  103. MessageShow.Show(e);
  104. result = false;
  105. }
  106. Log4Net.Debug("更新楼层数据结束");
  107. return result;
  108. }
  109. /// <summary>
  110. /// 根据用户选择,进行删除,或者清除BimId的保留
  111. /// </summary>
  112. /// <returns></returns>
  113. public bool Del()
  114. {
  115. bool result = true;
  116. try
  117. {
  118. //修改空间
  119. MSpaces.ForEach(t => t.DelOrClearBIMIDSave());
  120. //修改设备
  121. MEquipments.ForEach(t => t.DelOrClearBIMIDSave());
  122. //修改部件
  123. MEquipmentParts.ForEach(t => t.DelOrClearBIMIDSave());
  124. //修改信标
  125. MBeacons.ForEach(t => t.DelOrClearBIMIDSave());
  126. }
  127. catch (Exception e)
  128. {
  129. MessageShow.Show(e);
  130. result = false;
  131. }
  132. return result;
  133. }
  134. /// <summary>
  135. /// 添加
  136. /// </summary>
  137. /// <param name="mode"></param>
  138. public void Add(MRevitEquipBase mode)
  139. {
  140. if (mode == null) return;
  141. if (mode is MEquipment equip)
  142. {
  143. this.MEquipments.Add(equip);
  144. }
  145. else if (mode is MEquipmentPart part)
  146. {
  147. this.MEquipmentParts.Add(part);
  148. }
  149. else if (mode is MISpace space)
  150. {
  151. this.MSpaces.Add(space);
  152. }
  153. else if (mode is MBeacon beacon)
  154. {
  155. this.MBeacons.Add(beacon);
  156. }
  157. }
  158. /// <summary>
  159. /// 查找云平台是否有此条数据
  160. /// </summary>
  161. /// <param name="bimId"></param>
  162. /// <returns></returns>
  163. public MRevitEquipBase FindItem(string bimId)
  164. {
  165. MRevitEquipBase mode = null;
  166. while (true)
  167. {
  168. mode = MSpaces.FirstOrDefault(t => t.BimID == bimId);
  169. if (mode != null) break;
  170. mode = MEquipments.FirstOrDefault(t => t.BimID == bimId);
  171. if (mode != null) break;
  172. mode = MEquipmentParts.FirstOrDefault(t => t.BimID == bimId);
  173. if (mode != null) break;
  174. mode = MBeacons.FirstOrDefault(t => t.BimID == bimId);
  175. if (mode != null) break;
  176. break;
  177. }
  178. return mode;
  179. }
  180. /// <summary>
  181. /// 集合不为null
  182. /// </summary>
  183. /// <returns></returns>
  184. public bool HasItem()
  185. {
  186. bool result = false;
  187. foreach (PropertyInfo info in base.GetType().GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance))
  188. {
  189. try
  190. {
  191. Type type = info.PropertyType;
  192. if (type.IsClass && !type.IsAbstract)
  193. {
  194. InitAttribute[] customAttributes = info.GetCustomAttributes(typeof(InitAttribute), true) as InitAttribute[];
  195. if (customAttributes.Any<InitAttribute>())
  196. {
  197. if (type == typeof(string))
  198. {
  199. info.SetValue(this, string.Empty, null);
  200. }
  201. else
  202. {
  203. var item = info.GetValue(this) as IEnumerable;
  204. if (item?.GetCount() > 0)
  205. {
  206. result = true;
  207. break;
  208. }
  209. }
  210. }
  211. }
  212. }
  213. catch (Exception)
  214. {
  215. }
  216. }
  217. return result;
  218. }
  219. /// <summary>
  220. /// 写到临时日志文件
  221. /// </summary>
  222. /// <param name="operate"></param>
  223. public void WriteToTempLogFile(DocumentChangedOperator operate = DocumentChangedOperator.None)
  224. {
  225. string pathName = MFloor.FullPath;
  226. //楼层文件名称,无后缀
  227. var docName = pathName.GetFileName();
  228. var doc = this.RevitDoc;
  229. Action<MRevitEquipBase> writeToLogAction = (t) =>
  230. {
  231. DocumentChangedLogMode mode = new DocumentChangedLogMode();
  232. mode.DataTime = DateTime.Now.ToString();
  233. mode.DocPath = pathName;
  234. mode.DocName = docName;
  235. mode.Operator = t.Operator != DocumentChangedOperator.None ? t.Operator.ToString() : operate.ToString();
  236. mode.Id = t.BimID.GetBIMID().ToString();
  237. if (doc?.IsValidObject == true)
  238. {
  239. var elem = doc.GetElement(mode.Id.ToInt());
  240. if (elem != null)
  241. {
  242. mode.Type = elem.GetType().Name;
  243. mode.Family = elem.GetFamily()?.Name;
  244. mode.Category = elem.GetCategory().ToString();
  245. }
  246. }
  247. DocumentChangedLog.Log(mode.DocPath, mode.ToJson());
  248. };
  249. //修改空间
  250. MSpaces.ForEach(writeToLogAction);
  251. //修改设备
  252. MEquipments.ForEach(writeToLogAction);
  253. //修改部件
  254. MEquipmentParts.ForEach(writeToLogAction);
  255. //修改信标
  256. MBeacons.ForEach(writeToLogAction);
  257. }
  258. }
  259. }