93 lines
3.7 KiB
C#
93 lines
3.7 KiB
C#
using System.Reflection;
|
|
|
|
using Autodesk.Revit.UI;
|
|
|
|
using Microsoft.CodeAnalysis.CSharp.Scripting;
|
|
using Microsoft.CodeAnalysis.CSharp.Scripting.Hosting;
|
|
using Microsoft.CodeAnalysis.Scripting;
|
|
|
|
namespace Szmedi.AIScriptRunner.RvScript
|
|
{
|
|
public class ScriptRunnerHandler : IExternalEventHandler
|
|
{
|
|
public string ScriptText { get; internal set; }
|
|
|
|
public string ScriptResult { get; private set; }
|
|
|
|
public RoslynEditorViewModel RoslynEditorViewModel { get; set; }
|
|
public IProgress<string> Progress { get; set; }
|
|
|
|
public void Execute(UIApplication app)
|
|
{
|
|
var assembliesToRef = new List<Assembly>
|
|
{
|
|
typeof(object).Assembly, //mscorlib
|
|
typeof(Autodesk.Revit.UI.UIApplication).Assembly, // Microsoft.CodeAnalysis.Workspaces
|
|
typeof(Autodesk.Revit.DB.Document).Assembly, // Microsoft.Build
|
|
};
|
|
|
|
var namespaces = new List<string>
|
|
{
|
|
"Autodesk.Revit.UI",
|
|
"Autodesk.Revit.DB",
|
|
"Autodesk.Revit.DB.Structure",
|
|
"System",
|
|
"Autodesk.Revit.DB.Mechanical",
|
|
"System.Collections.Generic",
|
|
"Autodesk.Revit.DB.Plumbing",
|
|
"Autodesk.Revit.DB.Electrical",
|
|
"System.IO",
|
|
"System.Linq"
|
|
};
|
|
|
|
ScriptGlobals globals = new ScriptGlobals(Progress) { doc = app.ActiveUIDocument.Document, uidoc = app.ActiveUIDocument };
|
|
|
|
var options = ScriptOptions.Default.AddReferences(assembliesToRef).WithImports(namespaces);
|
|
//AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
|
|
|
|
try
|
|
{
|
|
object result = CSharpScript.EvaluateAsync<object>(ScriptText, options, globals).Result;
|
|
if (!(result is null))
|
|
RoslynEditorViewModel.Result += CSharpObjectFormatter.Instance.FormatObject(result) + Environment.NewLine;
|
|
RoslynEditorViewModel.IsRunning = System.Windows.Visibility.Collapsed;
|
|
}
|
|
catch (AggregateException AggEx)
|
|
{
|
|
AggEx.Handle(ex =>
|
|
{
|
|
RoslynEditorViewModel.Result += CSharpObjectFormatter.Instance.FormatObject(ex) + Environment.NewLine;
|
|
return true;
|
|
}
|
|
);
|
|
RoslynEditorViewModel.IsRunning = System.Windows.Visibility.Collapsed;
|
|
}
|
|
catch (System.Exception ex)
|
|
{
|
|
RoslynEditorViewModel.Result += CSharpObjectFormatter.Instance.FormatObject(ex) + Environment.NewLine;
|
|
RoslynEditorViewModel.IsRunning = System.Windows.Visibility.Collapsed;
|
|
}
|
|
finally
|
|
{
|
|
RoslynEditorViewModel.IsRunning = System.Windows.Visibility.Collapsed;
|
|
//AppDomain.CurrentDomain.AssemblyResolve -= CurrentDomain_AssemblyResolve;
|
|
}
|
|
}
|
|
//private Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
|
|
//{
|
|
// if (args.Name.StartsWith("System.Runtime.CompilerServices.Unsafe"))
|
|
// {
|
|
// string services = Path.Combine(GlobalVariables.DirAssembly, "System.Runtime.CompilerServices.Unsafe.dll");
|
|
// if (File.Exists(services))
|
|
// {
|
|
// return Assembly.LoadFrom(services);
|
|
// }
|
|
// }
|
|
// return null;
|
|
//}
|
|
public string GetName()
|
|
{
|
|
return "执行脚本";
|
|
}
|
|
}
|
|
} |