-
Notifications
You must be signed in to change notification settings - Fork 113
Expand file tree
/
Copy pathGenerateWebDVH.cs
More file actions
197 lines (183 loc) · 7.71 KB
/
GenerateWebDVH.cs
File metadata and controls
197 lines (183 loc) · 7.71 KB
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
////////////////////////////////////////////////////////////////////////////////
// GenerateWebDVH.cs
//
// A ESAPI v11/v13 Script that generates a DVH in javascript using
// Google's LineChart. Works only in Chrome, works only when an
// internet connection is present. Loads the base javascript
// that does the heavy lifting from https://www.google.com/jsapi.
//
// Change the structure names on Line 64 to those you want displayed
// on the DVH chart.
//
// To test whether this Script will work in your environment, load the included
// generated HTML file "sample_dvh.html" in Chrome. If you see a DVH
// chart with two graphs for 'Parotid LT' and 'Parotid RT' then this
// script will work for you.
//
// Copyright (c) 2014 Varian Medical Systems, Inc.
//
// 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.
////////////////////////////////////////////////////////////////////////////////
using System;
using System.Linq;
using System.Text;
using System.Windows;
using System.Collections.Generic;
using VMS.TPS.Common.Model.API;
using VMS.TPS.Common.Model.Types;
using System.Xml;
using System.Xml.Linq;
namespace VMS.TPS
{
public class Script
{
public Script()
{
}
public void Execute(ScriptContext context /*, System.Windows.Window window*/)
{
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.IndentChars = ("\t");
System.IO.MemoryStream mStream = new System.IO.MemoryStream();
using (XmlWriter writer = XmlWriter.Create(mStream, settings))
{
// generate DVHs in an HTML report for selected structures
string[] selectedStructures = { "Parotid LT", "Parotid RT" }; // TODO: change these to the structures you want to generate DVHs for
ExportDVHs(context.PlanSetup, context.StructureSet, selectedStructures, writer);
// done writing
writer.Flush();
mStream.Flush();
// write the XML file.
string temp = System.Environment.GetEnvironmentVariable("TEMP");
string htmlPath = string.Format("{0}\\{1}({2})-plan-{3}.html", temp,
context.Patient.LastName, context.Patient.Id, context.PlanSetup.Id);
using (System.IO.FileStream file = new System.IO.FileStream
(htmlPath, System.IO.FileMode.Create, System.IO.FileAccess.Write))
{
// Have to rewind the MemoryStream in order to read its contents.
mStream.Position = 0;
mStream.CopyTo(file);
file.Flush();
file.Close();
}
// 'Start' generated HTML file to launch browser window
System.Diagnostics.Process.Start(htmlPath);
// Sleep for a few seconds to let internet browser window to start
System.Threading.Thread.Sleep(TimeSpan.FromSeconds(3));
}
}
void ExportDVHs(PlanSetup plan, StructureSet ss, string[] structureIds, XmlWriter writer)
{
writer.WriteStartElement("html");
writer.WriteStartElement("head");
writer.WriteElementString("title", "web dvh");
XElement script = new XElement("script",
new XAttribute("type", "text/javascript"),
new XAttribute("src", "https://www.google.com/jsapi"),
"// need this due to bug in google LineChart javascript"
);
script.WriteTo(writer);
script = new XElement("script",
new XAttribute("type", "text/javascript"),
@"
google.load('visualization', '1', { packages: ['corechart'] });
");
script.WriteTo(writer);
Tuple<string, DVHData>[] listDVHs = new Tuple<string, DVHData>[structureIds.Length];
int index = 0, maxPtsIndex=0;
int maxDVHPts = int.MinValue;
// search through the list of structure ids until we find one
Structure structure = null;
foreach (string volumeId in structureIds)
{
structure = (from s in ss.Structures
where s.Id.ToUpper().CompareTo(volumeId.ToUpper()) == 0
select s).FirstOrDefault();
if (structure == null)
continue; // structure not found
DVHData dvh = plan.GetDVHCumulativeData(structure, DoseValuePresentation.Absolute, VolumePresentation.Relative, 0.1);
listDVHs[index] = new Tuple<string, DVHData>(volumeId, dvh);
if (maxDVHPts < dvh.CurveData.Count())
{
maxPtsIndex = index;
}
maxDVHPts = Math.Max(maxDVHPts, dvh.CurveData.Count());
index++;
}
// collect the dose / volume data into a single matrix
double [,] dvhData = new double[maxDVHPts, listDVHs.Count()];
for(int j = 0; j < listDVHs.Count(); j++)
for (int i = 0; i < maxDVHPts; i++)
{
dvhData[i, j] = 0.0;
DVHData dvh = listDVHs[j].Item2;
if (dvh.CurveData.Count() > i)
dvhData[i, j] = dvh.CurveData[i].Volume;
}
string javascript = @"
function drawVisualization() {
var data = google.visualization.arrayToDataTable([
['Dose'";
foreach (Tuple<string, DVHData> dvhItemData1 in listDVHs)
{
javascript += ", '" + dvhItemData1.Item1 + "'";
}
javascript += @"],
";
int x = 0;
foreach (DVHPoint pt in listDVHs[maxPtsIndex].Item2.CurveData)
{
string doseVolumes = string.Format("[{0:0.0}", pt.DoseValue.Dose);
for(int j = 0; j < listDVHs.Count(); j++)
{
doseVolumes += string.Format(",{0:0.00000}", dvhData[x,j]);
}
doseVolumes += @"],";
javascript += doseVolumes;
x++;
}
javascript += @"
]);
// Create and draw the visualization.
new google.visualization.LineChart(document.getElementById('xxxxx')).
draw(data, {
width: 1000, height: 800,
vAxis: { maxValue: 100 }
}
);
}
google.setOnLoadCallback(drawVisualization);
";
XElement script2 = new XElement("script",
new XAttribute("type", "text/javascript"),
javascript
);
script2.WriteTo(writer);
writer.WriteEndElement();// head
XElement body = new XElement("body",
new XElement("div",
new XAttribute("id", "xxxxx"),
new XAttribute("style", "width: 900px; height: 500px;")
));
body.WriteTo(writer);
writer.WriteEndElement();// html
}
}
}