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
Microscope.cs
Go to the documentation of this file.
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 {
72 Recorder.AddLine("Microscope.Stage.SetPosition(" + px + "," + py + ");");
73 x = px;
74 y = py;
75 if(Properties.Settings.Default.PMicroscope)
76 {
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 {
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 {
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 {
236 new double[] {xmin,xmax,ymin,ymax}));
237 }
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 {
260 Recorder.AddLine("Microscope.Focus.SetFocus(" + f + ");");
261 if (Properties.Settings.Default.PMicroscope)
262 {
264 Microscope.pMicroscope.SetPosition(new Point3D(p.X,p.Y,f));
265 }
266 else
267 if (Properties.Settings.Default.PycroManager)
268 {
270 MicroManager.SetPosition(new Point3D(p.X, p.Y, f));
271 }
272 else
273 if (Properties.Settings.Default.LibPath.Contains("MTB"))
274 {
277 z = f;
278 }
279 else if (Properties.Settings.Default.LibPath.Contains("Prio"))
280 {
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
339 {
340 if (Properties.Settings.Default.LibPath.Contains("MTB"))
341 {
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 {
362 new double[] { xd, yd }));
363 }
364 upperLimit = xd;
365 lowerLimit = yd;
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 {
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 {
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];
447 return;
448 }
449 else if(Properties.Settings.Default.LibPath.Contains("MTB"))
450 {
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 {
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 {
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 }
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 {
573 Recorder.AddLine("Microscope.TLShutter.SetPosition(" + p + ");");
574 if (Properties.Settings.Default.LibPath.Contains("MTB"))
575 {
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)
584 else
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 {
617 Recorder.AddLine("Microscope.TLShutter.SetPosition(" + p + ");");
618 if (Properties.Settings.Default.LibPath.Contains("MTB"))
619 {
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 {
653 new BioImager.MicroscopeConsole.Command(BioImager.MicroscopeConsole.Command.Type.SetRLShutter, new double[] { p }));
654 }
655 position = p;
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 {
711 Recorder.AddLine("Microscope.LightSource.SetPosition(" + f + ");");
712 if (Properties.Settings.Default.LibPath.Contains("MTB"))
713 {
714 if (type == Type.HXP)
715 {
718 }
719 else if(type == Type.RL)
720 {
722 new BioImager.MicroscopeConsole.Command(BioImager.MicroscopeConsole.Command.Type.SetRLHalogen, new double[] { f }));
723 }
724 else if (type == Type.TL)
725 {
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;
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 {
774 new BioImager.MicroscopeConsole.Command(BioImager.MicroscopeConsole.Command.Type.SetFilterWheel, new double[] { i }));
775 }
776 if (Properties.Settings.Default.PMicroscope)
777 {
778 int pos;
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,
793 StageDown,
794 StageLeft,
799 FocusUp,
800 FocusDown,
801 TL,
802 RL,
803 TakeImage,
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. */
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();
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 {
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 {
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();
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);
981 Stage = new Stage();
982 Focus = new Focus();
983 }
984
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 {
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)
1046 }
1047
1048
1050 public static void OpenTL()
1051 {
1052 //If shutter is closed we open it.
1053 if (TLShutter.GetPosition() == 0)
1055 }
1056
1058 public static void CloseRL()
1059 {
1060 //If shutter is open then we close it.
1061 if (RLShutter.GetPosition() == 0)
1063 }
1064
1066 public static void CloseTL()
1067 {
1068 //If shutter is open then we close it.
1069 if (TLShutter.GetPosition() == 0)
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 {
1157 }
1159 public static void MoveFieldRight()
1160 {
1162 }
1164 public static void MoveFieldDown()
1165 {
1167 }
1169 public static void MoveFieldLeft()
1170 {
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 == "")
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();
1252 Point3D p = GetPosition();
1253 b.Volume.Location = new Point3D(p.X,p.Y,p.Z);
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.UpdateCoords(1, 1, 1);
1301 bm.Resolutions.Add(res);
1302 for (int c = 0; c < bf.RGBChannelsCount; c++)
1303 {
1304 bm.Channels.Add(new Channel(c, bf.BitsPerPixel, bf.RGBChannelsCount));
1305 }
1306 BioImage.AutoThreshold(bm, false);
1307 if (!imagingStack)
1308 {
1309 if (addToImages)
1310 {
1311 Images.AddImage(bm);
1312 App.tabsView.AddTab(bm);
1313 }
1314 if (save)
1315 BioImage.SaveAsync(file + ".ome.tif", bm.ID,0,true);
1316 if (bm.bitsPerPixel > 8)
1317 bm.StackThreshold(true);
1318 else
1319 bm.StackThreshold(false);
1320 }
1321 else
1322 bi.Buffers.Add(bm.Buffers[0]);
1323 //We delete the temporary file created by camera.
1324 File.Delete(file);
1325 currentImage = bm;
1326 return bi;
1327 }
1328 else if (Properties.Settings.Default.PycroManager)
1329 {
1330 folder = GetFolder();
1331 string file;
1332 if (folder == "" || folder == null)
1333 file = Properties.Settings.Default.ImageName + (ImageCount++).ToString();
1334 else
1335 file = folder + "/" + Properties.Settings.Default.ImageName + (ImageCount++);
1336 BioImage bm = MicroManager.TakeImage(file);
1337
1338 if (!imagingStack)
1339 {
1340 if (addToImages)
1341 {
1342 Images.AddImage(bm);
1343 App.tabsView.AddTab(bm);
1344 }
1345 if (save)
1346 BioImage.SaveAsync(file + ".ome.tif", bm.ID,0,true);
1347 }
1348 else
1349 {
1350 bi.Buffers.AddRange(bm.Buffers);
1351 }
1352 currentImage = bm;
1353 if (bi == null)
1354 return bm;
1355 else
1356 return bi;
1357 }
1358 else
1359 {
1360 if (Function.Functions.ContainsKey("TakeImage"))
1361 {
1363 return null;
1364 }
1365 else
1366 {
1367 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.");
1368 return null;
1369 }
1370 }
1371 }
1375 public static BioImage TakeImageStack()
1376 {
1378 }
1379 static FileSystemWatcher watcher = new FileSystemWatcher();
1382 public static void Watcher()
1383 {
1384 string s = GetFolder();
1385 watcher.Created += fileSystemWatcher1_Created;
1386 watcher.Path = s;
1387 watcher.BeginInit();
1388 watcher.EnableRaisingEvents = true;
1389 watcher.EndInit();
1390 }
1391 static BioImage bi;
1392 static BioImage currentImage;
1393 static bool imagingStack = false;
1394 static List<BioImage> images = new List<BioImage>();
1395 static Dictionary<int,string> locked = new Dictionary<int, string>();
1397 static void LockWait()
1398 {
1399 do
1400 {
1401 Thread.Sleep(50);
1402 for (int i = 0; i < locked.Count; i++)
1403 {
1404 //We wait till the new file is no longer written into.
1405 do
1406 {
1407 List<Process> pr = FileUtil.Locking(locked[i]);
1408 FileInfo fi = new FileInfo(locked[i]);
1409 if (pr.Count == 0 && fi.Length > 0)
1410 break;
1411 fi = null;
1412 } while (true);
1413 }
1414 } while (imagingStack);
1415 }
1417 private static void WaitLocking()
1418 {
1419 System.Threading.Thread t = new Thread(LockWait);
1420 t.Start();
1421 }
1428 private static void fileSystemWatcher1_Created(object sender, FileSystemEventArgs e)
1429 {
1430 if (!Properties.Settings.Default.PMicroscope)
1431 {
1432 //We will only open files with an extension
1433 if (!Path.HasExtension(e.FullPath))
1434 return;
1435 locked.Add(ImageCount-1,e.FullPath);
1436 }
1437 }
1447 public static BioImage TakeImageStack(double UpperLimit, double LowerLimit, double interval)
1448 {
1449 imagingStack = true;
1450 //We start the locking wait loop
1451 WaitLocking();
1452 bi = new BioImage(Properties.Settings.Default.ImageName);
1453 watcher.Path = GetFolder();
1455 double d = UpperLimit - LowerLimit;
1456 double dd = d / interval;
1457 for (int i = 0; i < dd; i++)
1458 {
1459 TakeImage();
1460 //If this is the last image we don't need to move the focus.
1461 if (i < dd)
1463 Application.DoEvents();
1464 }
1465 //If we are using the imaging application to take images we need to add them to the viewer as the filesystemwatcher
1466 //can't add them due to thread access violation
1467 if (!Properties.Settings.Default.PMicroscope)
1468 {
1469 //First we wait till all the images are loaded
1470 do
1471 {
1472 Thread.Sleep(100);
1473 Application.DoEvents();
1474 } while (bi.Buffers.Count < (int)dd);
1475 }
1476
1477 bi.UpdateCoords(bi.Buffers.Count, 1, 1);
1478 for (int c = 0; c < bi.Buffers[0].RGBChannelsCount; c++)
1479 {
1480 bi.Channels.Add(new Channel(c, bi.bitsPerPixel, bi.Buffers[c].RGBChannelsCount));
1481 }
1482 //Set the physical size based on objective view
1484 /*
1485 bi.StageSizeX = rec.X;
1486 bi.StageSizeY = rec.Y;
1487 bi.StageSizeZ = UpperLimit;
1488 */
1489 bi.Resolutions.Add(new Resolution(bi.SizeX, bi.SizeY, bi.Buffers[0].PixelFormat,
1490 rec.W / bi.SizeX, rec.H / bi.SizeY, (UpperLimit - LowerLimit) / bi.SizeZ, rec.X,rec.Y,UpperLimit));
1491 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));
1492 BioImage.AutoThreshold(bi, false);
1493 if (bi.bitsPerPixel > 8)
1494 bi.StackThreshold(true);
1495 else
1496 bi.StackThreshold(false);
1497 Images.AddImage(bi);
1498 App.tabsView.AddTab(bi);
1499 imagingStack = false;
1500 currentImage = bi;
1501 images.Clear();
1502 locked.Clear();
1503 ImageCount = 0;
1504 return bi;
1505 }
1510 public static BioImage[] TakeTiles(int width, int height, bool save = true, bool addToImages = true, bool newTab = false)
1511 {
1512 List<BioImage> ims = new List<BioImage>();
1513 imagingStack = false;
1514 bool leftright = false;
1515 if (Properties.Settings.Default.PMicroscope)
1516 {
1517 Point3D p = Microscope.GetPosition();
1518 //We ensure that the cached position is same as actual position
1520 }
1521 for (int y = 0; y < height; y++)
1522 {
1523 if (y != 0)
1524 {
1526 }
1527 leftright = !leftright;
1528 ims.Add(Microscope.TakeImage(save, addToImages, newTab));
1529 for (int x = 0; x < width - 1; x++)
1530 {
1531 if (leftright)
1532 {
1534 }
1535 else
1536 {
1538 }
1539 ims.Add(Microscope.TakeImage(save,addToImages,newTab));
1540 }
1541 }
1542 //If we are using the imaging application to take images we need to add them to the viewer as the filesystemwatcher
1543 //can't add them due to thread access violation
1544 if(!Properties.Settings.Default.PMicroscope)
1545 {
1546 //First we wait till all the images are loaded
1547 do
1548 {
1549 Application.DoEvents();
1550 } while (images.Count < width * height);
1551 for (int i = 0; i < images.Count; i++)
1552 {
1553 App.viewer.AddImage(images[i]);
1554 }
1555 images.Clear();
1556 }
1557 imagingStack = false;
1558 return ims.ToArray();
1559 }
1564 {
1566 PointD d = Stage.GetPosition(false);
1567 return new RectangleD(d.X, d.Y, o.ViewWidth, o.ViewHeight);
1568 }
1576 public static RectangleD GetViewRectangle(bool update)
1577 {
1578 if (update)
1579 {
1580 PointD d = Stage.GetPosition(false);
1581 return new RectangleD(d.X, d.Y, viewSize.X, viewSize.Y);
1582 }
1583 else
1584 {
1585 if(Properties.Settings.Default.PMicroscope)
1586 {
1588 }
1589 else
1590 {
1591 PointD d = Stage.GetPosition(false);
1592 return new RectangleD(d.X, d.Y, viewSize.X, viewSize.Y);
1593 }
1594 }
1595 }
1596 public static void Close()
1597 {
1598 PMicroscope.Stop();
1599 }
1600 }
1601}
AForge.Bitmap Bitmap
AForge.RectangleD RectangleD
AForge.Point Point
System.Windows.Forms.Application Application
Definition ImageView.cs:14
AForge.PointD PointD
AForge.PixelFormat PixelFormat
BioLib.BioImage BioImage
Definition TabsView.cs:15
AForge.ZCT ZCT
Definition TabsView.cs:17
static TabsView tabsView
Definition App.cs:16
static ImageView viewer
Definition App.cs:23
static Imager imager
Definition App.cs:20
static void SetProperty(string prop, string val)
static object GetProperty(string prop)
static Dictionary< string, Recording > Properties
Definition Automation.cs:15
static List< Process > Locking(string path)
Definition Win32.cs:277
void SetPosition(int i)
void SetSWLimit(double xd, double yd)
double GetFocus()
PointD GetSWLimit()
double GetFocus(bool update)
static double lowerLimit
static double upperLimit
void SetFocus(double f)
static Dictionary< string, Function > Functions
void SetPosition(int p)
static object tlShutter
void UpdateView(bool refreshImages=true)
void AddImage(BioImage im)
Definition ImageView.cs:475
object PerformFunction(Function f)
Definition Imager.cs:1343
override string ToString()
void SetPosition(double f)
static List< Objective > List
static int GetPosition(string shutterName)
static void SetPosition(string shutterName, int state)
static void SetPosition(Point3D p)
static bool Initialize(string config)
static void GetPosition(out PointD p, bool update)
static void GetPosition3D(out Point3D p, bool update)
static BioImage TakeImage(string name)
static Command RunCommand(Command c)
static double LowerLimit
static Objectives Objectives
static BioImage[] TakeTiles(int width, int height, bool save=true, bool addToImages=true, bool newTab=false)
static void SetPosition(PointD p)
static void MoveFieldUp()
Move the field up by the height of the view.
static double UpperLimit
static string GetFolder()
static StringBuilder dllVersion
static LightSource HXP
static void CloseRL()
If the shutter is open, then close it.
static void SetPosition(Point3D p)
static PMicroscope pMicroscope
static void OpenRL()
If the shutter is closed, open it.
static RectangleD GetObjectiveViewRectangle()
static void CalibrateXYZ(int calibMode)
static string userRx
static RectangleD GetViewRectangle(bool update)
static PointD viewSize
static Focus Focus
static FilterWheel RLFilterWheel
static void CloseTL()
If the shutter is open, then we close it.
static void MoveRight(double d)
static RLShutter RLShutter
static BioImage TakeImageStack(double UpperLimit, double LowerLimit, double interval)
static Point3D GetPosition()
static void MoveFieldDown()
Move the field down by one unit.
static BioImage TakeImage()
static bool ArrowKeysEnabled
static void SetFocus(double d)
static void SetFolder(string fol)
static Stage Stage
static void MoveDown(double d)
static void OpenTL()
If the shutter is closed, open it.
static void MoveFieldRight()
Move the field right by the width of the view.
static object CmdSetMode
static BioImage TakeImage(bool save, bool addToImages, bool newTab)
static void MoveLeft(double d)
static int GetShutterPosition(string shutter)
Gets the current position of a named shutter.
static Point3D defaultPos
static FilterWheel TLFilterWheel
static TLShutter TLShutter
static int ImageCount
static void SetShutterPosition(string shutter, int state)
Sets the current position of a named shutter.
static BioImage TakeImageStack()
static object root
static void MoveFieldLeft()
Move the field left by the width of the viewport.
static void Initialize()
static LightSource TLHalogen
static LightSource RLHalogen
static HXPShutter HXPShutter
static Objectives.Objective Objective
static double fInterVal
static void MoveUp(double d)
static double GetFocus()
static void SetRL(uint tr)
static void Watcher()
the textbox, sets the watcher's path to that folder, and enables the watcher.
static bool initialized
static void SetTL(uint tl)
static FilterWheel FilterWheel
static void Close()
Dictionary< string, object > config
List< Objective > List
void SetPosition(int index)
Objective GetObjective()
Objectives(int count)
bool GetFilterWheelPosition(out int p)
bool TakeImage(string path)
bool GetPosition3D(out Point3D p, bool update)
bool GetPosition(out PointD p, bool update)
bool SetPosition(Point3D p)
bool GetSize(out int width, out int height)
bool SetFilterWheelPosition(int p)
const int PRIOR_OK
SDK ERROR CODES returned directly by the SDk.
Definition constants.cs:13
static Type rlType
static object rlShutter
void SetPosition(int p)
static bool recordMicroscope
Definition Recorder.cs:21
static void AddLine(string s)
Definition Recorder.cs:16
static object GetProperty(Automation.Action.ValueType automation, string pro)
int GetNosePiece()
Definition SDK.cs:235
PointD GetPosition()
Definition SDK.cs:156
int Initialise()
Definition SDK.cs:339
bool shutterOpen()
Definition SDK.cs:279
double GetZ()
Definition SDK.cs:217
int Cmd(int session, string usertx, ref string userrx, bool displayError=true)
Definition SDK.cs:354
int OpenSession()
Definition SDK.cs:344
bool shutterClose()
Definition SDK.cs:288
bool SetPosition(PointD p)
Definition SDK.cs:147
int GetNosePiecePositions()
Definition SDK.cs:243
bool SetZ(double d)
Definition SDK.cs:207
int GetVersion(StringBuilder version)
Definition SDK.cs:334
Stage(object st)
Definition Microscope.cs:28
static double maxX
Definition Microscope.cs:24
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)
static double minX
Definition Microscope.cs:23
static double maxY
Definition Microscope.cs:26
void SetPositionX(double px)
static double minY
Definition Microscope.cs:25
static int position
void SetPosition(int p)
static object tlShutter
static Type tlType
void AddTab(BioImage b)
Definition TabsView.cs:134