-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathStartForm.cs
288 lines (227 loc) · 9.61 KB
/
StartForm.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.IO;
using System.Diagnostics;
using VMS.TPS.Common.Model.API;
using VMS.TPS.Common.Model.Types;
namespace DoseVolumeGUI {
public partial class StartForm : Form {
private static VMS.TPS.Common.Model.API.Application app;
private bool isPatientFound = false;
private bool isCourseSelected = false;
private bool isPlanSelected = false;
private List<PlanResults> planResultsList = new List<PlanResults>();
public StartForm() {
InitializeComponent();
}
public void SetApplication(VMS.TPS.Common.Model.API.Application varianApp) {
app = varianApp;
}
private void searchPatient(String patientHNText) {
// Close patient first
app.ClosePatient();
// remove all course in previous patient
resetCourse();
// remove all plan in previous course
resetPlanSetup();
Patient patient = app.OpenPatientById(patientHNText);
if (patient == null) {
errorLabel.Text = "Patient not found.";
errorLabel.ForeColor = System.Drawing.Color.Red;
return;
} else {
patientSelection();
}
// Set patient name
nameLabel.Text = $"Patient: {patient.FirstName} {patient.LastName}";
// Check if there is course in patient
if (patient.Courses.Count() == 0){
errorCourseLabel.Text = "No course found.";
errorCourseLabel.ForeColor = System.Drawing.Color.Red;
return;
}
// Add course to Combobox
foreach (Course course in patient.Courses) {
courseBox.Items.Add(course);
}
}
private void courseBox_SelectionChangeCommitted(object sender, EventArgs e) {
// Remove plan in previous course
resetPlanSetup();
Course course;
// Get selected course
try {
course = (Course)courseBox.SelectedItem;
courseSelection();
} catch {
errorCourseLabel.Text = "Course error.";
return;
}
// Add planSetup to Checklistbox
foreach (PlanSetup planSetup in course.PlanSetups) {
if (planSetup.Dose != null) {
planSetupCheckLists.Items.Add(planSetup);
}
}
// Add Structure of first plan to checklistbox
if (planSetupCheckLists.Items.Count != 0) {
PlanSetup planSetup = (PlanSetup)planSetupCheckLists.Items[0];
StructureSet structureSet = planSetup.StructureSet;
foreach (Structure structure in structureSet.Structures) {
structureCheckLists.Items.Add(structure);
}
} else {
}
}
private void patientHN_KeyDown(object sender, KeyEventArgs e) {
if (e.KeyCode == Keys.Enter) {
searchPatient(patientHN.Text.ToString());
}
}
private void searchHNButton_Click(object sender, EventArgs e) {
searchPatient(patientHN.Text.ToString());
}
private void patientSelection() {
errorLabel.Text = "Patient found.";
errorLabel.ForeColor = System.Drawing.Color.Green;
isPatientFound = true;
}
private void courseSelection() {
errorCourseLabel.Text = "";
isCourseSelected = true;
}
private void resetCourse() {
// Delete config file
File.Delete("config.json");
courseBox.Items.Clear();
errorCourseLabel.Text = "Please select course";
isCourseSelected = false;
}
private void resetPlanSetup() {
// Delete config file
File.Delete("config.json");
planSetupCheckLists.Items.Clear();
// errorPlanSetupLabel.Text = "Please select plan";
isPlanSelected = false;
}
private void resetStructure() {
File.Delete("config.json");
}
private void structureCheckLists_SelectedValueChanged(object sender, EventArgs e) {
resetStructure();
}
private void configButton_Click(object sender, EventArgs e) {
if (structureCheckLists.CheckedItems.Count == 0) {
errorCalculateLabel.ForeColor = System.Drawing.Color.Red;
errorCalculateLabel.Text = "Please select structure for config";
return;
}
if (!File.Exists("config.json")) {
createJsonConfig();
}
Process.Start("notepad.exe", "config.json");
}
private void createJsonConfig() {
Config config = new Config();
List<StructConfig> eud = new List<StructConfig>();
foreach (Structure structure in structureCheckLists.CheckedItems) {
eud.Add(new StructConfig() {
structureName = structure.Id
}) ;
}
config.structConfig = eud;
string strJson = JsonSerializer.Serialize<Config>(config);
File.WriteAllText("config.json", strJson);
}
private Config readConfig() {
string jsonStr = File.ReadAllText("config.json");
Config config = JsonSerializer.Deserialize<Config>(jsonStr);
return config;
}
private void calculateButton_Click(object sender, EventArgs e) {
if (planSetupCheckLists.CheckedItems.Count == 0) {
errorCalculateLabel.ForeColor = System.Drawing.Color.Red;
errorCalculateLabel.Text = "Please select plan before calculate";
return;
}
if (structureCheckLists.CheckedItems.Count == 0) {
errorCalculateLabel.ForeColor = System.Drawing.Color.Red;
errorCalculateLabel.Text = "Please select structure before calculate";
return;
}
resultTextBox.Text = "";
errorCalculateLabel.Text = "";
planResultsList.Clear();
if (!File.Exists("config.json")) {
createJsonConfig();
}
Config config = readConfig();
foreach (PlanSetup planSetup in planSetupCheckLists.CheckedItems) {
List<StructResults> structResultsList = new List<StructResults>();
foreach (Structure structure in structureCheckLists.CheckedItems) {
DVHData normalPlanDVH = DVH.GetDVH(planSetup, structure);
/*foreach (PlanUncertainty planUncertainty in planSetup.PlanUncertainties) {
DVHData uncertaintyPlanDVH = DVH.GetDVH(planUncertainty, structure);
}*/
StructConfig structConfig = config.structConfig.Where(tmp => tmp.structureName == structure.Id).FirstOrDefault();
Dictionary<string, double> parameters = DVH.DVHToParameters(normalPlanDVH, structConfig);
String text = $@"
Plan: {planSetup.Id}
ROIs: {structure.Id}
Max dose: {normalPlanDVH.MaxDose} gEUD: {parameters["eud"]} cGy
Min dose: {normalPlanDVH.MinDose} NTCP:
Mean dose: {normalPlanDVH.MeanDose} TCP:
Median dose: {normalPlanDVH.MedianDose}
Volume: {normalPlanDVH.Volume}
";
StructResults structResults = new StructResults(
planSetup.Id,
normalPlanDVH.MaxDose.ToString(),
normalPlanDVH.MinDose.ToString(),
normalPlanDVH.MeanDose.ToString(),
normalPlanDVH.MedianDose.ToString(),
normalPlanDVH.Volume.ToString(),
parameters["eud"],
0,
0
);
structResultsList.Add(structResults);
resultTextBox.AppendText(text);
}
PlanResults planResults = new PlanResults(planSetup.Id, structResultsList);
planResultsList.Add(planResults);
}
}
private void selectAllStructure_CheckedChanged(object sender, EventArgs e) {
if (selectAllStructure.Checked == true) {
for (int i = 0; i < structureCheckLists.Items.Count; i++) {
structureCheckLists.SetItemChecked(i, true);
}
} else {
for (int i = 0; i < structureCheckLists.Items.Count; i++) {
structureCheckLists.SetItemChecked(i, false);
}
}
}
private void seletectAllPlan_CheckedChanged(object sender, EventArgs e) {
if (seletectAllPlan.Checked == true) {
for (int i = 0; i < planSetupCheckLists.Items.Count; i++) {
planSetupCheckLists.SetItemChecked(i, true);
}
} else {
for (int i = 0; i < planSetupCheckLists.Items.Count; i++) {
planSetupCheckLists.SetItemChecked(i, false);
}
}
}
}
}