/// <summary> /// Renames the model system if possible /// </summary> /// <param name="modelSystem">The model system to rename</param> /// <param name="newName">The name to save it as</param> /// <param name="error">An error message if the operation fails</param> /// <returns>True if the operation succeeds, false otherwise with a message.</returns> public bool Rename(ModelSystem modelSystem, string newName, ref string error) { var newNameLower = newName.ToLowerInvariant(); if (ModelSystems.Any(ms => ms.Name.ToLowerInvariant() == newNameLower)) { error = "There was already a model system with the name " + newName + "!"; return(false); } var oldName = modelSystem.Name; modelSystem.Name = newName; var success = modelSystem.Save(ref error); // if the rename worked we need to cleanup the old save file if (success) { try { File.Delete(Path.Combine(this.Config.ModelSystemDirectory, oldName + ".xml")); } catch (IOException) { // if we were unable to delete the file that means it was already removed, so there is nothing to do } } return(success); }
/// <summary> /// Save the changes into the real model system structure. /// This should only be called by ModelSystemEditingSession. /// </summary> /// <param name="error">The error if there was one</param> internal bool Save(ref string error) { if (!Root.Save(ref error)) { return(false); } if (ModelSystem != null) { ModelSystem.ModelSystemStructure = ClonedModelSystemRoot; ModelSystem.Description = Description; ModelSystem.LinkedParameters = LinkedParameters.LinkedParameters.Select(lp => (ILinkedParameter)lp.RealLinkedParameter).ToList(); return(ModelSystem.Save(ref error)); } else if (ModelSystemIndex >= 0) { Name = ClonedModelSystemRoot.Name; Project.ModelSystemStructure[ModelSystemIndex] = ClonedModelSystemRoot; Project.ModelSystemDescriptions[ModelSystemIndex] = Description; Project.LinkedParameters[ModelSystemIndex] = LinkedParameters.LinkedParameters.Select(lp => (ILinkedParameter)lp.RealLinkedParameter).ToList(); return(Project.Save(ref error)); } else { error = "You can not save over previous runs!"; return(false); } }
/// <summary> /// Save the changes into the real model system structure. /// This should only be called by ModelSystemEditingSession. /// </summary> /// <param name="error">The error if there was one</param> internal bool Save(ref string error) { if (!Root.Save(ref error)) { return(false); } if (ModelSystem != null) { ModelSystem.ModelSystemStructure = ClonedModelSystemRoot; ModelSystem.Description = Description; ModelSystem.LinkedParameters = LinkedParameters.LinkedParameters.Select(lp => (ILinkedParameter)lp.RealLinkedParameter).ToList(); return(ModelSystem.Save(ref error)); } else if (ModelSystemIndex >= 0) { Project.ModelSystemStructure[ModelSystemIndex] = ClonedModelSystemRoot; Project.ModelSystemDescriptions[ModelSystemIndex] = Description; Project.LinkedParameters[ModelSystemIndex] = LinkedParameters.LinkedParameters.Select(lp => (ILinkedParameter)lp.RealLinkedParameter).ToList(); // changing the name should go last because it will bubble up to the GUI and if the models are not in the right place the old name still be read in Name = ClonedModelSystemRoot.Name; return(Project.Save(ref error)); } else { error = "You can not save over previous runs!"; return(false); } }
/// <summary> /// Export a model system to a string /// </summary> /// <param name="ms">The model system to export</param> /// <param name="modelSystemAsString">The string to save the model system into</param> /// <param name="error">A description of the error if one occurs</param> /// <returns>True if the export was successful, false with description otherwise</returns> public bool ExportModelSystemAsString(ModelSystem ms, out string modelSystemAsString, ref string error) { using (var stream = new MemoryStream()) { if (!ms.Save(stream, ref error)) { modelSystemAsString = null; return(false); } var buffer = stream.ToArray(); modelSystemAsString = new string(Encoding.Unicode.GetChars(buffer, 0, buffer.Length)); return(true); } }
/// <summary> /// Makes a copy of the model system with the given new name /// </summary> /// <param name="modelSystem">The model system to create a clone of</param> /// <param name="newName">The name to give the model system</param> /// <param name="error">An error message if the operation fails</param> /// <returns>True if successful, false otherwise with an error message.</returns> public bool CloneModelSystem(ModelSystem modelSystem, string newName, ref string error) { var newNameLower = newName.ToLowerInvariant(); if (ModelSystems.Any(ms => ms.Name.ToLowerInvariant() == newNameLower)) { error = "There was already a model system with the name " + newName + "!"; return(false); } ModelSystem clone = new ModelSystem(Config, newName); clone.Description = modelSystem.Description; clone.LinkedParameters = modelSystem.LinkedParameters; clone.Name = newName; clone.ModelSystemStructure = modelSystem.ModelSystemStructure; var saved = clone.Save(ref error); // unload so there are no references to the current model system clone.Unload(); Add(clone); return(saved); }
internal bool ExportModelSystem(string fileName, Project project, int modelSystemIndex, ref string error) { var root = project.ModelSystemStructure[modelSystemIndex]; return(ModelSystem.Save(fileName, root, root.Description, project.LinkedParameters[modelSystemIndex], ref error)); }