public static void CopyBeamToPlan(Beam beam, ExternalPlanSetup plansetup, VVector isocenter) { string energyModeDisp = beam.EnergyModeDisplayName; Char[] sep = { '-' }; string energyMode = energyModeDisp.Split(sep).First(); string pfm = energyModeDisp.Split(sep).Count() > 1 ? energyModeDisp.Split(sep).Last() : null; ExternalBeamMachineParameters extParams = new ExternalBeamMachineParameters(beam.TreatmentUnit.Id, energyMode, beam.DoseRate, beam.Technique.Id, pfm); List <double> metersetWeights = GetControlPointWeights(beam); Beam copyBeam = null; if (beam.MLCPlanType == MLCPlanType.VMAT) { copyBeam = plansetup.AddVMATBeam(extParams, metersetWeights, beam.ControlPoints[0].CollimatorAngle, beam.ControlPoints[0].GantryAngle, beam.ControlPoints[beam.ControlPoints.Count - 1].GantryAngle, beam.GantryDirection, beam.ControlPoints[0].PatientSupportAngle, isocenter); } else if (beam.MLCPlanType == MLCPlanType.ArcDynamic) { copyBeam = plansetup.AddConformalArcBeam(extParams, beam.ControlPoints[0].CollimatorAngle, beam.ControlPoints.Count, beam.ControlPoints[0].GantryAngle, beam.ControlPoints[beam.ControlPoints.Count - 1].GantryAngle, beam.GantryDirection, beam.ControlPoints[0].PatientSupportAngle, isocenter); } else if (beam.MLCPlanType == MLCPlanType.DoseDynamic) { var cppPairs = metersetWeights.Zip(metersetWeights.Skip(1), (a, b) => new Tuple <double, double>(a, b)).ToList(); var oddCppPairs = cppPairs.Where((p, i) => i % 2 == 1); if (metersetWeights.Count >= 4 && metersetWeights.Count % 2 == 0 && oddCppPairs.All(p => p.Item1 == p.Item2)) { copyBeam = plansetup.AddMultipleStaticSegmentBeam(extParams, metersetWeights, beam.ControlPoints[0].CollimatorAngle, beam.ControlPoints[0].GantryAngle, beam.ControlPoints[0].PatientSupportAngle, isocenter); } else { copyBeam = plansetup.AddSlidingWindowBeam(extParams, metersetWeights, beam.ControlPoints[0].CollimatorAngle, beam.ControlPoints[0].GantryAngle, beam.ControlPoints[0].PatientSupportAngle, isocenter); } } else { throw new NotImplementedException("Copying this type of beam not implemented"); } var beamParams = copyBeam.GetEditableParameters(); CopyJawAndLeafPositions(beam, beamParams); beamParams.WeightFactor = beam.WeightFactor; copyBeam.ApplyParameters(beamParams); copyBeam.Id = beam.Id; }
// Add beam to the plan public Beam AddBeamToVerifPlan(Beam currBm) { if (verifPln == null) { return(null); } else { bmTech = getMLCBmTechnique(currBm); // find beam technique // Create machine parameters String energy = currBm.EnergyModeDisplayName; String fluence = null; Match EMode = Regex.Match(currBm.EnergyModeDisplayName, @"^([0-9]+[A-Z]+)-?([A-Z]+)?", RegexOptions.IgnoreCase); //format is... e.g. 6X(-FFF) if (EMode.Success) { if (EMode.Groups[2].Length > 0) // fluence mode { energy = EMode.Groups[1].Value; fluence = EMode.Groups[2].Value; } // else normal modes uses default in decleration } ExternalBeamMachineParameters machParam = new ExternalBeamMachineParameters(currBm.TreatmentUnit.Id.ToString(), energy, currBm.DoseRate, currBm.Technique.Id.ToString(), fluence); // Define collimator, gantry and couch angles // Double gantryAng = currBm.ControlPoints.First().GantryAngle; Double collAng = currBm.ControlPoints.First().CollimatorAngle; Double couchAng = 0.0; // MU values for each control point // IEnumerable <double> muSet = currBm.ControlPoints.Select(cp => cp.MetersetWeight).ToList(); // Add beam MU to the list of MU values // muValues.Add(new KeyValuePair <string, MetersetValue>(currBm.Id, currBm.Meterset)); // Start adding beam based on beam technique // if (bmTech == "StaticMLC") { Beam verifBm = verifPln.AddMLCBeam(machParam, new float[2, 60], new VRect <double>(-10.0, -10.0, 10.0, 10.0), collAng, gantryAng, couchAng, verifPlnIso); verifBm.Id = currBm.Id; BeamParameters ctrPtParam = copyControlPoints(currBm, verifBm); verifBm.ApplyParameters(ctrPtParam); return(verifBm); } else if (bmTech == "StaticSegWin") { Beam verifBm = verifPln.AddMultipleStaticSegmentBeam(machParam, muSet, collAng, gantryAng, couchAng, verifPlnIso); verifBm.Id = currBm.Id; BeamParameters ctrPtParam = copyControlPoints(currBm, verifBm); verifBm.ApplyParameters(ctrPtParam); return(verifBm); } else if (bmTech == "StaticSlidingWin") { Beam verifBm = verifPln.AddSlidingWindowBeam(machParam, muSet, collAng, gantryAng, couchAng, verifPlnIso); verifBm.Id = currBm.Id; BeamParameters ctrPtParam = copyControlPoints(currBm, verifBm); verifBm.ApplyParameters(ctrPtParam); return(verifBm); } else if (bmTech == "ConformalArc") { Beam verifBm = verifPln.AddConformalArcBeam(machParam, collAng, currBm.ControlPoints.Count(), currBm.ControlPoints.First().GantryAngle, currBm.ControlPoints.Last().GantryAngle, currBm.GantryDirection, couchAng, verifPlnIso); verifBm.Id = currBm.Id; BeamParameters ctrPtParam = copyControlPoints(currBm, verifBm); verifBm.ApplyParameters(ctrPtParam); return(verifBm); } else if (bmTech == "VMAT") { Beam verifBm = verifPln.AddVMATBeam(machParam, muSet, collAng, currBm.ControlPoints.First().GantryAngle, currBm.ControlPoints.Last().GantryAngle, currBm.GantryDirection, couchAng, verifPlnIso); verifBm.Id = currBm.Id; BeamParameters ctrPtParam = copyControlPoints(currBm, verifBm); verifBm.ApplyParameters(ctrPtParam); return(verifBm); } else // null { return(null); } } }
public bool CopyAndCreate(Application app, string ptId_Tgt, string crsId_Tgt, string plnId_Tgt, string structId_Tgt, PlanParameters plnParam_Ref) { //Open patient// PatientSummary ptSumm = app.PatientSummaries.FirstOrDefault(ps => ps.Id == ptId_Tgt); if (ptSumm == null) { Console.WriteLine("--Cannot find patient" + ptId_Tgt + "."); return(false); } Patient pt = app.OpenPatient(ptSumm); try { pt.BeginModifications(); //Open or create course// Course crs = pt.Courses.FirstOrDefault(c => c.Id == crsId_Tgt); if (crs == null) { Console.WriteLine("-Create course " + crsId_Tgt + "."); crs = pt.AddCourse(); crs.Id = crsId_Tgt; } //Create plan// ExternalPlanSetup pln = crs.ExternalPlanSetups.FirstOrDefault(p => p.Id == plnId_Tgt); if (pln == null) { StructureSet structSet = pt.StructureSets.FirstOrDefault(ss => ss.Id == structId_Tgt); if (structSet == null) { Console.WriteLine("--Cannot find structure set " + structId_Tgt + ". Plan is not created\n"); app.ClosePatient(); return(false); } pln = crs.AddExternalPlanSetup(structSet); pln.Id = plnId_Tgt; } //Return if there is already a plan with the same name// else { Console.WriteLine("--A plan with name " + plnId_Tgt + " already exists. Plan is not created\n"); app.ClosePatient(); return(false); } Console.WriteLine("-Start creating plan " + plnId_Tgt + "."); //Set plan prescription properties// pln.SetPrescription(plnParam_Ref.N_Fx, plnParam_Ref.DoseperFx, plnParam_Ref.TrtPct); ///////////Create beam by copying from the beams in reference plan parameters//////////// //Create empty list of MU values for each beam// List <KeyValuePair <string, MetersetValue> > muValues = new List <KeyValuePair <string, MetersetValue> >(); foreach (PlanParameters.BmParam bmParam in plnParam_Ref.BmParamLs) { //Add beam, type based on reference MLC beam technique// IEnumerable <double> muSet = bmParam.CtrPtParam.ControlPoints.Select(cp => cp.MetersetWeight).ToList(); switch (bmParam.MLCBmTechnique) { case "StaticMLC": Beam bm = pln.AddMLCBeam(bmParam.MachParam, new float[2, 60], new VRect <double>(-10.0, -10.0, 10.0, 10.0), 0.0, 0.0, 0.0, bmParam.CtrPtParam.Isocenter); bm.Id = bmParam.bmId; bm.ApplyParameters(bmParam.CtrPtParam); break; case "StaticSegWin": bm = pln.AddMultipleStaticSegmentBeam(bmParam.MachParam, muSet, 0.0, 0.0, 0.0, bmParam.CtrPtParam.Isocenter); bm.Id = bmParam.bmId; muValues.Add(new KeyValuePair <string, MetersetValue>(bmParam.bmId, bmParam.mu)); bm.ApplyParameters(bmParam.CtrPtParam); break; case "StaticSlidingWin": bm = pln.AddSlidingWindowBeam(bmParam.MachParam, muSet, 0.0, 0.0, 0.0, bmParam.CtrPtParam.Isocenter); bm.Id = bmParam.bmId; muValues.Add(new KeyValuePair <string, MetersetValue>(bmParam.bmId, bmParam.mu)); bm.ApplyParameters(bmParam.CtrPtParam); break; case "ConformalArc": bm = pln.AddConformalArcBeam(bmParam.MachParam, 0.0, bmParam.CtrPtParam.ControlPoints.Count(), bmParam.CtrPtParam.ControlPoints.First().GantryAngle, bmParam.CtrPtParam.ControlPoints.Last().GantryAngle, bmParam.CtrPtParam.GantryDirection, 0.0, bmParam.CtrPtParam.Isocenter); bm.Id = bmParam.bmId; bm.ApplyParameters(bmParam.CtrPtParam); break; case "VMAT": bm = pln.AddVMATBeam(bmParam.MachParam, muSet, 0.0, bmParam.CtrPtParam.ControlPoints.First().GantryAngle, bmParam.CtrPtParam.ControlPoints.Last().GantryAngle, bmParam.CtrPtParam.GantryDirection, 0.0, bmParam.CtrPtParam.Isocenter); bm.Id = bmParam.bmId; bm.ApplyParameters(bmParam.CtrPtParam); break; default: Console.WriteLine("--At least one of the beams is unidentified, plan is not created.\n"); app.ClosePatient(); return(false); } } //Set the plan normalization value// pln.PlanNormalizationValue = plnParam_Ref.PlnNormFactr; //Set the plan calculation model// pln.SetCalculationModel(CalculationType.PhotonVolumeDose, plnParam_Ref.CalcModel); //If one of the beams is static IMRT, compute dose to enforce MUs to beams// if (plnParam_Ref.BmParamLs.Any(bm => bm.MLCBmTechnique == "StaticSegWin" || bm.MLCBmTechnique == "StaticSlidingWin")) { Console.WriteLine("--Start computing static beam IMRT plan."); pln.CalculateDoseWithPresetValues(muValues); } Console.WriteLine("-Finish plan creation.\n"); app.SaveModifications(); app.ClosePatient(); return(true); } catch (Exception ex) { Console.WriteLine(ex.ToString()); app.ClosePatient(); return(false); } }