public void SerializeProblem(IModelProblem modelProblem, string filePath) { if (modelProblem is null) { throw new ArgumentNullException(nameof(modelProblem)); } if (string.IsNullOrEmpty(filePath)) { throw new ArgumentException(nameof(filePath)); } var directoryPath = Path.GetDirectoryName(filePath); if (!_fileSystem.DirectoryExists(directoryPath)) { _fileSystem.CreateDirectory(directoryPath); } if (_fileSystem.FileExists(filePath)) { _fileSystem.FileDelete(filePath); } var memento = BuildMemento(modelProblem); var json = JsonConvert.SerializeObject(memento); _fileSystem.FileWrite(filePath, json); }
public ProblemViewModel(IModelProblem modelProblem, IProblemSerializer problemSerializer, IDialogService dialogService) { Nodes = new ObservableCollection <NodeViewModel>(); Elements = new ObservableCollection <ElementViewModel>(); Forces = new ObservableCollection <ForceViewModel>(); Materials = new ObservableCollection <MaterialViewModel>(); _dialogService = dialogService; _problemSerializer = problemSerializer; LoadProblem(modelProblem); Nodes.CollectionChanged += CollectionChanged; Elements.CollectionChanged += CollectionChanged; Forces.CollectionChanged += CollectionChanged; Materials.CollectionChanged += CollectionChanged; ProblemCalculationViewModel = new ProblemCalculationViewModel(this); SaveProblemCommand = new Command(SaveProblem); LoadProblemCommand = new Command(LoadProblem); NewProblemCommand = new Command(NewProblem); }
private static ProblemMemento BuildMemento(IModelProblem modelProblem) { var result = new ProblemMemento() { Name = modelProblem.Name }; foreach (var modelNode in modelProblem.Nodes) { var nodeMemento = new NodeMemento() { Id = modelNode.Id, IsXFixed = modelNode.IsXFixed, IsYFixed = modelNode.IsYFixed, IsZFixed = modelNode.IsZFixed, X = modelNode.Coordinates.X, Y = modelNode.Coordinates.Y, Z = modelNode.Coordinates.Z }; result.Nodes.Add(nodeMemento); } foreach (var modelForce in modelProblem.Forces) { var forceMemento = new ForceMemento() { Id = modelForce.Id, ApplicationVectorX = modelForce.ApplicationVector.X, ApplicationVectorY = modelForce.ApplicationVector.Y, ApplicationVectorZ = modelForce.ApplicationVector.Z, NodeId = modelForce.Node.Id }; result.Forces.Add(forceMemento); } foreach (var modelMaterial in modelProblem.Materials) { var materialMemento = new MaterialMemento { Id = modelMaterial.Id, Name = modelMaterial.Name, YoungsModulus = modelMaterial.YoungsModulus }; result.Materials.Add(materialMemento); } foreach (var modelBarElement in modelProblem.Elements) { var barElementMemento = new BarElementMemento { Id = modelBarElement.Id, OriginNodeId = modelBarElement.OriginNode.Id, DestinationNodeId = modelBarElement.DestinationNode.Id, MaterialId = modelBarElement.Material.Id, CrossSectionArea = modelBarElement.CrossSectionArea }; result.Elements.Add(barElementMemento); } return(result); }