BioCore  5.0.0
A Windows .NET microscopy library and application for viewing, editing, and annotating images.
Loading...
Searching...
No Matches
Scripting.cs
Go to the documentation of this file.
1using BioCore;
2using CSScriptLib;
3
4namespace BioCore
5{
6 public partial class Scripting : Form
7 {
8 public static string log;
9 public CodeView view = null;
10 public static string ImageJPath = Properties.Settings.Default.ImageJPath;
11
12 public static void LogLine(string s)
13 {
14 log += s + Environment.NewLine;
15 }
16 public static Dictionary<string, Script> Scripts = new Dictionary<string, Script>();
17 public class Script
18 {
19 public string name;
20 public string file;
21 public string scriptString;
22 public dynamic script;
23 public object obj;
24 public string output = "";
25 public bool done = false;
26 public static List<string> usings = new List<string>();
27 public Exception ex = null;
28 public Thread thread;
29 public ScriptType type = ScriptType.script;
30 /* Creating a new script object. */
31 public Script(string file, string scriptStr)
32 {
33 name = Path.GetFileName(file);
34 scriptString = scriptStr;
35 if (file.EndsWith(".txt") || file.EndsWith(".ijm"))
36 type = ScriptType.imagej;
37 }
38 /* Reading the file and storing the file name, file path, and file contents in the
39 variables name, file, and scriptString. */
40 public Script(string file)
41 {
42 name = Path.GetFileName(file);
43 scriptString = File.ReadAllText(file);
44 this.file = file;
45 if (file.EndsWith(".txt") || file.EndsWith(".ijm"))
46 type = ScriptType.imagej;
47 }
48
75 public static void Run(Script rn)
76 {
77 scriptName = rn.name;
78 Thread t = new Thread(new ThreadStart(RunScript));
79 t.Start();
80 }
81 private static string scriptName = "";
82 private static string str = "";
84 private static void RunScript()
85 {
86 Script rn = Scripts[scriptName];
87 rn.ex = null;
88 if (rn.type == ScriptType.imagej)
89 {
90 try
91 {
92 rn.done = false;
93 if (App.UseFiji)
94 Fiji.RunStringFiji(ImageView.SelectedImage, rn.scriptString, "", false);
95 else
96 ImageJ.RunString(ImageView.SelectedImage, rn.scriptString, "", false);
97 rn.done = true;
98 }
99 catch (Exception e)
100 {
101 rn.ex = e;
102 }
103 }
104 else
105 {
106 try
107 {
108 rn.done = false;
109 rn.script = CSScript.Evaluator.LoadCode(rn.scriptString);
110 rn.obj = rn.script.Load();
111 rn.output = rn.obj.ToString();
112 rn.done = true;
113 }
114 catch (Exception e)
115 {
116 rn.ex = e;
117 }
118 }
119 }
126 public static object RunString(string st)
127 {
128 try
129 {
130 string loader =
131 @"//css_reference " + System.Reflection.Assembly.GetExecutingAssembly().GetName().Name + @".dll
132 using System;
133 using System.Windows.Forms;
134 using System.Drawing;
135 using System.Threading;
136 using Bio;";
137 foreach (string s in usings)
138 {
139 loader += usings + Environment.NewLine;
140 }
141 loader += @"
142 public class Loader
143 {
144 public object Load()
145 {" +
146 st + @";
147 return true;
148 }
149 }";
150 dynamic script = CSScript.Evaluator.LoadCode(loader);
151 return script.Load();
152 }
153 catch (Exception e)
154 {
155 MessageBox.Show(e.Message, e.Source);
156 return e;
157 }
158 }
160 public void Run()
161 {
162 if (!Scripts.ContainsKey(name))
163 Scripts.Add(name, this);
164 scriptName = this.name;
165 thread = new Thread(new ThreadStart(RunScript));
166 thread.Start();
167 }
169 public void Stop()
170 {
171 if (thread != null)
172 thread.Abort();
173 }
178 public override string ToString()
179 {
180 if (thread != null)
181 {
182 return name.ToString() + ", " + thread.ThreadState.ToString();
183 }
184 else
185 return name.ToString();
186 }
187 }
188 public class State
189 {
190 public Event type;
198 public static State GetUp(PointD pf, MouseButtons mb)
199 {
200 State st = new State();
201 st.type = Event.Up;
202 st.p = pf;
203 st.buts = mb;
204 return st;
205 }
213 public static State GetDown(PointD pf, MouseButtons mb)
214 {
215 State st = new State();
216 st.type = Event.Down;
217 st.p = pf;
218 st.buts = mb;
219 return st;
220 }
228 public static State GetMove(PointD pf, MouseButtons mb)
229 {
230 State st = new State();
231 st.type = Event.Move;
232 st.p = pf;
233 st.buts = mb;
234 return st;
235 }
241 public static State GetNone()
242 {
243 State st = new State();
244 st.type = Event.None;
245 st.p = new PointD();
246 st.buts = MouseButtons.None;
247 return st;
248 }
249
250 public PointD p;
251 public MouseButtons buts;
252 public bool processed = false;
253 public override string ToString()
254 {
255 return type.ToString() + " ,(" + p.X.ToString() + ", " + p.Y.ToString() + "), " + buts.ToString();
256 }
257
258 }
259 public enum Event
260 {
261 Down,
262 Up,
263 Move,
264 None
265 }
266 public enum ScriptType
267 {
268 tool,
269 script,
270 imagej
271 }
272 private static State state;
276 public static State GetState()
277 {
278 return state;
279 }
288 public static void UpdateState(State s)
289 {
290 if (s == null)
291 return;
292 if (state == null)
293 state = s;
294 if (s.p.X == state.p.X && s.p.Y == state.p.Y && s.type == state.type)
295 {
296 state.processed = true;
297 }
298 else
299 state = s;
300 }
302 public void RefreshItems()
303 {
304 Scripts.Clear();
305 string st = Application.StartupPath;
306 foreach (string file in Directory.GetFiles(st + "/Scripts"))
307 {
308 if (!Scripts.ContainsKey(Path.GetFileName(file)))
309 {
310 //This is a script file.
311 Script sc = new Script(file, File.ReadAllText(file));
312 ListViewItem lv = new ListViewItem();
313 lv.Tag = sc;
314 lv.Text = sc.ToString();
315 scriptView.Items.Add(lv);
316 Scripts.Add(lv.Text, sc);
317 }
318 }
319 foreach (string file in Directory.GetFiles(st + "/Tools"))
320 {
321 if (file.EndsWith(".cs"))
322 {
323 if (!Scripts.ContainsKey(Path.GetFileName(file)))
324 {
325 //This is a script file.
326 Script sc = new Script(file, File.ReadAllText(file));
327 sc.type = ScriptType.tool;
328 ListViewItem lv = new ListViewItem();
329 lv.Tag = sc;
330 lv.Text = sc.ToString();
331 scriptView.Items.Add(lv);
332 Scripts.Add(lv.Text, sc);
333 }
334 }
335 }
336 }
338 public void RefreshStatus()
339 {
340 errorView.Clear();
341 foreach (ListViewItem item in scriptView.SelectedItems)
342 {
343 Script s = (Script)item.Tag;
344 //We update item text to show Script status.
345 item.Text = s.ToString();
346 outputBox.Text = s.output;
347 if (s.ex != null)
348 {
349 string[] sps = s.ex.Message.Split('>');
350 //ListViewItem it = new ListViewItem(s.ex.ToString());
351 for (int i = 1; i < sps.Length; i++)
352 {
353 ListViewItem er = new ListViewItem(sps[i]);
354 er.Tag = s.ex;
355 errorView.Items.Add(er);
356 }
357
358 }
359 }
360 foreach (ListViewItem item in scriptView.SelectedItems)
361 {
362 Script s = (Script)item.Tag;
363 //We update item text to show Script status.
364 item.Text = s.ToString();
365 }
366 logBox.Text = log;
367 //We scroll to end of text so we see latest log output.
368 if (logBox.SelectionStart != logBox.Text.Length)
369 {
370 logBox.SelectionStart = logBox.Text.Length;
371 logBox.ScrollToCaret();
372 }
373 }
374 private CodeView codeview;
375 private RichTextBox textBox;
376
377 /* Creating a new directory called Scripts and Tools. */
378 public Scripting()
379 {
380 if (!Directory.Exists(Application.StartupPath + "//" + "Scripts"))
381 Directory.CreateDirectory(Application.StartupPath + "//" + "Scripts");
382 if (!Directory.Exists(Application.StartupPath + "//" + "Tools"))
383 Directory.CreateDirectory(Application.StartupPath + "//" + "Tools");
384 InitializeComponent();
385 scriptView.MultiSelect = false;
386 RefreshItems();
387 timer.Start();
388 codeview = new CodeView();
389 codeview.Dock = DockStyle.Fill;
390 scriptLabel.Text = "NewScript.cs";
391 //splitContainer.Dock = DockStyle.Fill;
392 textBox = codeview.TextBox;
393 splitContainer.Panel1.Controls.Add(codeview);
394 }
398 public void RunScriptFile(string file)
399 {
400 Script sc = new Script(file);
401 Scripts.Add(sc.name, sc);
402 RefreshItems();
403 RunByName(sc.name);
404 }
408 public static void RunScript(string file)
409 {
410 Script sc = new Script(file);
411 Scripts.Add(sc.name, sc);
412 RunByName(sc.name);
413 }
417 public static void RunString(string st)
418 {
419 Script.RunString(st);
420 }
424 public void Run()
425 {
426 log = "";
427 outputBox.Text = "";
428 logBox.Text = "";
429 if (scriptView.SelectedItems.Count == 0)
430 return;
431 foreach (ListViewItem item in scriptView.SelectedItems)
432 {
433 //We run this script
434 Script sc = (Script)item.Tag;
435 sc.scriptString = textBox.Text;
436 sc.output = "";
437 sc.Run();
438 outputBox.Text = sc.output;
439 logBox.Text = log;
440 }
441 }
445 public void Stop()
446 {
447 if (scriptView.SelectedItems.Count == 0)
448 return;
449 foreach (ListViewItem item in scriptView.SelectedItems)
450 {
451 //We run this script
452 Script sc = (Script)item.Tag;
453 sc.Stop();
454 }
455 }
459 public static void RunByName(string name)
460 {
461 Scripts[name].Run();
462 }
463
468 private void scriptView_MouseDoubleClick(object sender, MouseEventArgs e)
469 {
470 Run();
471 }
472
477 private void runToolStripMenuItem_Click(object sender, EventArgs e)
478 {
479 Run();
480 }
481
486 private void openScriptFolderToolStripMenuItem_Click(object sender, EventArgs e)
487 {
488 string path = Application.StartupPath + "\\Scripts";
489 System.Diagnostics.Process.Start("explorer.exe", path);
490 }
491
496 private void refreshToolStripMenuItem_Click(object sender, EventArgs e)
497 {
498 RefreshItems();
500 }
501
506 private void timer_Tick(object sender, EventArgs e)
507 {
508 if (this.WindowState != FormWindowState.Minimized)
509 {
511 }
512 }
513
518 private void ScriptRunner_FormClosing(object sender, FormClosingEventArgs e)
519 {
520 e.Cancel = true;
521 this.WindowState = FormWindowState.Minimized;
522 }
523
531 private void scriptView_SelectedIndexChanged(object sender, EventArgs e)
532 {
533 if (scriptView.SelectedItems.Count == 0)
534 return;
535 ListViewItem item = scriptView.SelectedItems[0];
536 Script s = (Script)item.Tag;
537 textBox.Text = s.scriptString;
538 scriptLabel.Text = s.name;
539 }
540
545 private void runButton_Click(object sender, EventArgs e)
546 {
547 if (scriptLabel.Text.EndsWith(".ijm"))
548 {
549 if (App.UseFiji)
550 Fiji.RunStringFiji(ImageView.SelectedImage, textBox.Text, ImageView.SelectedImage.ID, headlessBox.Checked);
551 else
552 ImageJ.RunOnImage(ImageView.SelectedImage, textBox.Text, headlessBox.Checked, BioConsole.onTab, BioConsole.useBioformats, true);
553 }
554 else
555 Run();
556 }
557
564 private void scriptLoadBut_Click(object sender, EventArgs e)
565 {
566 openFileDialog.InitialDirectory = Application.StartupPath + "\\Scripts";
567 if (openFileDialog.ShowDialog() != DialogResult.OK)
568 return;
569 Script script = new Script(openFileDialog.FileName, File.ReadAllText(openFileDialog.FileName));
570 script.name = Path.GetFileName(openFileDialog.FileName);
571 textBox.Text = script.scriptString;
572 scriptLabel.Text = Path.GetFileName(openFileDialog.FileName);
573 ListViewItem item = new ListViewItem();
574 item.Tag = script;
575 item.Text = script.name;
576 scriptView.Items.Add(item);
577 }
578
588 private void saveButton_Click(object sender, EventArgs e)
589 {
590 saveFileDialog.InitialDirectory = Application.StartupPath + "\\Scripts";
591 saveFileDialog.FileName = scriptLabel.Text;
592 if (saveFileDialog.ShowDialog() != DialogResult.OK)
593 return;
594 scriptLabel.Text = Path.GetFileName(saveFileDialog.FileName);
595 File.WriteAllText(saveFileDialog.FileName, textBox.Text);
596 }
597
602 private void stopBut_Click(object sender, EventArgs e)
603 {
604 Stop();
605 }
606
611 private void topMostBox_CheckedChanged(object sender, EventArgs e)
612 {
613 TopMost = topMostBox.Checked;
614 }
615
621 private void Scripting_KeyDown(object sender, KeyEventArgs e)
622 {
623 if (e.KeyCode == Keys.S && e.Modifiers == Keys.Control)
624 {
625 saveButton.PerformClick();
626 }
627 }
628
634 private void errorBox_SelectionChanged(object sender, EventArgs e)
635 {
636 Script sc = (Script)scriptView.SelectedItems[0].Tag;
637 Exception ex = sc.ex;
638 }
639
645 private void errorView_SelectedIndexChanged(object sender, EventArgs e)
646 {
647 Exception ex = (Exception)errorView.SelectedItems[0].Tag;
648 string exs = ex.Message.Substring(ex.Message.IndexOf('('), ex.Message.IndexOf(')'));
649 string ls = exs.Substring(1, exs.IndexOf(',') - 1);
650 int line = int.Parse(ls);
651 string c = exs.Substring(exs.IndexOf(',') + 1, exs.IndexOf(")") - exs.IndexOf(',') - 1);
652 int cr = int.Parse(c);
653 //textBox.SelectionLength = cr;
654 textBox.SelectionStart = line;
655 textBox.Focus();
656 }
657 }
658}
System.Windows.Forms.Keys Keys
Definition ImageView.cs:16
System.Windows.Forms.Application Application
Definition ImageView.cs:14
static bool UseFiji
Definition App.cs:287
static bool useBioformats
Definition BioConsole.cs:21
static bool onTab
Definition BioConsole.cs:20
RichTextBox TextBox
Definition CodeView.cs:31
ImageView control for image stacks, pyramidal and whole-slide-images.
static BioImage SelectedImage
Definition ImageView.cs:306
static List< string > usings
Definition Scripting.cs:26
void Stop()
The function stops the thread.
Definition Scripting.cs:169
override string ToString()
Definition Scripting.cs:178
static void Run(Script rn)
Definition Scripting.cs:75
static object RunString(string st)
Definition Scripting.cs:126
void Run()
It creates a new thread and starts it.
Definition Scripting.cs:160
Script(string file, string scriptStr)
Definition Scripting.cs:31
static State GetDown(PointD pf, MouseButtons mb)
Definition Scripting.cs:213
override string ToString()
Definition Scripting.cs:253
static State GetMove(PointD pf, MouseButtons mb)
Definition Scripting.cs:228
static State GetUp(PointD pf, MouseButtons mb)
Definition Scripting.cs:198
static State GetNone()
Definition Scripting.cs:241
static string log
Definition Scripting.cs:8
static void RunScript(string file)
Definition Scripting.cs:408
static void LogLine(string s)
Definition Scripting.cs:12
void RefreshStatus()
It updates the status of the script and the log.
Definition Scripting.cs:338
static string ImageJPath
Definition Scripting.cs:10
static State GetState()
Definition Scripting.cs:276
static Dictionary< string, Script > Scripts
Definition Scripting.cs:16
CodeView view
Definition Scripting.cs:9
void RunScriptFile(string file)
Definition Scripting.cs:398
static void UpdateState(State s)
Definition Scripting.cs:288
static void RunString(string st)
Definition Scripting.cs:417
static void RunByName(string name)
Definition Scripting.cs:459
void RefreshItems()
It reads all the files in the Scripts and Tools folders and adds them to a listview.
Definition Scripting.cs:302