RevitCmdExecutor.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. namespace Client.Start
  14. {
  15. /// <summary>
  16. /// RevitCmdExecutor
  17. /// </summary>
  18. public class RevitCmdExecutor : IRevitCommandExcutor
  19. {
  20. public string ExecuteCmd(string revitCmd, string param, HashSet<string> filePathList)
  21. {
  22. string msg = null;
  23. try
  24. {
  25. //由revitCmd生成实体类
  26. //实体类传入参数、参考楼层列表
  27. //执行方法,返回执行结果
  28. Console.WriteLine(DateTime.Now + " 准备执行命令:" + revitCmd);
  29. string fullPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "StartVisionSelector.exe");
  30. Process process = new Process();//AppDomain.CurrentDomain.BaseDirectory +
  31. process.StartInfo.FileName = fullPath;//执行的exe路径
  32. process.StartInfo.UseShellExecute = false;//不显示shell
  33. process.StartInfo.CreateNoWindow = true;//不创建窗口
  34. process.StartInfo.RedirectStandardInput = true;//打开流输入
  35. process.StartInfo.RedirectStandardOutput = true;//打开流输出
  36. process.StartInfo.RedirectStandardError = true;//打开错误流
  37. string resultFilePath = GetReusltFileName();
  38. string newParam = AddResultFileNameToParam(param, resultFilePath);
  39. Console.WriteLine(filePathList.First());
  40. //输入参数,多个参数使用空间分割,如果一个参数包含空格,使用""包括此参数
  41. //注意Json的传入格式
  42. process.StartInfo.Arguments = revitCmd + " " +
  43. "\"" + newParam + "\"" + " " +
  44. "\"" + filePathList.First() + "\"";
  45. process.Start();//执行
  46. //string msg = process.StandardOutput.ReadToEnd();//读取输出
  47. process.WaitForExit(3*60*60*1000);//等待执行完成
  48. //process.Close();//结束
  49. if(!process.HasExited)
  50. process.Kill();
  51. msg = ReadResultString(resultFilePath);
  52. Console.WriteLine(DateTime.Now + " 命令执行完成:" + revitCmd);
  53. #if !DEBUG
  54. //执行完成后,删除文件
  55. foreach (var f in filePathList)
  56. {
  57. File.Delete(f);
  58. }
  59. #endif
  60. }
  61. catch (Exception e)
  62. {
  63. Console.WriteLine(e.Message + "\r\n" + e.StackTrace);
  64. }
  65. return msg;
  66. }
  67. /// <summary>
  68. /// 将检查结果的保存位置添加到参数中
  69. /// </summary>
  70. /// <param name="param"></param>
  71. /// <param name="fileName"></param>
  72. /// <returns></returns>
  73. private string AddResultFileNameToParam(string param, string filePath)
  74. {
  75. JObject jObject = null;
  76. if (string.IsNullOrEmpty(param))
  77. {
  78. jObject = new JObject();
  79. }
  80. else
  81. {
  82. jObject = JObject.Parse(param);
  83. };
  84. string key = "ResultFileName";
  85. jObject.Add(new JProperty(key, filePath));
  86. return jObject.ToString().Replace("\"", "\\\"");
  87. }
  88. /// <summary>
  89. /// 生成 结果的保存文件名
  90. /// </summary>
  91. /// <returns></returns>
  92. private string GetReusltFileName()
  93. {
  94. string localPath = System.Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
  95. string fileName = "Result_" + Guid.NewGuid().ToString() + ".txt";
  96. return Path.Combine(localPath, "RevitService", fileName);
  97. }
  98. /// <summary>
  99. /// 读取执行的结果,并清理结果文件
  100. /// </summary>
  101. /// <param name="filePath"></param>
  102. /// <returns></returns>
  103. private string ReadResultString(string filePath)
  104. {
  105. Console.WriteLine(filePath);
  106. string str = "";
  107. if (File.Exists(filePath))
  108. {
  109. str = File.ReadAllText(filePath);
  110. #if !DEBUG
  111. File.Delete(filePath);
  112. #endif
  113. }
  114. return str;
  115. }
  116. }
  117. }