Program.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. File.WriteAllText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, SelectorConst.RevitFileVisionFile), revitVision);
  68. #endregion
  69. #region 删除生成的Revit dll
  70. var loadingRevitPath = Path.Combine(runPath, SelectorConst.LoadingRevit);
  71. //先删除,后Copy,如果可以指定Revit的安装目录,不需要Copy
  72. #region Del RevitDll
  73. foreach (string loadingItem in File.ReadAllLines(loadingRevitPath))
  74. {
  75. string destFile = Path.Combine(runPath, $"{loadingItem}.dll");
  76. if(File.Exists(destFile))
  77. File.Delete(destFile);
  78. }
  79. #endregion
  80. #endregion
  81. #region 启动
  82. Process process = new Process();//AppDomain.CurrentDomain.BaseDirectory +
  83. process.StartInfo.FileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ExportStart.exe");//执行的exe路径
  84. process.StartInfo.UseShellExecute = false;//不显示shell
  85. process.StartInfo.CreateNoWindow = true;//不创建窗口
  86. process.StartInfo.RedirectStandardInput = true;//打开流输入
  87. process.StartInfo.RedirectStandardOutput = true;//打开流输出
  88. process.StartInfo.RedirectStandardError = true;//打开错误流
  89. process.StartInfo.Arguments = command + " "+
  90. "\"" + param + "\"" + " "+
  91. "\"" + path + "\"";//输入参数,多个参数使用空间分割,如果一个参数包含空格,使用""包括此参数
  92. process.Start();//执行
  93. //process.WaitForExit(60*60*1000);//等待执行完成
  94. // process.WaitForExit();
  95. try
  96. {
  97. while (process != null && !process.HasExited)
  98. {
  99. process.WaitForExit(60 * 1000); //等待执行完成
  100. if (!process.HasExited && File.Exists(path))
  101. {
  102. Thread.Sleep(5000);
  103. if (process != null && !process.HasExited)
  104. {
  105. process.Close();
  106. process = null;
  107. //process.Kill();
  108. //process.Dispose();
  109. }
  110. }
  111. }
  112. }
  113. catch (Exception ex)
  114. {
  115. File.AppendAllText(@"d:\error.txt", ex.Message + ex.StackTrace);
  116. Console.WriteLine(ex.Message + "\r\n" + ex.StackTrace);
  117. }
  118. // string msg = process.StandardOutput.ReadToEnd();//读取输出
  119. // Console.WriteLine(msg);
  120. #endregion
  121. Console.WriteLine("end");
  122. //强制退出
  123. Environment.Exit(0);
  124. //Console.ReadKey();
  125. }
  126. /// <summary>
  127. /// 保存检查或导出的结果
  128. /// </summary>
  129. /// <param name="param"></param>
  130. /// <param name="result"></param>
  131. /// <returns></returns>
  132. private static void SaveFileErrorResult(string param)
  133. {
  134. Console.WriteLine(param);
  135. try
  136. {
  137. var tempParam = param.Replace("\\\"", "\"");
  138. JObject jObject = JObject.Parse(tempParam);
  139. string key = @"ResultFileName";
  140. string path = jObject[key].ToString();
  141. if (path.IsNotNullEmpty())
  142. {
  143. Console.WriteLine(path);
  144. var dir = Directory.GetParent(path);
  145. if (!dir.Exists)
  146. dir.Create();
  147. JObject jResult=new JObject();
  148. jResult.Add("Result", "Failure");
  149. jResult.Add("ResultMsg","文件格式错误或文件打开失败");
  150. File.AppendAllText(path, jResult.ToString());
  151. }
  152. }
  153. catch (Exception e)
  154. {
  155. Console.WriteLine(e);
  156. }
  157. }
  158. }
  159. }