DocExtend.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /* ==============================================================================
  2. * 功能描述:DocExtend
  3. * 创 建 者:Garrett
  4. * 创建日期:2018/6/11 16:00:53
  5. * ==============================================================================*/
  6. using System;
  7. using System.Collections.Generic;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Text.RegularExpressions;
  11. using Autodesk.Revit.DB;
  12. using SAGA.DotNetUtils;
  13. using SAGA.RevitUtils.Extends;
  14. namespace MBIUtility.Extend
  15. {
  16. /// <summary>
  17. /// DocExtend
  18. /// </summary>
  19. public static class DocExtend
  20. {
  21. public static readonly string MBIResourcePath = Path.Combine(AppBaseInfo.AppRunPath, "MBIResource");
  22. public static readonly string EmptyFilePath = Path.Combine(MBIResourcePath, "EmptyFile\\EmptyProject.rvt");
  23. /// <summary>
  24. /// 关闭文档
  25. /// </summary>
  26. /// <param name="path"></param>
  27. /// <returns></returns>
  28. public static bool CloseDoc(string path)
  29. {
  30. if (path == null || !File.Exists(path)) return false;
  31. bool result = true;
  32. try
  33. {
  34. //打开空文件并激活
  35. var placeholderFile = EmptyFilePath;
  36. var uiApp = ExternalDataWrapper.Current.UiApp;
  37. var docs = uiApp.Application.Documents;
  38. if (docs.IsEmpty) return result;
  39. if (uiApp.ActiveUIDocument != null && uiApp.ActiveUIDocument.Document.PathName == path)
  40. {
  41. var docCur = uiApp.ActiveUIDocument;
  42. uiApp.OpenAndActivateDocument(placeholderFile);
  43. result = docCur.SaveAndClose();
  44. }
  45. else
  46. {
  47. foreach (Document doc in docs)
  48. {
  49. if (doc.PathName == path)
  50. {
  51. //版本升级,保存
  52. var verionName = doc.Application.VersionName;
  53. var info = BasicFileInfo.Extract(doc.PathName);
  54. var savedInVersion = info.SavedInVersion; //<==Autodesk Revit 2014
  55. result = doc.Close(verionName != savedInVersion || doc.IsModified);
  56. }
  57. }
  58. }
  59. }
  60. catch (Exception e)
  61. {
  62. Console.WriteLine(e);
  63. }
  64. return result;
  65. }
  66. /// <summary>
  67. /// 关闭文档
  68. /// </summary>
  69. /// <param name="path"></param>
  70. /// <returns></returns>
  71. public static bool CloseDoc(this Document doc)
  72. {
  73. return CloseDoc(doc?.PathName);
  74. }
  75. /// <summary>
  76. /// 简单关闭模型不保存
  77. /// </summary>
  78. /// <param name="doc"></param>
  79. /// <returns></returns>
  80. public static bool CloseDocSimple(this Document doc)
  81. {
  82. bool result = true;
  83. var basePath = doc.PathName;
  84. var uiApp = ExternalDataWrapper.Current.UiApp;
  85. if (uiApp.ActiveUIDocument != null && uiApp.ActiveUIDocument.Document.PathName == basePath)
  86. {
  87. result= false;
  88. }
  89. else
  90. {
  91. result= doc.Close();
  92. }
  93. return result;
  94. }
  95. /// <summary>
  96. /// 由code获取FamilySymbol
  97. /// </summary>
  98. /// <param name="doc"></param>
  99. /// <param name="code"></param>
  100. /// <returns></returns>
  101. public static FamilySymbol GetFamilySymbol(this Document doc, string code)
  102. {
  103. Func<string, bool> filterFamily = (t) =>
  104. {
  105. return Regex.IsMatch(t, $@"{code}\s*-\s*\S*");
  106. };
  107. List<Family> listFamily = doc.GetFamilys();
  108. var fa = listFamily.FirstOrDefault(t => filterFamily(t.Name));
  109. List<FamilySymbol> symbolList = fa?.GetFamilySymbols();
  110. var fs= symbolList?.FirstOrDefault();
  111. if (fs!=null&&!fs.IsActive)
  112. fs.Activate();
  113. return fs;
  114. }
  115. }
  116. }