Program.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. //command = "DataCheck";
  39. path = @"E:\导出测试\testSpace.rvt";
  40. path = @"E:\导出测试\徐州系统图模型1127\冷源系统图模型第一版V1128 .rvt";
  41. JObject jObject=new JObject();
  42. jObject.Add("ResultFileName", @"C:\Users\SAGACLOUD\AppData\Local\RevitService\Result_e26be2fd-2097-462b-bdd0-a2a86b616928.txt");
  43. param = jObject.ToString();
  44. //path= @"E:\导出测试\延庆园-B1.rvt";
  45. //path = @"E:\导出测试\testR18.rvt";
  46. //path = @"E:\导出测试\testR16.rvt";
  47. }
  48. #endif
  49. if (string.IsNullOrEmpty(command))
  50. {
  51. Console.WriteLine("命令参数错误");
  52. }
  53. Console.WriteLine(command);
  54. Console.WriteLine(path);
  55. if (File.Exists(path) && Enum.TryParse(command, out CommandType commandType))
  56. {
  57. var app = RevitCoreContext.Instance.Application;
  58. var doc = app.OpenDocumentFile(path);
  59. string result = null;
  60. switch (commandType)
  61. {
  62. case CommandType.DataCheck:
  63. result=Check(doc,param);
  64. break;
  65. case CommandType.DataExport:
  66. result=Export(doc,param);
  67. break;
  68. }
  69. SaveResult(param, result);
  70. RevitCoreContext.Instance.Stop();
  71. }
  72. Console.WriteLine("ExportStart End");
  73. //强制退出
  74. Environment.Exit(0);
  75. }
  76. /// <summary>
  77. /// 保存检查或导出的结果
  78. /// </summary>
  79. /// <param name="param"></param>
  80. /// <param name="result"></param>
  81. /// <returns></returns>
  82. private static void SaveResult(string param, string result)
  83. {
  84. Console.WriteLine(param);
  85. try
  86. {
  87. JObject jObject = JObject.Parse(param);
  88. string key = "ResultFileName";
  89. string path = jObject[key].ToString();
  90. if (path.IsNotNullEmpty())
  91. {
  92. Console.WriteLine(path);
  93. var dir = Directory.GetParent(path);
  94. if(!dir.Exists)
  95. dir.Create();
  96. File.AppendAllText(path, result);
  97. }
  98. }
  99. catch (Exception e)
  100. {
  101. Console.WriteLine(e);
  102. }
  103. }
  104. enum CommandType
  105. {
  106. None = 0,
  107. DataCheck,
  108. DataExport
  109. }
  110. public static string Export(Document doc,string param)
  111. {
  112. string result = null;
  113. try
  114. {
  115. Console.WriteLine("Start Export");
  116. result=RevitToJBim.MbiExport.Export(doc,param);
  117. Console.WriteLine("End Export");
  118. }
  119. catch (Exception e)
  120. {
  121. Console.WriteLine("导出失败");
  122. Console.WriteLine(e.StackTrace);
  123. }
  124. return result;
  125. }
  126. public static string Check(Document doc,string param)
  127. {
  128. string result = null;
  129. try
  130. {
  131. Console.WriteLine("Start DataCheck");
  132. result=ServiceDataCheckTest.Check(doc);
  133. Console.WriteLine("End DataCheck");
  134. }
  135. catch (Exception e)
  136. {
  137. Console.WriteLine("导出失败");
  138. Console.WriteLine(e.StackTrace);
  139. }
  140. return result;
  141. }
  142. }
  143. }