-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathIronPythonConsoleCommand.cs
More file actions
108 lines (102 loc) · 4.26 KB
/
IronPythonConsoleCommand.cs
File metadata and controls
108 lines (102 loc) · 4.26 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using Microsoft.Scripting;
using System.Threading;
using System.Windows.Interop;
using System.Windows.Threading;
using NavisPythonShell.NpsRuntime;
using Forms = System.Windows.Forms;
using Autodesk.Navisworks.Api;
using Autodesk.Navisworks.Api.Plugins;
namespace NavisPythonShell
{
/// <summary>
/// Start an interactive shell in a modal window.
/// </summary>
[PluginAttribute("NavisPythonShell.IronPythonConsoleCommand",
"ACOM",
ToolTip = "NavisPythonShell IronPython Console",
DisplayName = "Run NPS")]
[AddInPluginAttribute(AddInLocation.AddIn,
Icon = "Icons\\Python-16.ico",
LargeIcon = "Icons\\Python-32.ico",
LoadForCanExecute = true)]
public class IronPythonConsoleCommand : AddInPlugin
{
/// <summary>
/// Open a window to let the user enter python code.
/// </summary>
/// <returns></returns>
public override int Execute(params string[] parameters)
{
//load the application
if (!NavisPythonShellApplication.applicationLoaded)
{
NavisPythonShellApplication.OnLoaded();
}
var gui = new IronPythonConsole();
gui.consoleControl.WithConsoleHost((host) =>
{
// now that the console is created and initialized, the script scope should
// be accessible...
new ScriptExecutor(NavisPythonShellApplication.GetConfig() )
.SetupEnvironment(host.Engine, host.Console.ScriptScope);
host.Console.ScriptScope.SetVariable("__window__", gui);
// run the initscript
var initScript = NavisPythonShellApplication.GetInitScript();
if (initScript != null)
{
try
{
var scriptSource = host.Engine.CreateScriptSourceFromString(initScript, SourceCodeKind.Statements);
scriptSource.Execute(host.Console.ScriptScope);
}
catch (Exception ex)
{
Forms.MessageBox.Show(ex.ToString(), "Something went horribly wrong!");
}
}
});
var dispatcher = Dispatcher.FromThread(Thread.CurrentThread);
gui.consoleControl.WithConsoleHost((host) =>
{
host.Console.SetCommandDispatcher((command) =>
{
if (command != null)
{
// Slightly involved form to enable keyboard interrupt to work.
var executing = true;
var operation = dispatcher.BeginInvoke(DispatcherPriority.Normal, command);
while (executing)
{
if (operation.Status != DispatcherOperationStatus.Completed)
operation.Wait(TimeSpan.FromSeconds(1));
if (operation.Status == DispatcherOperationStatus.Completed)
executing = false;
}
}
});
host.Editor.SetCompletionDispatcher((command) =>
{
var executing = true;
var operation = dispatcher.BeginInvoke(DispatcherPriority.Normal, command);
while (executing)
{
if (operation.Status != DispatcherOperationStatus.Completed)
operation.Wait(TimeSpan.FromSeconds(1));
if (operation.Status == DispatcherOperationStatus.Completed)
executing = false;
}
});
});
WindowInteropHelper helper = new WindowInteropHelper(gui);
IntPtr hander = Application.Gui.MainWindow.Handle;
helper.Owner = hander;
gui.ShowDialog();
return 0;
}
}
}