123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
-
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.IO;
- using System.Linq;
- using System.Windows.Forms;
- using Newtonsoft.Json.Linq;
- namespace Client.Start
- {
-
-
-
- public class RevitCmdExecutor : IRevitCommandExcutor
- {
- public string ExecuteCmd(string revitCmd, string param, HashSet<string> filePathList)
- {
- string msg = null;
- try
- {
-
-
-
- Console.WriteLine(DateTime.Now + " 准备执行命令:" + revitCmd);
- string fullPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "StartVisionSelector.exe");
- Process process = new Process();
- process.StartInfo.FileName = fullPath;
- process.StartInfo.UseShellExecute = false;
- process.StartInfo.CreateNoWindow = true;
- process.StartInfo.RedirectStandardInput = true;
- process.StartInfo.RedirectStandardOutput = true;
- process.StartInfo.RedirectStandardError = true;
- string resultFilePath = GetReusltFileName();
- string newParam = AddResultFileNameToParam(param, resultFilePath);
-
-
- process.StartInfo.Arguments = revitCmd + " " +
- "\"" + newParam + "\"" + " " +
- "\"" + filePathList.First() + "\"";
- process.Start();
-
- process.WaitForExit();
- process.Close();
- msg = ReadResultString(resultFilePath);
-
- Console.WriteLine(DateTime.Now + " 命令执行完成:" + revitCmd);
- #if !DEBUG
-
- foreach (var f in filePathList)
- {
- File.Delete(f);
- }
- #endif
- }
- catch (Exception e)
- {
- Console.WriteLine(e.Message + "\r\n" + e.StackTrace);
- }
- return msg;
- }
-
-
-
-
-
-
- private string AddResultFileNameToParam(string param, string filePath)
- {
- JObject jObject = null;
- if (string.IsNullOrEmpty(param))
- {
- jObject = new JObject();
- }
- else
- {
- jObject = JObject.Parse(param);
- };
- string key = "ResultFileName";
- jObject.Add(new JProperty(key, filePath));
- return jObject.ToString().Replace("\"", "\\\"");
- }
-
-
-
-
- private string GetReusltFileName()
- {
- string localPath = System.Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
- string fileName = "Result_" + Guid.NewGuid().ToString() + ".txt";
- return Path.Combine(localPath, "RevitService", fileName);
- }
-
-
-
-
-
- private string ReadResultString(string filePath)
- {
- Console.WriteLine(filePath);
- string str = "";
- if (File.Exists(filePath))
- {
- str = File.ReadAllText(filePath);
- #if !DEBUG
- File.Delete(filePath);
- #endif
- }
- return str;
- }
- }
- }
|