/* ==============================================================================
* 功能描述:RevitCmdExecutor
* 创 建 者:Garrett
* 创建日期:2019/4/16 11:46:37
* ==============================================================================*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using Newtonsoft.Json.Linq;
using System.Windows.Forms;
using SAGA.DotNetUtils;
using ServiceMBI.Modes;
namespace Client.Start
{
///
/// RevitCmdExecutor
///
public class RevitCmdExecutor : IRevitCommandExcutor
{
public string ExecuteCmd(string revitCmd, string param, HashSet filePathList)
{
//由revitCmd生成实体类
//实体类传入参数、参考楼层列表
//执行方法,返回执行结果
Console.WriteLine("准备执行命令");
IServiceCommand command = GetServiceCommand(revitCmd);
if (command == null) return $"命令 {revitCmd} 解析异常";
CustomMessage addMessage = null;
string combineParam = AddFilePathParam(param, filePathList, ref addMessage);
if (addMessage?.Result == false) return addMessage.ResultMsg;
string msg = "";
command.Execute(combineParam, ref msg);
return msg;
}
///
/// 由Command的字符串转化为相应的命令
///
///
///
private IServiceCommand GetServiceCommand(string revitCmd)
{
IServiceCommand command = null;
string dllName = "ServiceMBI.dll";
string className = "ServiceMBI.Commands." + revitCmd;
string path = Path.Combine(AppBaseInfo.DllRunPath, dllName);
Console.WriteLine(path);
if (File.Exists(path))
{
Assembly tempAsembly = Assembly.LoadFrom(path);
command = (tempAsembly.CreateInstance(className)) as IServiceCommand;
}
return command;
}
///
/// 将ReferFloors参数,添加至参数列表中
///
///
///
///
///
private string AddFilePathParam(string param, HashSet filePathList, ref CustomMessage message)
{
string floorskey = ServiceMBIBuiltInParameter.ReferFloors;
message = new CustomMessage(true, "Add Success");
try
{
JObject jObject = JObject.Parse(param);
jObject.Add(floorskey, new JArray((filePathList ?? new HashSet()).ToArray()));
return jObject.ToString();
}
catch (Exception e)
{
Console.WriteLine(e);
message = new CustomMessage(false, "添加ReferFloors参数异常");
}
return null;
}
}
}