-
Notifications
You must be signed in to change notification settings - Fork 1
/
Microscope.cs
1227 lines (1156 loc) · 45.7 KB
/
Microscope.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Globalization;
using System.Drawing;
using System.Diagnostics;
using System.Threading;
using System.Reflection;
using System.IO;
using Newtonsoft.Json;
namespace MicroscopeConsole
{
public static class CommandRunner
{
[Serializable]
public struct Command
{
public enum Type
{
SetStage,
SetFocus,
SetObjective,
SetFilterWheel,
SetHXP,
SetRLHalogen,
SetTLHalogen,
SetHXPShutter,
SetRLShutter,
SetTLShutter,
SetStageSWLimit,
SetFocusSWLimit,
SetCalibration,
GetStage,
GetFocus,
GetObjective,
GetFilterWheel,
GetHXP,
GetRLHalogen,
GetTLHalogen,
GetHXPShutter,
GetRLShutter,
GetTLShutter,
GetStageSWLimit,
GetFocusSWLimit,
GetCalibration,
}
public double[] doubles;
public Command.Type type;
public Command(Type t, double[] ds)
{
doubles = ds;
type = t;
}
}
public static void Run(Command c)
{
switch (c.type)
{
case Command.Type.SetStage:
{
Microscope.Stage.SetPosition(c.doubles[0], c.doubles[1]);
Output(c.type, c); break;
}
case Command.Type.SetFocus:
{
Microscope.Focus.SetFocus(c.doubles[0]);
Output(c.type, c); break;
}
case Command.Type.SetObjective:
{
Microscope.Objectives.SetPosition((int)c.doubles[0]);
Output(c.type, c); break;
}
case Command.Type.SetFilterWheel:
{
Microscope.FilterWheel.SetPosition((int)c.doubles[0]);
Output(c.type, c); break;
}
case Command.Type.SetHXP:
{
Microscope.HXP.SetPosition(c.doubles[0]);
Output(c.type, c); break;
}
case Command.Type.SetRLHalogen:
{
Microscope.RLHalogen.SetPosition(c.doubles[0]);
Output(c.type, c); break;
}
case Command.Type.SetTLHalogen:
{
Microscope.TLHalogen.SetPosition(c.doubles[0]);
Output(c.type, c); break;
}
case Command.Type.SetHXPShutter:
{
Microscope.HXPShutter.SetPosition((int)c.doubles[0]);
Output(c.type, c); break;
}
case Command.Type.SetRLShutter:
{
Microscope.RLShutter.SetPosition((int)c.doubles[0]);
Output(c.type, c); break;
}
case Command.Type.SetTLShutter:
{
Microscope.TLShutter.SetPosition((int)c.doubles[0]);
Output(c.type, c); break;
}
case Command.Type.SetStageSWLimit:
{
Microscope.Stage.SetSWLimit(c.doubles[0], c.doubles[1], c.doubles[2], c.doubles[3]);
Output(c.type, c); break;
}
case Command.Type.SetCalibration:
{
Microscope.CalibrateXYZ((Microscope.Calibration)c.doubles[0]);
Output(c.type, c); break;
}
case Command.Type.GetStage:
Output(c.type, Microscope.Stage.GetPosition(true)); break;
case Command.Type.GetFocus:
Output(c.type, Microscope.Focus.GetFocus()); break;
case Command.Type.GetObjective:
Output(c.type, (double)Microscope.Objectives.GetPosition()); break;
case Command.Type.GetFilterWheel:
Output(c.type, (double)Microscope.FilterWheel.GetPosition()); break;
case Command.Type.GetHXP:
Output(c.type, Microscope.HXP.GetPosition()); break;
case Command.Type.GetRLHalogen:
Output(c.type, Microscope.RLHalogen.GetPosition()); break;
case Command.Type.GetTLHalogen:
Output(c.type, Microscope.TLHalogen.GetPosition()); break;
case Command.Type.GetHXPShutter:
Output(c.type, (double)Microscope.HXPShutter.GetPosition()); break;
case Command.Type.GetRLShutter:
Output(c.type, (double)Microscope.RLShutter.GetPosition()); break;
case Command.Type.GetTLShutter:
Output(c.type, (double)Microscope.TLShutter.GetPosition()); break;
case Command.Type.GetStageSWLimit:
Output(c.type, Microscope.Stage.GetSWLimit()); break;
case Command.Type.GetFocusSWLimit:
Output(c.type, Microscope.Focus.GetSWLimit()); break;
default:break;
}
}
private static void Output(Command.Type t, object p)
{
string s = "";
if(p.GetType() == typeof(Command))
{
s = JsonConvert.SerializeObject((Command)p);
}
else if(p.GetType() == typeof(PointD))
{
PointD pd = (PointD)p;
Command com = new Command(t, new double[] {pd.X,pd.Y});
s = JsonConvert.SerializeObject(com);
}
else if (p.GetType() == typeof(double))
{
Command com = new Command(t, new double[] { (double)p});
s = JsonConvert.SerializeObject(com);
}
Console.WriteLine(s);
}
}
/* It's a wrapper for the stage*/
public class Stage
{
public static object stage;
public static Type stageType;
public static Type axisType;
public static object xAxis;
public static object yAxis;
public static double minX;
public static double maxX;
public static double minY;
public static double maxY;
/* Creating a new instance of the Stage class. */
public Stage(object st)
{
stageType = Microscope.Types["IMTBStage"];
stage = st;
axisType = Microscope.Types["IMTBAxis"];
xAxis = Microscope.GetProperty("IMTBStage", "XAxis", Stage.stage);
yAxis = Microscope.GetProperty("IMTBStage", "YAxis", Stage.stage);
CommandRunner.Command com = GetPosition(true);
x = com.doubles[0];
y = com.doubles[1];
GetSWLimit();
}
public Stage()
{
}
private double x;
private double y;
public double X
{
get
{
return x;
}
}
public double Y
{
get
{
return y;
}
}
public int moveWait = 250;
private void MoveWait()
{
Thread.Sleep(moveWait);
}
/// The function sets the position of the stage to the given coordinates
///
/// @param px x position in microns
/// @param py y-coordinate
public void SetPosition(double px, double py)
{
x = px;
y = py;
object[] setPosArgs = new object[5];
setPosArgs[0] = px;
setPosArgs[1] = py;
setPosArgs[2] = "µm";
setPosArgs[3] = Microscope.CmdSetMode;
setPosArgs[4] = 10000;
bool resStage = (bool)stageType.InvokeMember("SetPosition", BindingFlags.InvokeMethod, null, stage, setPosArgs);
}
/// This function sets the position of the object to the given x and y coordinates
///
/// @param px The new x position of the object
public void SetPositionX(double px)
{
x = px;
SetPosition(px, y);
}
/// This function sets the position of the object to the given x and y coordinates
///
/// @param py The y position of the object
public void SetPositionY(double py)
{
y = py;
SetPosition(x, py);
}
/// This function returns the X position of the mouse cursor
///
/// @param update If true, the position will be updated before returning the value.
///
/// @return The position of the object in the X axis.
public double GetPositionX(bool update)
{
x = GetPosition(update).doubles[0];
return x;
}
/// GetPositionY() returns the Y coordinate of the current position of the mouse cursor
///
/// @param update If true, the position will be updated before returning the value.
///
/// @return The position of the object in the Y axis.
public double GetPositionY(bool update)
{
y = GetPosition(update).doubles[1];
return y;
}
/// If the microscope is a Prior, get the position from the Prior SDK. If the microscope is a
/// MTB, get the position from the MTB SDK. If the microscope is a Piezosystem, get the position
/// from the Piezosystem SDK
///
/// @param update true if the position should be updated from the microscope, false if the
/// position should be returned from the cache.
///
/// @return A PointD object.
public CommandRunner.Command GetPosition(bool update)
{
object[] args = new object[3];
args[2] = "µm";
Microscope.Types["IMTBStage"].InvokeMember("GetPosition", BindingFlags.InvokeMethod, null, stage, args);
x = (double)args[0];
y = (double)args[1];
return new CommandRunner.Command(CommandRunner.Command.Type.GetStage,new double[] { (double)args[0], (double)args[1] });
}
/// Move the stage up by the specified amount of microns
///
/// @param m The amount of pixels to move the object up by.
public void MoveUp(double m)
{
double y = GetPositionY(true) - m;
SetPositionY(y);
}
/// Move the stage down by the specified amount of microns
///
/// @param m The amount to move the object by.
public void MoveDown(double m)
{
double y = GetPositionY(true) + m;
SetPositionY(y);
}
/// Move the stage right by the specified amount of microns
///
/// @param m The amount to move the object by.
public void MoveRight(double m)
{
double x = GetPositionX(true) + m;
SetPositionX(x);
}
/// Move the stage left by the specified amount of microns
///
/// @param m The amount to move the object by.
public void MoveLeft(double m)
{
double x = GetPositionX(true) - m;
SetPositionX(x);
}
/// It gets the software limits of the microscope stage
public CommandRunner.Command GetSWLimit()
{
object[] args = new object[2];
args[0] = true;
args[1] = "µm";
maxX = (double)axisType.InvokeMember("GetSWLimit", BindingFlags.InvokeMethod, null, xAxis, args);
args[0] = false;
minX = (double)axisType.InvokeMember("GetSWLimit", BindingFlags.InvokeMethod, null, xAxis, args);
args[0] = true;
maxY = (double)axisType.InvokeMember("GetSWLimit", BindingFlags.InvokeMethod, null, yAxis, args);
args[0] = false;
minY = (double)axisType.InvokeMember("GetSWLimit", BindingFlags.InvokeMethod, null, yAxis, args);
return new CommandRunner.Command(CommandRunner.Command.Type.GetStageSWLimit, new double[] { minX, minY, maxX, maxY });
}
/// It sets the limits of the stage movement
///
/// @param xmin minimum x value
/// @param xmax the maximum x value
/// @param ymin -0.0025
/// @param ymax -0.0015
public void SetSWLimit(double xmin, double xmax, double ymin, double ymax)
{
object[] args = new object[3];
args[0] = true;
args[1] = xmax;
args[2] = "µm";
axisType.InvokeMember("SetSWLimit", BindingFlags.InvokeMethod, null, xAxis, args);
args[0] = false;
args[1] = xmin;
axisType.InvokeMember("SetSWLimit", BindingFlags.InvokeMethod, null, xAxis, args);
args[0] = true;
args[1] = ymax;
args[2] = "µm";
axisType.InvokeMember("SetSWLimit", BindingFlags.InvokeMethod, null, yAxis, args);
args[0] = false;
args[1] = ymin;
axisType.InvokeMember("SetSWLimit", BindingFlags.InvokeMethod, null, yAxis, args);
}
}
/* The Focus class is used to set and get the focus of the microscope */
public class Focus
{
public static Type focusType;
public static Type axisType;
public static object focus;
public static double upperLimit;
public static double lowerLimit;
private static double z;
/* Creating a new instance of the Focus class. */
public Focus(object foc)
{
focus = foc;
axisType = Microscope.Types["IMTBAxis"];
}
public Focus()
{
}
/// Sets the z-axis based on microscope configuration.
///
/// @param f the focus value
public void SetFocus(double f)
{
object[] args = new object[3];
args[0] = f;
args[1] = "µm";
args[2] = Microscope.CmdSetMode;
bool resFoc = (bool)Microscope.Types["IMTBContinual"].InvokeMember("SetPosition", BindingFlags.InvokeMethod, null, focus, args);
z = f;
}
/// This function returns the z-axis position
///
/// @return The z-axis position of the microscope.
public double GetFocus()
{
return GetFocus(false);
}
/// If the microscope is a Prior, get the Z position from the SDK. If it's a MTB, get the Z
/// position from the MTB SDK. If it's a PMicroscope, get the Z position from the PMicroscope
/// SDK. If it's a custom microscope, get the Z position from the custom SDK
///
/// @param update boolean, if true, the focus is updated from the microscope
///
/// @return The z position of the microscope.
public double GetFocus(bool update)
{
object[] getPosFocArgs = new object[1];
getPosFocArgs[0] = "µm";
z = (double)Microscope.Types["IMTBContinual"].InvokeMember("GetPosition", BindingFlags.InvokeMethod, null, focus, getPosFocArgs);
return z;
}
/// It gets the lower and upper limits of the focus axis
///
/// @return The return value is a PointD object.
public PointD GetSWLimit()
{
PointD d = new PointD();
object[] args = new object[2];
args[0] = true;
args[1] = "µm";
d.X = (double)axisType.InvokeMember("GetSWLimit", BindingFlags.InvokeMethod, null, focus, args);
args[0] = false;
d.Y = (double)axisType.InvokeMember("GetSWLimit", BindingFlags.InvokeMethod, null, focus, args);
upperLimit = d.X;
lowerLimit = d.Y;
return d;
}
/// The function takes two double values, xd and yd, and sets the upper and lower limits of the
/// focus axis
///
/// @param xd the upper limit of the focus
/// @param yd the lower limit of the focus
public void SetSWLimit(double xd, double yd)
{
object[] args = new object[3];
args[0] = true;
args[1] = xd;
args[2] = "µm";
axisType.InvokeMember("SetSWLimit", BindingFlags.InvokeMethod, null, focus, args);
args[0] = false;
args[1] = yd;
axisType.InvokeMember("SetSWLimit", BindingFlags.InvokeMethod, null, focus, args);
upperLimit = xd;
lowerLimit = yd;
}
}
/* The Objectives class is a class that contains a list of objectives */
public class Objectives
{
public List<Objective> List = new List<Objective>();
public static Type changerType = null;
public static object changer;
/* Creating a list of objectives. */
public Objectives(object objs)
{
changerType = Microscope.Types["IMTBChanger"];
changer = objs;
int count = (int)changerType.InvokeMember("GetElementCount", BindingFlags.InvokeMethod, null, objs, null);
object[] args3 = new object[1];
for (int i = 0; i < count; i++)
{
args3[0] = i;
object o = changerType.InvokeMember("GetElement", BindingFlags.InvokeMethod, null, objs, args3);
if (o != null)
List.Add(new Objective(o, i));
}
}
/* Creating a list of objectives. */
public Objectives(int count)
{
for (int i = 0; i < count; i++)
{
List.Add(new Objective(null, i));
}
}
/* The Objective class is a class that contains all the information about the objective */
public class Objective
{
public Dictionary<string, object> config = new Dictionary<string, object>();
public string Name = "";
public string UniqueName = "";
public string ElementType = "";
public bool Oil = false;
public int Magnification = 0;
public float NumericAperture = 0;
public int Index;
public string Modes = "";
public string Features = "";
public double LocateExposure = 50;
public int WorkingDistance = 0;
public double AcquisitionExposure = 50;
public string Configuration = "";
public double MoveAmountL = 40;
public double MoveAmountR = 10;
public double FocusMoveAmount = 0.02;
public double ViewWidth;
public double ViewHeight;
public Objective(object o,int index)
{
Type t = Microscope.Types["IMTBChangerElement"];
Type id = Microscope.Types["IMTBIdent"];
Name = (string)id.InvokeMember("get_Name", BindingFlags.InvokeMethod, null, o, null);
UniqueName = (string)id.InvokeMember("get_UniqueName", BindingFlags.InvokeMethod, null, o, null);
Configuration = (string)id.InvokeMember("GetConfiguration", BindingFlags.InvokeMethod, null, o, null);
string[] sts = Configuration.Split('>');
for (int i = 0; i < sts.Length; i++)
{
string item = sts[i];
int ind = item.IndexOf("</");
if (item.StartsWith("<") || item.Length == 0)
continue;
string s = item.Remove(ind, item.Length - ind);
if (i == 2)
Magnification = int.Parse(s);
else
if (i == 4)
NumericAperture = float.Parse(s, CultureInfo.InvariantCulture);
else
if (i == 6)
{
if (s.Contains("Air"))
Oil = false;
else
Oil = true;
}
else
if (i == 8)
{
Modes = s;
}
else
if (i == 10)
{
Features = s;
}
else
if (i == 12)
{
WorkingDistance = int.Parse(s);
}
}
Index = index;
}
public Objective()
{
}
public override string ToString()
{
return Name.ToString() + " " + Index;
}
}
private int index;
public int moveWait = 1000;
/// It waits for a second to give time for stage movements.
private void MoveWait()
{
Thread.Sleep(moveWait);
}
public int Index
{
get
{
return GetPosition();
}
set
{
SetPosition(value);
}
}
/// The function is called with an integer argument, and it sets the microscope objective to
/// that integer
///
/// @param index the index of the objective to be set
///
/// @return The return value is a boolean.
public void SetPosition(int index)
{
this.index = index;
object[] setObjPosArgs = new object[3];
setObjPosArgs[0] = (short)index;
setObjPosArgs[1] = Microscope.CmdSetMode;
setObjPosArgs[2] = 10000;
bool resObj = (bool)changerType.InvokeMember("SetPosition", BindingFlags.InvokeMethod, null, changer, setObjPosArgs);
}
public int GetPosition()
{
return List[(short)changerType.InvokeMember("get_Position", BindingFlags.InvokeMethod, null, changer, null)].Index;
}
/// If the library path contains "MTB", then return the list at the position of the
/// changerType.InvokeMember("get_Position", BindingFlags.InvokeMethod, null, changer, null)
///
/// If the library path does not contain "MTB", then return the list at the index
///
/// @return The Objective object.
public Objective GetObjective()
{
return List[(short)changerType.InvokeMember("get_Position", BindingFlags.InvokeMethod, null, changer, null)];
}
}
/* The TLShutter class is a wrapper class for the shutter object */
public class TLShutter
{
public static Type tlType;
public static object tlShutter = null;
public static int position;
/* Creating a new instance of the TLShutter class. */
public TLShutter(object tlShut)
{
tlShutter = tlShut;
tlType = Microscope.Types["IMTBChanger"];
}
/// If the library path contains "MTB", then invoke the get_Position method on the tlShutter
/// object. Otherwise, return the position variable
///
/// @return The position of the shutter.
public short GetPosition()
{
return (short)tlType.InvokeMember("get_Position", BindingFlags.InvokeMethod, null, tlShutter, null);
}
/// The function takes an integer as an argument and sets the position of the shutter to the
/// value of the integer.
///
/// The function is called by the following line of code:
///
/// @param p 0 or 1
public void SetPosition(int p)
{
object[] args = new object[2];
args[0] = (short)p;
args[1] = Activator.CreateInstance(Microscope.Types["MTBCmdSetModes"]);
bool res = (bool)tlType.InvokeMember("SetPosition", BindingFlags.InvokeMethod, null, tlShutter, args);
position = p;
}
}
/* The HXPShutter class is a wrapper class for the TLShutter class */
public class HXPShutter
{
public static Type tlType;
public static object tlShutter = null;
public static int position;
/* Creating a new instance of the TLShutter class. */
public HXPShutter(object tlShut)
{
tlShutter = tlShut;
tlType = Microscope.Types["IMTBChanger"];
}
/// If the library path contains "MTB", then invoke the get_Position method on the tlShutter
/// object. Otherwise, return the position variable
///
/// @return The position of the shutter.
public short GetPosition()
{
return (short)tlType.InvokeMember("get_Position", BindingFlags.InvokeMethod, null, tlShutter, null);
}
/// The function takes an integer as an argument and sets the position of the shutter to the
/// value of the integer.
///
/// The function is called by the following line of code:
///
/// @param p 0 or 1
public void SetPosition(int p)
{
object[] args = new object[2];
args[0] = (short)p;
args[1] = Activator.CreateInstance(Microscope.Types["MTBCmdSetModes"]);
bool res = (bool)tlType.InvokeMember("SetPosition", BindingFlags.InvokeMethod, null, tlShutter, args);
position = p;
}
}
/* The RLShutter class is a wrapper class for the IMTBChanger interface */
public class RLShutter
{
public static Type rlType;
public static object rlShutter = null;
public static int position;
/* Creating a new instance of the RLShutter class. */
public RLShutter(object rlShut)
{
rlShutter = rlShut;
rlType = Microscope.Types["IMTBChanger"];
}
/// If the library path contains "MTB", then return the position of the shutter, otherwise
/// return the position of the shutter
///
/// @return The position of the shutter.
public short GetPosition()
{
return (short)rlType.InvokeMember("get_Position", BindingFlags.InvokeMethod, null, rlShutter, null);
}
/// The function takes an integer as an argument, and if the microscope is an MTB, it sets the
/// position of the RLShutter to the value of the integer
///
/// @param p
public void SetPosition(int p)
{
object[] args = new object[2];
args[0] = (short)p;
args[1] = Activator.CreateInstance(Microscope.Types["MTBCmdSetModes"]);
bool res = (bool)rlType.InvokeMember("SetPosition", BindingFlags.InvokeMethod, null, rlShutter, args);
position = p;
}
}
/* The LightSource class is a wrapper class for the IMTBContinual interface. */
public class LightSource
{
public Type continualType;
public object light = null;
public double position;
string name;
public LightSource()
{
}
/* Creating a new instance of the LightSource class. */
public LightSource(object tlShut,string name)
{
this.name = name;
light = tlShut;
continualType = Microscope.Types["IMTBContinual"];
}
/// If the microscope is a MTB, then get the position of the light source.
///
/// @return The position of the light source.
public double GetPosition()
{
if (light == null)
return -1;
object[] getPosFocArgs = new object[1];
getPosFocArgs[0] = "%";
object o = Microscope.Types["IMTBContinual"].InvokeMember("GetPosition", BindingFlags.InvokeMethod, null, light, getPosFocArgs);
return (double)o;
}
/// The function takes a double as an argument and then calls the SetPosition function of the
/// light source object
///
/// @param f the position of the light source
public void SetPosition(double f)
{
object[] args = new object[3];
args[0] = f;
args[1] = "%";
args[2] = Microscope.CmdSetMode;
bool resFoc = (bool)Microscope.Types["IMTBContinual"].InvokeMember("SetPosition", BindingFlags.InvokeMethod, null, light, args);
}
public override string ToString()
{
return name;
}
}
/* The class is a wrapper for the filter wheel. It has two methods, one to get the current position
of the filter wheel and one to set the position of the filter wheel */
public class FilterWheel
{
static Type filterType;
static object filterWheel = null;
static int position;
public FilterWheel() { }
public FilterWheel(object o)
{
filterWheel = o;
filterType = Microscope.Types["IMTBChanger"];
}
/// Get the current position of the filter wheel
///
/// @return The position of the filter wheel.
public int GetPosition()
{
return (int)filterType.InvokeMember("get_Position", BindingFlags.InvokeMethod, null, filterWheel, null);
}
/// The function takes an integer as an argument and sets the filter wheel position to that
/// integer
///
/// @param i The position to set the filter wheel to.
public void SetPosition(int i)
{
object[] args = new object[2];
args[0] = (short)i;
args[1] = Activator.CreateInstance(Microscope.Types["MTBCmdSetModes"]);
bool res = (bool)filterType.InvokeMember("SetPosition", BindingFlags.InvokeMethod, null, filterWheel, args);
return;
}
}
public static class Microscope
{
/* Defining an enum. */
public enum Actions
{
StageUp,
StageRight,
StageDown,
StageLeft,
StageFieldUp,
StageFieldRight,
StageFieldDown,
StageFieldLeft,
FocusUp,
FocusDown,
TL,
RL,
TakeImage,
TakeImageStack,
Acquisition,
Locate,
}
public static bool redraw = false;
public static Focus Focus = null;
public static Stage Stage = null;
public static Objectives Objectives = null;
public static TLShutter TLShutter = null;
public static RLShutter RLShutter = null;
public static HXPShutter HXPShutter = null;
public static LightSource TLHalogen = null;
public static LightSource RLHalogen = null;
public static LightSource HXP = null;
public static FilterWheel TLFilterWheel = null;
public static FilterWheel RLFilterWheel = null;
public static FilterWheel FilterWheel = new FilterWheel();
public static double UpperLimit, LowerLimit, fInterVal;
public static object CmdSetMode = null;
public static bool initialized = false;
public static bool ArrowKeysEnabled = true;
public static Point3D defaultPos = new Point3D(30000, 30000, 23900);
public static Assembly dll = null;
public static Dictionary<string, Type> Types = new Dictionary<string, Type>();
public static object root = null;
public static StringBuilder dllVersion = new StringBuilder();
public static int sessionID = -1;
public static string userRx = "";
public static PointD viewSize;
public static int ImageCount = 0;
/* A property that returns the value of the GetObjective method. */
public static Objectives.Objective Objective
{
get
{
return Objectives.GetObjective();
}
}
/// The function initializes the microscope and the imaging library
///
/// @return The return value is an object.
public static void Initialize(string path)
{
if (initialized)
return;
int err;
//We dynamically load the dll installed on the system.);
dll = Assembly.LoadFrom(path);
Type[] tps = dll.GetExportedTypes();
foreach (Type type in tps)
{
if (type == null)
continue;
string s = type.ToString();
s = s.Remove(0, s.LastIndexOf('.') + 1);
Types.Add(s, type);
}
CmdSetMode = GetEnum("MTBCmdSetModes", "Synchronous");
Type con = Types["MTBConnection"];
var c = Activator.CreateInstance(con);
object[] args = new object[2];
args[0] = "en";
args[1] = "";
con.InvokeMember("Login", BindingFlags.InvokeMethod, null, c, args);
object[] args2 = new object[1];
args2[0] = args[1];
con.InvokeMember("Init", BindingFlags.InvokeMethod, null, c, args2);
root = con.InvokeMember("GetRoot", BindingFlags.InvokeMethod, null, c, args2);
Type r = Types["IMTBRoot"];
object[] st = new object[1];
st[0] = "MTBStage";
var sta = r.InvokeMember("GetComponent", BindingFlags.InvokeMethod, null, root, st);
st[0] = "MTBFocus";
var foc = r.InvokeMember("GetComponent", BindingFlags.InvokeMethod, null, root, st);
st[0] = "MTBTLShutter";
var tlShutter = r.InvokeMember("GetComponent", BindingFlags.InvokeMethod, null, root, st);
st[0] = "MTBRLShutter";
var rlShutter = r.InvokeMember("GetComponent", BindingFlags.InvokeMethod, null, root, st);
st[0] = "MTBObjectiveChanger";
var objs = r.InvokeMember("GetComponent", BindingFlags.InvokeMethod, null, root, st);
st[0] = "MTBTLHalogenLamp";
var tllamp = r.InvokeMember("GetComponent", BindingFlags.InvokeMethod, null, root, st);
st[0] = "MTBRLHalogenLamp";
var rllamp = r.InvokeMember("GetComponent", BindingFlags.InvokeMethod, null, root, st);
st[0] = "MTBHXPLamp";
var hxp = r.InvokeMember("GetComponent", BindingFlags.InvokeMethod, null, root, st);
st[0] = "MTBHXP120Shutter";
var hxpShut = r.InvokeMember("GetComponent", BindingFlags.InvokeMethod, null, root, st);
st[0] = "MTBTLFilterChanger1";
var tlfilter = r.InvokeMember("GetComponent", BindingFlags.InvokeMethod, null, root, st);
st[0] = "MTBRLFilterChanger1";
var rlfilter = r.InvokeMember("GetComponent", BindingFlags.InvokeMethod, null, root, st);
TLFilterWheel = new FilterWheel(tlfilter);
RLFilterWheel = new FilterWheel(rlfilter);
Stage = new Stage(sta);
Focus = new Focus(foc);
Objectives = new Objectives(objs);
TLShutter = new TLShutter(tlShutter);
RLShutter = new RLShutter(rlShutter);
TLHalogen = new LightSource(tllamp,"TL Halogen");
RLHalogen = new LightSource(rllamp,"RL Halogen");
HXP = new LightSource(hxp,"HXP");
HXPShutter = new HXPShutter(hxpShut);
Point3D.SetLimits(Stage.minX, Stage.maxX, Stage.minY, Stage.maxY, Focus.lowerLimit, Focus.upperLimit);
PointD.SetLimits(Stage.minX, Stage.maxX, Stage.minY, Stage.maxY);
//We calibrate the stage and focus, so that images are taken always with same calibration
CalibrateXYZ(Microscope.Calibration.OnLowerLimit);
initialized = true;
}
/// It invokes a method on an object
///
/// @param Type The type of the object you want to invoke the method on.
/// @param name The name of the method to invoke.
/// @param o The object to invoke the method on.
/// @param args The arguments to pass to the method. This array of arguments must match in number,
/// order, and type the parameters of the method to be invoked. If there are no parameters, args must be
/// null.
///
/// @return The return value of the method.
public static object Invoke(Type type, string name, object o, object[] args)
{
return type.InvokeMember(name, BindingFlags.InvokeMethod, null, o, args);
}
/// It takes a type name, a method name, an object, and an array of arguments, and returns the result of
/// invoking the method on the object with the arguments
///
/// @param type The type of the object you want to invoke the method on.
/// @param name The name of the method to invoke.
/// @param o The object to invoke the method on.
/// @param args The arguments to pass to the method. This array of arguments must match in number,
/// order, and type the parameters of the method to be invoked. If there are no parameters, args must be
/// null.
///
/// @return The return value of the method.
public static object Invoke(string type, string name, object o, object[] args)
{
Type t = Types[type];
return t.InvokeMember(name, BindingFlags.InvokeMethod, null, o, args);
}
/// > Get the property value of the object of the given type and name
///
/// @param type The type of the object you want to get the property from.
/// @param name The name of the property you want to get the value of.
/// @param obj The object you want to get the property from
///
/// @return The value of the property.
public static object GetProperty(string type, string name, object obj)
{
Type myType = Types[type];
PropertyInfo p = myType.GetProperty(name);
return p.GetValue(obj);
}
/// It takes a string type and a string name and returns an object that is the enum value of the
/// type with the name
///