Program.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. using Autodesk.Revit.DB;
  2. using ServiceRevitLib;
  3. using System;
  4. using System.IO;
  5. using Newtonsoft.Json.Linq;
  6. using SAGA.DotNetUtils.Extend;
  7. //using RevitToJBim.Common;
  8. namespace ExportStart
  9. {
  10. class Program
  11. {
  12. static Program()
  13. {
  14. RevitCoreContext.Instance.Run();
  15. }
  16. [STAThread]
  17. static void Main(string[] args)
  18. {
  19. string command = null;
  20. string param = null;
  21. string path = null;
  22. if (args.Length > 0)
  23. {
  24. command = args[0];
  25. }
  26. if (args.Length > 1)
  27. {
  28. param = args[1];
  29. }
  30. if (args.Length > 2)
  31. {
  32. path = args[2];
  33. }
  34. #if DEBUG
  35. if (string.IsNullOrWhiteSpace(command))
  36. {
  37. command = "DataExport";
  38. path = @"E:\导出测试\testR17.rvt";
  39. path = @"E:\导出测试\延庆园-B1.rvt";
  40. JObject jObject=new JObject();
  41. jObject.Add("ResultFileName", @"C:\Users\SAGACLOUD\AppData\Local\RevitService\Result_e26be2fd-2097-462b-bdd0-a2a86b616928.txt");
  42. param = jObject.ToString();
  43. //path= @"E:\导出测试\延庆园-B1.rvt";
  44. //path = @"E:\导出测试\testR18.rvt";
  45. //path = @"E:\导出测试\testR16.rvt";
  46. }
  47. #endif
  48. if (string.IsNullOrEmpty(command))
  49. {
  50. Console.WriteLine("命令参数错误");
  51. }
  52. Console.WriteLine(command);
  53. Console.WriteLine(path);
  54. if (File.Exists(path) && Enum.TryParse(command, out CommandType commandType))
  55. {
  56. var app = RevitCoreContext.Instance.Application;
  57. var doc = app.OpenDocumentFile(path);
  58. string result = null;
  59. switch (commandType)
  60. {
  61. case CommandType.DataCheck:
  62. result=Check(doc,param);
  63. break;
  64. case CommandType.DataExport:
  65. result=Export(doc,param);
  66. break;
  67. }
  68. SaveResult(param, result);
  69. RevitCoreContext.Instance.Stop();
  70. }
  71. Console.WriteLine("ExportStart End");
  72. }
  73. /// <summary>
  74. /// 保存检查或导出的结果
  75. /// </summary>
  76. /// <param name="param"></param>
  77. /// <param name="result"></param>
  78. /// <returns></returns>
  79. private static void SaveResult(string param, string result)
  80. {
  81. Console.WriteLine(param);
  82. try
  83. {
  84. JObject jObject = JObject.Parse(param);
  85. string key = "ResultFileName";
  86. string path = jObject[key].ToString();
  87. if (path.IsNotNullEmpty())
  88. {
  89. Console.WriteLine(path);
  90. var dir = Directory.GetParent(path);
  91. if(!dir.Exists)
  92. dir.Create();
  93. File.AppendAllText(path, result);
  94. }
  95. }
  96. catch (Exception e)
  97. {
  98. Console.WriteLine(e);
  99. }
  100. }
  101. enum CommandType
  102. {
  103. None = 0,
  104. DataCheck,
  105. DataExport
  106. }
  107. public static string Export(Document doc,string param)
  108. {
  109. string result = null;
  110. try
  111. {
  112. Console.WriteLine("Start Export");
  113. result=RevitToJBim.MbiExport.Export(doc,param);
  114. Console.WriteLine("End Export");
  115. }
  116. catch (Exception e)
  117. {
  118. Console.WriteLine("导出失败");
  119. Console.WriteLine(e.StackTrace);
  120. }
  121. return result;
  122. }
  123. public static string Check(Document doc,string param)
  124. {
  125. string result = null;
  126. try
  127. {
  128. Console.WriteLine("Start DataCheck");
  129. result=ServiceDataCheckTest.Check(doc);
  130. Console.WriteLine("End DataCheck");
  131. }
  132. catch (Exception e)
  133. {
  134. Console.WriteLine("导出失败");
  135. Console.WriteLine(e.StackTrace);
  136. }
  137. return result;
  138. }
  139. }
  140. }