Program.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 SAGA.DotNetUtils.Logger;
  8. using System.Configuration;
  9. using System.Diagnostics;
  10. //using RevitToJBim.Common;
  11. namespace ExportStart
  12. {
  13. class Program
  14. {
  15. static Program()
  16. {
  17. RevitCoreContext.Instance.Run();
  18. }
  19. [STAThread]
  20. static void Main(string[] args)
  21. {
  22. string command = null;
  23. string param = null;
  24. string path = null;
  25. if (args.Length > 0)
  26. {
  27. command = args[0];
  28. }
  29. if (args.Length > 1)
  30. {
  31. param = args[1];
  32. }
  33. if (args.Length > 2)
  34. {
  35. path = args[2];
  36. }
  37. #if DEBUG
  38. if (string.IsNullOrWhiteSpace(command))
  39. {
  40. command = "DataExport";
  41. //command = "DataCheck";
  42. //path = @"E:\导出测试\testSpace.rvt";
  43. //path = @"E:\导出测试\系统图修改编码版\消防系统图模型第一版V1219.rvt";
  44. //path = @"E:\导出测试\无锡系统图1225\热源系统图模型第一版V1225.rvt";
  45. //path = @"D:\给排水系统图模型第一版V1225.rvt";
  46. //path = @"E:\1245.rvt";
  47. //path = @"C:\Users\SAGACLOUD\Desktop\海天中心项目_AM_T1_21F .rvt";
  48. //path = @"D:\1.rvt";
  49. //path = @"testdata\嘉明\F1.rvt";
  50. //path = @"testdata\万达\LGWDGC-Q-F01-ALL.rvt";
  51. //path = @"testdata\龙岗万达2020.12.10\LG-WDGC-B02-ALL.rvt";
  52. //path = @"testdata\龙岗万达2020.12.10\LGWDGC-F01-ALL.rvt";
  53. //path = @"testdata\龙岗万达2020.12.10\LG-WDGC-F03-ALL.rvt";
  54. path = @"E:\LGWDGC-F01-ALL.rvt";
  55. //path = @"testdata\龙岗万达2020.12.10\LG-WDGC-F06-ALL.rvt";
  56. //path = @"testdata\龙岗万达2020.12.10\LGWDGC-Q-B01-ALL.rvt";
  57. //path = @"testdata\龙岗万达2020.12.10\LGWDGC-Q-F02-ALL.rvt";
  58. //path = @"testdata\龙岗万达2020.12.10\LGWDGC-Q-F05-ALL.rvt";
  59. //path = @"testdata\龙岗万达2020.12.10\LGWDGC-R0F-ALL.rvt";
  60. //path = @"testdata\万达\未通过\LG-WDGC-F03-ALL.rvt";
  61. //path = @"testdata\生命科学园\生命科学园2#地块201号楼B1模型文件v6.rvt";
  62. //path = @"testdata\亚投行\B1一期11.24.rvt";
  63. //path = @"testdata\前四项问题的对应模型\1#F4模型文件v5.rvt";
  64. //path = @"D:\安装包\CAD\测试模型\F4测试.rvt";
  65. JObject jObject = new JObject();
  66. //jObject.Add("ResultFileName", @"C:\Users\SAGACLOUD\AppData\Local\RevitService\Result_e26be2fd-2097-462b-bdd0-a2a86b616928.txt");
  67. jObject.Add("ResultFileName", @"D:\1.txt");
  68. param = jObject.ToString();
  69. //path= @"E:\导出测试\延庆园-B1.rvt";
  70. //path = @"E:\导出测试\testR18.rvt";
  71. //path = @"E:\导出测试\testR16.rvt";
  72. }
  73. #endif
  74. if (string.IsNullOrEmpty(command))
  75. {
  76. Log4Net.Debug("命令参数错误");
  77. }
  78. Log4Net.Info(command);
  79. Log4Net.Info(path);
  80. if (File.Exists(path) && Enum.TryParse(command, out CommandType commandType))
  81. {
  82. var app = RevitCoreContext.Instance.Application;
  83. string result = null;
  84. Console.WriteLine("Task StartTime:" + DateTime.Now);
  85. try
  86. {
  87. var doc = app.OpenDocumentFile(path);
  88. switch (commandType)
  89. {
  90. case CommandType.DataCheck:
  91. result = Check(doc, param);
  92. break;
  93. case CommandType.DataExport:
  94. result = Export(doc, param);
  95. break;
  96. }
  97. }
  98. catch (Exception e)
  99. {
  100. JObject errorJObject = new JObject();
  101. errorJObject.Add("ResultMsg", e.Message);
  102. errorJObject.Add("Result", "Failure");
  103. result = errorJObject.ToString();
  104. }
  105. SaveResult(param, result);
  106. Console.WriteLine("Task EndTime:" + DateTime.Now);
  107. RevitCoreContext.Instance.Stop();
  108. StartRevitSimplify(path);
  109. }
  110. //强制退出
  111. Environment.Exit(0);
  112. }
  113. /// <summary>
  114. /// 保存检查或导出的结果
  115. /// </summary>
  116. /// <param name="param"></param>
  117. /// <param name="result"></param>
  118. /// <returns></returns>
  119. private static void SaveResult(string param, string result)
  120. {
  121. try
  122. {
  123. JObject jObject = JObject.Parse(param);
  124. string key = "ResultFileName";
  125. string path = jObject[key].ToString();
  126. if (path.IsNotNullEmpty())
  127. {
  128. var dir = Directory.GetParent(path);
  129. if (!dir.Exists)
  130. dir.Create();
  131. File.AppendAllText(path, result);
  132. }
  133. }
  134. catch (Exception e)
  135. {
  136. Log4Net.Debug(e.StackTrace);
  137. }
  138. }
  139. static void StartRevitSimplify(string filePath)
  140. {
  141. var filename = Path.GetFileNameWithoutExtension(filePath);
  142. var project = ConfigurationManager.AppSettings["Project"];
  143. Console.WriteLine(DateTime.Now + " 准备执行轻量化:");
  144. string fullPath = ConfigurationManager.AppSettings["RevitSimplifyPath"];
  145. Process process = new Process();
  146. process.StartInfo.FileName = fullPath;//执行的exe路径
  147. process.StartInfo.WorkingDirectory = Path.GetDirectoryName(fullPath);
  148. process.StartInfo.UseShellExecute = true;
  149. process.StartInfo.CreateNoWindow = false;
  150. //process.StartInfo.RedirectStandardInput = true;//打开流输入
  151. //process.StartInfo.RedirectStandardOutput = true;//打开流输出
  152. //process.StartInfo.RedirectStandardError = true;//打开错误流
  153. process.StartInfo.Arguments = $" InputFile={filePath} OutputDir={project}/{filename}";
  154. process.Start();//执行
  155. process.WaitForExit();
  156. }
  157. enum CommandType
  158. {
  159. None = 0,
  160. DataCheck,
  161. DataExport
  162. }
  163. public static string Export(Document doc, string param)
  164. {
  165. string result = null;
  166. try
  167. {
  168. Console.WriteLine("Start Export");
  169. result = RevitToJBim.MbiExport.Export(doc, param);
  170. Console.WriteLine("End Export");
  171. }
  172. catch (Exception e)
  173. {
  174. Console.WriteLine("导出失败");
  175. Console.WriteLine(e.StackTrace);
  176. }
  177. return result;
  178. }
  179. public static string Check(Document doc, string param)
  180. {
  181. string result = null;
  182. try
  183. {
  184. Console.WriteLine("Start DataCheck");
  185. result = ServiceDataCheckTest.Check(doc);
  186. Console.WriteLine("End DataCheck");
  187. }
  188. catch (Exception e)
  189. {
  190. Console.WriteLine("导出失败");
  191. Console.WriteLine(e.StackTrace);
  192. }
  193. return result;
  194. }
  195. }
  196. }