-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathScriptExecute.cs
More file actions
350 lines (279 loc) · 18 KB
/
ScriptExecute.cs
File metadata and controls
350 lines (279 loc) · 18 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
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
using System;
using System.Media;
using System.Numerics;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Media.Media3D;
using g3;
using System.ComponentModel;
using System.IO;
using System.Reflection;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using VMS.TPS.Common.Model.API;
using VMS.TPS.Common.Model.Types;
using CollisionCheck;
/*
Collision Check - Main program
Copyright (c) 2020 Radiation Oncology Department, Lahey Hospital and Medical Center
Written by Zackary T. Morelli
Release 2.0 August 8, 2020
This program is multi-threaded using the Task Parallel Library (TPL) found in .NET Framework 4.5. It also uses traditional threading from earlier versions of .NET.
Thread safety is achieved mostly through the use of custom classes to completley extract ESAPI (which cannot be used in a multi-threaded environment).
The Main thread starts here when the Execute method the ESAPI Script class is called. The GUI is then called on it's own Task, and the execute method (button) of the of GUI is called on it's own Task as well.
This ensures that the GUI is always responsive. The Execute method from the GUI then conducts the Collision analysis for each beam in parallel (at the same time), and does any ARIA database querying on a separate thread.
This ensures the program runs smoothly.
This program is expressely written as a plug-in script for use with Varian's Eclipse Treatment Planning System, and requires Varian's API files to run properly.
This program also requires .NET Framework 4.5.0 to run properly.
This program also uses the Gradientspace package (g3) to generate and manipulate 3D mesh structures, and write them as STL files.
This program also uses the HelixToolkit package to render the STL files of each beam if a collision happens. This is not located here, but is in another class/file called 3DRender. 3DRender is used by the GUI class.
Also, very important, in the case of a static MLC plan, this program will start a completley new process (on it's own asychronous Task) on the computer and run a complete separate program called Colliosncheck_Inforetrieval.
Collisioncheck_inforetrieval is an executable command-line program (written by me) saved on the ARIA server which uses an Entity Framework 6 model called ARIAQUERY to query the ARIA database for gantry information that the Collision check program needs. This information is not available in the Eclipse API if the plan does not have Control points.
The gantry information obtained by CollisonCheck_inforetrieval is written to the standard output stream of the command-line, which is actually redirected and read in to collision check. The retrieval program then ends.
ARIAQUERY is a separate class (also made by me) that is an Entity Framework 6 (a popular microsoft package for dealing with SQL databases) model of the department's ARIA database SQL Server.
The details of how this works are not explained for security purposes.
all linear dimensions are expressed in millimeters and all angles are expressed in degrees (many angles are converted from radians).
the program is specifically based off the physical dimensions of the department's C-ARM Varian Linacs.
This includes the API's internal vector objects and the positions it reports of various objects
THE COORDINATE SYSTEM IS IN DICOM AND IS COMPLETLEY DEFINED BY THE TREATMENT PLANNING CT OF THE PATIENT. THIS MEANS THE COORDINATE SYSTEM IS AFFECTED BY TREATMENT ORIENTATION. COUCH KICKS ROTATE THE ENTIRE COORDINATE SYSTEM AS WELL.
The program is designed around this and is able to produce an accurate geometric model that accounts for these coordinate system complications. You may notice that the part of this program that makes the gantry structure includes an absurd amount of vector transformations. This is because of the coordinate system.
Description: This is the main program that is called when Collision Check is run. It includes a GUI, which is called from here.
Specifically, The execute method of the VMS Script class calles the GUI (separate file). When the user clicks "Execute" on the GUI, the GUI then calls the CollionCheck method, located here.
CollisonCheck calls the Boxmaker method. CollisionCheck ouputs collision information using the custom CollisonAlert class (located here).
The User uses the GUI to select which plan to run the program on (out of the courses that they have open) and the Body area of the CT Scan and the patient's Height.
The GUI also outputs collision information if neccesary.
*/
namespace VMS.TPS
{
public class Script // creates a class called Script within the VMS.TPS Namesapce
{
public Script() { } // instantiates a Script class
// Declaration space for all the functions which make up the program.
// Execution begins with the "Execute" function.
// gantry head 77 cm wide
// 41.5 cm distance from iso to gantry head
// This is called in the collision analysis
//This is the execute function of the script class which calls the GUI. The GUI in turn calls the CollionCheck method.
public void Execute(ScriptContext context)
{
//Variable declaration space
IEnumerable<PlanSetup> Plans = context.PlansInScope;
Patient patient = context.Patient;
Image image = context.Image;
// MessageBox.Show("Trig 1");
if (context.Patient == null)
{
System.Windows.Forms.MessageBox.Show("Please load a patient with a treatment plan before running this script!");
return;
}
//populate my own classes with ESAPI info in order to completley isolate ESAPI, that way we can multi-thread.
// that requires figuring out a lot of stiff here though before we call the GUI (on its own thread)
List<PLAN> PLANS = new List<PLAN>();
string strctid = null;
bool couchexist = false;
bool breastboardexists = false;
try
{
foreach (PlanSetup plan in Plans)
{
try
{
strctid = plan.StructureSet.Id;
}
catch (NullReferenceException e)
{
System.Windows.Forms.MessageBox.Show("The plan " + plan.Id + " does not have a structure set!");
// no structure set, skip
continue;
}
string patientId = plan.Course.Patient.Id;
string patientsex = plan.Course.Patient.Sex;
string courseId = plan.Course.Id;
// retrieves the body structure
IEnumerator BR = plan.StructureSet.Structures.GetEnumerator();
BR.MoveNext();
BR.MoveNext();
BR.MoveNext();
Structure Body = (Structure)BR.Current;
//IEnumerator CSR = plan.StructureSet.Structures.GetEnumerator();
//CSR.MoveNext();
//CSR.MoveNext();
//CSR.MoveNext();
//Structure CouchSurface = (Structure)CSR.Current;
IEnumerator CIR = plan.StructureSet.Structures.GetEnumerator();
CIR.MoveNext();
CIR.MoveNext();
CIR.MoveNext();
Structure CouchInterior = (Structure)CIR.Current;
IEnumerator CPR = plan.StructureSet.Structures.GetEnumerator();
CPR.MoveNext();
CPR.MoveNext();
CPR.MoveNext();
Structure Prone_Brst_Board = (Structure)CPR.Current;
Vector3d bodycenter = new Vector3d();
Vector3d couchinteriorcenter = new Vector3d();
Vector3d breastboardcenter = new Vector3d();
foreach (Structure STR in plan.StructureSet.Structures)
{
if (STR.Id == "Body")
{
Body = STR;
bodycenter = new Vector3d(Body.CenterPoint.x, Body.CenterPoint.y, Body.CenterPoint.z);
}
else if (STR.Id.Contains("CouchInterior") || STR.Id.Contains("couchinterior") || STR.Id.Contains("couch interior") || STR.Id.Contains("couch_interior") || STR.Id.Contains("Couch_Interior") || STR.Id.Contains("Couch Interior"))
{
if (STR.IsEmpty == true || STR.Volume < 0.0)
{
System.Windows.Forms.MessageBox.Show("The Couch Interior structure is not contoured!");
continue;
}
// System.Windows.Forms.MessageBox.Show("Found couch interior");
couchexist = true;
CouchInterior = STR;
couchinteriorcenter = new Vector3d(CouchInterior.CenterPoint.x, CouchInterior.CenterPoint.y, CouchInterior.CenterPoint.z);
}
else if (STR.Id.Contains("Prone_Brst_Board") || STR.Id.Contains("NS_Prone_Bst_Brd") || STR.Id.Contains("Prone_Bst_Brd") || STR.Id.Contains("Prone_Brst_Brd") || STR.Id.Contains("Prone_Bst_Board") || STR.Id.Contains("Prone Brst Board") || STR.Id.Contains("Prone Bst Brd") || STR.Id.Contains("Prone Brst Brd") || STR.Id.Contains("Prone Bst Board") || STR.Id.Contains("prone_brst_board") || STR.Id.Contains("prone_bst_brd") || STR.Id.Contains("prone_brst_brd") || STR.Id.Contains("prone_bst_board") || STR.Id.Contains("prone brst board") || STR.Id.Contains("prone bst brd") || STR.Id.Contains("pron brst brd") || STR.Id.Contains("prone bst board"))
{
if (STR.IsEmpty == true || STR.Volume < 0.0)
{
System.Windows.Forms.MessageBox.Show("The Prone Breast Board structure is not contoured!");
continue;
}
// System.Windows.Forms.MessageBox.Show("Found breast board");
Prone_Brst_Board = STR;
breastboardexists = true;
breastboardcenter = new Vector3d(Prone_Brst_Board.CenterPoint.x, Prone_Brst_Board.CenterPoint.y, Prone_Brst_Board.CenterPoint.z);
}
//else if (STR.Id == "CouchSurface" || )
//{
// CouchSurface = STR;
// findCouchSurf = true;
//}
// findCouchSurf = false;
}
//System.Windows.Forms.MessageBox.Show(plan.TreatmentOrientation.ToString());
string PATIENTORIENTATION = null;
// Head first prone
if (plan.TreatmentOrientation == PatientOrientation.HeadFirstSupine)
{
PATIENTORIENTATION = "HeadFirstSupine";
}
else if (plan.TreatmentOrientation == PatientOrientation.HeadFirstProne)
{
PATIENTORIENTATION = "HeadFirstProne";
}
else if (plan.TreatmentOrientation == PatientOrientation.FeetFirstSupine)
{
PATIENTORIENTATION = "FeetFirstSupine";
}
else if (plan.TreatmentOrientation == PatientOrientation.FeetFirstProne)
{
PATIENTORIENTATION = "FeetFirstProne";
}
List<BEAM> Beams = new List<BEAM>();
foreach (Beam beam in plan.Beams)
{
string mlctype = null;
if (beam.MLCPlanType == MLCPlanType.Static)
{
mlctype = "Static";
}
else
{
mlctype = "Not Static";
}
Vector3d iso = new Vector3d(beam.IsocenterPosition.x, beam.IsocenterPosition.y, beam.IsocenterPosition.z);
string gantrydir = null;
if (beam.GantryDirection == GantryDirection.Clockwise)
{
gantrydir = "Clockwise";
}
else if (beam.GantryDirection == GantryDirection.CounterClockwise)
{
gantrydir = "CounterClockwise";
}
else if (beam.GantryDirection == GantryDirection.None)
{
gantrydir = "None";
}
string beamId = beam.Id;
bool setupfield = beam.IsSetupField;
double arclength = beam.ArcLength;
List<CONTROLPOINT> controlpoints = new List<CONTROLPOINT>();
foreach (ControlPoint cp in beam.ControlPoints)
{
controlpoints.Add(new CONTROLPOINT { Couchangle = cp.PatientSupportAngle, Gantryangle = cp.GantryAngle });
}
Beams.Add(new BEAM { MLCtype = mlctype, Isocenter = iso, gantrydirection = gantrydir, beamId = beamId, ControlPoints = controlpoints, setupfield = setupfield, arclength = arclength });
}
PLANS.Add(new PLAN { planId = plan.Id, StructureSetId = plan.StructureSet.Id, TreatmentOrientation = PATIENTORIENTATION, Body = Body.MeshGeometry, CouchInterior = CouchInterior.MeshGeometry, ProneBreastBoard = Prone_Brst_Board.MeshGeometry, breastboardexists = breastboardexists, couchexists = couchexist, Beams = Beams, patientId = patientId, patientsex = patientsex, courseId = courseId, Bodycenter = bodycenter, BreastBoardcenter = breastboardcenter, CouchInteriorcenter = couchinteriorcenter, Bodyvects = new List<Vector3d>(), Bodyindices = new List<int>(), CouchInteriorvects = new List<Vector3d>(), CouchInteriorindices = new List<int>(), BreastBoardvects = new List<Vector3d>(), BreastBoardindices = new List<int>(), BodyBoxXsize = 1000000.0, BodyBoxYSize = 1000000.0 , BodyBoxZSize = 1000000.0});
}
foreach(PLAN Plan in PLANS)
{
// System.Windows.Forms.MessageBox.Show(Plan.planId + "START body vector conversion");
//System.Windows.Forms.MessageBox.Show("Body positions size: " + Plan.Body.Positions.Count);
foreach (Point3D p in Plan.Body.Positions)
{
double XP = p.X;
double YP = p.Y;
double ZP = p.Z;
Vector3d Vect = new Vector3d(XP, YP, ZP);
Plan.Bodyvects.Add(Vect);
}
foreach (int t in Plan.Body.TriangleIndices)
{
Plan.Bodyindices.Add(t);
}
if (Plan.couchexists == true)
{
// System.Windows.Forms.MessageBox.Show("couch vector conversion");
foreach (Point3D p in Plan.CouchInterior.Positions)
{
Plan.CouchInteriorvects.Add(new g3.Vector3d { x = p.X, y = p.Y, z = p.Z });
}
foreach (int t in Plan.CouchInterior.TriangleIndices)
{
Plan.CouchInteriorindices.Add(t);
}
}
if (Plan.breastboardexists == true)
{
// System.Windows.Forms.MessageBox.Show("breast board vector conversion");
foreach (Point3D p in Plan.ProneBreastBoard.Positions)
{
Plan.BreastBoardvects.Add(new g3.Vector3d { x = p.X, y = p.Y, z = p.Z });
}
foreach (int t in Plan.ProneBreastBoard.TriangleIndices)
{
Plan.BreastBoardindices.Add(t);
}
}
Plan.BodyBoxXsize = Plan.Body.Bounds.SizeX;
Plan.BodyBoxYSize = Plan.Body.Bounds.SizeY;
Plan.BodyBoxZSize = Plan.Body.Bounds.SizeZ;
}
}
catch(Exception e)
{
System.Windows.Forms.MessageBox.Show(e.ToString() + "\n\n\n" + e.StackTrace + "\n\n\n" + e.InnerException);
}
IMAGE Image = new IMAGE();
Image.imageuserorigin = new Vector3d(image.UserOrigin.x, image.UserOrigin.y, image.UserOrigin.z);
Image.imageorigin = new Vector3d(image.Origin.x, image.Origin.y, image.Origin.z);
System.Windows.Forms.Application.EnableVisualStyles();
//Starts GUI
// System.Windows.Forms.MessageBox.Show("Starting GUI on separate thread.");
Task.Run(() => System.Windows.Forms.Application.Run(new GUI(PLANS, Image)));
// System.Windows.Forms.MessageBox.Show("After GUI Call, main script thread will now return and close.");
//should put this on it's own thread.
return;
}
} // end of the script class and VMS namespace
}