RevitFileSwitch.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*-------------------------------------------------------------------------
  2. * 功能描述:FileSwitch
  3. * 作者:xulisong
  4. * 创建时间: 2019/5/28 10:31:29
  5. * 版本号:v1.0
  6. * -------------------------------------------------------------------------*/
  7. using Autodesk.Revit.DB;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.IO;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Threading.Tasks;
  14. namespace FWindSoft.Revit
  15. {
  16. /// <summary>
  17. /// 文件状态
  18. /// </summary>
  19. [Flags]
  20. public enum DocumentState
  21. {
  22. /// <summary>
  23. /// 不可用
  24. /// </summary>
  25. Invalid=0,
  26. /// <summary>
  27. /// 操作前,已经打开
  28. /// </summary>
  29. Exist=1,
  30. /// <summary>
  31. /// 操作时,实时打开
  32. /// </summary>
  33. New=2,
  34. }
  35. /// <summary>
  36. /// revit 文件开关
  37. /// </summary>
  38. public class RevitFileSwitch
  39. {
  40. public RevitFileSwitch(string path)
  41. {
  42. Path = path;
  43. State = DocumentState.Invalid;
  44. }
  45. /// <summary>
  46. /// 文件路径
  47. /// </summary>
  48. public string Path { get; private set; }
  49. /// <summary>
  50. /// 关联revit文件
  51. /// </summary>
  52. public Document Document { get; private set; }
  53. /// <summary>
  54. /// 文件状态
  55. /// </summary>
  56. public DocumentState State { get; private set; }
  57. /// <summary>
  58. /// 打开模型
  59. /// </summary>
  60. public void On()
  61. {
  62. if (this.Document != null)
  63. {
  64. return;
  65. }
  66. string path = Path;
  67. var sets = RevitCore.App.Documents;
  68. if (Path == null || !File.Exists(path))
  69. {
  70. return;
  71. }
  72. foreach (Document document in sets)
  73. {
  74. if (document.PathName == path)
  75. {
  76. Document = document;
  77. State = DocumentState.Exist;
  78. break;
  79. }
  80. }
  81. if (Document == null)
  82. {
  83. Document = RevitCore.App.OpenDocumentFile(path);
  84. State = DocumentState.New;
  85. }
  86. }
  87. /// <summary>
  88. /// 关闭模型
  89. /// </summary>
  90. public void Off()
  91. {
  92. Document?.Close();
  93. }
  94. /// <summary>
  95. /// 关闭新状态的模型
  96. /// </summary>
  97. public void OffNew()
  98. {
  99. if (DocumentState.New == State)
  100. {
  101. Document?.Close();
  102. }
  103. }
  104. }
  105. }