RevitCmdExecutor.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. Thread.Sleep(15000);
  72. msg = ReadResultString(resultFilePath);
  73. Console.WriteLine(DateTime.Now + " 命令执行完成:" + revitCmd);
  74. #if !DEBUG
  75. //执行完成后,删除文件
  76. foreach (var f in filePathList)
  77. {
  78. File.Delete(f);
  79. }
  80. #endif
  81. }
  82. catch (Exception e)
  83. {
  84. Console.WriteLine(e.Message + "\r\n" + e.StackTrace);
  85. }
  86. return msg;
  87. }
  88. /// <summary>
  89. /// 将检查结果的保存位置添加到参数中
  90. /// </summary>
  91. /// <param name="param"></param>
  92. /// <param name="fileName"></param>
  93. /// <returns></returns>
  94. private string AddResultFileNameToParam(string param, string filePath)
  95. {
  96. JObject jObject = null;
  97. if (string.IsNullOrEmpty(param))
  98. {
  99. jObject = new JObject();
  100. }
  101. else
  102. {
  103. jObject = JObject.Parse(param);
  104. };
  105. string key = "ResultFileName";
  106. jObject.Add(new JProperty(key, filePath));
  107. return jObject.ToString().Replace("\"", "\\\"");
  108. }
  109. /// <summary>
  110. /// 生成 结果的保存文件名
  111. /// </summary>
  112. /// <returns></returns>
  113. private string GetReusltFileName(string taskId, string revitCmd)
  114. {
  115. string localPath = System.Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
  116. // string fileName = "Result_" + Guid.NewGuid().ToString() + ".txt";
  117. string fileName = "Result_" + taskId + revitCmd + ".txt";
  118. return Path.Combine(localPath, "RevitService", fileName);
  119. }
  120. /// <summary>
  121. /// 读取执行的结果,并清理结果文件
  122. /// </summary>
  123. /// <param name="filePath"></param>
  124. /// <returns></returns>
  125. private string ReadResultString(string filePath)
  126. {
  127. Console.WriteLine(filePath);
  128. string str = "";
  129. if (File.Exists(filePath))
  130. {
  131. str = File.ReadAllText(filePath);
  132. #if !DEBUG
  133. File.Delete(filePath);
  134. #endif
  135. }
  136. return str;
  137. }
  138. }
  139. }