SECI  1
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties Events
LabViewPanel.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections.Generic;
3 using System.Collections;
4 using System.Text;
5 using System.IO;
6 
7 namespace Seci.LabView
8 {
14  {
18  private strongnameLabview.VirtualInstrument _VI;
19 
23  private Dictionary<String, Controls.LvControl> _controls;
24 
25  private String _frontPanelTitle; //The name on the front panel - needed for winapi calls
26 
27  //Properties
28  public strongnameLabview.VirtualInstrument VI { get { return _VI; } }
29  public String FrontPanelTitle { get { return _frontPanelTitle; } }
30 
35  public LabViewPanel(strongnameLabview.VirtualInstrument VI)
36  {
37  //Create an new instance of the Controls
38  _controls = new Dictionary<String, Controls.LvControl>();
39 
40  //Assign VI
41  _VI = VI;
42  }
43 
49  public void GetFrontPanelTitle(strongnameLabview.VirtualInstrument exporter, String fileName)
50  {
51  exporter.SetControlValue("VI Path", fileName);
52  exporter.Run(false);
53  _frontPanelTitle = exporter.GetControlValue("Title").ToString();
54  exporter.SetControlValue("Title", ""); //Clear for future use
55  }
56 
57  #region Get Controls
58 
68  public void getVIControls(strongnameLabview.VirtualInstrument exporter, String fileName, Boolean includeClusters)
69  {
70  try
71  {
72  String savefile = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\LVPanelControls.txt";
73  exportVIControls(exporter, fileName, savefile);
74  extractVIControls(savefile, includeClusters);
75  }
76  catch (GetControlsException e)
77  {
78  //Log it and carry on
79  Helpers.ErrorLogger.LabViewError("GetControls", e.Message + ": " + fileName);
80  }
81  catch (Exception e)
82  {
83  //throw error up to next level
84  throw new LabViewException(e.Message);
85  }
86  }
87 
95  private static void exportVIControls(strongnameLabview.VirtualInstrument exporter, String fileName, String saveFile)
96  {
97  //Check dump file deleted
98  FileInfo fi = new FileInfo(saveFile);
99 
100  if (fi.Exists)
101  {
102  fi.Delete();
103  }
104 
105  //First export the control list
106  exporter.SetControlValue("VI Path", fileName);
107  exporter.SetControlValue("Output Path", saveFile);
108  exporter.Run(false);
109  }
110 
122  private void extractVIControls(String fileName, Boolean includeClusters)
123  {
124  //Extract the name of the controls on the frontpanel from the textfile
125  try
126  {
127  //Check file exists
128  FileInfo FI = new FileInfo(fileName);
129 
130  if (FI.Exists)
131  {
132  //Create new StreamReader
133  using (StreamReader sr = new StreamReader(fileName))
134  {
135  //Readlines until <CONTENT> is found; this is the start of the control list
136  bool FoundStart = false;
137 
138  do
139  {
140  String temp = sr.ReadLine();
141 
142  if (temp.StartsWith("<CONTENT>", StringComparison.Ordinal))
143  {
144  FoundStart = true;
145  break;
146  }
147 
148  } while (sr.Peek() >= 0); //end if next line is EOF
149 
150  //Now look for controls
151  if (FoundStart == true) //else end of file was reached and there are no controls
152  {
153  String myLine;
154  String controlType;
155  String controlName;
156 
157  do
158  {
159  //Read next line and trim whitespace
160  myLine = sr.ReadLine();
161  myLine = myLine.Trim();
162 
163  //Check to see if line starts with <PARTS>, if so readline until </PARTS>
164  if (myLine.StartsWith("<PARTS>", StringComparison.Ordinal))
165  {
166  do
167  {
168  //Step through
169  myLine = sr.ReadLine();
170  myLine = myLine.Trim();
171  } while (!myLine.Contains("</PARTS>"));
172  }
173 
174  if (myLine.Contains("<LF>"))
175  {
176  //control name contains a \n!!
177  //So read next line and cobble together the correct string
178  String myLine2 = sr.ReadLine();
179  myLine = myLine.Substring(0, myLine.IndexOf("<LF>", StringComparison.Ordinal)) + myLine2;
180  myLine = myLine.Trim();
181  }
182 
183  //Debug
184  //Console.WriteLine(myLine);
185 
186  //Check for a control
187  if (myLine.StartsWith("<CONTROL ID"))
188  {
189  //Find control name
190  controlName = findControlName(myLine);
191 
192  //Only do if control name is not null
193  if (controlName != null)
194  {
195  //Find control type
196  controlType = findControlType(myLine);
197 
198  //Check to see the type of control
199  if ((controlType == "Cluster"))
200  {
201  //it is a cluster
202  getClusterInfo(sr, controlName, null, includeClusters);
203  }
204  else if (controlType == "Radio Buttons")
205  {
206  //It is a Radio button
207  //When a radio button is queried for a value, it returns an integer
208  //representing the option which is true.
209 
210  addRadioButton(sr, controlName, null);
211  }
212  else if (controlType == "Ring")
213  {
214  addRing(sr, controlName, null);
215  }
216  else if (controlType == "Listbox")
217  {
218  addListbox(sr, controlName, null);
219  }
220  else if (controlType == "Numeric" || controlType == "Boolean"
221  || controlType == "String" || controlType == "Enum"
222  || controlType == "Array")
223  {
224  //It is a numeric, boolean, enum, array, or string, so add it to the controls list
225  addSimpleControl(sr, controlName, controlType, null);
226  }
227  else
228  {
229  //unknown control, do nothing
230  }
231  }
232  }
233  else
234  {
235  //go to next line
236  }
237 
238  } while (sr.Peek() >= 0); //read to end of file
239  }
240 
241  } //End of using statement
242 
243  //Delete the file
244  FI.Delete();
245  }
246  else
247  {
248  //throw error up to next level
249  throw new GetControlsException("Could not get control list");
250  }
251  }
252  catch
253  {
254  //throw error up to next level
255  throw;
256  }
257  }
258 
264  private static String findControlType(String line)
265  {
266  //See what the type is and return that
267  if (line.Contains("type=\"String\""))
268  {
269  return "String";
270  }
271  else if (line.Contains("type=\"Numeric\""))
272  {
273  return "Numeric";
274  }
275  else if (line.Contains("type=\"Boolean\""))
276  {
277  return "Boolean";
278  }
279  else if (line.Contains("type=\"Array\""))
280  {
281  return "Array";
282  }
283  else if (line.Contains("type=\"Cluster\""))
284  {
285  return "Cluster";
286  }
287  else if (line.Contains("type=\"Radio Buttons\""))
288  {
289  return "Radio Buttons";
290  }
291  else if (line.Contains("type=\"Enum\""))
292  {
293  return "Enum";
294  }
295  else if (line.Contains("type=\"Ring\""))
296  {
297  return "Ring";
298  }
299  else if (line.Contains("type=\"Listbox\""))
300  {
301  return "Listbox";
302  }
303  else if (line.Contains("type=\"Type Definition\""))
304  {
305  //Special case, as far as is known only the ILM uses these
306  //for data that is likely to be shown as a block
307  return "Numeric";
308  }
309  else if (line.Contains("type=\"Thermometer\""))
310  {
311  //Special case - treat as a numeric
312  return "Numeric";
313  }
314  else if (line.Contains("type=\"Tank\""))
315  {
316  //Special case - treat as a numeric
317  return "Numeric";
318  }
319  else if (line.Contains("type=\"Slide\""))
320  {
321  //Special case - treat as a numeric
322  return "Numeric";
323  }
324  else if (line.Contains("type=\"Dial\""))
325  {
326  //Special case - treat as a numeric
327  return "Numeric";
328  }
329  else if (line.Contains("type=\"Knob\""))
330  {
331  //Special case - treat as a numeric
332  return "Numeric";
333  }
334  else if (line.Contains("type=\"Meter\""))
335  {
336  //Special case - treat as a numeric
337  return "Numeric";
338  }
339  else
340  {
341  return null;
342  }
343  }
344 
350  private static String findControlName(String line)
351  {
352  //Split line where " occurs; we need the fourth portion
353  String[] tempString = line.Split('\"');
354 
355  //If it is not the size expected then return null
356  if (tempString.GetLength(0) == 5)
357  {
358  return tempString[3].ToString();
359  }
360  else
361  {
362  return null;
363  }
364  }
365 
375  private void addSimpleControl(StreamReader sr, String name, String type, SerialisableList<int> clusterIndices)
376  {
377  try
378  {
379  Controls.LvControl newControl;
380 
381  if (type == "Numeric")
382  {
383  newControl = new Controls.LvNumeric(name, clusterIndices);
384 
385  //Add the control to the dictionary
386  AddControlToDictionary(newControl);
387  }
388  else if (type == "String")
389  {
390  newControl = new Controls.LvString(name, clusterIndices);
391 
392  //Add the control to the dictionary
393  AddControlToDictionary(newControl);
394  }
395  else if (type == "Boolean")
396  {
397  newControl = new Controls.LvBoolean(name, clusterIndices);
398 
399  //Add the control to the dictionary
400  AddControlToDictionary(newControl);
401  }
402  else if (type == "Enum")
403  {
404  newControl = new Controls.LvEnum(name, clusterIndices);
405 
406  //Add the control to the dictionary
407  AddControlToDictionary(newControl);
408  }
409  else if (type == "Array")
410  {
411  newControl = new Controls.LvArray(name, clusterIndices);
412 
413  //Add the control to the dictionary
414  AddControlToDictionary(newControl);
415 
416  //Need to skip past the remaining info in the array definition
417  String myLine;
418  int count = 1;
419 
420  do
421  {
422  myLine = sr.ReadLine();
423  myLine = myLine.Trim();
424 
425  if (myLine.Contains("<CONTROL"))
426  {
427  count++;
428  }
429  else if (myLine.Contains("</CONTROL>"))
430  {
431  count--;
432  }
433 
434  } while (count != 0);
435  }
436  }
437  catch (System.ArgumentException)
438  {
439  //Is most likely that the VI has two items with the same name,
440  //in this case just carry on without adding it.
441  //Won't log it as it is not a problem.
442  //ErrorLogger.SeciError("addSimpleControl, ControlName: " + name, e);
443  }
444  catch (Exception e)
445  {
446  //throw error up to next level
447  throw new ArgumentException(e.Message);
448  }
449  }
450 
459  private void addRing(StreamReader sr, String ringName, SerialisableList<int> clusterIndices)
460  {
461  //Get the ring items
462  try
463  {
464  String myLine;
465  String[] items = null;
466 
467  do
468  {
469  myLine = sr.ReadLine();
470  myLine = myLine.Trim();
471 
472  //Debug
473  //Console.WriteLine(myLine);
474 
475  //Check for a control
476  if (myLine.StartsWith("<PART ID=12 order=0 type=\"Ring Text\">", StringComparison.Ordinal))
477  {
478  //Find item names
479  String temp = myLine.Substring(myLine.IndexOf("<STRING>", StringComparison.Ordinal) + 8);
480  temp = temp.Remove(temp.IndexOf("</STRING></STRINGS>", StringComparison.Ordinal));
481  temp = temp.Replace("</STRING><STRING>", "|");
482 
483  items = temp.Split('|');
484  break;
485  }
486 
487  } while (myLine.Contains("</CONTROL>") == false);
488 
489  //Add the control
490  Controls.LvRing newControl = new Controls.LvRing(ringName, items, clusterIndices);
491 
492  //Add the control to the dictionary
493  AddControlToDictionary(newControl);
494  }
495  catch (Exception e)
496  {
497  //throw error up to next level
498  throw new ArgumentException(e.Message);
499  }
500  }
501 
512  private void addListbox(StreamReader sr, String boxName, SerialisableList<int> clusterIndices)
513  {
514  //Get the ring items
515  try
516  {
517  String myLine;
518  String[] items = null;
519 
520  do
521  {
522  myLine = sr.ReadLine();
523  myLine = myLine.Trim();
524 
525  //Debug
526  //Console.WriteLine(myLine);
527 
528  //Check for a control
529  if (myLine.StartsWith("<PRIV><ITEMS><STRING>", StringComparison.Ordinal))
530  {
531  //Find item names
532  String temp = myLine.Substring(myLine.IndexOf("<STRING>", StringComparison.Ordinal) + 8);
533  temp = temp.Remove(temp.IndexOf("</STRING></ITEMS></PRIV", StringComparison.Ordinal));
534  temp = temp.Replace("</STRING><STRING>", "|");
535 
536  items = temp.Split('|');
537  break;
538  }
539 
540  } while (myLine.Contains("</CONTROL>") == false);
541 
542  //Add the control
543  Controls.LvRing newControl = new Controls.LvRing(boxName, items, clusterIndices);
544 
545  //Add the control to the dictionary
546  AddControlToDictionary(newControl);
547  }
548  catch (Exception e)
549  {
550  //throw error up to next level
551  throw new ArgumentException(e.Message);
552  }
553  }
554 
559  private void AddControlToDictionary(Controls.LvControl newControl)
560  {
561  try
562  {
563  if (!_controls.ContainsKey(newControl.Name))
564  {
565  _controls.Add(newControl.Name, newControl);
566  }
567  }
568  catch (Exception err)
569  {
570  //Fail silently and don't log anymore
571  //Helpers.ErrorLogger.SeciError("addControlToDictionary", err);
572  }
573  }
574 
584  private void addRadioButton(StreamReader sr, String radioName, SerialisableList<int> clusterIndices)
585  {
586  //Get the radio buttons
587  try
588  {
589  String myLine;
590  List<String> items = new List<String>();
591 
592  do
593  {
594  myLine = sr.ReadLine();
595  myLine = myLine.Trim();
596 
597  //Check for a control
598  if (myLine.StartsWith("<CONTROL ID", StringComparison.Ordinal))
599  {
600  //Add the item name to the list
601  items.Insert(0, findControlName(myLine));
602  }
603 
604  } while (myLine.Contains("</CONTENT>") == false);
605 
606  //Add the control
607  Controls.LvRadioButtons newControl = new Controls.LvRadioButtons(radioName, items.ToArray(), clusterIndices);
608 
609  //Add the control to the dictionary
610  AddControlToDictionary(newControl);
611 
612  }
613  catch (Exception e)
614  {
615  //throw error up to next level
616  throw new ArgumentException(e.Message);
617  }
618  }
619 
631  private void getClusterInfo(StreamReader sr, String clusterName, SerialisableList<int> clusterList, Boolean includeClusters)
632  {
633  //Get all the controls in the cluster or array and add them to the list
634  try
635  {
636  String myLine;
637  String controlType;
638  String controlName;
639  int clusterCount = 0;
640 
641  //we use the arraylist to store all the indices of the cluster, this
642  //should handle nested clusters
643  if (clusterList == null)
644  {
645  //If it is a toplevel cluster then we need to create the arraylist
646  clusterList = new SerialisableList<int>();
647  }
648 
649  do
650  {
651  myLine = sr.ReadLine();
652  myLine = myLine.Trim();
653 
654  //Debug
655  //Console.WriteLine(myLine);
656 
657  //Check for a control
658  if (myLine.StartsWith("<CONTROL ID", StringComparison.Ordinal))
659  {
660  //Find control name
661  controlName = findControlName(myLine);
662 
663  //Find control type
664  controlType = findControlType(myLine);
665 
666  //Check to see the type of control
667  if (controlType == "Cluster")
668  {
669  //it is a cluster
670  //Add the index to the list
671  clusterList.Add(clusterCount);
672 
673  //Fill the cluster - recursion!
674  getClusterInfo(sr, clusterName + ":" + controlName, clusterList, includeClusters);
675 
676  //Remove the last index in case there are more controls to add in this cluster
677  clusterList.RemoveAt(clusterList.Count - 1);
678  }
679  else if (controlType == "Radio Buttons")
680  {
681  clusterList.Add(clusterCount);
682 
683  if (includeClusters)
684  {
685  addRadioButton(sr, clusterName + ":" + controlName, clusterList);
686  }
687 
688  //Remove the last index in case there are more controls to add in this cluster
689  clusterList.RemoveAt(clusterList.Count - 1);
690  }
691  else if (controlType == "Ring")
692  {
693  //Add the index to the list
694  clusterList.Add(clusterCount);
695 
696  //Add the control
697  if (includeClusters)
698  {
699  addRing(sr, clusterName + ":" + controlName, clusterList);
700  }
701 
702  //Remove the last index in case there are more controls to add in this cluster
703  clusterList.RemoveAt(clusterList.Count - 1);
704  }
705  else if (controlType == "Numeric" || controlType == "Boolean"
706  || controlType == "String" || controlType == "Enum"
707  || controlType == "Array")
708  {
709  //It is a numeric, boolean, array or string, so add it to the controls list
710  //Add the index to the list
711  clusterList.Add(clusterCount);
712 
713  //Add the control
714  if (includeClusters)
715  {
716  addSimpleControl(sr, clusterName + ":" + controlName, controlType, clusterList);
717  }
718 
719  //Remove the last index in case there are more controls to add in this cluster
720  clusterList.RemoveAt(clusterList.Count - 1);
721  }
722  else
723  {
724  //It is a unknown control
725  }
726  clusterCount++;
727  }
728 
729  } while (myLine.Contains("</CONTENT>") == false);
730 
731  }
732  catch (Exception e)
733  {
734  //throw error up to next level
735  throw new ArgumentException(e.Message);
736  }
737  }
738 
743  public List<String> getListOfControls()
744  {
745  try
746  {
747  Dictionary<string, Controls.LvControl>.KeyCollection keys = _controls.Keys;
748  List<String> listControls = new List<String>(); //this is used to store the controls as they are added
749 
750  foreach (String name in keys)
751  {
752  listControls.Add(_controls[name].GetNameAndType());
753  }
754 
755  return listControls;
756  }
757  catch (Exception e)
758  {
759  //throw error up to next level
760  throw new ArgumentException(e.Message);
761  }
762  }
763 
764  public String GetControlType(String name)
765  {
766  if (name.EndsWith(")"))
767  {
768  name = name.Substring(0, name.LastIndexOf('('));
769  }
770 
771  return _controls[name.Trim()].GetControlType();
772  }
773 
774  #endregion
775 
776  #region Get control value
777 
786  public String[] getControlValue(String controlName)
787  {
788  try
789  {
790  //Get the control
791  Controls.LvControl control = _controls[controlName];
792 
793  return control.GetValue(ref _VI);
794  }
795  catch (Exception e)
796  {
797  //throw error up to next level
798  throw new ArgumentException(e.Message);
799  }
800  }
801 
808  public Object getRawControlValue(String controlName)
809  {
810  //Just get the value straight from LV without doing any checks and do nothing to it
811  try
812  {
813  return _VI.GetControlValue(controlName);
814  }
815  catch (Exception e)
816  {
817  //throw error up to next level
818  throw new ArgumentException(e.Message);
819  }
820  }
821 
830  public Object[] getWholeCluster(string clusterName)
831  {
832  try
833  {
834  Object[] vals = (Object[])_VI.GetControlValue(clusterName);
835 
836  return vals;
837  }
838  catch (Exception e)
839  {
840  //throw error up to next level
841  throw new ArgumentException(e.Message);
842  }
843  }
844 
845  #endregion
846 
847  #region Set values on Panel
848 
857  public void setControlValue(String controlName, Object value)
858  {
859  //Set the value on the front panel, this can also include pushing buttons
860  Controls.LvControl control = _controls[controlName];
861 
862  control.SetValue(ref _VI, value);
863  }
864 
871  public void setRawControlValue(String controlName, Object value)
872  {
873  _VI.SetControlValue(controlName, value);
874  }
875 
884  public void setWholeCluster(string clusterName, Object[] values)
885  {
886  try
887  {
888  _VI.SetControlValue(clusterName, values);
889 
890  }
891  catch (Exception e)
892  {
893  //throw error up to next level
894  throw new ArgumentException(e.Message);
895  }
896  }
897 
898  #endregion
899 
906  internal String FindControlNameAndType(String partialName)
907  {
908  List<String> keys = new List<string>();
909  keys.AddRange(_controls.Keys);
910 
911  foreach (String key in keys)
912  {
913  if (key == partialName)
914  {
915  return _controls[key].GetNameAndType();
916  }
917  }
918 
919  return partialName;
920  }
921  }
922 }
Object[] getWholeCluster(string clusterName)
Method for grabbing a whole cluster directly from LabVIEW. No formatting or checking applied...
static String findControlName(String line)
Method for extracting the control name.
This class inherits from LVControl class and is specialised for String controls.
Definition: LVString.cs:11
void getClusterInfo(StreamReader sr, String clusterName, SerialisableList< int > clusterList, Boolean includeClusters)
Method for getting the information from a cluster. A cluster is a container for other types of contro...
void setRawControlValue(String controlName, Object value)
Method for setting a control value via DCOM with no checks from SECI. The control does not have to be...
This class inherits from LVControl class and is specialised for Ring controls. A Ring is like an enum...
Definition: LVRing.cs:13
void AddControlToDictionary(Controls.LvControl newControl)
Method for adding controls to the control dictionary
Dictionary< String, Controls.LvControl > _controls
Dictionary for containing the VI&#39;s control information. The control&#39;s name is the key...
Definition: LabViewPanel.cs:23
This class inherits from LVControl class and is specialised for Numeric controls. ...
Definition: LVNumeric.cs:11
A simple custom exception used for LabVIEW exceptions
void GetFrontPanelTitle(strongnameLabview.VirtualInstrument exporter, String fileName)
Gets the title of the front panel
Definition: LabViewPanel.cs:49
This class inherits from LVControl class and is specialised for reading and writing to LabVIEW array ...
Definition: LVArray.cs:11
void setControlValue(String controlName, Object value)
Method for setting a control value. Only sets values for controls inside the dictionary. If the control is in a cluster the name of the control is of the form: &quot;clustername:controlname&quot;.
void addRing(StreamReader sr, String ringName, SerialisableList< int > clusterIndices)
Method for adding a Ring control to the dictionary. A Ring is like an enum, but it does not have to r...
This class inherits from LVControl class and is specialised for Boolean controls. ...
Definition: LVBoolean.cs:11
void extractVIControls(String fileName, Boolean includeClusters)
Main method for extracting the control information from the XML file that was produced by the exportV...
String GetControlType(String name)
This class inherits from LVControl class and is specialised for Enum controls.
Definition: LVEnum.cs:11
static void exportVIControls(strongnameLabview.VirtualInstrument exporter, String fileName, String saveFile)
Method for extracting the control information from the VI. Uses an intermediate VI for saving the inf...
Definition: LabViewPanel.cs:95
void addSimpleControl(StreamReader sr, String name, String type, SerialisableList< int > clusterIndices)
Method for adding simple controls to the controls dictionary. Used for Numerics, Booleans, Strings, Enums and Arrays.
LabViewPanel(strongnameLabview.VirtualInstrument VI)
Constructor
Definition: LabViewPanel.cs:35
String[] getControlValue(String controlName)
Method for retrieving a value from a control. Only returns values for controls inside the dictionary...
Object getRawControlValue(String controlName)
Method for return a control value straight from DCOM with no clean-up. The control does not have to b...
This class contains the reference to the LabVIEW VI and keeps a list of all the front panel controls...
Definition: LabViewPanel.cs:13
strongnameLabview.VirtualInstrument _VI
The reference to the actual VI
Definition: LabViewPanel.cs:18
A simple custom exception used for a LabVIEW control cannot be read
void getVIControls(strongnameLabview.VirtualInstrument exporter, String fileName, Boolean includeClusters)
The method that initialises the finding of the controls on the front panel. The controls are read fro...
Definition: LabViewPanel.cs:68
void setWholeCluster(string clusterName, Object[] values)
Method for setting a whole cluster directly. No formatting or checking applied. Cluster are not store...
void addRadioButton(StreamReader sr, String radioName, SerialisableList< int > clusterIndices)
Method for adding a Radio Button control to the dictionary. Radio buttons only return the index of th...
void addListbox(StreamReader sr, String boxName, SerialisableList< int > clusterIndices)
Method for adding a Listbox control to the dictionary. A Listbox is like a enum; however the enum dat...
static String findControlType(String line)
Method for determining the control type and what it should be treated as.
List< String > getListOfControls()
Method for requesting a list of the controls on the front panel of this VI.
This class is the abstract base class of the objects used to store the VI control information...
Definition: LVControl.cs:12
This class inherits from LVControl class and is specialised for Radio Button controls.