-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathNonModalConsoleCommand.cs
More file actions
141 lines (132 loc) · 5.09 KB
/
NonModalConsoleCommand.cs
File metadata and controls
141 lines (132 loc) · 5.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.UI;
using Autodesk.Revit.DB;
using System.Diagnostics;
using Microsoft.Scripting;
using System.Threading;
using System.Windows.Threading;
using RevitPythonShell.RpsRuntime;
using System.Threading.Tasks;
using IronPython.Runtime;
using Microsoft.Scripting.Hosting;
namespace RevitPythonShell
{
/// <summary>
/// An object of this class is instantiated every time the user clicks on the
/// button for opening the shell.
/// </summary>
///
[Regeneration(RegenerationOption.Manual)]
[Transaction(TransactionMode.Manual)]
public class NonModalConsoleCommand : IExternalCommand
{
/// <summary>
/// Open a window to let the user enter python code.
/// </summary>
/// <returns></returns>
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
var messageCopy = message;
var gui = new IronPythonConsole();
gui.consoleControl.WithConsoleHost((host) =>
{
// now that the console is created and initialized, the script scope should
// be accessible...
new ScriptExecutor(RevitPythonShellApplication.GetConfig(), commandData, messageCopy, elements)
.SetupEnvironment(host.Engine, host.Console.ScriptScope);
host.Console.ScriptScope.SetVariable("__window__", gui);
// run the initscript
var initScript = RevitPythonShellApplication.GetInitScript();
if (initScript != null)
{
var scriptSource = host.Engine.CreateScriptSourceFromString(initScript, SourceCodeKind.Statements);
scriptSource.Execute(host.Console.ScriptScope);
}
});
var commandCompletedEvent = new AutoResetEvent(false);
var externalEventHandler = new IronPythonExternalEventDispatcher(gui, commandCompletedEvent);
var externalEvent = ExternalEvent.Create(externalEventHandler);
gui.consoleControl.WithConsoleHost((host) =>
{
var oldDispatcher = host.Console.GetCommandDispatcher();
host.Console.SetCommandDispatcher((command) =>
{
//externalEventHandler.Enqueue(() => oldDispatcher(command));
externalEventHandler.Enqueue(command);
externalEvent.Raise();
commandCompletedEvent.WaitOne();
});
host.Editor.SetCompletionDispatcher((command) =>
{
externalEventHandler.Enqueue(command);
externalEvent.Raise();
commandCompletedEvent.WaitOne();
});
});
gui.Topmost = true;
gui.Title = "RevitPythonShell (non-modal)";
gui.Show();
return Result.Succeeded;
}
}
/// <summary>
/// Make sure commands are executed in a RevitAPI context for non-modal RPS interactive shells.
/// </summary>
public class IronPythonExternalEventDispatcher : IExternalEventHandler
{
private IronPythonConsole _gui;
private Queue<Action> _commands = new Queue<Action>();
private AutoResetEvent _commandCompletedEvent;
public void Enqueue(Action command)
{
_commands.Enqueue(command);
}
public IronPythonExternalEventDispatcher(IronPythonConsole gui, AutoResetEvent commandCompletedEvent)
{
_gui = gui;
_commandCompletedEvent = commandCompletedEvent;
}
public void Execute(UIApplication app)
{
while (_commands.Count > 0)
{
var command = _commands.Dequeue();
try
{
command();
}
catch (Exception ex)
{
try
{
_gui.consoleControl.WithConsoleHost((host) =>
{
ExceptionOperations eo;
eo = host.Engine.GetService<ExceptionOperations>();
var error = eo.FormatException(ex);
host.Console.WriteLine(error, Microsoft.Scripting.Hosting.Shell.Style.Error);
//TaskDialog.Show("Error", error);
});
}
catch (Exception exception)
{
Debugger.Launch();
Trace.WriteLine(exception.ToString());
}
}
finally
{
_commandCompletedEvent.Set();
}
}
}
public string GetName()
{
return "IronPythonExternalEventDispatcher";
}
}
}