BioImager  5.0.0
A .NET microscopy imaging application based on Bio library. Supports various microscopes by using imported libraries & GUI automation. Supports XInput game controllers to move stage, take images, run ImageJ macros on images or Bio C# scripts.
Loading...
Searching...
No Matches
Scripting.cs
Go to the documentation of this file.
1using CSScriptLib;
2using AForge;
3namespace BioImager
4{
5 public partial class Scripting : Form
6 {
7 public static string log;
8 public CodeView view = null;
9 public static string ImageJPath = Properties.Settings.Default.ImageJPath;
10
11 public static void LogLine(string s)
12 {
13 log += s + Environment.NewLine;
14 }
15 public static Dictionary<string, Script> Scripts = new Dictionary<string, Script>();
16 public class Script
17 {
18 public string name;
19 public string file;
20 public string scriptString;
21 public dynamic script;
22 public object obj;
23 public string output = "";
24 public bool done = false;
25 public static List<string> usings = new List<string>();
26 public Exception ex = null;
27 public Thread thread;
28 public ScriptType type = ScriptType.script;
29 /* Creating a new script object. */
30 public Script(string file, string scriptStr)
31 {
32 name = Path.GetFileName(file);
33 scriptString = scriptStr;
34 if (file.EndsWith(".txt") || file.EndsWith(".ijm"))
35 type = ScriptType.imagej;
36 }
37 /* Reading the file and storing the file name, file path, and file contents in the
38 variables name, file, and scriptString. */
39 public Script(string file)
40 {
41 name = Path.GetFileName(file);
42 scriptString = File.ReadAllText(file);
43 this.file = file;
44 if (file.EndsWith(".txt") || file.EndsWith(".ijm"))
45 type = ScriptType.imagej;
46 }
47
51 public static void Run(Script rn)
52 {
53 scriptName = rn.name;
54 Thread t = new Thread(new ThreadStart(RunScript));
55 t.Start();
56 }
57 private static string scriptName = "";
58 private static string str = "";
60 private static void RunScript()
61 {
62 Script rn = Scripts[scriptName];
63 rn.ex = null;
64 if (rn.type == ScriptType.imagej)
65 {
66 try
67 {
68 rn.done = false;
69 ImageJ.RunString(rn.scriptString, "", false);
70 rn.done = true;
71 }
72 catch (Exception e)
73 {
74 rn.ex = e;
75 }
76 }
77 else
78 {
79 try
80 {
81 rn.done = false;
82 rn.script = CSScript.Evaluator.LoadCode(rn.scriptString);
83 rn.obj = rn.script.Load();
84 rn.output = rn.obj.ToString();
85 rn.done = true;
86 }
87 catch (Exception e)
88 {
89 rn.ex = e;
90 }
91 }
92 }
99 public static object RunString(string st)
100 {
101 try
102 {
103 string loader =
104 @"//css_reference " + System.Reflection.Assembly.GetExecutingAssembly().GetName().Name + @".dll
105 using System;
106 using System.Windows.Forms;
107 using System.Drawing;
108 using System.Threading;
109 using Bio;";
110 foreach (string s in usings)
111 {
112 loader += usings + Environment.NewLine;
113 }
114 loader += @"
115 public class Loader
116 {
117 public object Load()
118 {" +
119 st + @";
120 return true;
121 }
122 }";
123 dynamic script = CSScript.Evaluator.LoadCode(loader);
124 return script.Load();
125 }
126 catch (Exception e)
127 {
128 MessageBox.Show(e.Message, e.Source);
129 return e;
130 }
131 }
133 public void Run()
134 {
135 if (!Scripts.ContainsKey(name))
136 Scripts.Add(name, this);
137 scriptName = this.name;
138 thread = new Thread(new ThreadStart(RunScript));
139 thread.Start();
140 }
142 public void Stop()
143 {
144 if (thread != null)
145 thread.Abort();
146 }
151 public override string ToString()
152 {
153 if (thread != null)
154 {
155 return name.ToString() + ", " + thread.ThreadState.ToString();
156 }
157 else
158 return name.ToString();
159 }
160 }
161 public class State
162 {
163 public Event type;
171 public static State GetUp(PointD pf, MouseButtons mb)
172 {
173 State st = new State();
174 st.type = Event.Up;
175 st.p = pf;
176 st.buts = mb;
177 return st;
178 }
186 public static State GetDown(PointD pf, MouseButtons mb)
187 {
188 State st = new State();
189 st.type = Event.Down;
190 st.p = pf;
191 st.buts = mb;
192 return st;
193 }
201 public static State GetMove(PointD pf, MouseButtons mb)
202 {
203 State st = new State();
204 st.type = Event.Move;
205 st.p = pf;
206 st.buts = mb;
207 return st;
208 }
214 public static State GetNone()
215 {
216 State st = new State();
217 st.type = Event.None;
218 st.p = new PointD();
219 st.buts = MouseButtons.None;
220 return st;
221 }
222
223 public PointD p;
224 public MouseButtons buts;
225 public bool processed = false;
226 public override string ToString()
227 {
228 return type.ToString() + " ,(" + p.X.ToString() + ", " + p.Y.ToString() + "), " + buts.ToString();
229 }
230
231 }
232 public enum Event
233 {
234 Down,
235 Up,
236 Move,
237 None
238 }
239 public enum ScriptType
240 {
241 tool,
242 script,
243 imagej
244 }
245 private static State state;
249 public static State GetState()
250 {
251 return state;
252 }
261 public static void UpdateState(State s)
262 {
263 if (s == null)
264 return;
265 if (state == null)
266 state = s;
267 if (s.p.X == state.p.X && s.p.Y == state.p.Y && s.type == state.type)
268 {
269 state.processed = true;
270 }
271 else
272 state = s;
273 }
275 public void RefreshItems()
276 {
277 Scripts.Clear();
278 string st = Application.StartupPath;
279 foreach (string file in Directory.GetFiles(st + "/Scripts"))
280 {
281 if (!Scripts.ContainsKey(Path.GetFileName(file)))
282 {
283 //This is a script file.
284 Script sc = new Script(file, File.ReadAllText(file));
285 ListViewItem lv = new ListViewItem();
286 lv.Tag = sc;
287 lv.Text = sc.ToString();
288 scriptView.Items.Add(lv);
289 Scripts.Add(lv.Text, sc);
290 }
291 }
292 foreach (string file in Directory.GetFiles(st + "/Tools"))
293 {
294 if (file.EndsWith(".cs"))
295 {
296 if (!Scripts.ContainsKey(Path.GetFileName(file)))
297 {
298 //This is a script file.
299 Script sc = new Script(file, File.ReadAllText(file));
300 sc.type = ScriptType.tool;
301 ListViewItem lv = new ListViewItem();
302 lv.Tag = sc;
303 lv.Text = sc.ToString();
304 scriptView.Items.Add(lv);
305 Scripts.Add(lv.Text, sc);
306 }
307 }
308 }
309 }
311 public void RefreshStatus()
312 {
313 errorView.Clear();
314 foreach (ListViewItem item in scriptView.SelectedItems)
315 {
316 Script s = (Script)item.Tag;
317 //We update item text to show Script status.
318 item.Text = s.ToString();
319 outputBox.Text = s.output;
320 if (s.ex != null)
321 {
322 string[] sps = s.ex.Message.Split('>');
323 //ListViewItem it = new ListViewItem(s.ex.ToString());
324 for (int i = 1; i < sps.Length; i++)
325 {
326 ListViewItem er = new ListViewItem(sps[i]);
327 er.Tag = s.ex;
328 errorView.Items.Add(er);
329 }
330
331 }
332 }
333 foreach (ListViewItem item in scriptView.SelectedItems)
334 {
335 Script s = (Script)item.Tag;
336 //We update item text to show Script status.
337 item.Text = s.ToString();
338 }
339 logBox.Text = log;
340 //We scroll to end of text so we see latest log output.
341 if (logBox.SelectionStart != logBox.Text.Length)
342 {
343 logBox.SelectionStart = logBox.Text.Length;
344 logBox.ScrollToCaret();
345 }
346 }
347 private CodeView codeview;
348 private RichTextBox textBox;
349
350 /* Creating a new directory called Scripts and Tools. */
351 public Scripting()
352 {
353 if (!Directory.Exists(Application.StartupPath + "//" + "Scripts"))
354 Directory.CreateDirectory(Application.StartupPath + "//" + "Scripts");
355 if (!Directory.Exists(Application.StartupPath + "//" + "Tools"))
356 Directory.CreateDirectory(Application.StartupPath + "//" + "Tools");
357 InitializeComponent();
358 scriptView.MultiSelect = false;
359 RefreshItems();
360 timer.Start();
361 codeview = new CodeView();
362 codeview.Dock = DockStyle.Fill;
363 scriptLabel.Text = "NewScript.cs";
364 //splitContainer.Dock = DockStyle.Fill;
365 textBox = codeview.TextBox;
366 splitContainer.Panel1.Controls.Add(codeview);
367 }
371 public void RunScriptFile(string file)
372 {
373 Script sc = new Script(file);
374 Scripts.Add(sc.name, sc);
375 RefreshItems();
376 RunByName(sc.name);
377 }
381 public static void RunScript(string file)
382 {
383 Script sc = new Script(file);
384 Scripts.Add(sc.name, sc);
385 RunByName(sc.name);
386 }
387 public static void RunScript(Script sc)
388 {
389 if(!Scripts.ContainsKey(sc.name))
390 Scripts.Add(sc.name, sc);
391 RunByName(sc.name);
392 }
396 public static void RunString(string st)
397 {
398 Script.RunString(st);
399 }
403 public void Run()
404 {
405 log = "";
406 outputBox.Text = "";
407 logBox.Text = "";
408 if (scriptView.SelectedItems.Count == 0)
409 return;
410 foreach (ListViewItem item in scriptView.SelectedItems)
411 {
412 //We run this script
413 Script sc = (Script)item.Tag;
414 sc.scriptString = textBox.Text;
415 sc.output = "";
416 sc.Run();
417 outputBox.Text = sc.output;
418 logBox.Text = log;
419 }
420 }
424 public void Stop()
425 {
426 if (scriptView.SelectedItems.Count == 0)
427 return;
428 foreach (ListViewItem item in scriptView.SelectedItems)
429 {
430 //We run this script
431 Script sc = (Script)item.Tag;
432 sc.Stop();
433 }
434 }
438 public static void RunByName(string name)
439 {
440 Scripts[name].Run();
441 }
442
447 private void scriptView_MouseDoubleClick(object sender, MouseEventArgs e)
448 {
449 Run();
450 }
451
456 private void runToolStripMenuItem_Click(object sender, EventArgs e)
457 {
458 Run();
459 }
460
465 private void openScriptFolderToolStripMenuItem_Click(object sender, EventArgs e)
466 {
467 string path = Application.StartupPath + "\\Scripts";
468 System.Diagnostics.Process.Start("explorer.exe", path);
469 }
470
475 private void refreshToolStripMenuItem_Click(object sender, EventArgs e)
476 {
477 RefreshItems();
479 }
480
485 private void timer_Tick(object sender, EventArgs e)
486 {
487 if (this.WindowState != FormWindowState.Minimized)
488 {
490 }
491 }
492
497 private void ScriptRunner_FormClosing(object sender, FormClosingEventArgs e)
498 {
499 e.Cancel = true;
500 this.WindowState = FormWindowState.Minimized;
501 }
502
510 private void scriptView_SelectedIndexChanged(object sender, EventArgs e)
511 {
512 if (scriptView.SelectedItems.Count == 0)
513 return;
514 ListViewItem item = scriptView.SelectedItems[0];
515 Script s = (Script)item.Tag;
516 textBox.Text = s.scriptString;
517 scriptLabel.Text = s.name;
518 }
519
524 private void runButton_Click(object sender, EventArgs e)
525 {
526 if (scriptLabel.Text.EndsWith(".ijm"))
527 {
528 ImageJ.RunString(textBox.Text, ImageView.SelectedImage.ID, headlessBox.Checked);
529 }
530 else
531 Run();
532 }
533
540 private void scriptLoadBut_Click(object sender, EventArgs e)
541 {
542 openFileDialog.InitialDirectory = Application.StartupPath + "\\Scripts";
543 if (openFileDialog.ShowDialog() != DialogResult.OK)
544 return;
545 Script script = new Script(openFileDialog.FileName, File.ReadAllText(openFileDialog.FileName));
546 script.name = Path.GetFileName(openFileDialog.FileName);
547 textBox.Text = script.scriptString;
548 scriptLabel.Text = Path.GetFileName(openFileDialog.FileName);
549 ListViewItem item = new ListViewItem();
550 item.Tag = script;
551 item.Text = script.name;
552 scriptView.Items.Add(item);
553 }
554
564 private void saveButton_Click(object sender, EventArgs e)
565 {
566 saveFileDialog.InitialDirectory = Application.StartupPath + "\\Scripts";
567 saveFileDialog.FileName = scriptLabel.Text;
568 if (saveFileDialog.ShowDialog() != DialogResult.OK)
569 return;
570 scriptLabel.Text = Path.GetFileName(saveFileDialog.FileName);
571 File.WriteAllText(saveFileDialog.FileName, textBox.Text);
572 }
573
578 private void stopBut_Click(object sender, EventArgs e)
579 {
580 Stop();
581 }
582
587 private void topMostBox_CheckedChanged(object sender, EventArgs e)
588 {
589 TopMost = topMostBox.Checked;
590 }
591
597 private void Scripting_KeyDown(object sender, KeyEventArgs e)
598 {
599 if (e.KeyCode == Keys.S && e.Modifiers == Keys.Control)
600 {
601 saveButton.PerformClick();
602 }
603 }
604
610 private void errorBox_SelectionChanged(object sender, EventArgs e)
611 {
612 Script sc = (Script)scriptView.SelectedItems[0].Tag;
613 Exception ex = sc.ex;
614 }
615
621 private void errorView_SelectedIndexChanged(object sender, EventArgs e)
622 {
623 Exception ex = (Exception)errorView.SelectedItems[0].Tag;
624 string exs = ex.Message.Substring(ex.Message.IndexOf('('), ex.Message.IndexOf(')'));
625 string ls = exs.Substring(1, exs.IndexOf(',') - 1);
626 int line = int.Parse(ls);
627 string c = exs.Substring(exs.IndexOf(',') + 1, exs.IndexOf(")") - exs.IndexOf(',') - 1);
628 int cr = int.Parse(c);
629 //textBox.SelectionLength = cr;
630 textBox.SelectionStart = line;
631 textBox.Focus();
632 }
633 }
634}
System.Windows.Forms.Keys Keys
Definition ImageView.cs:17
System.Windows.Forms.Application Application
Definition ImageView.cs:14
AForge.PointD PointD
RichTextBox TextBox
Definition CodeView.cs:38
static void RunString(string con, string param, bool headless)
Definition ImageJ.cs:137
static object RunString(string st)
Definition Scripting.cs:99
Script(string file, string scriptStr)
Definition Scripting.cs:30
void Stop()
The function stops the thread.
Definition Scripting.cs:142
void Run()
It creates a new thread and starts it.
Definition Scripting.cs:133
static void Run(Script rn)
Definition Scripting.cs:51
static List< string > usings
Definition Scripting.cs:25
override string ToString()
Definition Scripting.cs:151
static State GetDown(PointD pf, MouseButtons mb)
Definition Scripting.cs:186
static State GetNone()
Definition Scripting.cs:214
static State GetMove(PointD pf, MouseButtons mb)
Definition Scripting.cs:201
static State GetUp(PointD pf, MouseButtons mb)
Definition Scripting.cs:171
override string ToString()
Definition Scripting.cs:226
static void RunScript(string file)
Definition Scripting.cs:381
static void RunScript(Script sc)
Definition Scripting.cs:387
static Dictionary< string, Script > Scripts
Definition Scripting.cs:15
void RefreshItems()
It reads all the files in the Scripts and Tools folders and adds them to a listview.
Definition Scripting.cs:275
static void LogLine(string s)
Definition Scripting.cs:11
static State GetState()
Definition Scripting.cs:249
void RunScriptFile(string file)
Definition Scripting.cs:371
static void RunString(string st)
Definition Scripting.cs:396
void RefreshStatus()
It updates the status of the script and the log.
Definition Scripting.cs:311
static void RunByName(string name)
Definition Scripting.cs:438
static void UpdateState(State s)
Definition Scripting.cs:261
static string ImageJPath
Definition Scripting.cs:9
static string log
Definition Scripting.cs:7