public SaveObjectiveFunctionsViewModel(string objectiveFunctionsJson, string defaultDirectoryPath) { DefaultDirectoryPath = defaultDirectoryPath; var objectiveFunctionsJObject = JObject.Parse(objectiveFunctionsJson); var objectiveFunctionArguments = (JArray)objectiveFunctionsJObject["Arguments"]; ObjectiveFunctions.Clear(); foreach (var a in objectiveFunctionArguments) { var jObject = JObject.Parse(a.ToString()); ObjectiveFunctions.Add(new Models.ObjectiveFunction(jObject)); } var prescriptionsJArray = (JArray)objectiveFunctionsJObject["Prescriptions"]; var PrescriptionsAll = prescriptionsJArray.ToObject <List <Models.Prescription> >(); Prescriptions.Clear(); foreach (var p in PrescriptionsAll) { if (ObjectiveFunctions.Where(o => (o.PlanLabel == p.PlanLabel)).Count() > 0) { Prescriptions.Add(p); } } OkCommand = new DelegateCommand(() => { CanExecute = true; }); CancelCommand = new DelegateCommand(() => { CanExecute = false; }); SaveFileCommand = new DelegateCommand(SaveFile); }
private void ChooseFile() { var dialog = new CommonOpenFileDialog("Choose File"); if (Directory.Exists(DefaultDirectoryPath)) { dialog.InitialDirectory = DefaultDirectoryPath; } dialog.IsFolderPicker = false; if (dialog.ShowDialog() == CommonFileDialogResult.Ok) { FilePath = dialog.FileName; using (var sr = new StreamReader(FilePath)) using (JsonTextReader reader = new JsonTextReader(sr)) { ObjectiveFunctionsJObject = (JObject)JToken.ReadFrom(reader); } var prescriptionsJArray = (JArray)ObjectiveFunctionsJObject["Prescriptions"]; Prescriptions = prescriptionsJArray.ToObject <ObservableCollection <Models.Prescription> >(); var descriptionJObject = ObjectiveFunctionsJObject["Description"]; if (descriptionJObject != null) { Description = descriptionJObject.ToObject <string>(); } else { Description = string.Empty; } var argumentsJArray = (JArray)ObjectiveFunctionsJObject["Arguments"]; ObjectiveFunctions.Clear(); foreach (var a in argumentsJArray) { var jObject = JObject.Parse(a.ToString()); ObjectiveFunctions.Add(new Models.ObjectiveFunction(jObject)); string roiName = jObject["RoiName"].ToObject <string>(); if (!RoiNamesInObjectiveFunctions.Contains(roiName)) { RoiNamesInObjectiveFunctions.Add(roiName); } string planLabel = jObject["PlanLabel"].ToObject <string>(); if (!(planLabel == PlanLabelCombinedDose) && !PlanLabelsInObjectiveFuntions.Contains(planLabel)) { PlanLabelsInObjectiveFuntions.Add(planLabel); } } RoiNamesInObjectiveFunctions.OrderBy(r => r); foreach (var r in Rois) { if (RoiNamesInObjectiveFunctions.Contains(r.Name)) { r.InUse = true; r.NameInObjectiveFunction = r.Name; } } foreach (var p in PlanLabels) { if (p.InUse && PlanLabelsInObjectiveFuntions.Contains(p.Label)) { p.LabelInObjectiveFunction = p.Label; } } var planSumDose = PlanLabels.Select(p => p.PrescribedDose).Sum(); foreach (var p in Prescriptions) { var query = PlanLabels.Where(pl => pl.Label == p.PlanLabel); if (query.Count() > 0) { p.PrescribedDose = query.First().PrescribedDose; continue; } if (p.PlanLabel == PlanLabelCombinedDose) { p.PrescribedDose = planSumDose; } } } else { Message = "\"Choose File\" is canceled"; } }