// Validate the context parameter to make sure patient and plan are loaded private PlanResult ValidateContext() { PlanResult planResult = new PlanResult(); // Validate input // If a plansum is loaded take only a single plansum, otherwise take the active plansetup. // A plan is loaded if (ContextPlanSetup != null) { planResult = AddPlan(ContextPlanSetup); } else { // A plansum is loaded if (ContextPlanSumsInScope.Count() == 1) { planResult = AddPlan(ContextPlanSumsInScope.Single()); } // No plans or plansums are loaded else if (ContextPlanSumsInScope.Count() == 0) { throw new ApplicationException("Please load a plan or plansum."); } // Too many plansums are loaded. Eclipse cannot determine which plansum is in scope if there is more than one. else if (ContextPlanSumsInScope.Count() > 1) { throw new ApplicationException("Please close all but one plan sum."); } } return(planResult); }
// Main Program // Reads in the CSV file and saves as a list of StructureObjective objects // For each plan, creates a PlanResults object public void InitializeViewModel() { // Read in the objectives. The same objectives are used for all plans CreateObjectives(); // Add plans to check // Determine whether this is a standard DVHEvaluator ("singlePlan"), Plan Comparison tool ("planComparison"), or debugging ("debug") situation switch (Mode) { case "singlePlan": // Validate context and add it to plan. PlanResult contextPlanResult = ValidateContext(); Results.Add(contextPlanResult); break; case "planComparison": // Open GUI to allow user to choose plans PlanChooserViewModel planChooserViewModel = new PlanChooserViewModel(this); if (planChooserViewModel.ChosenPlans.Any()) { planChooserViewModel.ChosenPlans.ForEach(x => Results.Add(AddPlan(x))); } // If no plan, exit. Error-handling is dealth with in the GUI. else { return; } break; case "debug": // Load all plans in scope ContextPlanSetupsInScope.ToList().ForEach(x => Results.Add(AddPlan(x))); ContextPlanSumsInScope.ToList().ForEach(x => Results.Add(AddPlan(x))); break; default: throw new ApplicationException(string.Format("Chosen mode '{0}' does not exist.", Mode)); } // Reorder results by given convention ReOrderResults(); // Compute results for all plans Results.ForEach(x => x.ComputeResults(Objectives)); // Find all unique warnings in the plan and assign codes to them. List <string> warn = (from res in Results from s in res.StructureObjectiveResults from w in s.Warnings select w).ToList().Distinct().ToList(); foreach (int i in Enumerable.Range(1, warn.Count)) { WarningDictionary.Add(warn[i - 1], i.ToString()); } // Check plan ID for invalid filename characters and delete if present string regexSearch = new string(System.IO.Path.GetInvalidFileNameChars()) + new string(System.IO.Path.GetInvalidPathChars()); Regex r = new Regex(string.Format("[{0}]", Regex.Escape(regexSearch))); string strPlanId = r.Replace(Results.First().PlanningItem.Id.ToString(), "_"); string strPatientId = r.Replace(Results.First().Patient.Id.ToString(), "_"); string outputpath = System.IO.Path.Combine(WORKBOOK_RESULT_DIR, strPatientId + "-" + strPlanId + "-" + DateTime.Now.ToString("yyyy-MM-dd-hh-mm-ss") + ".html"); // Print to HTML and open it DataOut = UpdateWorkbook(CSVSheet, Results); DataTable table = ConvertListToDataTable(DataOut, CSVHeader); string HtmlBody = ExportToHtml(table); System.IO.File.WriteAllText(outputpath, HtmlBody); System.Diagnostics.Process.Start(outputpath); }
// Validate a PlanSetup or PlanSum and return a PlanResult item. private PlanResult AddPlan(PlanningItem planningItem) { Course course; StructureSet ss = planningItem.StructureSet; // Determine plan type as best as we can (Initial, CD, PlanSum, Other) string planType; if (planningItem is PlanSum) { planType = "PlanSum"; course = (planningItem as PlanSum).Course; } else { if (planningItem.Id.ToUpper().Contains("INI")) { planType = "Initial"; } else if (planningItem.Id.ToUpper().Contains("CD")) { planType = "CD"; } else { planType = "Other"; } course = (planningItem as PlanSetup).Course; } Patient patient = course.Patient; // Validate the Plan if (planningItem == null) { throw new ApplicationException(string.Format("Plan or plansum {0}:{1} unable to be loaded: plan is invalid", course.Id, planningItem.Id)); } if (patient == null) { throw new ApplicationException(string.Format("Plan or plansum {0}:{1} unable to be loaded: associated patient is invalid", course.Id, planningItem.Id)); } if (ss == null) { throw new ApplicationException(string.Format("Plan or plansum {0}:{1} unable to be loaded: associated structure set is invalid", course.Id, planningItem.Id)); } if (!planningItem.IsDoseValid()) { throw new ApplicationException(string.Format("Please calculate dose for plan or plansum {0}:{1} before running this script.", course.Id, planningItem.Id)); } Dictionary <string, Structure> ssDict; // Find structures for structure set if (StructureDictionary.Keys.Contains(ss)) { ssDict = StructureDictionary[ss]; } else { ssDict = AddStructureSetToDictionary(ss); StructureDictionary.Add(ss, ssDict); } // return Plan Result PlanResult planResult = (new PlanResult() { Patient = patient, PlanningItem = planningItem, StructureSet = ss, StructureSetDict = ssDict, PlanName = planningItem.Id, PlanType = planType, CourseName = course.Id }); return(planResult); }