using SAGA.DotNetUtils.Revit;
using System;
using System.Diagnostics;
using System.IO;
using System.Text.RegularExpressions;
using Newtonsoft.Json.Linq;
using SAGA.DotNetUtils.Extend;
using System.Threading;
namespace StartVisionSelector
{
public class Program
{
public static void Main(string[] args)
{
string command = null;
string param = null;
string path = null;
if (args.Length > 0)
{
command = args[0];
}
if (args.Length > 1)
{
//注意,这个Replace很重要,不能缺少,
param = args[1].Replace("\"", "\\\""); ;
}
if (args.Length > 2)
{
path = args[2];
}
#if DEBUG
if (string.IsNullOrWhiteSpace(command))
{
command = "DataExport";
path = @"E:\导出测试\testR17.rvt";
path = @"E:\导出测试\TestEmptyR17.rvt";
JObject jObject = new JObject();
jObject.Add("ResultFileName", @"C:\Users\SAGACLOUD\AppData\Local\RevitService\Result_e26be2fd-2097-462b-bdd0-a2a86b616928.txt");
param = jObject.ToString().Replace("\"","\\\"");
//JObject jObject = new JObject();
//jObject.Add("ResultFileName", @"D:\abc.txt");
//param = jObject.ToString();
//path= @"E:\导出测试\延庆园-B1.rvt";
//path = @"E:\导出测试\testR18.rvt";
//path = @"E:\导出测试\testR16.rvt";
}
#endif
Console.WriteLine("start");
#region 保存版本号
//保存版本号
var runPath = AppDomain.CurrentDomain.BaseDirectory;
string revitVision = null;
try
{
revitVision = RevitVisionUtil.GetRevitVision(path);
if (string.IsNullOrEmpty(revitVision))
{
SaveFileErrorResult(param);
return;
}
}
catch (Exception e)
{
SaveFileErrorResult(param);
return;
}
string destination = GetObjectiveFilePath(param);
//File.WriteAllText(@"d:\error.txt", destination);
File.WriteAllText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, SelectorConst.RevitFileVisionFile), revitVision);
#endregion
#region 删除生成的Revit dll
var loadingRevitPath = Path.Combine(runPath, SelectorConst.LoadingRevit);
//先删除,后Copy,如果可以指定Revit的安装目录,不需要Copy
#region Del RevitDll
foreach (string loadingItem in File.ReadAllLines(loadingRevitPath))
{
string destFile = Path.Combine(runPath, $"{loadingItem}.dll");
if(File.Exists(destFile))
File.Delete(destFile);
}
#endregion
#endregion
#region 启动
Process process = new Process();//AppDomain.CurrentDomain.BaseDirectory +
process.StartInfo.FileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "ExportStart.exe");//执行的exe路径
process.StartInfo.UseShellExecute = false;//不显示shell
process.StartInfo.CreateNoWindow = true;//不创建窗口
process.StartInfo.RedirectStandardInput = true;//打开流输入
process.StartInfo.RedirectStandardOutput = true;//打开流输出
process.StartInfo.RedirectStandardError = true;//打开错误流
process.StartInfo.Arguments = command + " "+
"\"" + param + "\"" + " "+
"\"" + path + "\"";//输入参数,多个参数使用空间分割,如果一个参数包含空格,使用""包括此参数
process.Start();//执行
//process.WaitForExit(60*60*1000);//等待执行完成
// process.WaitForExit();
try
{
while (process != null && !process.HasExited)
{
process.WaitForExit(60 * 1000); //等待执行完成
if (!process.HasExited) // && File.Exists(destination)
{
Thread.Sleep(5000);
if (process != null && !process.HasExited)
{
process.Close();
process = null;
//process.Kill();
//process.Dispose();
}
}
}
}
catch (Exception ex)
{
File.AppendAllText(@"e:\error.txt", ex.Message + ex.StackTrace);
Console.WriteLine(ex.Message + "\r\n" + ex.StackTrace);
}
// string msg = process.StandardOutput.ReadToEnd();//读取输出
// Console.WriteLine(msg);
#endregion
Console.WriteLine("end");
//强制退出
Environment.Exit(0);
//Console.ReadKey();
}
private static string GetObjectiveFilePath(string param)
{
Console.WriteLine(param);
string path = string.Empty;
try
{
var tempParam = param.Replace("\\\"", "\"");
JObject jObject = JObject.Parse(tempParam);
string key = @"ResultFileName";
path = jObject[key].ToString();
}
catch (Exception e)
{
Console.WriteLine(e);
}
return path;
}
///
/// 保存检查或导出的结果
///
///
///
///
private static void SaveFileErrorResult(string param)
{
Console.WriteLine(param);
try
{
var tempParam = param.Replace("\\\"", "\"");
JObject jObject = JObject.Parse(tempParam);
string key = @"ResultFileName";
string path = jObject[key].ToString();
if (path.IsNotNullEmpty())
{
Console.WriteLine(path);
var dir = Directory.GetParent(path);
if (!dir.Exists)
dir.Create();
JObject jResult=new JObject();
jResult.Add("Result", "Failure");
jResult.Add("ResultMsg","文件格式错误或文件打开失败");
File.AppendAllText(path, jResult.ToString());
}
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
}
}