forked from chuongmep/CadPythonShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIronPythonConsoleCommand.cs
More file actions
96 lines (91 loc) · 3.86 KB
/
IronPythonConsoleCommand.cs
File metadata and controls
96 lines (91 loc) · 3.86 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
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Runtime;
using CADPythonShell.App;
using CADRuntime;
using Microsoft.Scripting;
using System.Windows;
using System.Windows.Interop;
using System.Windows.Threading;
using Application = Autodesk.AutoCAD.ApplicationServices.Core.Application;
using Exception = System.Exception;
using Forms = System.Windows.Forms;
namespace CADPythonShell.Command
{
public class IronPythonConsoleCommand : ICadCommand
{
/// <summary>
/// Open a window to let the user enter python code.
/// </summary>
/// <returns></returns>
[CommandMethod("PythonConsole")]
public override void Execute()
{
Document doc = Application.DocumentManager.MdiActiveDocument;
//load the application
if (!CADPythonShellApplication.applicationLoaded)
{
CADPythonShellApplication.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(CADPythonShellApplication.GetConfig())
.SetupEnvironment(host.Engine, host.Console.ScriptScope);
host.Console.ScriptScope.SetVariable("__window__", gui);
// run the initscript
var initScript = CADPythonShellApplication.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;
}
});
});
gui.WindowStartupLocation = WindowStartupLocation.CenterScreen;
WindowInteropHelper helper = new WindowInteropHelper(gui);
IntPtr hander = Application.MainWindow.Handle;
helper.Owner = hander;
gui.Show();
}
}
}