BioImager  4.9.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
Microscope.cs
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Threading.Tasks;
6using System.Windows.Forms;
7using System.Globalization;
8using System.Drawing;
9using System.Diagnostics;
10using System.Threading;
11using System.Reflection;
12using System.IO;
13using AForge;
14using Point = AForge.Point;
15using RectangleD = AForge.RectangleD;
16using Bitmap = AForge.Bitmap;
17using BioLib;
18namespace BioImager
19{
20 /* It's a wrapper for the stage*/
21 public class Stage
22 {
23 public static double minX;
24 public static double maxX;
25 public static double minY;
26 public static double maxY;
27 /* Creating a new instance of the Stage class. */
28 public Stage(object st)
29 {
30 if (Properties.Settings.Default.LibPath.Contains("MTB"))
31 {
32 PointD d = GetPosition(true);
33 x = d.X;
34 y = d.Y;
36 }
37 }
38 public Stage()
39 {
40
41 }
42
43 private double x;
44 private double y;
45 public double X
46 {
47 get
48 {
49 return x;
50 }
51 }
52 public double Y
53 {
54 get
55 {
56 return y;
57 }
58 }
59 public int moveWait = 250;
60 private void MoveWait()
61 {
62 Thread.Sleep(moveWait);
63 }
64
69 public void SetPosition(double px, double py)
70 {
71 if (Recorder.recordMicroscope)
72 Recorder.AddLine("Microscope.Stage.SetPosition(" + px + "," + py + ");");
73 x = px;
74 y = py;
75 if(Properties.Settings.Default.PMicroscope)
76 {
77 Microscope.pMicroscope.SetPosition(new PointD(px, py));
78 }
79 else
80 if (Properties.Settings.Default.PycroManager)
81 {
82 MicroManager.SetPosition(new PointD(px, py));
83 }
84 else
85 if (Properties.Settings.Default.LibPath.Contains("MTB"))
86 {
87 BioImager.MicroscopeConsole.RunCommand(new BioImager.MicroscopeConsole.Command(BioImager.MicroscopeConsole.Command.Type.SetStage, new double[]{px,py }));
88 }
89 else if (Properties.Settings.Default.LibPath.Contains("Prio"))
90 {
91 Microscope.sdk.SetPosition(new PointD(px, py));
92 }
93 if (App.viewer != null)
94 App.viewer.UpdateView(true);
95 }
96
100 public void SetPositionX(double px)
101 {
102 x = px;
103 SetPosition(px, y);
104 }
105
109 public void SetPositionY(double py)
110 {
111 y = py;
112 SetPosition(x, py);
113 }
114
120 public double GetPositionX(bool update)
121 {
122 x = GetPosition(update).X;
123 return x;
124 }
125
131 public double GetPositionY(bool update)
132 {
133 y = GetPosition(update).Y;
134 return y;
135 }
136
145 public PointD GetPosition(bool update)
146 {
147 if (Properties.Settings.Default.PMicroscope)
148 {
149 PointD pd;
150 Microscope.pMicroscope.GetPosition(out pd,false);
151 return pd;
152 }
153 else
154 if (Properties.Settings.Default.PycroManager)
155 {
156 PointD pd;
157 MicroManager.GetPosition(out pd, false);
158 return pd;
159 }
160 else
161 if (Properties.Settings.Default.LibPath.Contains("MTB"))
162 {
163 BioImager.MicroscopeConsole.Command com = BioImager.MicroscopeConsole.RunCommand
165 return new PointD(com.doubles[0], com.doubles[1]);
166 }
167 else if (Properties.Settings.Default.LibPath.Contains("Prio"))
168 {
169 return Microscope.sdk.GetPosition();
170 }
171 return new PointD(x, y);
172 }
173
177 public void MoveUp(double m)
178 {
179 double y = GetPositionY(true) - m;
180 SetPositionY(y);
181 }
182
186 public void MoveDown(double m)
187 {
188 double y = GetPositionY(true) + m;
189 SetPositionY(y);
190 }
191
195 public void MoveRight(double m)
196 {
197 double x = GetPositionX(true) + m;
198 SetPositionX(x);
199 }
200
204 public void MoveLeft(double m)
205 {
206 double x = GetPositionX(true) - m;
207 SetPositionX(x);
208 }
209
211 public void UpdateSWLimit()
212 {
213 if (Properties.Settings.Default.LibPath.Contains("MTB"))
214 {
216 new BioImager.MicroscopeConsole.Command(BioImager.MicroscopeConsole.Command.Type.GetStageSWLimit, null));
217 minX = com.doubles[0];
218 minY = com.doubles[1];
219 maxX = com.doubles[2];
220 maxY = com.doubles[3];
221 }
222 }
223
230 public void SetSWLimit(double xmin, double xmax, double ymin, double ymax)
231 {
232 if (Properties.Settings.Default.LibPath.Contains("MTB"))
233 {
234 BioImager.MicroscopeConsole.RunCommand(
236 new double[] {xmin,xmax,ymin,ymax}));
237 }
238 if (Recorder.recordMicroscope)
239 Recorder.AddLine("Microscope.Focus.SetSWLimit(" + xmin + "," + xmax + "," + ymin + "," + ymax + ");");
240 }
241 }
242
243 /* The Focus class is used to set and get the focus of the microscope */
244 public class Focus
245 {
246 public static double upperLimit;
247 public static double lowerLimit;
248 private static double z;
249 public Focus()
250 {
251
252 }
253
257 public void SetFocus(double f)
258 {
259 if (Recorder.recordMicroscope)
260 Recorder.AddLine("Microscope.Focus.SetFocus(" + f + ");");
261 if (Properties.Settings.Default.PMicroscope)
262 {
263 PointD p = Microscope.Stage.GetPosition(true);
264 Microscope.pMicroscope.SetPosition(new Point3D(p.X,p.Y,f));
265 }
266 else
267 if (Properties.Settings.Default.PycroManager)
268 {
269 PointD p = Microscope.Stage.GetPosition(true);
270 MicroManager.SetPosition(new Point3D(p.X, p.Y, f));
271 }
272 else
273 if (Properties.Settings.Default.LibPath.Contains("MTB"))
274 {
275 BioImager.MicroscopeConsole.RunCommand(
276 new BioImager.MicroscopeConsole.Command(BioImager.MicroscopeConsole.Command.Type.SetFocus,new double[] { f}));
277 z = f;
278 }
279 else if (Properties.Settings.Default.LibPath.Contains("Prio"))
280 {
281 Microscope.sdk.SetZ(f);
282 }
283 if (Automation.Properties.ContainsKey("SetFocus"))
284 {
285 Automation.SetProperty("SetFocus", f.ToString());
286 }
287 }
288
292 public double GetFocus()
293 {
294 return GetFocus(false);
295 }
296
304 public double GetFocus(bool update)
305 {
306 if (Properties.Settings.Default.PMicroscope)
307 {
308 Point3D p;
309 Microscope.pMicroscope.GetPosition3D(out p, update);
310 return p.Z;
311 }
312 else if(Properties.Settings.Default.PycroManager)
313 {
314 Point3D p;
315 MicroManager.GetPosition3D(out p, update);
316 return p.Z;
317 }
318 if (Properties.Settings.Default.LibPath.Contains("MTB"))
319 {
320 BioImager.MicroscopeConsole.Command com = BioImager.MicroscopeConsole.RunCommand
322 return com.doubles[0];
323 }
324 if (Properties.Settings.Default.LibPath.Contains("Prio"))
325 {
326 return Microscope.sdk.GetZ();
327 }
328 if(Automation.Properties.ContainsKey("GetFocus"))
329 {
330 return (double)Automation.GetProperty("GetFocus");
331 }
332 return z;
333 }
334
338 public PointD GetSWLimit()
339 {
340 if (Properties.Settings.Default.LibPath.Contains("MTB"))
341 {
343 new BioImager.MicroscopeConsole.Command(BioImager.MicroscopeConsole.Command.Type.GetStageSWLimit, null));
344 upperLimit = com.doubles[0];
345 lowerLimit = com.doubles[1];
346 return new PointD(upperLimit,lowerLimit);
347 }
348 return new PointD(lowerLimit, upperLimit);
349 }
350
356 public void SetSWLimit(double xd, double yd)
357 {
358 if (Properties.Settings.Default.LibPath.Contains("MTB"))
359 {
360 BioImager.MicroscopeConsole.RunCommand(
362 new double[] { xd, yd }));
363 }
364 upperLimit = xd;
365 lowerLimit = yd;
366 if (Recorder.recordMicroscope)
367 Recorder.AddLine("Microscope.Focus.SetSWLimit(" + xd + "," + yd + ");");
368 }
369 }
370
371 /* The Objectives class is a class that contains a list of objectives */
372 public class Objectives
373 {
374 public List<Objective> List = new List<Objective>();
375 /* Creating a list of objectives. */
376 public Objectives(int count)
377 {
378 for (int i = 0; i < count; i++)
379 {
380 List.Add(new Objective(i));
381 }
382 }
383 /* The Objective class is a class that contains all the information about the objective */
384 public class Objective
385 {
386 public Dictionary<string, object> config = new Dictionary<string, object>();
387 public int Index;
388 public string Name;
389 public double LocateExposure = 50;
390 public int WorkingDistance = 0;
391 public double AcquisitionExposure = 50;
392 public string Configuration = "";
393 public double MoveAmountL = 40;
394 public double MoveAmountR = 10;
395 public double FocusMoveAmount = 0.02;
396 public double ViewWidth;
397 public double ViewHeight;
398 public Objective(int index)
399 {
400 Index = index;
401 }
402 public Objective()
403 {
404 }
405 public override string ToString()
406 {
407 if (Name == "" || Name == null)
408 return "Objective " + Index.ToString();
409 else return Name;
410 }
411 }
412 private int index;
413 public int moveWait = 1000;
414
416 private void MoveWait()
417 {
418 Thread.Sleep(moveWait);
419 }
420 public int Index
421 {
422 get
423 {
424 return GetPosition();
425 }
426 set
427 {
428 SetPosition(value);
429 }
430 }
431
438 public void SetPosition(int index)
439 {
440 if (Recorder.recordMicroscope)
441 Recorder.AddLine("Microscope.Objectives.SetObjective(" + index + ");");
442 this.index = index;
443 if (!Properties.Settings.Default.LibPath.Contains("MTB") && !Properties.Settings.Default.LibPath.Contains("Prio") && Function.Functions.ContainsKey("SetO" + index))
444 {
445 Function f = Function.Functions["SetO" + index];
446 App.imager.PerformFunction(f);
447 return;
448 }
449 else if(Properties.Settings.Default.LibPath.Contains("MTB"))
450 {
451 BioImager.MicroscopeConsole.RunCommand(
452 new BioImager.MicroscopeConsole.Command(BioImager.MicroscopeConsole.Command.Type.SetObjective, new double[] { index }));
453 this.index = index;
454 }
455 else if(Properties.Settings.Default.PycroManager)
456 {
457 MicroManager.Objectives.SetPosition(index);
458 }
459 }
460
461 public int GetPosition()
462 {
463 if (Properties.Settings.Default.LibPath.Contains("MTB"))
464 {
465 BioImager.MicroscopeConsole.Command com = BioImager.MicroscopeConsole.RunCommand
467 return (int)com.doubles[0];
468 }
469 else if (Properties.Settings.Default.LibPath.Contains("Prio"))
470 {
471 return Microscope.sdk.GetNosePiece();
472 }
473 else if(Properties.Settings.Default.PycroManager)
474 {
475 return MicroManager.Objectives.GetPosition();
476 }
477 else if(!Properties.Settings.Default.PMicroscope && Automation.Properties.ContainsKey("GetO1"))
478 {
479 if (Function.Functions.ContainsKey("GetO7"))
480 {
481 bool b;
482 //We check each objective button state to determine which objective we currently are in.
483 b = (bool)Automation.GetProperty("Get01");
484 if (b)
485 return 0;
486 b = (bool)Automation.GetProperty("Get02");
487 if (b)
488 return 1;
489 b = (bool)Automation.GetProperty("Get03");
490 if (b)
491 return 2;
492 b = (bool)Automation.GetProperty("Get04");
493 if (b)
494 return 3;
495 b = (bool)Automation.GetProperty("Get05");
496 if (b)
497 return 4;
498 b = (bool)Automation.GetProperty("Get06");
499 if (b)
500 return 5;
501 b = (bool)Automation.GetProperty("Get07");
502 if (b)
503 return 6;
504 }
505 else
506 {
507 bool b;
508 //We check each objective button state to determine which objective we currently are in.
509 b = (bool)Automation.GetProperty("Get01");
510 if (b)
511 return 0;
512 b = (bool)Automation.GetProperty("Get02");
513 if (b)
514 return 1;
515 b = (bool)Automation.GetProperty("Get03");
516 if (b)
517 return 2;
518 b = (bool)Automation.GetProperty("Get04");
519 if (b)
520 return 3;
521 b = (bool)Automation.GetProperty("Get05");
522 if (b)
523 return 4;
524 b = (bool)Automation.GetProperty("Get06");
525 if (b)
526 return 5;
527 }
528 }
529 return 0;
530 }
531 public Objective GetObjective()
532 {
533 int p = 0;
534 try
535 {
536 p = (short)GetPosition();
537 return List[p];
538 }
539 catch (Exception e)
540 {
541 Console.WriteLine(e.ToString());
542 }
543 return new Objective();
544 }
545 }
546
547 /* The TLShutter class is a wrapper class for the shutter object */
548 public class TLShutter
549 {
550 public static Type tlType;
551 public static object tlShutter = null;
552 public static int position;
557 public short GetPosition()
558 {
559 BioImager.MicroscopeConsole.Command com = BioImager.MicroscopeConsole.RunCommand
561 return (short)com.doubles[0];
562 }
563
570 public void SetPosition(int p)
571 {
572 if (Recorder.recordMicroscope)
573 Recorder.AddLine("Microscope.TLShutter.SetPosition(" + p + ");");
574 if (Properties.Settings.Default.LibPath.Contains("MTB"))
575 {
576 BioImager.MicroscopeConsole.RunCommand(
577 new BioImager.MicroscopeConsole.Command(BioImager.MicroscopeConsole.Command.Type.SetTLShutter, new double[] { p }));
578 position = p;
579 }
580 if (Properties.Settings.Default.LibPath.Contains("Prio"))
581 {
582 if (p == 0)
583 Microscope.sdk.shutterClose();
584 else
585 Microscope.sdk.shutterOpen();
586 }
587 }
588 }
589 /* The HXPShutter class is a wrapper class for the TLShutter class */
590 public class HXPShutter
591 {
592 public static Type tlType;
593 public static object tlShutter = null;
594 public static int position;
599 public short GetPosition()
600 {
601 BioImager.MicroscopeConsole.Command com = BioImager.MicroscopeConsole.RunCommand
603 if (com.doubles == null)
604 return 0;
605 return (short)com.doubles[0];
606 }
607
614 public void SetPosition(int p)
615 {
616 if (Recorder.recordMicroscope)
617 Recorder.AddLine("Microscope.TLShutter.SetPosition(" + p + ");");
618 if (Properties.Settings.Default.LibPath.Contains("MTB"))
619 {
620 BioImager.MicroscopeConsole.RunCommand(
621 new BioImager.MicroscopeConsole.Command(BioImager.MicroscopeConsole.Command.Type.SetHXPShutter, new double[] { p }));
622 position = p;
623 }
624 }
625 }
626
627 /* The RLShutter class is a wrapper class for the IMTBChanger interface */
628 public class RLShutter
629 {
630 public static Type rlType;
631 public static object rlShutter = null;
632 internal static int position;
637 public short GetPosition()
638 {
639 BioImager.MicroscopeConsole.Command com = BioImager.MicroscopeConsole.RunCommand
641 return (short)com.doubles[0];
642 }
643
648 public void SetPosition(int p)
649 {
650 if (Properties.Settings.Default.LibPath.Contains("MTB"))
651 {
652 BioImager.MicroscopeConsole.RunCommand(
653 new BioImager.MicroscopeConsole.Command(BioImager.MicroscopeConsole.Command.Type.SetRLShutter, new double[] { p }));
654 }
655 position = p;
656 if (Recorder.recordMicroscope)
657 Recorder.AddLine("Microscope.RLShutter.SetPosition(" + p + ");");
658 }
659 }
660
661 /* The LightSource class is a wrapper class for the IMTBContinual interface. */
662 public class LightSource
663 {
664 public enum Type
665 {
666 HXP,
667 RL,
668 TL
669 }
670 public double position;
671 public Type type;
672 string name;
673 public LightSource()
674 {
675 }
679 public double GetPosition()
680 {
681 if (Properties.Settings.Default.LibPath.Contains("MTB"))
682 {
683 if (type == Type.HXP)
684 {
685 BioImager.MicroscopeConsole.Command com = BioImager.MicroscopeConsole.RunCommand
687 return (short)com.doubles[0];
688 }
689 else if(type == Type.RL)
690 {
691 BioImager.MicroscopeConsole.Command com = BioImager.MicroscopeConsole.RunCommand
693 return (short)com.doubles[0];
694 }
695 else if (type == Type.TL)
696 {
697 BioImager.MicroscopeConsole.Command com = BioImager.MicroscopeConsole.RunCommand
699 return (short)com.doubles[0];
700 }
701 }
702 return position;
703 }
708 public void SetPosition(double f)
709 {
710 if (Recorder.recordMicroscope)
711 Recorder.AddLine("Microscope.LightSource.SetPosition(" + f + ");");
712 if (Properties.Settings.Default.LibPath.Contains("MTB"))
713 {
714 if (type == Type.HXP)
715 {
716 BioImager.MicroscopeConsole.RunCommand(
717 new BioImager.MicroscopeConsole.Command(BioImager.MicroscopeConsole.Command.Type.SetHXP, new double[] { f }));
718 }
719 else if(type == Type.RL)
720 {
721 BioImager.MicroscopeConsole.RunCommand(
722 new BioImager.MicroscopeConsole.Command(BioImager.MicroscopeConsole.Command.Type.SetRLHalogen, new double[] { f }));
723 }
724 else if (type == Type.TL)
725 {
726 BioImager.MicroscopeConsole.RunCommand(
727 new BioImager.MicroscopeConsole.Command(BioImager.MicroscopeConsole.Command.Type.SetTLHalogen, new double[] { f }));
728 }
729 }
730 }
731 public override string ToString()
732 {
733 return name;
734 }
735 }
736 /* The class is a wrapper for the filter wheel. It has two methods, one to get the current position
737 of the filter wheel and one to set the position of the filter wheel */
738 public class FilterWheel
739 {
740 static int position;
741 public FilterWheel() { }
742 public FilterWheel(object o)
743 {
744 }
748 public int GetPosition()
749 {
750 if(Properties.Settings.Default.LibPath.Contains("MTB"))
751 {
752 BioImager.MicroscopeConsole.Command com = BioImager.MicroscopeConsole.RunCommand
754 return (int)com.doubles[0];
755 }
756 if (Properties.Settings.Default.PMicroscope)
757 {
758 int pos;
759 Microscope.pMicroscope.GetFilterWheelPosition(out pos);
760 return pos;
761 }
762 return -1;
763 }
764
769 public void SetPosition(int i)
770 {
771 if (Properties.Settings.Default.LibPath.Contains("MTB"))
772 {
773 BioImager.MicroscopeConsole.RunCommand(
774 new BioImager.MicroscopeConsole.Command(BioImager.MicroscopeConsole.Command.Type.SetFilterWheel, new double[] { i }));
775 }
776 if (Properties.Settings.Default.PMicroscope)
777 {
778 int pos;
779 Microscope.pMicroscope.SetFilterWheelPosition(i);
780 if (Recorder.recordMicroscope)
781 Recorder.AddLine("Microscope.FilterWheel.SetPosition(" + i + ");");
782 }
783 }
784 }
785
786 public static class Microscope
787 {
788 /* Defining an enum. */
789 public enum Actions
790 {
791 StageUp,
792 StageRight,
793 StageDown,
794 StageLeft,
795 StageFieldUp,
796 StageFieldRight,
797 StageFieldDown,
798 StageFieldLeft,
799 FocusUp,
800 FocusDown,
801 TL,
802 RL,
803 TakeImage,
804 TakeImageStack,
805 Acquisition,
806 Locate,
807 }
808 public static bool redraw = false;
809 public static Focus Focus = null;
810 public static Stage Stage = null;
811 public static Objectives Objectives = null;
812 public static TLShutter TLShutter = null;
813 public static RLShutter RLShutter = null;
814 public static HXPShutter HXPShutter = null;
815 public static LightSource TLHalogen = null;
816 public static LightSource RLHalogen = null;
817 public static LightSource HXP = null;
818 public static FilterWheel TLFilterWheel = null;
819 public static FilterWheel RLFilterWheel = null;
820 public static FilterWheel FilterWheel = new FilterWheel();
821 public static double UpperLimit, LowerLimit, fInterVal;
822 public static object CmdSetMode = null;
823 public static bool initialized = false;
824 public static bool ArrowKeysEnabled = true;
825 public static Point3D defaultPos = new Point3D(30000, 30000, 23900);
826 public static object root = null;
827 public static StringBuilder dllVersion = new StringBuilder();
828 public static SDK sdk;
829 public static int sessionID = -1;
830 public static string userRx = "";
831 public static PointD viewSize;
832 public static PMicroscope pMicroscope = null;
833
834 public static int ImageCount = 0;
835 /* A property that returns the value of the GetObjective method. */
836 public static Objectives.Objective Objective
837 {
838 get
839 {
840 return Objectives.GetObjective();
841 }
842 }
843
847 public static void Initialize()
848 {
849 if (initialized)
850 return;
851 int err;
852 folder = Properties.Settings.Default.ImagingPath;
853 if (Properties.Settings.Default.PMicroscope)
854 {
855 pMicroscope = new PMicroscope();
856 pMicroscope.Initialize();
857 bool b = pMicroscope.SetPosition(new Point3D(100, 100, 100));
858 Point3D p;
859 bool res = pMicroscope.GetPosition3D(out p, false);
860 bool bb = pMicroscope.TakeImage(Application.StartupPath + "\\Image");
861 Objectives = new Objectives(6);
862 Stage = new Stage();
863 Focus = new Focus();
864 }
865 else
866 if (Properties.Settings.Default.PycroManager)
867 {
868 //First we determine which MMConfig file we are using. There is currently no way of doing this programmatically. So we will ask the user.
869 if (Properties.Settings.Default.MMConfig == "")
870 {
871 OpenFileDialog fl = new OpenFileDialog();
872 fl.Filter = "Config Files (*.cfg)|*.cfg";
873 fl.InitialDirectory = Properties.Settings.Default.AppPath;
874 MessageBox.Show("Micro-Manager current configuration file not set please select the current configuration.");
875 fl.Title = "Set Current Config File (Shown in MicroManager GUI)";
876 if (fl.ShowDialog() != DialogResult.OK)
877 Application.Exit();
878 Properties.Settings.Default.MMConfig = fl.FileName;
879 }
880 MicroManager.Initialize(Properties.Settings.Default.MMConfig);
881 Objectives = new Objectives(7);
882 for (int i = 0; i < MicroManager.Objectives.List.Count; i++)
883 {
884 Objectives.List[MicroManager.Objectives.List[i].Index].Name = MicroManager.Objectives.List[i].Name;
885 }
886 Stage = new Stage();
887 Focus = new Focus();
888 }
889 else
890 if (Properties.Settings.Default.AppPath == "")
891 {
892 OpenFileDialog fl = new OpenFileDialog();
893 MessageBox.Show("Imaging application path is not set. Please set executable location.");
894 fl.Title = "Set imaging application location.";
895 if (fl.ShowDialog() != DialogResult.OK)
896 Application.Exit();
897 Properties.Settings.Default.AppPath = fl.FileName;
898 fl.Dispose();
899 }
900 else
901 if (Properties.Settings.Default.LibPath == "")
902 {
903 OpenFileDialog fl = new OpenFileDialog();
904 MessageBox.Show("Imaging library path is not set. Please set imaging library.");
905 fl.Title = "Set imaging library location.";
906 if (fl.ShowDialog() != DialogResult.OK)
907 Application.Exit();
908 Properties.Settings.Default.LibPath = fl.FileName;
909 fl.Dispose();
910 }
911 if (Properties.Settings.Default.LibPath.Contains("MTB"))
912 {
913 TLFilterWheel = new FilterWheel();
914 RLFilterWheel = new FilterWheel();
915 Stage = new Stage();
916 Focus = new Focus();
917 Objectives = new Objectives(7);
918 TLShutter = new TLShutter();
919 RLShutter = new RLShutter();
920 HXPShutter = new HXPShutter();
921 TLHalogen = new LightSource();
922 RLHalogen = new LightSource();
923 HXP = new LightSource();
924 Point3D.SetLimits(Stage.minX, Stage.maxX, Stage.minY, Stage.maxY, Focus.lowerLimit, Focus.upperLimit);
925 PointD.SetLimits(Stage.minX, Stage.maxX, Stage.minY, Stage.maxY);
926 //We calibrate the stage and focus, so that images are taken always with same calibration
927 CalibrateXYZ(0);
928 }
929 else if (Properties.Settings.Default.LibPath.Contains("Prio"))
930 {
931 sdk = new SDK();
932 /* get the version number of the dll */
933 if ((err = sdk.GetVersion(dllVersion)) != Prior.PRIOR_OK)
934 {
935 MessageBox.Show("Error getting Prior SDK version (" + err.ToString() + ")");
936 return;
937 }
938
939 /* SDK must be initialised before any real use
940 */
941 if ((err = sdk.Initialise()) != Prior.PRIOR_OK)
942 {
943 MessageBox.Show("Error initialising Prior SDK (" + err.ToString() + ")");
944 return;
945 }
946
947 /* create a session in the DLL, this gives us one controller and currently an ODS and SL160 robot loader.
948 * Multiple connections allow control of multiple stage/loaders but is outside the brief for this demo
949 */
950 if ((sessionID = sdk.OpenSession()) < 0)
951 {
952 MessageBox.Show("Error (" + sessionID.ToString() + ") Creating session in SDK " + dllVersion);
953 return;
954 }
955 int open = 0;
956 try
957 {
958 for (int port = 0; port < 10; port++)
959 {
960 //specify path name or PriorSDK.log is written to working directory
961 //priorSDK.Cmd(sessionID, "dll.log.on", ref userRx);
962 /* my controller identifies on COM1, yours will probably be different.
963 */
964 /* try to connect to the ps3 */
965 open = sdk.Cmd(sessionID, "controller.connect " + port.ToString(), ref userRx, false);
966 }
967 }
968 catch (Exception)
969 {
970
971 }
972
973 if (open != Prior.PRIOR_OK)
974 {
975 MessageBox.Show("Error (" + open.ToString() + ") connecting to stage controller ", "",
976 MessageBoxButtons.OK, MessageBoxIcon.Error);
977 return;
978 }
979 err = sdk.Cmd(sessionID, "controller.stage.hostdirection.set 1 1", ref userRx);
980 Objectives = new Objectives(sdk.GetNosePiecePositions());
981 Stage = new Stage();
982 Focus = new Focus();
983 }
984
985 RectangleD rec = GetObjectiveViewRectangle();
986 viewSize = new PointD(rec.W, rec.H);
987 Watcher();
988 initialized = true;
989 }
994 public static void CalibrateXYZ(int calibMode)
995 {
996 if (Properties.Settings.Default.LibPath.Contains("MTB"))
997 {
998 BioImager.MicroscopeConsole.RunCommand(
999 new BioImager.MicroscopeConsole.Command(BioImager.MicroscopeConsole.Command.Type.SetCalibration, new double[] { calibMode }));
1000 //We check to see if focus & stage are correctly calibrated on lower limit and perform calibration if necessary
1001 PointD cur = Stage.GetPosition(true);
1002 double z = Focus.GetFocus();
1003 //After calibration we return to the position before calibration.
1004 SetPosition(cur);
1005 Focus.SetFocus(z);
1006 }
1007 }
1008
1009
1014 public static Point3D GetPosition()
1015 {
1016 PointD p = Stage.GetPosition(false);
1017 double f = Focus.GetFocus();
1018 return new Point3D(p.X, p.Y, f);
1019 }
1020
1021
1025 public static void SetPosition(Point3D p)
1026 {
1027 Stage.SetPosition(p.X,p.Y);
1028 Focus.SetFocus(p.Z);
1029 Microscope.redraw = true;
1030 }
1031
1035 public static void SetPosition(PointD p)
1036 {
1037 Stage.SetPosition(p.X, p.Y);
1038 Microscope.redraw = true;
1039 }
1041 public static void OpenRL()
1042 {
1043 //If shutter is closed we open it.
1044 if (RLShutter.GetPosition() == 0)
1045 RLShutter.SetPosition(1);
1046 }
1047
1048
1050 public static void OpenTL()
1051 {
1052 //If shutter is closed we open it.
1053 if (TLShutter.GetPosition() == 0)
1054 TLShutter.SetPosition(1);
1055 }
1056
1058 public static void CloseRL()
1059 {
1060 //If shutter is open then we close it.
1061 if (RLShutter.GetPosition() == 0)
1062 RLShutter.SetPosition(1);
1063 }
1064
1066 public static void CloseTL()
1067 {
1068 //If shutter is open then we close it.
1069 if (TLShutter.GetPosition() == 0)
1070 TLShutter.SetPosition(1);
1071 }
1072
1076 public static void SetTL(uint tl)
1077 {
1078 TLShutter.SetPosition((short)tl);
1079 }
1080
1084 public static void SetRL(uint tr)
1085 {
1086 RLShutter.SetPosition((short)tr);
1087 }
1088
1092 public static int GetTL()
1093 {
1094 return TLShutter.GetPosition();
1095 }
1096
1100 public static int GetRL()
1101 {
1102 return RLShutter.GetPosition();
1103 }
1109 public static int GetShutterPosition(string shutter)
1110 {
1111 return MicroManager.Shutters.GetPosition(shutter);
1112 }
1118 public static void SetShutterPosition(string shutter, int state)
1119 {
1120 MicroManager.Shutters.SetPosition(shutter, state);
1121 }
1125 public static void MoveUp(double d)
1126 {
1127 Stage.MoveUp(d);
1128 }
1129
1133 public static void MoveRight(double d)
1134 {
1135 Stage.MoveRight(d);
1136 }
1137
1141 public static void MoveDown(double d)
1142 {
1143 Stage.MoveDown(d);
1144 }
1145
1149 public static void MoveLeft(double d)
1150 {
1151 Stage.MoveLeft(d);
1152 }
1154 public static void MoveFieldUp()
1155 {
1156 Stage.MoveUp(viewSize.Y);
1157 }
1159 public static void MoveFieldRight()
1160 {
1161 Stage.MoveRight(viewSize.X);
1162 }
1164 public static void MoveFieldDown()
1165 {
1166 Stage.MoveDown(viewSize.Y);
1167 }
1169 public static void MoveFieldLeft()
1170 {
1171 Stage.MoveLeft(viewSize.X);
1172 }
1177 public static void SetFocus(double d)
1178 {
1179 Focus.SetFocus(d);
1180 }
1184 public static double GetFocus()
1185 {
1186 return Focus.GetFocus();
1187 }
1188 private static string folder = "";
1195 public static string GetFolder()
1196 {
1197 object o = Recordings.GetProperty(Automation.Action.ValueType.ValuePattern, "GetFolder");
1198 string s = null;
1199 if (o != null)
1200 s = o.ToString();
1201 if (s == null && folder == "" && Properties.Settings.Default.ImagingPath == "")
1202 {
1203 FolderBrowserDialog fs = new FolderBrowserDialog();
1204 fs.Description = "Select Imaging Folder";
1205 if (fs.ShowDialog() != DialogResult.OK)
1206 {
1207 fs.Dispose();
1208 return null;
1209 }
1210 else
1211 {
1212 fs.Dispose();
1213 Properties.Settings.Default.ImagingPath = fs.SelectedPath;
1214 return fs.SelectedPath;
1215 }
1216 }
1217 else
1218 {
1219 folder = Properties.Settings.Default.ImagingPath;
1220 return folder;
1221 }
1222 }
1227 public static void SetFolder(string fol)
1228 {
1229 if (!Directory.Exists(fol))
1230 Directory.CreateDirectory(fol);
1231 if(Automation.Properties.ContainsKey("SetFolder"))
1232 Automation.SetProperty("SetFolder",fol);
1233 folder = fol;
1234 Properties.Settings.Default.ImagingPath = folder;
1235 Properties.Settings.Default.Save();
1236 }
1237
1241 public static BioImage TakeImage()
1242 {
1243 return TakeImage(true,true,false);
1244 }
1245
1246 public static BioImage TakeImage(bool save, bool addToImages, bool newTab)
1247 {
1248 if (Properties.Settings.Default.SimulateCamera && !Properties.Settings.Default.PMicroscope)
1249 {
1250 folder = GetFolder();
1251 BioImage b = MicroscopeSetup.simImage.Copy();
1252 Point3D p = GetPosition();
1253 b.Volume.Location = new Point3D(p.X,p.Y,p.Z);
1254 RectangleD view = Microscope.GetViewRectangle(true);
1255 b.Resolutions[0] = new Resolution(b.SizeX, b.SizeY, b.Buffers[0].PixelFormat,view.W / b.SizeX, view.H / b.SizeY, b.PhysicalSizeZ,p.X,p.Y,p.Z);
1256 b.Volume.Location = p;
1257 b.Volume.Width = view.W;
1258 b.Volume.Height = view.H;
1259 string file;
1260 if (folder == "" || folder == null)
1261 file = Properties.Settings.Default.ImageName + (ImageCount++) + ".ome.tif";
1262 else
1263 file = folder + "/" + Properties.Settings.Default.ImageName + (ImageCount++) + ".ome.tif";
1264 b.ID = file;
1265 b.file = file;
1266 if (addToImages)
1267 {
1268 Images.AddImage(b);
1269 App.tabsView.AddTab(b);
1270 }
1271 if(save)
1272 BioImage.SaveOME(file, b.ID);
1273 return b;
1274 }
1275 else if (Properties.Settings.Default.PMicroscope)
1276 {
1277 folder = GetFolder();
1278 string file;
1279 if (folder == "" || folder == null)
1280 file = Properties.Settings.Default.ImageName + (ImageCount++).ToString();
1281 else
1282 file = folder + "/" + Properties.Settings.Default.ImageName + (ImageCount++);
1283 pMicroscope.TakeImage(file);
1284 byte[] bts = File.ReadAllBytes(file);
1285 int w, h;
1286 PixelFormat px;
1287 Enum.TryParse<PixelFormat>(Properties.Settings.Default.PCameraFormat, out px);
1288 pMicroscope.GetSize(out w, out h);
1289 Bitmap bf = new Bitmap(file, w, h, px, bts, new ZCT(0, 0, 0), 0);
1290 bf.Stats = Statistics.FromBytes(bf);
1291 BioImage bm = new BioImage(file + ".ome.tif");
1292 bm.Buffers.Add(bf);
1293 //Set the physical size based on objective view
1294 RectangleD rec = GetViewRectangle(false);
1295 Point3D p = Microscope.GetPosition();
1296 Resolution res = new Resolution(w,h,px,rec.W / bm.SizeX,rec.H / bm.SizeY,1, p.X,p.Y,p.Z);
1297 bm.Volume.Location = p;
1298 bm.Volume.Width = rec.W;
1299 bm.Volume.Height = rec.H;
1300 bm.bitsPerPixel = bf.BitsPerPixel;
1301 bm.UpdateCoords(1, 1, 1);
1302 bm.Resolutions.Add(res);
1303 for (int c = 0; c < bf.RGBChannelsCount; c++)
1304 {
1305 bm.Channels.Add(new Channel(c, bf.BitsPerPixel, bf.RGBChannelsCount));
1306 }
1307 BioImage.AutoThreshold(bm, false);
1308 if (!imagingStack)
1309 {
1310 if (addToImages)
1311 {
1312 Images.AddImage(bm);
1313 App.tabsView.AddTab(bm);
1314 }
1315 if (save)
1316 BioImage.SaveAsync(file + ".ome.tif", bm.ID,0,true);
1317 if (bm.bitsPerPixel > 8)
1318 bm.StackThreshold(true);
1319 else
1320 bm.StackThreshold(false);
1321 }
1322 else
1323 bi.Buffers.Add(bm.Buffers[0]);
1324 //We delete the temporary file created by camera.
1325 File.Delete(file);
1326 currentImage = bm;
1327 return bi;
1328 }
1329 else if (Properties.Settings.Default.PycroManager)
1330 {
1331 folder = GetFolder();
1332 string file;
1333 if (folder == "" || folder == null)
1334 file = Properties.Settings.Default.ImageName + (ImageCount++).ToString();
1335 else
1336 file = folder + "/" + Properties.Settings.Default.ImageName + (ImageCount++);
1337 BioImage bm = MicroManager.TakeImage(file);
1338
1339 if (!imagingStack)
1340 {
1341 if (addToImages)
1342 {
1343 Images.AddImage(bm);
1344 App.tabsView.AddTab(bm);
1345 }
1346 if (save)
1347 BioImage.SaveAsync(file + ".ome.tif", bm.ID,0,true);
1348 }
1349 else
1350 {
1351 bi.Buffers.AddRange(bm.Buffers);
1352 }
1353 currentImage = bm;
1354 if (bi == null)
1355 return bm;
1356 else
1357 return bi;
1358 }
1359 else
1360 {
1361 if (Function.Functions.ContainsKey("TakeImage"))
1362 {
1363 App.imager.PerformFunction(Function.Functions["TakeImage"]);
1364 return null;
1365 }
1366 else
1367 {
1368 MessageBox.Show("Go to Microscope Setup to set 'TakeImage' function eg. a Button shortcut or GUI recording. Or set camera simulation on & set Camera image.", "Function 'TakeImage' not defined.");
1369 return null;
1370 }
1371 }
1372 }
1376 public static BioImage TakeImageStack()
1377 {
1378 return TakeImageStack(UpperLimit, LowerLimit, fInterVal);
1379 }
1380 static FileSystemWatcher watcher = new FileSystemWatcher();
1383 public static void Watcher()
1384 {
1385 string s = GetFolder();
1386 watcher.Created += fileSystemWatcher1_Created;
1387 watcher.Path = s;
1388 watcher.BeginInit();
1389 watcher.EnableRaisingEvents = true;
1390 watcher.EndInit();
1391 }
1392 static BioImage bi;
1393 static BioImage currentImage;
1394 static bool imagingStack = false;
1395 static List<BioImage> images = new List<BioImage>();
1396 static Dictionary<int,string> locked = new Dictionary<int, string>();
1398 static void LockWait()
1399 {
1400 do
1401 {
1402 Thread.Sleep(50);
1403 for (int i = 0; i < locked.Count; i++)
1404 {
1405 //We wait till the new file is no longer written into.
1406 do
1407 {
1408 List<Process> pr = FileUtil.Locking(locked[i]);
1409 FileInfo fi = new FileInfo(locked[i]);
1410 if (pr.Count == 0 && fi.Length > 0)
1411 break;
1412 fi = null;
1413 } while (true);
1414 }
1415 } while (imagingStack);
1416 }
1418 private static void WaitLocking()
1419 {
1420 System.Threading.Thread t = new Thread(LockWait);
1421 t.Start();
1422 }
1429 private static void fileSystemWatcher1_Created(object sender, FileSystemEventArgs e)
1430 {
1431 if (!Properties.Settings.Default.PMicroscope)
1432 {
1433 //We will only open files with an extension
1434 if (!Path.HasExtension(e.FullPath))
1435 return;
1436 locked.Add(ImageCount-1,e.FullPath);
1437 }
1438 }
1448 public static BioImage TakeImageStack(double UpperLimit, double LowerLimit, double interval)
1449 {
1450 imagingStack = true;
1451 //We start the locking wait loop
1452 WaitLocking();
1453 bi = new BioImage(Properties.Settings.Default.ImageName);
1454 watcher.Path = GetFolder();
1455 Focus.SetFocus(UpperLimit);
1456 double d = UpperLimit - LowerLimit;
1457 double dd = d / interval;
1458 for (int i = 0; i < dd; i++)
1459 {
1460 TakeImage();
1461 //If this is the last image we don't need to move the focus.
1462 if (i < dd)
1463 Focus.SetFocus(Focus.GetFocus() - fInterVal);
1464 Application.DoEvents();
1465 }
1466 //If we are using the imaging application to take images we need to add them to the viewer as the filesystemwatcher
1467 //can't add them due to thread access violation
1468 if (!Properties.Settings.Default.PMicroscope)
1469 {
1470 //First we wait till all the images are loaded
1471 do
1472 {
1473 Thread.Sleep(100);
1474 Application.DoEvents();
1475 } while (bi.Buffers.Count < (int)dd);
1476 }
1477
1478 bi.UpdateCoords(bi.Buffers.Count, 1, 1);
1479 for (int c = 0; c < bi.Buffers[0].RGBChannelsCount; c++)
1480 {
1481 bi.Channels.Add(new Channel(c, bi.bitsPerPixel, bi.Buffers[c].RGBChannelsCount));
1482 }
1483 //Set the physical size based on objective view
1484 RectangleD rec = GetObjectiveViewRectangle();
1485 bi.bitsPerPixel = bi.Buffers[0].BitsPerPixel;
1486 /*
1487 bi.StageSizeX = rec.X;
1488 bi.StageSizeY = rec.Y;
1489 bi.StageSizeZ = UpperLimit;
1490 */
1491 bi.Resolutions.Add(new Resolution(bi.SizeX, bi.SizeY, bi.Buffers[0].PixelFormat,
1492 rec.W / bi.SizeX, rec.H / bi.SizeY, (UpperLimit - LowerLimit) / bi.SizeZ, rec.X,rec.Y,UpperLimit));
1493 bi.Volume = new VolumeD(new Point3D(bi.StageSizeX, bi.StageSizeY, bi.StageSizeZ),new Point3D(bi.SizeX * bi.PhysicalSizeX, bi.SizeY * bi.PhysicalSizeY, bi.StageSizeZ * bi.PhysicalSizeZ));
1494 BioImage.AutoThreshold(bi, false);
1495 if (bi.bitsPerPixel > 8)
1496 bi.StackThreshold(true);
1497 else
1498 bi.StackThreshold(false);
1499 Images.AddImage(bi);
1500 App.tabsView.AddTab(bi);
1501 imagingStack = false;
1502 currentImage = bi;
1503 images.Clear();
1504 locked.Clear();
1505 ImageCount = 0;
1506 return bi;
1507 }
1512 public static BioImage[] TakeTiles(int width, int height, bool save = true, bool addToImages = true, bool newTab = false)
1513 {
1514 List<BioImage> ims = new List<BioImage>();
1515 imagingStack = false;
1516 bool leftright = false;
1517 if (Properties.Settings.Default.PMicroscope)
1518 {
1519 Point3D p = Microscope.GetPosition();
1520 //We ensure that the cached position is same as actual position
1521 Microscope.SetPosition(p);
1522 }
1523 for (int y = 0; y < height; y++)
1524 {
1525 if (y != 0)
1526 {
1527 Microscope.MoveFieldDown();
1528 }
1529 leftright = !leftright;
1530 ims.Add(Microscope.TakeImage(save, addToImages, newTab));
1531 for (int x = 0; x < width - 1; x++)
1532 {
1533 if (leftright)
1534 {
1535 Microscope.MoveFieldRight();
1536 }
1537 else
1538 {
1539 Microscope.MoveFieldLeft();
1540 }
1541 ims.Add(Microscope.TakeImage(save,addToImages,newTab));
1542 }
1543 }
1544 //If we are using the imaging application to take images we need to add them to the viewer as the filesystemwatcher
1545 //can't add them due to thread access violation
1546 if(!Properties.Settings.Default.PMicroscope)
1547 {
1548 //First we wait till all the images are loaded
1549 do
1550 {
1551 Application.DoEvents();
1552 } while (images.Count < width * height);
1553 for (int i = 0; i < images.Count; i++)
1554 {
1555 App.viewer.AddImage(images[i]);
1556 }
1557 images.Clear();
1558 }
1559 imagingStack = false;
1560 return ims.ToArray();
1561 }
1565 public static RectangleD GetObjectiveViewRectangle()
1566 {
1567 Objectives.Objective o = Objectives.GetObjective();
1568 PointD d = Stage.GetPosition(false);
1569 return new RectangleD(d.X, d.Y, o.ViewWidth, o.ViewHeight);
1570 }
1578 public static RectangleD GetViewRectangle(bool update)
1579 {
1580 if (update)
1581 {
1582 PointD d = Stage.GetPosition(false);
1583 return new RectangleD(d.X, d.Y, viewSize.X, viewSize.Y);
1584 }
1585 else
1586 {
1587 if(Properties.Settings.Default.PMicroscope)
1588 {
1589 return new RectangleD(PMicroscope.location.X, PMicroscope.location.Y, viewSize.X, viewSize.Y);
1590 }
1591 else
1592 {
1593 PointD d = Stage.GetPosition(false);
1594 return new RectangleD(d.X, d.Y, viewSize.X, viewSize.Y);
1595 }
1596 }
1597 }
1598 public static void Close()
1599 {
1600 PMicroscope.Stop();
1601 }
1602 }
1603}
static void SetProperty(string prop, string val)
static object GetProperty(string prop)
void SetPosition(int i)
void SetSWLimit(double xd, double yd)
double GetFocus()
PointD GetSWLimit()
double GetFocus(bool update)
void SetFocus(double f)
void SetPosition(int p)
object PerformFunction(Function f)
Definition Imager.cs:1343
void SetPosition(double f)
void SetPosition(int index)
void SetPosition(int p)
double GetPositionY(bool update)
PointD GetPosition(bool update)
void MoveRight(double m)
void UpdateSWLimit()
It gets the software limits of the microscope stage.
void SetPosition(double px, double py)
Definition Microscope.cs:69
double GetPositionX(bool update)
void MoveUp(double m)
void MoveDown(double m)
void SetSWLimit(double xmin, double xmax, double ymin, double ymax)
void SetPositionY(double py)
void MoveLeft(double m)
void SetPositionX(double px)
void SetPosition(int p)