Program.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. using SAGA.DotNetUtils.Revit;
  2. using System;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Text.RegularExpressions;
  6. using Newtonsoft.Json.Linq;
  7. using SAGA.DotNetUtils.Extend;
  8. using System.Threading;
  9. namespace StartVisionSelector
  10. {
  11. public class Program
  12. {
  13. public static void Main(string[] args)
  14. {
  15. string command = null;
  16. string param = null;
  17. string path = null;
  18. if (args.Length > 0)
  19. {
  20. command = args[0];
  21. }
  22. if (args.Length > 1)
  23. {
  24. //注意,这个Replace很重要,不能缺少,
  25. param = args[1].Replace("\"", "\\\""); ;
  26. }
  27. if (args.Length > 2)
  28. {
  29. path = args[2];
  30. }
  31. #if DEBUG
  32. if (string.IsNullOrWhiteSpace(command))
  33. {
  34. command = "DataExport";
  35. path = @"E:\导出测试\testR17.rvt";
  36. path = @"E:\导出测试\TestEmptyR17.rvt";
  37. JObject jObject = new JObject();
  38. jObject.Add("ResultFileName", @"C:\Users\SAGACLOUD\AppData\Local\RevitService\Result_e26be2fd-2097-462b-bdd0-a2a86b616928.txt");
  39. param = jObject.ToString().Replace("\"","\\\"");
  40. //JObject jObject = new JObject();
  41. //jObject.Add("ResultFileName", @"D:\abc.txt");
  42. //param = jObject.ToString();
  43. //path= @"E:\导出测试\延庆园-B1.rvt";
  44. //path = @"E:\导出测试\testR18.rvt";
  45. //path = @"E:\导出测试\testR16.rvt";
  46. }
  47. #endif
  48. Console.WriteLine("start");
  49. #region 保存版本号
  50. //保存版本号
  51. var runPath = AppDomain.CurrentDomain.BaseDirectory;
  52. string revitVision = null;
  53. try
  54. {
  55. revitVision = RevitVisionUtil.GetRevitVision(path);
  56. if (string.IsNullOrEmpty(revitVision))
  57. {
  58. SaveFileErrorResult(param);
  59. return;
  60. }
  61. }
  62. catch (Exception e)
  63. {
  64. SaveFileErrorResult(param);
  65. return;
  66. }
  67. string destination = GetObjectiveFilePath(param);
  68. //File.WriteAllText(@"d:\error.txt", destination);
  69. File.WriteAllText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, SelectorConst.RevitFileVisionFile), revitVision);
  70. #endregion
  71. #region 删除生成的Revit dll
  72. var loadingRevitPath = Path.Combine(runPath, SelectorConst.LoadingRevit);
  73. //先删除,后Copy,如果可以指定Revit的安装目录,不需要Copy
  74. #region Del RevitDll
  75. foreach (string loadingItem in File.ReadAllLines(loadingRevitPath))
  76. {
  77. string destFile = Path.Combine(runPath, $"{loadingItem}.dll");
  78. if(File.Exists(destFile))
  79. File.Delete(destFile);
  80. }
  81. #endregion
  82. #endregion
  83. #region 启动
  84. Process process = new Process();//AppDomain.CurrentDomain.BaseDirectory +
  85. process.StartInfo.FileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ExportStart.exe");//执行的exe路径
  86. process.StartInfo.UseShellExecute = false;//不显示shell
  87. process.StartInfo.CreateNoWindow = true;//不创建窗口
  88. process.StartInfo.RedirectStandardInput = true;//打开流输入
  89. process.StartInfo.RedirectStandardOutput = true;//打开流输出
  90. process.StartInfo.RedirectStandardError = true;//打开错误流
  91. process.StartInfo.Arguments = command + " "+
  92. "\"" + param + "\"" + " "+
  93. "\"" + path + "\"";//输入参数,多个参数使用空间分割,如果一个参数包含空格,使用""包括此参数
  94. process.Start();//执行
  95. //process.WaitForExit(60*60*1000);//等待执行完成
  96. // process.WaitForExit();
  97. try
  98. {
  99. while (process != null && !process.HasExited)
  100. {
  101. process.WaitForExit(60 * 1000); //等待执行完成
  102. if (!process.HasExited) // && File.Exists(destination)
  103. {
  104. Thread.Sleep(5000);
  105. if (process != null && !process.HasExited)
  106. {
  107. process.Close();
  108. process = null;
  109. //process.Kill();
  110. //process.Dispose();
  111. }
  112. }
  113. }
  114. }
  115. catch (Exception ex)
  116. {
  117. File.AppendAllText(@"e:\error.txt", ex.Message + ex.StackTrace);
  118. Console.WriteLine(ex.Message + "\r\n" + ex.StackTrace);
  119. }
  120. // string msg = process.StandardOutput.ReadToEnd();//读取输出
  121. // Console.WriteLine(msg);
  122. #endregion
  123. Console.WriteLine("end");
  124. //强制退出
  125. Environment.Exit(0);
  126. //Console.ReadKey();
  127. }
  128. private static string GetObjectiveFilePath(string param)
  129. {
  130. Console.WriteLine(param);
  131. string path = string.Empty;
  132. try
  133. {
  134. var tempParam = param.Replace("\\\"", "\"");
  135. JObject jObject = JObject.Parse(tempParam);
  136. string key = @"ResultFileName";
  137. path = jObject[key].ToString();
  138. }
  139. catch (Exception e)
  140. {
  141. Console.WriteLine(e);
  142. }
  143. return path;
  144. }
  145. /// <summary>
  146. /// 保存检查或导出的结果
  147. /// </summary>
  148. /// <param name="param"></param>
  149. /// <param name="result"></param>
  150. /// <returns></returns>
  151. private static void SaveFileErrorResult(string param)
  152. {
  153. Console.WriteLine(param);
  154. try
  155. {
  156. var tempParam = param.Replace("\\\"", "\"");
  157. JObject jObject = JObject.Parse(tempParam);
  158. string key = @"ResultFileName";
  159. string path = jObject[key].ToString();
  160. if (path.IsNotNullEmpty())
  161. {
  162. Console.WriteLine(path);
  163. var dir = Directory.GetParent(path);
  164. if (!dir.Exists)
  165. dir.Create();
  166. JObject jResult=new JObject();
  167. jResult.Add("Result", "Failure");
  168. jResult.Add("ResultMsg","文件格式错误或文件打开失败");
  169. File.AppendAllText(path, jResult.ToString());
  170. }
  171. }
  172. catch (Exception e)
  173. {
  174. Console.WriteLine(e);
  175. }
  176. }
  177. }
  178. }