Program.cs 5.5 KB

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