-
Notifications
You must be signed in to change notification settings - Fork 0
/
DHV.cs
134 lines (107 loc) · 5.28 KB
/
DHV.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
using System;
using System.Linq;
using System.Collections.Generic;
using System.IO;
using System.Windows;
using VMS.TPS.Common.Model.API;
using VMS.TPS.Common.Model.Types;
using System.Text;
namespace VMS.TPS
{
class Script
{
public void Execute(ScriptContext context /*, System.Windows.Window window*/)
{
if (context.Patient == null || context.PlanSetup == null ||
context.PlanSetup.Dose == null || context.StructureSet == null)
MessageBox.Show("Please load a patient, structure set, and a plan that has dose calculated!");
// get reference to selected plan
PlanSetup plan = context.PlanSetup;
// linq query finds the first PTV structure
Structure target = (from s in context.StructureSet.Structures where
s.DicomType == "PTV" select s).FirstOrDefault();
if (target == null)
throw new ApplicationException("Plan '"+plan.Id+"' has no PTV!");
// build exported DVH filename, put it in the users temp directory
string temp = System.Environment.GetEnvironmentVariable("TEMP");
string dvhFilePath = temp + @"\dvh.csv";
// export DVH for 'target' to 'dvhFilePath'
exportDVH(plan, target, dvhFilePath);
// 'Start' generated CSV file to launch Excel window
System.Diagnostics.Process.Start(dvhFilePath);
// Sleep for a few seconds to let Excel window start
System.Threading.Thread.Sleep(TimeSpan.FromSeconds(3));
}
private void exportDVH(PlanSetup plan, Structure target, string fileName)
{
// extract DVH data
DVHData dvhData = plan.GetDVHCumulativeData(target,
DoseValuePresentation.Relative,
VolumePresentation.AbsoluteCm3, 0.1);
if (dvhData == null)
throw new ApplicationException("No DVH data for target '"+target.Id+"'.");
// export DVH data as a CSV file
using (System.IO.StreamWriter sw =
new System.IO.StreamWriter(fileName, false, Encoding.ASCII))
{
// write the header, assume the first dvh point represents the others
DVHPoint rep = dvhData.CurveData[0];
sw.WriteLine(
string.Format("Relative dose [{0}],Structure volume [{1}],",
rep.DoseValue.UnitAsString, rep.VolumeUnit)
);
// write each row of dose / volume data
foreach (DVHPoint pt in dvhData.CurveData)
sw.WriteLine(string.Format("{0:0.0},{1:0.00000}",
pt.DoseValue.Dose, pt.Volume));
}
}
}
}
// {
// public class Execute(ScriptContext context /*, System.Windows.Window window*/)
// {
// if (context.Patient == null || context.PlanSetup == null ||
// context.PlanSetup.Dose == null || context.StructureSet == null)
// MessageBox.Show("Please load a patient, structure set, and a plan that has dose calculated!");
// // get reference to selected plan
// PlanSetup plan = context.PlanSetup;
// // linq query finds the first PTV structure
// Structure target = (from s in context.StructureSet.Structures where
// s.DicomType == "PTV" select s).FirstOrDefault();
// if (target == null)
// throw new ApplicationException("Plan '"+plan.Id+"' has no PTV!");
// // build exported DVH filename, put it in the users temp directory
// string temp = System.Environment.GetEnvironmentVariable("TEMP");
// string dvhFilePath = temp + @"\dvh.csv";
// // export DVH for 'target' to 'dvhFilePath'
// exportDVH(plan, target, dvhFilePath);
// // 'Start' generated CSV file to launch Excel window
// System.Diagnostics.Process.Start(dvhFilePath);
// // Sleep for a few seconds to let Excel window start
// System.Threading.Thread.Sleep(TimeSpan.FromSeconds(3));
// }
// public void Execute(ScriptContext context /*, System.Windows.Window window*/)
// {
// if (context.Patient == null || context.PlanSetup == null ||
// context.PlanSetup.Dose == null || context.StructureSet == null)
// MessageBox.Show("Please load a patient, structure set, and a plan that has dose calculated!");
// // get reference to selected plan
// PlanSetup plan = context.PlanSetup;
// // linq query finds the first PTV structure
// Structure target = (from s in context.StructureSet.Structures where
// s.DicomType == "PTV" select s).FirstOrDefault();
// if (target == null)
// throw new ApplicationException("Plan '"+plan.Id+"' has no PTV!");
// // build exported DVH filename, put it in the users temp directory
// string temp = System.Environment.GetEnvironmentVariable("TEMP");
// string dvhFilePath = temp + @"\dvh.csv";
// // export DVH for 'target' to 'dvhFilePath'
// exportDVH(plan, target, dvhFilePath);
// // 'Start' generated CSV file to launch Excel window
// System.Diagnostics.Process.Start(dvhFilePath);
// // Sleep for a few seconds to let Excel window start
// System.Threading.Thread.Sleep(TimeSpan.FromSeconds(3));
// }
// }
// //public class TableForm