Digital Twin - GUI
CheckboxCreator.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Threading.Tasks;
6 using System.Data;
7 using System.Data.OleDb;
8 using GUI.Datatypes;
9 
10 namespace GUI.IO_Modules
11 {
16  {
18  public String Dir;
20  private String[] locationNames;
22  private LocationCB[] locations;
24  private List<MachineCB>[] machinesAtLocation;
26  private List<ProductCB>[] productsAtLocation;
28  private Dictionary<String, int> locationDictionary = new Dictionary<String, int>();
29 
34  public CheckboxCreator(String dir)
35  {
36  Dir = dir;
37  }
38 
42  public void Create()
43  {
44  OleDbConnectionStringBuilder csbuilder = new OleDbConnectionStringBuilder();
45  csbuilder.Provider = "Microsoft.ACE.OLEDB.12.0";
46  csbuilder.DataSource = Dir;
47  csbuilder.Add("Extended Properties", "Excel 12.0 Xml");
48 
49  DataTable sheet1 = new DataTable();
50  using (OleDbConnection connection = new OleDbConnection(csbuilder.ConnectionString))
51  {
52  connection.Open();
53  // select everything from worksheet "Allgemein"
54  string sqlQuery = @"SELECT * FROM [Allgemein$]";
55  using (OleDbDataAdapter adapter = new OleDbDataAdapter(sqlQuery, connection))
56  {
57  adapter.Fill(sheet1);
58  }
59  createLocations(sheet1);
60  connection.Close();
61  }
62 
63  foreach (String SheetName in locationNames)
64  {
65  sheet1 = new DataTable();
66  using (OleDbConnection connection = new OleDbConnection(csbuilder.ConnectionString))
67  {
68  connection.Open();
69  // select everything from the worksheet that is named after a location
70  string sqlQuery = @"SELECT * FROM [Standort_" + SheetName + "$]";
71  using (OleDbDataAdapter adapter = new OleDbDataAdapter(sqlQuery, connection))
72  {
73  adapter.Fill(sheet1);
74  }
75  createMachines(sheet1, SheetName);
76 
77  connection.Close();
78  }
79  }
80  }
81 
86  public String[] GetLocationNames()
87  {
88  return locationNames;
89  }
90 
95  public Dictionary<String, int> GetLocationDict()
96  {
97  return locationDictionary;
98  }
99 
105  {
106  return locations;
107  }
108 
113  public List<MachineCB>[] GetAllMachines()
114  {
115  return machinesAtLocation;
116  }
117 
122  public List<ProductCB>[] GetAllProducts()
123  {
124  return productsAtLocation;
125  }
126 
131  private void createLocations(DataTable sheet)
132  {
133  locationNames = new String[Convert.ToInt32(sheet.Columns[0].Caption)];
134  for (int i = 0; i < locationNames.Length; i++)
135  {
136  // locationnames is stored at sheet.Rows[i].ItemArray[0]
137  locationNames[i] = sheet.Rows[i].ItemArray[0].ToString();
138  }
139 
140  int numberOfLocations = locationNames.Length;
141  locations = new LocationCB[numberOfLocations];
142  machinesAtLocation = new List<MachineCB>[numberOfLocations];
143  productsAtLocation = new List<ProductCB>[numberOfLocations];
144 
145  int counter = 0;
146  foreach (String s in locationNames)
147  {
148  locationDictionary.Add(s, counter);
149  locations[counter] = new LocationCB(s, Convert.ToDouble(sheet.Rows[counter].ItemArray[4]));
150  machinesAtLocation[counter] = new List<MachineCB>();
151  productsAtLocation[counter] = new List<ProductCB>();
152  counter++;
153  }
154  }
155 
161  private void createMachines(DataTable sheet, String LocationName)
162  {
163  int numOfMachines = (sheet.Columns.Count - 1) / 3;
164  LocationCB loc = locations[locationDictionary[LocationName]];
165  for (int i = 0; i < numOfMachines; i++)
166  {
167  String Machinename = sheet.Columns[1 + 3 * i].ToString();
168  MachineCB m = new MachineCB(Machinename, loc);
169  machinesAtLocation[locationDictionary[LocationName]].Add(m);
170  createProducts(sheet, m, 1 + 3 * i);
171  }
172  }
173 
180  private void createProducts(DataTable sheet, MachineCB m, int indexOfMachine)
181  {
182  for (int i = 1; i < sheet.Rows.Count;i++)
183  {
184  DataRow d= sheet.Rows[i];
185  if (!d[indexOfMachine].ToString().Equals(""))
186  {
187  ProductCB p = new ProductCB(d[0].ToString(), m, Convert.ToDouble(d[indexOfMachine]), Convert.ToDouble(d[indexOfMachine+1]), Convert.ToDouble(d[indexOfMachine+2]));
188  productsAtLocation[0].Add(p);
189  }
190  }
191  }
192  }
193 }
void createMachines(DataTable sheet, String LocationName)
Creates the machine Checkboxes.
This class reads all the important data out of an Excel file and creates all the Checkboxes we need...
LocationCB[] GetAllLocations()
Grants access to the location array.
String Dir
Directory CheckboxCreator reads from.
LocationCB[] locations
Array that contains all of the locations.
CheckboxCreator(String dir)
Constructor.
void Create()
Reads all the Data from Dir + "\\Dataset.xlsx".
List< MachineCB >[] GetAllMachines()
Grants access to the machine checkboxes.
A wrapper class for products.
Definition: ProductCB.cs:18
A wrapper class for locations.
Definition: LocationCB.cs:19
List< ProductCB >[] GetAllProducts()
Grants access to the product checkboxes.
void createLocations(DataTable sheet)
Creates the location Checkboxes.
List< ProductCB >[] productsAtLocation
All products at a specific location.
String[] locationNames
Array that contains all of the locations&#39; names.
A wrapper class for machines.
Definition: MachineCB.cs:18
List< MachineCB >[] machinesAtLocation
All machines at a specific location.
Dictionary< String, int > GetLocationDict()
Grants access to the location dictionary.
void createProducts(DataTable sheet, MachineCB m, int indexOfMachine)
Creates the product Checkboxes.
String[] GetLocationNames()
Grants access to the all the location names.