MenuApp.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. if (!MenuConfig.LoadMenus(application))
  44. {
  45. return Result.Failed;
  46. }
  47. StartSetting.RegiterPane(application);
  48. //使用快捷键自动创建空间,需要先设置快捷键
  49. //ScreenMenuKeyBoardShortCut.SetKeyboardShortcuts(application.ControlledApplication.VersionName);
  50. //记录空间设备修改;Chang时仅保存到临时文件中;Save时保存到log文件中;关闭是清除临时文件
  51. TszDocChanged += App_DocumentChanged;
  52. application.ControlledApplication.DocumentClosing += ControlledApplication_DocumentClosing;
  53. RevitEventsBingding.AddEvents(application);
  54. //删除Addin文件
  55. //MenuConfig.ClearAddInFile(application);
  56. return Result.Succeeded;
  57. }
  58. private void ControlledApplication_DocumentClosing(object sender, DocumentClosingEventArgs e)
  59. {
  60. DocumentChangedLog.DeleteTempLog(e.Document.PathName);
  61. }
  62. /// <summary>
  63. /// 记录操作日志
  64. /// </summary>
  65. /// <param name="sender"></param>
  66. /// <param name="e"></param>
  67. private void App_DocumentChanged(object sender, Autodesk.Revit.DB.Events.DocumentChangedEventArgs e)
  68. {
  69. var doc = e.GetDocument();
  70. if (doc.IsFamilyDocument) return;
  71. var pathName = doc.PathName;
  72. //楼层文件名称,无后缀
  73. var docName = pathName.GetFileName();
  74. Func<ElementId, string> getTypeName = (elementId) => doc.GetElement(elementId).GetType().Name;
  75. Func<ElementId, string> getFamilyName = (elementId) => doc.GetElement(elementId).GetFamily()?.Name;
  76. Func<ElementId, string> getCategory = (elementId) => doc.GetElement(elementId).GetCategory().ToString();
  77. //删除
  78. var deletes = e.GetDeletedElementIds();
  79. if (deletes.Count > 0)
  80. {
  81. foreach (var elementId in deletes)
  82. {
  83. DocumentChangedLogMode mode = new DocumentChangedLogMode();
  84. mode.DataTime = DateTime.Now.ToString();
  85. mode.DocPath = pathName;
  86. mode.DocName = docName;
  87. mode.Operator = DocumentChangedOperator.Delete.ToString();
  88. mode.Id = elementId.ToString();
  89. DocumentChangedLog.Log(mode.DocPath, mode.ToJson());
  90. }
  91. }
  92. //增加
  93. var adds = e.GetAddedElementIds();
  94. if (adds.Count > 0)
  95. {
  96. foreach (var elementId in adds)
  97. {
  98. //if(!IsWatchType(doc,elementId))continue;
  99. DocumentChangedLogMode mode = new DocumentChangedLogMode();
  100. mode.DataTime = DateTime.Now.ToString();
  101. mode.DocPath = pathName;
  102. mode.DocName = docName;
  103. mode.Operator = DocumentChangedOperator.Add.ToString();
  104. mode.Id = elementId.ToString();
  105. mode.Type = getTypeName(elementId);
  106. mode.Family = getFamilyName(elementId);
  107. mode.Category = getCategory(elementId);
  108. DocumentChangedLog.Log(mode.DocPath, mode.ToJson());
  109. }
  110. }
  111. //修改
  112. var modifieds = e.GetModifiedElementIds();
  113. if (modifieds.Count > 0)
  114. {
  115. foreach (var elementId in modifieds)
  116. {
  117. //if (!IsWatchType(doc, elementId)) continue;
  118. DocumentChangedLogMode mode = new DocumentChangedLogMode();
  119. mode.DataTime = DateTime.Now.ToString();
  120. mode.DocPath = pathName;
  121. mode.DocName = docName;
  122. mode.Operator = DocumentChangedOperator.Modified.ToString();
  123. mode.Id = elementId.ToString();
  124. mode.Type = getTypeName(elementId);
  125. mode.Family = getFamilyName(elementId);
  126. mode.Category = getCategory(elementId);
  127. DocumentChangedLog.Log(mode.DocPath, mode.ToJson());
  128. }
  129. }
  130. }
  131. /// <summary>
  132. /// 是否为需要监视的类型
  133. /// </summary>
  134. /// <param name="doc"></param>
  135. /// <param name="elementId"></param>
  136. /// <returns></returns>
  137. private bool IsWatchType(Document doc, ElementId elementId)
  138. {
  139. var type = doc.GetElement(elementId).GetType().Name;
  140. //族类型命名规则具有特殊性,不做有判定依据
  141. var family = doc.GetElement(elementId).GetFamily()?.Name;
  142. bool result = false;
  143. switch (type)
  144. {
  145. case "SpaceTag":
  146. result = true;
  147. break;
  148. case "Space":
  149. result = true;
  150. break;
  151. case "FamilyInstance":
  152. result = true;
  153. break;
  154. }
  155. return result;
  156. }
  157. }
  158. }