RevitCmdExecutor.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /* ==============================================================================
  2. * 功能描述:RevitCmdExecutor
  3. * 创 建 者:Garrett
  4. * 创建日期:2019/4/16 11:46:37
  5. * ==============================================================================*/
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Diagnostics;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Windows.Forms;
  12. using Newtonsoft.Json.Linq;
  13. using System.Threading;
  14. namespace Client.Start
  15. {
  16. /// <summary>
  17. /// RevitCmdExecutor
  18. /// </summary>
  19. public class RevitCmdExecutor : IRevitCommandExcutor
  20. {
  21. public string ExecuteCmd(string revitCmd, string param, HashSet<string> filePathList, string taskId)
  22. {
  23. string msg = null;
  24. try
  25. {
  26. //由revitCmd生成实体类
  27. //实体类传入参数、参考楼层列表
  28. //执行方法,返回执行结果
  29. Console.WriteLine(DateTime.Now + " 准备执行命令:" + revitCmd);
  30. string fullPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "StartVisionSelector.exe");
  31. Process process = new Process();//AppDomain.CurrentDomain.BaseDirectory +
  32. process.StartInfo.FileName = fullPath;//执行的exe路径
  33. process.StartInfo.UseShellExecute = false;//不显示shell
  34. process.StartInfo.CreateNoWindow = true;//不创建窗口
  35. process.StartInfo.RedirectStandardInput = true;//打开流输入
  36. process.StartInfo.RedirectStandardOutput = true;//打开流输出
  37. process.StartInfo.RedirectStandardError = true;//打开错误流
  38. string resultFilePath = GetReusltFileName(taskId, revitCmd);
  39. string newParam = AddResultFileNameToParam(param, resultFilePath);
  40. Console.WriteLine(filePathList.First());
  41. //输入参数,多个参数使用空间分割,如果一个参数包含空格,使用""包括此参数
  42. //注意Json的传入格式
  43. process.StartInfo.Arguments = revitCmd + " " +
  44. "\"" + newParam + "\"" + " " +
  45. "\"" + filePathList.First() + "\"";
  46. process.Start();//执行
  47. process.WaitForExit();
  48. //string msg = process.StandardOutput.ReadToEnd();//读取输出
  49. //try
  50. //{
  51. // while (process != null && !process.HasExited)
  52. // {
  53. // process.WaitForExit(60 * 1000); //等待执行完成
  54. // if (!process.HasExited && File.Exists(resultFilePath))
  55. // {
  56. // Thread.Sleep(5000);
  57. // if (process != null && !process.HasExited)
  58. // {
  59. // //process.Close();
  60. // //process = null;
  61. // //process.Kill();
  62. // //process.Dispose();
  63. // }
  64. // }
  65. // }
  66. //}
  67. //catch(Exception ex)
  68. //{
  69. // Console.WriteLine(ex.Message + "\r\n" + ex.StackTrace);
  70. //}
  71. msg = ReadResultString(resultFilePath);
  72. Console.WriteLine(DateTime.Now + " 命令执行完成:" + revitCmd);
  73. #if !DEBUG
  74. //执行完成后,删除文件
  75. foreach (var f in filePathList)
  76. {
  77. File.Delete(f);
  78. }
  79. #endif
  80. }
  81. catch (Exception e)
  82. {
  83. Console.WriteLine(e.Message + "\r\n" + e.StackTrace);
  84. }
  85. return msg;
  86. }
  87. /// <summary>
  88. /// 将检查结果的保存位置添加到参数中
  89. /// </summary>
  90. /// <param name="param"></param>
  91. /// <param name="fileName"></param>
  92. /// <returns></returns>
  93. private string AddResultFileNameToParam(string param, string filePath)
  94. {
  95. JObject jObject = null;
  96. if (string.IsNullOrEmpty(param))
  97. {
  98. jObject = new JObject();
  99. }
  100. else
  101. {
  102. jObject = JObject.Parse(param);
  103. };
  104. string key = "ResultFileName";
  105. jObject.Add(new JProperty(key, filePath));
  106. return jObject.ToString().Replace("\"", "\\\"");
  107. }
  108. /// <summary>
  109. /// 生成 结果的保存文件名
  110. /// </summary>
  111. /// <returns></returns>
  112. private string GetReusltFileName(string taskId, string revitCmd)
  113. {
  114. string localPath = System.Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
  115. // string fileName = "Result_" + Guid.NewGuid().ToString() + ".txt";
  116. string fileName = "Result_" + taskId + revitCmd + ".txt";
  117. return Path.Combine(localPath, "RevitService", fileName);
  118. }
  119. /// <summary>
  120. /// 读取执行的结果,并清理结果文件
  121. /// </summary>
  122. /// <param name="filePath"></param>
  123. /// <returns></returns>
  124. private string ReadResultString(string filePath)
  125. {
  126. Console.WriteLine(filePath);
  127. string str = "";
  128. Console.WriteLine("read file try times: ");
  129. int retryTimes = 0;
  130. while (!File.Exists(filePath) && retryTimes < 30)
  131. {
  132. Thread.Sleep(10000);
  133. retryTimes++;
  134. }
  135. if (File.Exists(filePath))
  136. {
  137. retryTimes = 0;
  138. do
  139. {
  140. Thread.Sleep(3000);
  141. str = File.ReadAllText(filePath);
  142. retryTimes++;
  143. Console.WriteLine("read file try times: " + retryTimes);
  144. } while ((str == null || str.Length == 0) && retryTimes < 100);
  145. #if !DEBUG
  146. File.Delete(filePath);
  147. #endif
  148. }
  149. Console.WriteLine("read file try times: ");
  150. return str;
  151. }
  152. }
  153. }