Пример #1
0
        private bool removeFlashStr()
        {
            //remove the structures used to generate flash in the plan
            StructureSet ss = vmatPlan.StructureSet;
            //check to see if this structure set is used in any other calculated plans that are NOT the _VMAT TBI plan or any of the AP/PA legs plans
            string message = "";
            List <ExternalPlanSetup> otherPlans = new List <ExternalPlanSetup> {
            };

            foreach (Course c in vmatPlan.Course.Patient.Courses)
            {
                foreach (ExternalPlanSetup p in c.ExternalPlanSetups)
                {
                    if ((p != vmatPlan && !appaPlan.Where(x => x == p).Any()) && p.IsDoseValid && p.StructureSet == ss)
                    {
                        message += String.Format("Course: {0}, Plan: {1}", c.Id, p.Id) + System.Environment.NewLine;
                        otherPlans.Add(p);
                    }
                }
            }
            //photon dose calculation model type
            string calcModel = vmatPlan.GetCalculationModel(CalculationType.PhotonVolumeDose);

            if (otherPlans.Count > 0)
            {
                //if some plans were found that use this structure set and have dose calculated, inform the user and ask if they want to continue WITHOUT removing flash.
                message  = "The following plans have dose calculated and use the same structure set:" + System.Environment.NewLine + message + System.Environment.NewLine;
                message += "I need to remove the calculated dose from these plans before removing the flash structures." + System.Environment.NewLine;
                message += "Continue?";
                confirmUI CUI = new VMATTBIautoPlan.confirmUI();
                CUI.message.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                CUI.message.Text = message;
                CUI.ShowDialog();
                //need to return from this function regardless of what the user decides
                if (!CUI.confirm)
                {
                    return(true);
                }
                foreach (ExternalPlanSetup p in otherPlans)
                {
                    p.ClearCalculationModel(CalculationType.PhotonVolumeDose);
                    p.SetCalculationModel(CalculationType.PhotonVolumeDose, calcModel);
                }
            }
            //dumbass way around the issue of modifying structures in a plan that already has dose calculated... reset the calculation model, make the changes you need, then reset the calculation model
            vmatPlan.ClearCalculationModel(CalculationType.PhotonVolumeDose);
            vmatPlan.SetCalculationModel(CalculationType.PhotonVolumeDose, calcModel);
            foreach (ExternalPlanSetup p in appaPlan)
            {
                p.ClearCalculationModel(CalculationType.PhotonVolumeDose);
                p.SetCalculationModel(CalculationType.PhotonVolumeDose, calcModel);
            }
            IEnumerable <Structure> flashStr = ss.Structures.Where(x => x.Id.ToLower().Contains("flash"));
            List <Structure>        removeMe = new List <Structure> {
            };

            //can't remove directly from flashStr because the vector size would change on each loop iteration
            foreach (Structure s in flashStr)
            {
                if (!s.IsEmpty)
                {
                    if (ss.CanRemoveStructure(s))
                    {
                        removeMe.Add(s);
                    }
                }
            }
            foreach (Structure s in removeMe)
            {
                ss.RemoveStructure(s);
            }

            //from the generateTS class, the human_body structure was a copy of the body structure BEFORE flash was added. Therefore, if this structure still exists, we can just copy it back onto the body
            Structure bodyCopy = ss.Structures.FirstOrDefault(x => x.Id.ToLower() == "human_body");

            if (bodyCopy != null && !bodyCopy.IsEmpty)
            {
                Structure body = ss.Structures.First(x => x.Id.ToLower() == "body");
                body.SegmentVolume = bodyCopy.Margin(0.0);
                if (ss.CanRemoveStructure(bodyCopy))
                {
                    ss.RemoveStructure(bodyCopy);
                }
            }
            else
            {
                MessageBox.Show("WARNING 'HUMAN_BODY' STRUCTURE NOT FOUND! BE SURE TO RE-CONTOUR THE BODY STRUCTURE!");
            }
            flashRemoved = true;

            return(false);
        }
        /// <summary>
        /// Create verifications plans for a given treatment plan.
        /// </summary>
        public static ExternalPlanSetup CreateVerificationPlan(Course course, IEnumerable <Beam> beams, ExternalPlanSetup verifiedPlan, StructureSet verificationStructures,
                                                               string planId, bool calculateDose)
        {
            var verificationPlan = course.AddExternalPlanSetupAsVerificationPlan(verificationStructures, verifiedPlan);

            verificationPlan.Id = planId;

            // Put isocenter to the center of the body.
            var isocenter = verificationStructures.Structures.Single(st => st.Id.ToLower().StartsWith("body")).CenterPoint;

            // Copy the given beams to the verification plan and the meterset values.
            var getCollimatorAndGantryAngleFromBeam = beams.Count() > 1;
            var presetValues = (from beam in beams
                                let newBeamId = CopyBeam(beam, verificationPlan, isocenter, getCollimatorAndGantryAngleFromBeam)
                                                select new KeyValuePair <string, MetersetValue>(newBeamId, beam.Meterset)).ToList();

            // Set presciption
            const int numberOfFractions = 1;

            verificationPlan.SetPrescription(numberOfFractions, verifiedPlan.DosePerFraction, treatmentPercentage: 1.0);

            if (calculateDose)
            {
                verificationPlan.SetCalculationModel(CalculationType.PhotonVolumeDose, verifiedPlan.GetCalculationModel(CalculationType.PhotonVolumeDose));
//            Trace.WriteLine("\nCalculating dose for verification plan...\n");
                var res = verificationPlan.CalculateDoseWithPresetValues(presetValues);
                if (!res.Success)
                {
                    var message = string.Format("Dose calculation failed for verification plan. Output:\n{0}", res);
//                Trace.WriteLine(message);
                    throw new Exception(message);
                }
            }
            return(verificationPlan);
        }
