RevitParameterUpdate.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. 
  2. using System;
  3. using System.IO;
  4. using System.Linq;
  5. using Autodesk.Revit.DB;
  6. using Autodesk.Revit.DB.Mechanical;
  7. using Newtonsoft.Json.Linq;
  8. using SAGA.DotNetUtils;
  9. using SAGA.MBI.Common;
  10. using SAGA.MBI.DataArrange;
  11. using SAGA.MBI.Interaction;
  12. using SAGA.MBI.RequestData;
  13. using SAGA.MBI.RevitModelHandle;
  14. using SAGA.Models;
  15. using SAGA.RevitUtils.Extends;
  16. using SAGA.RevitUtils.Windows;
  17. namespace SAGA.MBI
  18. {
  19. /// <summary>
  20. /// revit相关参数哦修改扩展
  21. /// </summary>
  22. public static class RevitParameterUpdate
  23. {
  24. #region 基础方法
  25. /// <summary>
  26. /// 根据bimId获取模型对象
  27. /// </summary>
  28. /// <param name="bimId"></param>
  29. /// <returns></returns>
  30. public static Element GetElementByBimId(string bimId)
  31. {
  32. Element result = null;
  33. #region 获取revit对象
  34. if (bimId == null)
  35. return result;
  36. var ids = bimId.Split(':');
  37. if (ids.Count() < 2)
  38. return result;
  39. string floorId = ids[0];
  40. int elementID = ids[1].ToInt();
  41. var mFloor = DalProjectTree.GetFloorByFloorId(floorId);
  42. if (mFloor == null)
  43. {
  44. return result;
  45. }
  46. if (!File.Exists(mFloor.FullPath))
  47. {
  48. return result;
  49. }
  50. var doc = ExternalDataWrapper.Current.App.OpenDocumentFile(mFloor.FullPath);
  51. if (doc == null)
  52. return result;
  53. result = doc.GetElement(new ElementId(elementID));
  54. #endregion
  55. return result;
  56. }
  57. #endregion
  58. #region 同步模型
  59. #region 触发器控制,方法取消
  60. ///// <summary>
  61. ///// 更新模型空间名称
  62. ///// </summary>
  63. ///// <param name="element"></param>
  64. ///// <param name="name"></param>
  65. ///// <param name="stopLocalTrigger">是否暂停触发器,暂停触发器则不会发送对数据平台的同步</param>
  66. //public static void UdpateName(this Element element, string name, bool stopLocalTrigger)
  67. //{
  68. // if (!stopLocalTrigger)
  69. // {
  70. // //UdpateName(element, name);
  71. // return;
  72. // }
  73. // try
  74. // {
  75. // MBILocalNameTrigger.Suspend();
  76. // //UdpateName(element, name);
  77. // }
  78. // catch (Exception ex)
  79. // {
  80. // }
  81. // finally
  82. // {
  83. // MBILocalNameTrigger.Resume();
  84. // }
  85. //}
  86. #endregion
  87. public static void UpdateRevitParameter(string bimId, string parameterName,string value)
  88. {
  89. if (!ParameterDefinitionFactory.ContainParameter(parameterName))
  90. return;
  91. var element = GetElementByBimId(bimId);
  92. if (element == null)
  93. return;
  94. ExecuteCmd.ExecuteCommand(() =>
  95. {
  96. using (Transaction tran = new Transaction(element.Document, "更改"))
  97. {
  98. try
  99. {
  100. tran.Start();
  101. //不检测值是否为null
  102. UpdateElementByMbi(element, parameterName, value, false);
  103. tran.Commit();
  104. }
  105. catch (Exception e)
  106. {
  107. tran.RollBack();
  108. }
  109. }
  110. return Autodesk.Revit.UI.Result.Succeeded;
  111. });
  112. }
  113. #endregion
  114. #region 更新物理世界相关信息
  115. /// <summary>
  116. /// 更新空间的名称信息
  117. /// </summary>
  118. /// <remarks>触发器使用,现在被砍掉了</remarks>
  119. /// <param name="element"></param>
  120. /// <param name="name"></param>
  121. public static void UpdateMBIName(this Element element, string name)
  122. {
  123. //更新物理世界信息点内容
  124. JArray jArray = CommonTool.GetPropertyJArray(name);
  125. JObject jObject = new JObject();
  126. jObject.Add(MBIBuiltInParameter.RoomLocalName, jArray);
  127. var cloudElement = DalCommon.GetEquipmentQueryId(element);
  128. if (cloudElement == null || string.IsNullOrWhiteSpace(cloudElement.Id))
  129. return;
  130. CommonConvert.UpdateInfosSigle(cloudElement.Id, jObject);
  131. }
  132. #endregion
  133. #region 通过mbi更新,模型信息
  134. /// <summary>
  135. /// 更新元素信息值
  136. /// </summary>
  137. /// <param name="element"></param>
  138. /// <param name="mbiParameterName"></param>
  139. /// <param name="value"></param>
  140. public static void UpdateElementByMbi(Element element, string mbiParameterName, string value)
  141. {
  142. UpdateElementByMbi(element, mbiParameterName, value, true);
  143. }
  144. public static void UpdateElementByMbi(Element element, string mbiParameterName, string value, bool checkValueEmpty)
  145. {
  146. ////设备信息同步关闭
  147. //if (element is FamilyInstance)
  148. // return;
  149. if (checkValueEmpty && string.IsNullOrWhiteSpace(value))
  150. return;
  151. ParameterDefinitionFactory.GetParameterDefinition(mbiParameterName)
  152. ?.SetParameter(element, value);
  153. }
  154. #endregion
  155. }
  156. }