Exemplo n.º 1
0
        private void OnCalculatePlan()
        {
            patient.BeginModifications();
            Course            course_temp = patient.AddCourse();
            ExternalPlanSetup plan_temp   = course_temp.
                                            AddExternalPlanSetup(patient.StructureSets.FirstOrDefault());
            ExternalBeamMachineParameters exBeamParams = new ExternalBeamMachineParameters(
                "HESN10",
                "6X",
                600,
                "STATIC",
                null);

            foreach (string fs in FieldSizes.Split(';'))
            {
                double fsd = Convert.ToDouble(fs);
                plan_temp.AddStaticBeam(exBeamParams,
                                        new VRect <double>(-fsd / 2 * 10, -fsd / 2 * 10, fsd / 2 * 10, fsd / 2 * 10),
                                        0,
                                        0,
                                        0,
                                        new VVector(0, -200, 0));
            }
            plan_temp.SetPrescription(1, new DoseValue(100, DoseValue.DoseUnit.cGy), 1);
            plan_temp.CalculateDose();

            _app.SaveModifications();
            AddCourses(patient);
            SelectedCourse = course_temp.Id;
            SelectedPlan   = plan_temp.Id;
        }
Exemplo n.º 2
0
        /// <summary>
        /// Create PTV from CTV and fit collimator jaws to the target.
        /// </summary>
        public static void GenerateBeamGeometry(ExternalPlanSetup plan, double dosePerFraction, int numberOfFractions, double ptvMargin, string ctvId)
        {
            // Prescription
            const double prescribedPercentage = 1.0; // Note: 100% corresponds to 1.0
            var          dose = new DoseValue(dosePerFraction, DoseValue.DoseUnit.Gy);

            plan.UniqueFractionation.SetPrescription(numberOfFractions, dose, prescribedPercentage);

            Trace.WriteLine("\nCreating PTV from CTV...");
            CreatePTVFromCTV(plan, ptvMargin, ctvId);

            // Match plan structures to model structures
            var structureMatches = GetStructureMatches(plan);

            if (!structureMatches.Any())
            {
                MessageBox.Show("Structure match could not be found", "No model structures found", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            // Generate PTV that does not overlap with OARs.
            // Identify the structures to be spared by their IDs in the RapidPlan model.
            Trace.WriteLine("Subtracting OARs from PTV...");
            var sparedOrgans = new List <string> {
                "Rectum"
            };

            structureMatches = SubtractOARsFromPTV(plan, structureMatches, sparedOrgans);

            var ptvId = structureMatches.Single(x => x.Value.StructureType == ModelStructureType.Target).Key;
            var ptv   = plan.StructureSet.Structures.Single(x => x.Id == ptvId);

            Trace.WriteLine("PTV successfully generated.\n");

            // Fit jaws to target and add treatment fields.
            const double collRtn      = 0.0;
            var          jawPositions = GantryAngles.ToDictionary(angle => angle, angle => FitJawsToTarget(plan, ptv, angle, collRtn, MarginForJawFittingInMM));
            var          isocenter    = ptv.CenterPoint;

            foreach (var item in jawPositions)
            {
                plan.AddStaticBeam(MachineParameters, item.Value, CollimatorAngle, item.Key, PatientSupportAngle, isocenter);
            }

            Trace.WriteLine("\nJaws successfully fitted to target.\n");
        }
Exemplo n.º 3
0
        public void Execute(ScriptContext context /*, System.Windows.Window window, ScriptEnvironment environment*/)
        {
            //USER INPUT
            double s_ssd = 100;                                         // SSD
            string cmach = "HESN5";                                     // MACHINE

            string[] erg       = new string[] { "6X", "10X" };          // ENERGY LIST
            double[] jaw_sizes = new double[] { 4, 8, 10, 20 };         // FIELD SIZE LIST

            // Define local _patient, could be pointed to UI
            Patient _patient = context.Patient;

            _patient.BeginModifications();

            // SET STRUCTURE SET
            StructureSet _structureSet = _patient.StructureSets.FirstOrDefault();

            // DETERMINE EXTERNAL STRUCTURE
            var bst     = _structureSet.Structures.First(x => x.DicomType == "EXTERNAL");
            var bst_mgb = bst.MeshGeometry.Bounds;

            // CREATE NEW COURSE AND NAME IT
            Course C_Auto = _patient.AddCourse();

            C_Auto.Id = string.Format("C_{0}_{1}SSD", cmach, s_ssd.ToString());

            // LOOP THROUGH ENERGIES PROVIDED BY USER, CREATE A PLAN FOR EACH ENERGY
            foreach (var evar in erg)
            {
                // CREATE PLAN
                ExternalPlanSetup eps = C_Auto.AddExternalPlanSetup(_structureSet);

                // SET PRESCRIPTION
                eps.SetPrescription(1, new DoseValue(100, "cGy"), 1);
                eps.Id = evar.ToString();

                // SET FIELD PARAMETERS
                ExternalBeamMachineParameters ebmp = new ExternalBeamMachineParameters(cmach, evar, 600, "STATIC", null);

                // LOOP THROUGH FIELD SIZES, CREATE A BEAM FOR EACH FIELD SIZE
                foreach (double js in jaw_sizes)
                {
                    // SET JAW POSITION BASED ON USER FIELD SIZES - (ASSUMES ALL FIELDS ARE SYMMETRIC)
                    double xjaw = js * 10;
                    double yjaw = js * 10;

                    // DEFINE BEAM PARAMETERS - (ASSUMES NO ANGLES)
                    double coll    = 0;
                    double gant_a  = 0;
                    double couch_a = 0;

                    // SET ISOCENTER BASED ON USER SSD
                    double  iso_x  = 0;
                    double  iso_y  = -10 * (Math.Round(bst_mgb.SizeY / 10) + Math.Round(bst_mgb.Location.Y / 10) + (s_ssd - 100));
                    double  iso_z  = 0;
                    VVector isovec = new VVector(iso_x, iso_y, iso_z);

                    // CREATE BEAM
                    Beam b = eps.AddStaticBeam(
                        ebmp,
                        new VRect <double>(-0.5 * xjaw, -0.5 * yjaw, 0.5 * xjaw, 0.5 * yjaw),
                        coll, gant_a, couch_a, isovec);

                    // SET BEAM ID
                    b.Id = String.Format("{0}_{1:F1}x{2:F1}", evar, xjaw / 10, yjaw / 10);
                }
                // CALCULATE DOSE
                eps.CalculateDose();
            }
            MessageBox.Show("DONE");
        }
Exemplo n.º 4
0
        public void Execute(ScriptContext context /*, System.Windows.Window window*/)
        {
            Patient patient = context.Patient;

            if (patient == null)
            {
                throw new ApplicationException("Please load a patient.");
            }

            // Get active structureset
            StructureSet ss = context.StructureSet;

            if (ss == null)
            {
                throw new ApplicationException("Please load a structure set.");
            }

            // Check that unique body structure, PTV and Rectum structures exist
            Structure body   = ss.Structures.Where(o => o.Id == "BODY").FirstOrDefault();
            Structure ptv    = ss.Structures.Where(o => o.Id == "PTV").FirstOrDefault();
            Structure rectum = ss.Structures.Where(o => o.Id == "Rectum1").FirstOrDefault();

            if (body == null || ptv == null || rectum == null)
            {
                throw new ApplicationException(string.Format("Cannot find required structures (BODY, PTV, Rectum) from Structureset '{0}'", ss.Id));
            }

            // Enable modifications
            patient.BeginModifications();

            // Get or create course with Id 'Superplan'
            const string courseId = "Superplan";
            Course       course   = patient.Courses.Where(o => o.Id == courseId).SingleOrDefault();

            if (course == null)
            {
                course    = patient.AddCourse();
                course.Id = courseId;
            }

            // Create a new plan
            ExternalPlanSetup plan         = course.AddExternalPlanSetup(ss);
            int       fractions            = 20;
            double    prescribedPercentage = 1.0;
            DoseValue fractiondose         = new DoseValue(2.50, DoseValue.DoseUnit.Gy); // TODO: if needed change to cGy to match your system configuration

            plan.UniqueFractionation.SetPrescription(fractions, fractiondose, prescribedPercentage);

            // Add fields
            const int nfields = 5;
            ExternalBeamMachineParameters parameters = new ExternalBeamMachineParameters("Varian 23EX", "6X", 600, "STATIC", null); // TODO: change machine id to yours
            VVector isocenter = ptv.CenterPoint;

            for (int n = 0; n < nfields; n++)
            { // add a 10 cm x 10 cm field
                Beam beam = plan.AddStaticBeam(parameters, new VRect <double>(-50, -50, 50, 50), 0, Math.Round(360.0 / nfields * n, 0), 0, isocenter);
            }
            // end of first part.
            MessageBox.Show("Plan created, open course SuperPlan, Choose Plan1 to view results.", "Varian Developer");

            // Set optimization constraints
            OptimizationSetup optimizationSetup = plan.OptimizationSetup;

            optimizationSetup.AddPointObjective(ptv, OptimizationObjectiveOperator.Lower, new DoseValue(49.50, DoseValue.DoseUnit.Gy), 100, 100.0);
            optimizationSetup.AddPointObjective(ptv, OptimizationObjectiveOperator.Upper, new DoseValue(52.00, DoseValue.DoseUnit.Gy), 0, 100.0);
            optimizationSetup.AddPointObjective(rectum, OptimizationObjectiveOperator.Upper, new DoseValue(20.00, DoseValue.DoseUnit.Gy), 40.0, 100.0);
            MessageBox.Show("Plan '" + plan.Id + "' in course '" + course.Id + "' for patient '" + patient.Id + "' has been created");

            // optimize for 30 iterations
            if (!plan.Optimize(30).Success)
            {
                MessageBox.Show("Optimization failed", "Varian Developer", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            // run LMC
            if (!plan.CalculateLeafMotions().Success)
            {
                MessageBox.Show("LMC failed", "Varian Developer", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            // calculate final dose
            CalculationResult doseCalc = plan.CalculateDose();

            if (!doseCalc.Success)
            {
                MessageBox.Show("Dose calculation failed, logs shown next.", "Varian Developer", MessageBoxButton.OK, MessageBoxImage.Error);
                // write calculate logs to a file and show them to the user.
                string filename = writeCalculationLogs(plan);
                // 'Start' generated TXT file to launch Notepad window
                System.Diagnostics.Process.Start(filename);
                // Sleep for a few seconds to let Excel window start
                System.Threading.Thread.Sleep(TimeSpan.FromSeconds(3));
                return;
            }

            MessageBox.Show("Done");
        }