MenuApp.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. 
  2. /* ==============================================================================
  3. * 功能描述:启动revit时执行的Application
  4. * 创 建 者:SAGACLOUD
  5. * 创建日期:2017/8/30
  6. * ==============================================================================*/
  7. using System;
  8. using System.IO;
  9. using System.Windows;
  10. using Autodesk.Revit.Attributes;
  11. using Autodesk.Revit.DB.Events;
  12. using Autodesk.Revit.UI;
  13. using Autodesk.Revit.DB;
  14. using Autodesk.Revit.UI.Events;
  15. using SAGA.DotNetUtils;
  16. using SAGA.DotNetUtils.Extend;
  17. using SAGA.DotNetUtils.Logger;
  18. using SAGA.RevitMenu.Configuration;
  19. using SAGA.RevitMenu.Dockpane;
  20. using SAGA.RevitUtils.ErrorSupports;
  21. using SAGA.RevitUtils.Extends;
  22. using SAGA.RevitUtils.Log;
  23. using System.Threading;
  24. using SAGA.RevitUtils;
  25. namespace SAGA.RevitMenu
  26. {
  27. [Transaction(TransactionMode.Manual), Regeneration(RegenerationOption.Manual)]
  28. public class MenuApp : ExternalApplication
  29. {
  30. public override Result OnShutdown(UIControlledApplication application)
  31. {
  32. ErrorHandlers.Stop();
  33. RevitEventsBingding.RemoveEvents(application);
  34. base.OnShutdown(application);
  35. TszDocChanged -= App_DocumentChanged;
  36. application.ControlledApplication.DocumentClosing -= ControlledApplication_DocumentClosing;
  37. return Result.Succeeded;
  38. }
  39. public override Result OnStartup(UIControlledApplication application)
  40. {
  41. ErrorHandlers.Start();
  42. base.OnStartup(application);
  43. StartSetting.RegiterPane(application);
  44. //使用快捷键自动创建空间,需要先设置快捷键
  45. //ScreenMenuKeyBoardShortCut.SetKeyboardShortcuts(application.ControlledApplication.VersionName);
  46. //记录空间设备修改;Chang时仅保存到临时文件中;Save时保存到log文件中;关闭是清除临时文件
  47. TszDocChanged += App_DocumentChanged;
  48. application.ControlledApplication.DocumentClosing += ControlledApplication_DocumentClosing;
  49. RevitEventsBingding.AddEvents(application);
  50. //将加载菜单逻辑后移
  51. if (!MenuConfig.LoadMenus(application))
  52. {
  53. return Result.Failed;
  54. }
  55. //删除Addin文件
  56. MenuConfig.ClearAddInFile(application);
  57. return Result.Succeeded;
  58. }
  59. private void ControlledApplication_DocumentClosing(object sender, DocumentClosingEventArgs e)
  60. {
  61. DocumentChangedLog.DeleteTempLog(e.Document.PathName);
  62. }
  63. /// <summary>
  64. /// 记录操作日志
  65. /// </summary>
  66. /// <param name="sender"></param>
  67. /// <param name="e"></param>
  68. private void App_DocumentChanged(object sender, Autodesk.Revit.DB.Events.DocumentChangedEventArgs e)
  69. {
  70. var doc = e.GetDocument();
  71. if (doc.IsFamilyDocument) return;
  72. var pathName = doc.PathName;
  73. //楼层文件名称,无后缀
  74. var docName = pathName.GetFileName();
  75. Func<ElementId, string> getTypeName = (elementId) => doc.GetElement(elementId).GetType().Name;
  76. Func<ElementId, string> getFamilyName = (elementId) => doc.GetElement(elementId).GetFamily()?.Name;
  77. Func<ElementId, string> getCategory = (elementId) => doc.GetElement(elementId).GetCategory().ToString();
  78. //删除
  79. var deletes = e.GetDeletedElementIds();
  80. if (deletes.Count > 0)
  81. {
  82. foreach (var elementId in deletes)
  83. {
  84. DocumentChangedLogMode mode = new DocumentChangedLogMode();
  85. mode.DataTime = DateTime.Now.ToString();
  86. mode.DocPath = pathName;
  87. mode.DocName = docName;
  88. mode.Operator = DocumentChangedOperator.Delete.ToString();
  89. mode.Id = elementId.ToString();
  90. DocumentChangedLog.Log(mode.DocPath, mode.ToJson());
  91. }
  92. }
  93. //增加
  94. var adds = e.GetAddedElementIds();
  95. if (adds.Count > 0)
  96. {
  97. foreach (var elementId in adds)
  98. {
  99. //if(!IsWatchType(doc,elementId))continue;
  100. DocumentChangedLogMode mode = new DocumentChangedLogMode();
  101. mode.DataTime = DateTime.Now.ToString();
  102. mode.DocPath = pathName;
  103. mode.DocName = docName;
  104. mode.Operator = DocumentChangedOperator.Add.ToString();
  105. mode.Id = elementId.ToString();
  106. mode.Type = getTypeName(elementId);
  107. mode.Family = getFamilyName(elementId);
  108. mode.Category = getCategory(elementId);
  109. DocumentChangedLog.Log(mode.DocPath, mode.ToJson());
  110. }
  111. }
  112. //修改
  113. var modifieds = e.GetModifiedElementIds();
  114. if (modifieds.Count > 0)
  115. {
  116. foreach (var elementId in modifieds)
  117. {
  118. //if (!IsWatchType(doc, elementId)) continue;
  119. DocumentChangedLogMode mode = new DocumentChangedLogMode();
  120. mode.DataTime = DateTime.Now.ToString();
  121. mode.DocPath = pathName;
  122. mode.DocName = docName;
  123. mode.Operator = DocumentChangedOperator.Modified.ToString();
  124. mode.Id = elementId.ToString();
  125. mode.Type = getTypeName(elementId);
  126. mode.Family = getFamilyName(elementId);
  127. mode.Category = getCategory(elementId);
  128. DocumentChangedLog.Log(mode.DocPath, mode.ToJson());
  129. }
  130. }
  131. }
  132. /// <summary>
  133. /// 是否为需要监视的类型
  134. /// </summary>
  135. /// <param name="doc"></param>
  136. /// <param name="elementId"></param>
  137. /// <returns></returns>
  138. private bool IsWatchType(Document doc, ElementId elementId)
  139. {
  140. var type = doc.GetElement(elementId).GetType().Name;
  141. //族类型命名规则具有特殊性,不做有判定依据
  142. var family = doc.GetElement(elementId).GetFamily()?.Name;
  143. bool result = false;
  144. switch (type)
  145. {
  146. case "SpaceTag":
  147. result = true;
  148. break;
  149. case "Space":
  150. result = true;
  151. break;
  152. case "FamilyInstance":
  153. result = true;
  154. break;
  155. }
  156. return result;
  157. }
  158. }
  159. }