Exemplo n.º 1
0
        static void Execute(Application app, string inputfile, string outputfile)
        {
            CreatePlan   cPln = new CreatePlan();
            StreamReader readStream;
            StreamWriter writeStream;

            string[]          inputVals;
            List <PlnInfoSim> arcPlnLs = new List <PlnInfoSim>();
            string            pt_Ref;
            string            crs_Ref;
            string            pln_Ref;
            string            ds_Ref;
            PlanParameters    plnParam_Ref;
            string            pt_Tgt;
            string            crs_Tgt;
            string            pln_Tgt;
            string            struct_Tgt;
            PatientSummary    ptSumm;
            Patient           pt;
            bool cpSucc;
            bool psSucc;

            ////First, create list of patient plans already in the archive////
            readStream = new StreamReader(outputfile);
            //Skip title row//
            readStream.ReadLine();
            while (!readStream.EndOfStream)
            {
                PlnInfoSim plnInfo = new PlnInfoSim();
                inputVals     = readStream.ReadLine().Split(',');
                plnInfo.ptId  = inputVals[0];
                plnInfo.crsId = inputVals[1];
                plnInfo.plnId = inputVals[2];
                arcPlnLs.Add(plnInfo);
            }
            readStream.Close();
            ////Copy plans from the database file, and paste to the archived patients////
            readStream = new StreamReader(inputfile);
            //Skip title row//
            readStream.ReadLine();
            writeStream = new StreamWriter(outputfile, append: true);
            while (!readStream.EndOfStream)
            {
                ////Read input plan from csv file////
                inputVals = readStream.ReadLine().Split(',');
                pt_Ref    = inputVals[0];
                crs_Ref   = inputVals[1];
                pln_Ref   = inputVals[2];
                ds_Ref    = inputVals[3];
                msg       = "-Reference:\n  Patient: " + pt_Ref + ", course: " + crs_Ref + ", plan: " + pln_Ref;
                Console.WriteLine(msg);
                //Compare input plan with archived list//
                if (arcPlnLs.Exists(p => p.ptId == pt_Ref && p.crsId == crs_Ref && p.plnId == pln_Ref))
                {
                    Console.WriteLine("--Plan already exists in archive, skipping...\n");
                }
                else
                {
                    ////Copy reference plan parameters////
                    plnParam_Ref = new PlanParameters();
                    cpSucc       = plnParam_Ref.extractParametersFromPlan(app, pt_Ref, crs_Ref, pln_Ref);
                    if (cpSucc)
                    {
                        pt_Tgt = "AOA0002";
                        //Check which course to put plan into//
                        ptSumm  = app.PatientSummaries.FirstOrDefault(ps => ps.Id == pt_Tgt);
                        pt      = app.OpenPatient(ptSumm);
                        crs_Tgt = null;
                        foreach (Course crs in pt.Courses)
                        {
                            //Add at most 10 plans to a course//
                            if (crs.PlanSetups.Count() < 2)
                            {
                                crs_Tgt = crs.Id;
                                break;
                            }
                        }
                        if (crs_Tgt == null)
                        {
                            crs_Tgt = "C" + pt.Courses.Count().ToString();
                        }
                        app.ClosePatient();
                        //Create plan name//
                        pln_Tgt    = pt_Ref + crs_Ref.Substring(0, Math.Min(3, crs_Ref.Length)) + pln_Ref.Substring(0, Math.Min(3, pln_Ref.Length));
                        struct_Tgt = "AC_27cm";
                        msg        = "-Copy plan to:\n  Patient: " + pt_Tgt + ", course: " + crs_Tgt + ", plan: " + pln_Tgt;
                        Console.WriteLine(msg);
                        ////Copy and create new plan to archived patient////
                        psSucc = cPln.CopyAndCreate(app, pt_Tgt, crs_Tgt, pln_Tgt, struct_Tgt, plnParam_Ref);
                        if (psSucc)
                        {
                            msg = pt_Ref + "," + crs_Ref + "," + pln_Ref + "," + pt_Tgt + "," + crs_Tgt + "," + pln_Tgt + "," + ds_Ref;
                            writeStream.WriteLine(msg);
                        }
                    }
                }
            }
            //Close the file stream//
            readStream.Close();
            writeStream.Close();
        }
Exemplo n.º 2
0
        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);
            }
        }