Пример #3
0
        /// <summary>
        /// Create verifications plans for a given treatment plan.
        /// </summary>
        public static ExternalPlanSetup CreateVerificationPlan(Course course, IEnumerable <Beam> beams, ExternalPlanSetup verifiedPlan, StructureSet verificationStructures,
                                                               bool calculateDose)
        {
            var verificationPlan = course.AddExternalPlanSetupAsVerificationPlan(verificationStructures, verifiedPlan);

            try
            {
                verificationPlan.Id = verifiedPlan.Id;
            }
            catch (System.ArgumentException)
            {
                var message = "Plan already exists in QA course.";
                throw new Exception(message);
            }

            // Put isocenter to the center of the QAdevice
            VVector isocenter = verificationPlan.StructureSet.Image.UserOrigin;
            var     beamList  = verifiedPlan.Beams.ToList(); //used for looping later

            foreach (Beam beam in beams)
            {
                if (beam.IsSetupField)
                {
                    continue;
                }

                ExternalBeamMachineParameters MachineParameters =
                    new ExternalBeamMachineParameters(beam.TreatmentUnit.Id, beam.EnergyModeDisplayName, beam.DoseRate, beam.Technique.Id, string.Empty);

                if (beam.MLCPlanType.ToString() == "VMAT")
                {
                    // Create a new VMAT beam.
                    var collimatorAngle  = beam.ControlPoints.First().CollimatorAngle;
                    var gantryAngleStart = beam.ControlPoints.First().GantryAngle;
                    var gantryAngleEnd   = beam.ControlPoints.Last().GantryAngle;
                    var gantryDirection  = beam.GantryDirection;
                    var metersetWeights  = beam.ControlPoints.Select(cp => cp.MetersetWeight);

                    verificationPlan.AddVMATBeam(MachineParameters, metersetWeights, collimatorAngle, gantryAngleStart,
                                                 gantryAngleEnd, gantryDirection, 0.0, isocenter);
                    continue;
                }
                else
                {
                    if (beam.MLCPlanType.ToString() == "DoseDynamic")
                    {
                        // Create a new IMRT beam.
                        double gantryAngle;
                        double collimatorAngle;
                        if (beam.TreatmentUnit.Name == "Trilogy") //arccheck
                        {
                            gantryAngle     = beam.ControlPoints.First().GantryAngle;
                            collimatorAngle = beam.ControlPoints.First().CollimatorAngle;
                        }

                        else //ix with only mapcheck
                        {
                            gantryAngle     = 0.0;
                            collimatorAngle = 0.0;
                        }

                        var metersetWeights = beam.ControlPoints.Select(cp => cp.MetersetWeight);
                        verificationPlan.AddSlidingWindowBeam(MachineParameters, metersetWeights, collimatorAngle, gantryAngle,
                                                              0.0, isocenter);
                        continue;
                    }
                    else
                    {
                        var message = string.Format("Treatment field {0} is not VMAT or IMRT.", beam);
                        throw new Exception(message);
                    }
                }
            }

            int i = 0;

            foreach (Beam verificationBeam in verificationPlan.Beams)
            {
                verificationBeam.Id = beamList[i].Id;
                i++;
            }

            foreach (Beam verificationBeam in verificationPlan.Beams)
            {
                foreach (Beam beam in verifiedPlan.Beams)
                {
                    if (verificationBeam.Id == beam.Id)
                    {
                        var editableParams = beam.GetEditableParameters();
                        editableParams.Isocenter = verificationPlan.StructureSet.Image.UserOrigin;
                        verificationBeam.ApplyParameters(editableParams);
                        continue;
                    }
                }
            }
            // Set presciption
            const int numberOfFractions = 1;

            verificationPlan.SetPrescription(numberOfFractions, verifiedPlan.DosePerFraction, verifiedPlan.TreatmentPercentage);

            verificationPlan.SetCalculationModel(CalculationType.PhotonVolumeDose, verifiedPlan.GetCalculationModel(CalculationType.PhotonVolumeDose));

            CalculationResult res;

            if (verificationPlan.Beams.FirstOrDefault().MLCPlanType.ToString() == "DoseDynamic")
            {
                var getCollimatorAndGantryAngleFromBeam = beams.Count() > 1;
                var presetValues = (from beam in beams
                                    select new KeyValuePair <string, MetersetValue>(beam.Id, beam.Meterset)).ToList();
                res = verificationPlan.CalculateDoseWithPresetValues(presetValues);
            }
            else //vmat
            {
                res = verificationPlan.CalculateDose();
            }
            if (!res.Success)
            {
                var message = string.Format("Dose calculation failed for verification plan. Output:\n{0}", res);
                throw new Exception(message);
            }
            return(verificationPlan);
        }