-
Notifications
You must be signed in to change notification settings - Fork 0
/
UserControl1.xaml.cs
2321 lines (1968 loc) · 93.4 KB
/
UserControl1.xaml.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
// MIT License
//
// Copyright(c) 2022 Danish Centre for Particle Therapy
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
// ABOUT
// ESAPI 4D evaluation script
// Developed at the Danish Centre for Particle Therapy by medical physicist Maria Fuglsang Jensen
// The script can be used to:
// Automatical recalculation of a proton, IMRT or VMAT plan on all phases of a 4D
// Perform a simple evaluation on plans calculated on all phases of a 4D
// Export of DVHs from the main plan and phase plans.
// The script is still under development.Each clinic must perform their own quality assurance of script.
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using VMS.TPS.Common.Model.API;
using VMS.TPS.Common.Model.Types;
using System.Windows.Threading;
namespace Evaluering4D
{
/// <summary>
/// Interaction logic for UserControl1.xaml
/// </summary>
public partial class UserControl1 : UserControl
{
#region PublicVariables
//Several public parameters are used in methods and functions.
public ScriptContext ScriptInfo; //The database content for the selected patient.
public string errormessages = ""; //A string with all messages and errors for the user.
public StructureSet MainStructureSet; //Independent on if a sumplan or a single plan is chosen
// Single plans
public PlanSetup SelectedPlan; //The main plan that is to be copied to all phases
//As all the new plans that are copied cannot be saved before the user presses "save", therefore they are kept in these public variables.
public PlanSetup newPlan00;
public PlanSetup newPlan10;
public PlanSetup newPlan20;
public PlanSetup newPlan30;
public PlanSetup newPlan40;
public PlanSetup newPlan50;
public PlanSetup newPlan60;
public PlanSetup newPlan70;
public PlanSetup newPlan80;
public PlanSetup newPlan90;
// Sum plans
public PlanSum SelectedPlanSum; //The main sum plan that is to be copied to all phases
//As all the new plans that are copied cannot be saved before the user presses "save", therefore they are kept in these public variables.
public PlanSum newPlanSum00;
public PlanSum newPlanSum10;
public PlanSum newPlanSum20;
public PlanSum newPlanSum30;
public PlanSum newPlanSum40;
public PlanSum newPlanSum50;
public PlanSum newPlanSum60;
public PlanSum newPlanSum70;
public PlanSum newPlanSum80;
public PlanSum newPlanSum90;
//The 10 phases have a set of possible series UIDS. This is used later to find the correct image, as the names and image UIDs are not unique.
public List<string> UID_00 = new List<string> { };
public List<string> UID_10 = new List<string> { };
public List<string> UID_20 = new List<string> { };
public List<string> UID_30 = new List<string> { };
public List<string> UID_40 = new List<string> { };
public List<string> UID_50 = new List<string> { };
public List<string> UID_60 = new List<string> { };
public List<string> UID_70 = new List<string> { };
public List<string> UID_80 = new List<string> { };
public List<string> UID_90 = new List<string> { };
//The 10 3D images for each phase will be saved in these variables.
public VMS.TPS.Common.Model.API.Image img00;
public VMS.TPS.Common.Model.API.Image img10;
public VMS.TPS.Common.Model.API.Image img20;
public VMS.TPS.Common.Model.API.Image img30;
public VMS.TPS.Common.Model.API.Image img40;
public VMS.TPS.Common.Model.API.Image img50;
public VMS.TPS.Common.Model.API.Image img60;
public VMS.TPS.Common.Model.API.Image img70;
public VMS.TPS.Common.Model.API.Image img80;
public VMS.TPS.Common.Model.API.Image img90;
public bool[] skip_img;
#endregion PublicVariables
public UserControl1()
{
InitializeComponent();
}
#region UpdateAndClearUI
/// <summary>
/// All public variables and comboboxes are cleared by this function.
/// </summary>
private void clearAllPublicsAndCombos()
{
//Publics
SelectedPlan = null;
SelectedPlanSum = null;
MainStructureSet = null;
UID_00.Clear();
UID_10.Clear();
UID_20.Clear();
UID_30.Clear();
UID_40.Clear();
UID_50.Clear();
UID_60.Clear();
UID_70.Clear();
UID_80.Clear();
UID_90.Clear();
img00 = null;
img10 = null;
img20 = null;
img30 = null;
img40 = null;
img50 = null;
img60 = null;
img70 = null;
img80 = null;
img90 = null;
newPlan00 = null;
newPlan10 = null;
newPlan20 = null;
newPlan30 = null;
newPlan40 = null;
newPlan50 = null;
newPlan60 = null;
newPlan70 = null;
newPlan80 = null;
newPlan90 = null;
// PlanSum extention
newPlanSum00 = null;
newPlanSum10 = null;
newPlanSum20 = null;
newPlanSum30 = null;
newPlanSum40 = null;
newPlanSum50 = null;
newPlanSum60 = null;
newPlanSum70 = null;
newPlanSum80 = null;
newPlanSum90 = null;
errormessages = "";
Errors_txt.Text = errormessages;
//Combos
CTV1_cb.Items.Clear();
CTV2_cb.Items.Clear();
Spinal_cb.Items.Clear();
Spinal2_cb.Items.Clear();
Spinal3_cb.Items.Clear();
CT00_cb.Items.Clear();
CT10_cb.Items.Clear();
CT20_cb.Items.Clear();
CT30_cb.Items.Clear();
CT40_cb.Items.Clear();
CT50_cb.Items.Clear();
CT60_cb.Items.Clear();
CT70_cb.Items.Clear();
CT80_cb.Items.Clear();
CT90_cb.Items.Clear();
CT00_plan_cb.Items.Clear();
CT10_plan_cb.Items.Clear();
CT20_plan_cb.Items.Clear();
CT30_plan_cb.Items.Clear();
CT40_plan_cb.Items.Clear();
CT50_plan_cb.Items.Clear();
CT60_plan_cb.Items.Clear();
CT70_plan_cb.Items.Clear();
CT80_plan_cb.Items.Clear();
CT90_plan_cb.Items.Clear();
}
/// <summary>
/// This method forces the UI to update when ever it is called.
/// </summary>
void AllowUIToUpdate()
{
DispatcherFrame frame = new DispatcherFrame();
Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Render, new DispatcherOperationCallback(delegate (object parameter)
{
frame.Continue = false;
return null;
}), null);
Dispatcher.PushFrame(frame);
//EDIT:
System.Windows.Application.Current.Dispatcher.Invoke(DispatcherPriority.Background,
new Action(delegate { }));
}
#endregion UpdateAndClearUI
#region SelectPlans
/// <summary>
/// Based on a string course id and a string plan id, we determine if the plan is a sumplan or not.
/// </summary>
/// <param name="courseid">The course id</param>
/// <param name="planid">The plan id</param>
/// <returns></returns>
private bool SumPlanDetected(string courseid, string planid)
{
foreach (var sumplan in ScriptInfo.PlanSumsInScope)
{
if (sumplan.Id == planid && sumplan.Course.Id == courseid)
{
return true;
}
}
return false;
}
/// <summary>
/// The user selects the main plan or sumplan and comboboxes for structure selection and image selection are filled.
/// </summary>
private void SelectPlan_Click(object sender, RoutedEventArgs e)
{
progress_lb.Content = "... Select 4D phases below and define optional DVH points in 2. Continue hereafter the script by pressing 'Select images' in 3a. or 3b. ...";
AllowUIToUpdate();
//First everything is cleared.
clearAllPublicsAndCombos();
//Buttons that are not to be used are disabled.
EvalDoseE_btn.IsEnabled = false;
CopyPlan_btn.IsEnabled = false;
ExportDVH_btn.IsEnabled = false;
//The selected plan is found.
string[] planname = SelectPlan_cb.SelectedItem.ToString().Split('/');
string courseid = planname.First();
string planid = planname.Last();
//The selected plan is saved as a global variable and we determine if it is a sumplan or a single plan.
if (SumPlanDetected(courseid, planid) == true) //sum plan
{
PlanSum mainPlanSum = ScriptInfo.Patient.Courses.Where(c => c.Id == courseid).FirstOrDefault().PlanSums.Where(p => p.Id == planid).FirstOrDefault();
SelectedPlanSum = mainPlanSum;
MainStructureSet = mainPlanSum.StructureSet;
}
else //single plan
{
PlanSetup mainPlan = ScriptInfo.Patient.Courses.Where(c => c.Id == courseid).FirstOrDefault().PlanSetups.Where(p => p.Id == planid).FirstOrDefault();
SelectedPlan = mainPlan;
MainStructureSet = mainPlan.StructureSet;
}
// Combobox with structure names are filled and sorted alfabetically
CTV1_cb.Items.Add("Skip");
CTV2_cb.Items.Add("Skip");
Spinal_cb.Items.Add("Skip");
Spinal2_cb.Items.Add("Skip");
Spinal3_cb.Items.Add("Skip");
IEnumerable<Structure> sortedStructs = MainStructureSet.Structures.OrderBy(s => s.Id);
foreach (var struc in sortedStructs)
{
CTV1_cb.Items.Add(struc.Id);
CTV2_cb.Items.Add(struc.Id);
Spinal_cb.Items.Add(struc.Id);
Spinal2_cb.Items.Add(struc.Id);
Spinal3_cb.Items.Add(struc.Id);
}
//"Skip" is the default structure choice
CTV1_cb.SelectedItem = CTV1_cb.Items[0];
CTV2_cb.SelectedItem = CTV1_cb.Items[0];
Spinal_cb.SelectedItem = Spinal_cb.Items[0];
Spinal2_cb.SelectedItem = Spinal2_cb.Items[0];
Spinal3_cb.SelectedItem = Spinal2_cb.Items[0];
//The comboboxes with CT images are filled. We will try to select only relevant images.
//NB THIS SELECTION WILL DEPEND ON THE COMMENTS SENT FROM THE CT SCANNER!
CT00_cb.Items.Add("skip");
CT10_cb.Items.Add("skip");
CT20_cb.Items.Add("skip");
CT30_cb.Items.Add("skip");
CT40_cb.Items.Add("skip");
CT50_cb.Items.Add("skip");
CT60_cb.Items.Add("skip");
CT70_cb.Items.Add("skip");
CT80_cb.Items.Add("skip");
CT90_cb.Items.Add("skip");
//All 3D images in the study belonging to the primary planning image are now looped through.
//When the images are a potential match, their series UID is saved for later use, as this is a unique number.
foreach (var img in MainStructureSet.Image.Series.Study.Images3D)
{
//We do not want to calculate on a MIP
if (img.Series.Comment.ToString().ToUpper().Contains("MIP") || img.Id.ToUpper().Contains("MIP"))
{
continue;
}
if (img.Series.Comment.ToString().Contains(" 0.0%") || img.Series.Comment.ToString().Contains("TRIGGER_DELAY 0%"))
{
CT00_cb.Items.Add(img.Series.Id + "/" + img.Id);
UID_00.Add(img.Series.UID);
}
if (img.Series.Comment.ToString().Contains("10.0%") || img.Series.Comment.ToString().Contains("TRIGGER_DELAY 10%"))
{
CT10_cb.Items.Add(img.Series.Id + "/" + img.Id);
UID_10.Add(img.Series.UID);
}
if (img.Series.Comment.ToString().Contains("20.0%") || img.Series.Comment.ToString().Contains("TRIGGER_DELAY 20%"))
{
CT20_cb.Items.Add(img.Series.Id + "/" + img.Id);
UID_20.Add(img.Series.UID);
}
if (img.Series.Comment.ToString().Contains("30.0%") || img.Series.Comment.ToString().Contains("TRIGGER_DELAY 30%"))
{
CT30_cb.Items.Add(img.Series.Id + "/" + img.Id);
UID_30.Add(img.Series.UID);
}
if (img.Series.Comment.ToString().Contains("40.0%") || img.Series.Comment.ToString().Contains("TRIGGER_DELAY 40%"))
{
CT40_cb.Items.Add(img.Series.Id + "/" + img.Id);
UID_40.Add(img.Series.UID);
}
if (img.Series.Comment.ToString().Contains("50.0%") || img.Series.Comment.ToString().Contains("TRIGGER_DELAY 50%"))
{
CT50_cb.Items.Add(img.Series.Id + "/" + img.Id);
UID_50.Add(img.Series.UID);
}
if (img.Series.Comment.ToString().Contains("60.0%") || img.Series.Comment.ToString().Contains("TRIGGER_DELAY 60%"))
{
CT60_cb.Items.Add(img.Series.Id + "/" + img.Id);
UID_60.Add(img.Series.UID);
}
if (img.Series.Comment.ToString().Contains("70.0%") || img.Series.Comment.ToString().Contains("TRIGGER_DELAY 70%"))
{
CT70_cb.Items.Add(img.Series.Id + "/" + img.Id);
UID_70.Add(img.Series.UID);
}
if (img.Series.Comment.ToString().Contains("80.0%") || img.Series.Comment.ToString().Contains("TRIGGER_DELAY 80%"))
{
CT80_cb.Items.Add(img.Series.Id + "/" + img.Id);
UID_80.Add(img.Series.UID);
}
if (img.Series.Comment.ToString().Contains("90.0%") || img.Series.Comment.ToString().Contains("TRIGGER_DELAY 90%"))
{
CT90_cb.Items.Add(img.Series.Id + "/" + img.Id);
UID_90.Add(img.Series.UID);
}
}
//The first image in each combobox is selected, it it exists.
if (CT00_cb.Items.Count > 1)
{
CT00_cb.SelectedItem = CT00_cb.Items[1];
}
else
{
CT00_cb.SelectedItem = CT00_cb.Items[0];
}
if (CT10_cb.Items.Count > 1)
{
CT10_cb.SelectedItem = CT10_cb.Items[1];
}
else
{
CT10_cb.SelectedItem = CT10_cb.Items[0];
}
if (CT20_cb.Items.Count > 1)
{
CT20_cb.SelectedItem = CT20_cb.Items[1];
}
else
{
CT20_cb.SelectedItem = CT20_cb.Items[0];
}
if (CT30_cb.Items.Count > 1)
{
CT30_cb.SelectedItem = CT30_cb.Items[1];
}
else
{
CT30_cb.SelectedItem = CT30_cb.Items[0];
}
if (CT40_cb.Items.Count > 1)
{
CT40_cb.SelectedItem = CT40_cb.Items[1];
}
else
{
CT40_cb.SelectedItem = CT40_cb.Items[0];
}
if (CT50_cb.Items.Count > 1)
{
CT50_cb.SelectedItem = CT50_cb.Items[1];
}
else
{
CT50_cb.SelectedItem = CT50_cb.Items[0];
}
if (CT60_cb.Items.Count > 1)
{
CT60_cb.SelectedItem = CT60_cb.Items[1];
}
else
{
CT60_cb.SelectedItem = CT60_cb.Items[0];
}
if (CT70_cb.Items.Count > 1)
{
CT70_cb.SelectedItem = CT70_cb.Items[1];
}
else
{
CT70_cb.SelectedItem = CT70_cb.Items[0];
}
if (CT80_cb.Items.Count > 1)
{
CT80_cb.SelectedItem = CT80_cb.Items[1];
}
else
{
CT80_cb.SelectedItem = CT80_cb.Items[0];
}
if (CT90_cb.Items.Count > 1)
{
CT90_cb.SelectedItem = CT90_cb.Items[1];
}
else
{
CT90_cb.SelectedItem = CT90_cb.Items[0];
}
// Buttens for finalizing the image choice are activated
SelectImages_btn.IsEnabled = true;
SelectImagesE_btn.IsEnabled = true;
//Errormessages are written in the UI.
Errors_txt.Text = errormessages;
}
#endregion SelectPlans
#region FindAndSelectImages
/// <summary>
/// Finds the correct 3D image given a seried UID list and a string defining the selected phase
/// </summary>
/// <param name="uid_list">A list of UIDs</param>
/// <param name="v">The image id</param>
/// <param name="phase">The phase identification string</param>
/// <returns></returns>
private VMS.TPS.Common.Model.API.Image FindCorrectImage(List<string> uid_list, string v, string phase)
{
//All images with the correct name are selected but only one is needed.
//(The tests of this function are probably not all needed anymore after I implemented the seriesUID check.)
// The user chose to skip this phase
if (v == "skip")
{
//If no image is choosen an error is written to the UI.
errormessages += "Phase: " + phase + " is skipped \n";
return null;
}
IEnumerable<VMS.TPS.Common.Model.API.Image> temp = MainStructureSet.Image.Series.Study.Images3D.Where(p => p.Series.Id + "/" + p.Id == v);
if (uid_list.Count() > 1 && temp.Count() > 1) //Sevral UIDs and several CTs with the same name
{
//We are adding a message if there are more CTs with the same name AND several possible series IDs.
errormessages += phase + ":NB several phases have the same id. Please verify that the plans are created correctly.\n";
foreach (var item in temp)
{
if ((item.Series.Id + "/" + item.Id) == v && uid_list.Contains(item.Series.UID))
{
return item;
}
}
}
else if (temp.Count() > 1) //Several CTs but only one correct UID. No need for a message.
{
foreach (var item in temp)
{
if ((item.Series.Id + "/" + item.Id) == v && uid_list.Contains(item.Series.UID))
{
return item;
}
}
}
else //Only one CT og one series UID
{
return temp.First();
}
//If no image is choosen an error is written to the UI.
errormessages += "No image was selected for " + phase + "\n";
return null;
}
/// <summary>
/// Selecting images for evaluation only.
/// </summary>
private void SelectImagesE_btn_Click(object sender, RoutedEventArgs e)
{
bool writeYN = false;
SelectImages(writeYN);
}
/// <summary>
/// Selecting images for copy and recalculaton of the main plan.
/// </summary>
private void SelectImages_btn_Click(object sender, RoutedEventArgs e)
{
bool writeYN = true;
SelectImages(writeYN);
}
/// <summary>
/// The images are selected and saved to the public variables
/// If there are plans calculated on the images, they will be added to the plan-comboboxes.
/// The function destinquish between the writable version and the non-writable by using the boolean writeYN
/// </summary>
/// <param name="writeYN">Boolean determining if the script will evaluate or create 4D plans</param>
private void SelectImages(bool writeYN)
{
if (writeYN == true) //The script will create new plans on the phases
{
progress_lb.Content = "... Images are selected. Press 'Create plans' to continue the script ...";
AllowUIToUpdate();
}
else //The script will evaluate already created plans on the phases
{
progress_lb.Content = "... Images are selected. Choose the plans to evaluate an press 'Evaluate plans' ...";
AllowUIToUpdate();
}
//The selected images are found by using the function "findCorrectImage".
img00 = FindCorrectImage(UID_00, CT00_cb.SelectedItem.ToString(), "phase 00");
img10 = FindCorrectImage(UID_10, CT10_cb.SelectedItem.ToString(), "phase 10");
img20 = FindCorrectImage(UID_20, CT20_cb.SelectedItem.ToString(), "phase 20");
img30 = FindCorrectImage(UID_30, CT30_cb.SelectedItem.ToString(), "phase 30");
img40 = FindCorrectImage(UID_40, CT40_cb.SelectedItem.ToString(), "phase 40");
img50 = FindCorrectImage(UID_50, CT50_cb.SelectedItem.ToString(), "phase 50");
img60 = FindCorrectImage(UID_60, CT60_cb.SelectedItem.ToString(), "phase 60");
img70 = FindCorrectImage(UID_70, CT70_cb.SelectedItem.ToString(), "phase 70");
img80 = FindCorrectImage(UID_80, CT80_cb.SelectedItem.ToString(), "phase 80");
img90 = FindCorrectImage(UID_90, CT90_cb.SelectedItem.ToString(), "phase 90");
//The plan comboboxes are cleared and ready to be filled after
CT00_plan_cb.Items.Clear();
CT10_plan_cb.Items.Clear();
CT20_plan_cb.Items.Clear();
CT30_plan_cb.Items.Clear();
CT40_plan_cb.Items.Clear();
CT50_plan_cb.Items.Clear();
CT60_plan_cb.Items.Clear();
CT70_plan_cb.Items.Clear();
CT80_plan_cb.Items.Clear();
CT90_plan_cb.Items.Clear();
// It is possible to select a plan on the image if there is one.
// Only plans in the same course as the selected plans are checked.
// We used the series UID as this is unique.
if (SelectedPlanSum == null) //Single plans
{
foreach (var plan in SelectedPlan.Course.PlanSetups)
{
if (img00 != null && plan.StructureSet.Image.Series.UID == img00.Series.UID && plan.StructureSet.Image.Id == img00.Id) CT00_plan_cb.Items.Add(plan.Id);
if (img10 != null && plan.StructureSet.Image.Series.UID == img10.Series.UID && plan.StructureSet.Image.Id == img10.Id) CT10_plan_cb.Items.Add(plan.Id);
if (img20 != null && plan.StructureSet.Image.Series.UID == img20.Series.UID && plan.StructureSet.Image.Id == img20.Id) CT20_plan_cb.Items.Add(plan.Id);
if (img30 != null && plan.StructureSet.Image.Series.UID == img30.Series.UID && plan.StructureSet.Image.Id == img30.Id) CT30_plan_cb.Items.Add(plan.Id);
if (img40 != null && plan.StructureSet.Image.Series.UID == img40.Series.UID && plan.StructureSet.Image.Id == img40.Id) CT40_plan_cb.Items.Add(plan.Id);
if (img50 != null && plan.StructureSet.Image.Series.UID == img50.Series.UID && plan.StructureSet.Image.Id == img50.Id) CT50_plan_cb.Items.Add(plan.Id);
if (img60 != null && plan.StructureSet.Image.Series.UID == img60.Series.UID && plan.StructureSet.Image.Id == img60.Id) CT60_plan_cb.Items.Add(plan.Id);
if (img70 != null && plan.StructureSet.Image.Series.UID == img70.Series.UID && plan.StructureSet.Image.Id == img70.Id) CT70_plan_cb.Items.Add(plan.Id);
if (img80 != null && plan.StructureSet.Image.Series.UID == img80.Series.UID && plan.StructureSet.Image.Id == img80.Id) CT80_plan_cb.Items.Add(plan.Id);
if (img90 != null && plan.StructureSet.Image.Series.UID == img90.Series.UID && plan.StructureSet.Image.Id == img90.Id) CT90_plan_cb.Items.Add(plan.Id);
}
}
else // Plan sums
{
foreach (var plan in SelectedPlanSum.Course.PlanSums)
{
if (img00 != null && plan.StructureSet.Image.Series.UID == img00.Series.UID && plan.StructureSet.Image.Id == img00.Id) CT00_plan_cb.Items.Add(plan.Id);
if (img10 != null && plan.StructureSet.Image.Series.UID == img10.Series.UID && plan.StructureSet.Image.Id == img10.Id) CT10_plan_cb.Items.Add(plan.Id);
if (img20 != null && plan.StructureSet.Image.Series.UID == img20.Series.UID && plan.StructureSet.Image.Id == img20.Id) CT20_plan_cb.Items.Add(plan.Id);
if (img30 != null && plan.StructureSet.Image.Series.UID == img30.Series.UID && plan.StructureSet.Image.Id == img30.Id) CT30_plan_cb.Items.Add(plan.Id);
if (img40 != null && plan.StructureSet.Image.Series.UID == img40.Series.UID && plan.StructureSet.Image.Id == img40.Id) CT40_plan_cb.Items.Add(plan.Id);
if (img50 != null && plan.StructureSet.Image.Series.UID == img50.Series.UID && plan.StructureSet.Image.Id == img50.Id) CT50_plan_cb.Items.Add(plan.Id);
if (img60 != null && plan.StructureSet.Image.Series.UID == img60.Series.UID && plan.StructureSet.Image.Id == img60.Id) CT60_plan_cb.Items.Add(plan.Id);
if (img70 != null && plan.StructureSet.Image.Series.UID == img70.Series.UID && plan.StructureSet.Image.Id == img70.Id) CT70_plan_cb.Items.Add(plan.Id);
if (img80 != null && plan.StructureSet.Image.Series.UID == img80.Series.UID && plan.StructureSet.Image.Id == img80.Id) CT80_plan_cb.Items.Add(plan.Id);
if (img90 != null && plan.StructureSet.Image.Series.UID == img90.Series.UID && plan.StructureSet.Image.Id == img90.Id) CT90_plan_cb.Items.Add(plan.Id);
}
}
// If the user wants to create new plans the correct buttons are enables
if (writeYN)
{
//PrepareImages_btn.IsEnabled = true;
CopyPlan_btn.IsEnabled = true;
}
// If the user wants to evaluate existing plans the correct buttons are enables
// And if the plans are not to be copied, we try to select the plans that already exist
if (!writeYN)
{
EvalDoseE_btn.IsEnabled = true;
try
{
CT00_plan_cb.SelectedItem = CT00_plan_cb.Items[0];
}
catch (Exception)
{
}
try
{
CT10_plan_cb.SelectedItem = CT10_plan_cb.Items[0];
}
catch (Exception)
{
}
try
{
CT20_plan_cb.SelectedItem = CT20_plan_cb.Items[0];
}
catch (Exception)
{
}
try
{
CT30_plan_cb.SelectedItem = CT30_plan_cb.Items[0];
}
catch (Exception)
{
}
try
{
CT40_plan_cb.SelectedItem = CT40_plan_cb.Items[0];
}
catch (Exception)
{
}
try
{
CT50_plan_cb.SelectedItem = CT50_plan_cb.Items[0];
}
catch (Exception)
{
}
try
{
CT60_plan_cb.SelectedItem = CT60_plan_cb.Items[0];
}
catch (Exception)
{
}
try
{
CT70_plan_cb.SelectedItem = CT70_plan_cb.Items[0];
}
catch (Exception)
{
}
try
{
CT80_plan_cb.SelectedItem = CT80_plan_cb.Items[0];
}
catch (Exception)
{
}
try
{
CT90_plan_cb.SelectedItem = CT90_plan_cb.Items[0];
}
catch (Exception)
{
}
}
Errors_txt.Text = errormessages;
}
#endregion FindAndSelectImages
#region AdjustImages
/// <summary>
/// The plans are copied to the phases and new buttons are enabled.
/// The copy process depends on the plan type.
/// </summary>
private void CopyPlan_btn_Click(object sender, RoutedEventArgs e)
{
ScriptInfo.Patient.BeginModifications();
string[] body = new string[10];
string[] calib = new string[10];
string[] overwrite = new string[10];
VMS.TPS.Common.Model.API.Image[] imageList = new VMS.TPS.Common.Model.API.Image[10] { img00, img10, img20, img30, img40, img50, img60, img70, img80, img90 };
AdjustPhaseImages adjustPhaseImages = new AdjustPhaseImages(imageList, ScriptInfo, MainStructureSet);
if (body_chb.IsChecked == true)
{
progress_lb.Content = "... creating structure sets and BODY ...";
AllowUIToUpdate();
body = adjustPhaseImages.CreateBody();
}
if (copybody_chb.IsChecked == true)
{
progress_lb.Content = "... copying BODY ...";
AllowUIToUpdate();
body = adjustPhaseImages.CopyBody();
}
if (calib_chb.IsChecked == true)
{
progress_lb.Content = "... Setting calibration curves ...";
AllowUIToUpdate();
calib = adjustPhaseImages.CopyCalibration();
}
if (overw_chb.IsChecked == true)
{
progress_lb.Content = "... Copying and overwriting structures ...";
AllowUIToUpdate();
overwrite = adjustPhaseImages.OverwriteStructures();
}
for (int i = 0; i < 10; i++)
{
if (body[i] != "" || calib[i] != "" || overwrite[i] != "")
{
errormessages += " PHASE " + i.ToString() + " : \n";
errormessages += body[i];
errormessages += calib[i];
errormessages += overwrite[i];
}
}
Errors_txt.Text = errormessages;
if (SelectedPlan != null && SelectedPlan.PlanType.ToString().Contains("Proton")) //single plan and proton plan
{
progress_lb.Content = "... Copying proton plans ...";
AllowUIToUpdate();
CopyProtons();
}
else if (SelectedPlan != null) // single plan and photon plan
{
progress_lb.Content = "... Copying photon plans ...";
AllowUIToUpdate();
CopyPhotons();
}
if (SelectedPlanSum != null && SelectedPlanSum.PlanSetups.First().PlanType.ToString().Contains("Proton")) //Sum plan and proton plan
{
progress_lb.Content = "... Copying proton sum plans ...";
AllowUIToUpdate();
CopyProtonsSum();
}
else if (SelectedPlanSum != null) //Sum plan and photon plan
{
progress_lb.Content = "... Copying photon sum plans ...";
AllowUIToUpdate();
CopyPhotonsSum();
}
// The evaluation button is pressed automatically in this case.
EvalDoseE_btn.RaiseEvent(new RoutedEventArgs(ButtonBase.ClickEvent));
EvalDoseE_btn.IsEnabled = true;
Errors_txt.Text = errormessages;
}
#endregion AdjustImages
#region CopyPhotonPlans
/// <summary>
/// The photon sumplan is copied to the phases
/// </summary>
private void CopyPhotonsSum()
{
//Name of the photonplan. TODO: this is a bit stupid, as if the name already exists the script will crash...
string ph_prefix = FindPrefixForPlans();
if (ph_prefix == null)
{
return;
}
//The photon sum plans are copied and calculated
//The plans are added to the comboboxes.
//The plans are saved in the public variables.
//The UI is updated
List<ExternalPlanSetup> list0 = new List<ExternalPlanSetup> ();
List<ExternalPlanSetup> list1 = new List<ExternalPlanSetup> ();
List<ExternalPlanSetup> list2 = new List<ExternalPlanSetup> ();
List<ExternalPlanSetup> list3 = new List<ExternalPlanSetup> ();
List<ExternalPlanSetup> list4 = new List<ExternalPlanSetup> ();
List<ExternalPlanSetup> list5 = new List<ExternalPlanSetup> ();
List<ExternalPlanSetup> list6 = new List<ExternalPlanSetup> ();
List<ExternalPlanSetup> list7 = new List<ExternalPlanSetup> ();
List<ExternalPlanSetup> list8 = new List<ExternalPlanSetup> ();
List<ExternalPlanSetup> list9 = new List<ExternalPlanSetup> ();
for (int i = 0; i < SelectedPlanSum.PlanSetups.Count(); i++)
{
var copyFrom = SelectedPlanSum.PlanSetups.ElementAt(i);
progress_lb.Content = "... Copying to phase 00 for plan no. " + i.ToString() + " ...";
AllowUIToUpdate();
list0.Add(CalcPhoton(img00, rec_00, ph_prefix + "00_" + i.ToString(), copyFrom));
progress_lb.Content = "... Copying to phase 10 for plan no. " + i.ToString() + " ...";
AllowUIToUpdate();
list1.Add(CalcPhoton(img10, rec_10, ph_prefix + "10_" + i.ToString(), copyFrom));
progress_lb.Content = "... Copying to phase 20 for plan no. " + i.ToString() + " ...";
AllowUIToUpdate();
list2.Add(CalcPhoton(img20, rec_20, ph_prefix + "20_" + i.ToString(), copyFrom));
progress_lb.Content = "... Copying to phase 30 for plan no. " + i.ToString() + " ...";
AllowUIToUpdate();
list3.Add(CalcPhoton(img30, rec_30, ph_prefix + "30_" + i.ToString(), copyFrom));
progress_lb.Content = "... Copying to phase 40 for plan no. " + i.ToString() + " ...";
AllowUIToUpdate();
list4.Add(CalcPhoton(img40, rec_40, ph_prefix + "40_" + i.ToString(), copyFrom));
progress_lb.Content = "... Copying to phase 50 for plan no. " + i.ToString() + " ...";
AllowUIToUpdate();
list5.Add(CalcPhoton(img50, rec_50, ph_prefix + "50_" + i.ToString(), copyFrom));
progress_lb.Content = "... Copying to phase 60 for plan no. " + i.ToString() + " ...";
AllowUIToUpdate();
list6.Add(CalcPhoton(img60, rec_60, ph_prefix + "60_" + i.ToString(), copyFrom));
progress_lb.Content = "... Copying to phase 70 for plan no. " + i.ToString() + " ...";
AllowUIToUpdate();
list7.Add(CalcPhoton(img70, rec_70, ph_prefix + "70_" + i.ToString(), copyFrom));
progress_lb.Content = "... Copying to phase 80 for plan no. " + i.ToString() + " ...";
AllowUIToUpdate();
list8.Add(CalcPhoton(img80, rec_80, ph_prefix + "80_" + i.ToString(), copyFrom));
progress_lb.Content = "... Copying to phase 90 for plan no. " + i.ToString() + " ...";
AllowUIToUpdate();
list9.Add(CalcPhoton(img90, rec_90, ph_prefix + "90_" + i.ToString(), copyFrom));
progress_lb.Content = "...All plans are copied ...";
AllowUIToUpdate();
}
if (img00 != null)
{
newPlanSum00 = SelectedPlanSum.Course.CreatePlanSum(list0, img00);
newPlanSum00.Id = "Sum_" + ph_prefix + "00";
AddPhasePlanSum(CT00_plan_cb, newPlanSum00);
}
if (img10 != null)
{
newPlanSum10 = SelectedPlanSum.Course.CreatePlanSum(list1, img10);
newPlanSum10.Id = "Sum_" + ph_prefix + "10";
AddPhasePlanSum(CT10_plan_cb, newPlanSum10);
}
if (img20 != null)
{
newPlanSum20 = SelectedPlanSum.Course.CreatePlanSum(list2, img20);
newPlanSum20.Id = "Sum_" + ph_prefix + "20";
AddPhasePlanSum(CT20_plan_cb, newPlanSum20);
}
if (img30 != null)
{
newPlanSum30 = SelectedPlanSum.Course.CreatePlanSum(list3, img30);
newPlanSum30.Id = "Sum_" + ph_prefix + "30";
AddPhasePlanSum(CT30_plan_cb, newPlanSum30);
}
if (img40 != null)
{
newPlanSum40 = SelectedPlanSum.Course.CreatePlanSum(list4, img40);
newPlanSum40.Id = "Sum_" + ph_prefix + "40";
AddPhasePlanSum(CT40_plan_cb, newPlanSum40);
}
if (img50 != null)
{
newPlanSum50 = SelectedPlanSum.Course.CreatePlanSum(list5, img50);