private void SaveFile(string fileName) { SuspendEngine(); string tempFile = System.IO.Path.GetTempFileName(); FileStream file = File.Create(tempFile); foreach (ModuleView na in theNeuronArray.modules) { if (na.TheModule != null) { na.TheModule.SetUpBeforeSave(); } } //hide unused neurons to save on file size for (int i = 0; i < theNeuronArray.arraySize; i++) { if (!theNeuronArray.GetNeuron(i).InUse() && theNeuronArray.GetNeuron(i).Model == Neuron.modelType.Std) { theNeuronArray.SetNeuron(i, null); } } //Save the data from the network (NeuronArray and modules) to the file try { theNeuronArray.displayParams = theNeuronArrayView.Dp; XmlSerializer writer = new XmlSerializer(typeof(NeuronArray), GetModuleTypes()); writer.Serialize(file, theNeuronArray); file.Close(); currentFileName = fileName; Properties.Settings.Default["CurrentFile"] = currentFileName; Properties.Settings.Default.Save(); File.Copy(tempFile, currentFileName, true); File.Delete(tempFile); } catch (Exception e1) { MessageBox.Show("Save Failed because: " + e1.Message + "\r\n" + e1.InnerException.Message); if (File.Exists(tempFile)) { file.Close(); File.Delete(tempFile); } } //restore unused neurons for (int i = 0; i < theNeuronArray.arraySize; i++) { if (theNeuronArray.GetNeuron(i) == null) { theNeuronArray.SetNeuron(i, new Neuron(i)); } } ResumeEngine(); }
private bool LoadFile(string fileName) { CloseAllModuleDialogs(); CloseHistoryWindow(); CloseNotesWindow(); theNeuronArrayView.theSelection.selectedRectangles.Clear(); CloseAllModuleDialogs(); SuspendEngine(); try { // Load the data from the XML to the Brainsim Engine. FileStream file = File.Open(fileName, FileMode.Open); XmlSerializer reader = new XmlSerializer(typeof(NeuronArray), GetModuleTypes()); theNeuronArray = (NeuronArray)reader.Deserialize(file); file.Close(); } catch (Exception e1) { if (e1.InnerException != null) { MessageBox.Show("File Load failed because:\r\n " + e1.Message + "\r\nAnd:\r\n" + e1.InnerException.Message); } else { MessageBox.Show("File Load failed because:\r\n " + e1.Message); } return(false); } for (int i = 0; i < theNeuronArray.arraySize; i++) { if (theNeuronArray.GetNeuron(i) == null) { theNeuronArray.SetNeuron(i, new Neuron(i)); } if (theNeuronArray.GetNeuron(i).CurrentCharge > 0 || theNeuronArray.GetNeuron(i).LastCharge > 0) { theNeuronArray.AddToFiringQueue(theNeuronArray.GetNeuron(i).Id); } } //Update all the synapses to ensure that the synapse-from lists are correct foreach (Neuron n in theNeuronArray.Neurons()) { if (n.SynapsesFrom != null) { n.SynapsesFrom.Clear(); } } foreach (Neuron n in theNeuronArray.Neurons()) { if (n.Synapses != null) { foreach (Synapse s in n.Synapses) { n.AddSynapse(s.TargetNeuron, s.Weight, theNeuronArray, false); s.N = theNeuronArray.GetNeuron(s.TargetNeuron); } } if (n.CurrentCharge >= 1 || n.LastCharge >= 1 || n.Model == Neuron.modelType.LIF) { theNeuronArray.AddToFiringQueue(n.Id); } } theNeuronArray.CheckSynapseArray(); theNeuronArrayView.Update(); setTitleBar(); Task.Delay(1000).ContinueWith(t => ShowDialogs()); foreach (ModuleView na in theNeuronArray.modules) { if (na.TheModule != null) { na.TheModule.SetUpAfterLoad(); } } if (theNeuronArray.displayParams != null) { theNeuronArrayView.Dp = theNeuronArray.displayParams; } NeuronArrayView.SortAreas(); Update(); SetShowSynapsesCheckBox(theNeuronArray.ShowSynapses); OpenHistoryWindow(); ResumeEngine(); return(true); }
//copy the selection to a clipboard public void CopyNeurons() { //get list of neurons to copy List <int> neuronsToCopy = theSelection.EnumSelectedNeurons(); theSelection.GetSelectedBoundingRectangle(out int X1o, out int Y1o, out int X2o, out int Y2o); myClipBoard = new NeuronArray(); myClipBoard.Initialize((X2o - X1o + 1) * (Y2o - Y1o + 1), (Y2o - Y1o + 1)); //by setting neurons to null, we can handle odd-shaped selections for (int i = 0; i < myClipBoard.arraySize; i++) { myClipBoard.SetNeuron(i, null); } //copy the neurons foreach (int nID in neuronsToCopy) { int destId = GetClipboardId(X1o, Y1o, nID); //copy the source neuron to the clipboard Neuron sourceNeuron = MainWindow.theNeuronArray.GetNeuron(nID); Neuron destNeuron = sourceNeuron.Clone(); destNeuron.Id = destId; myClipBoard.SetNeuron(destId, destNeuron); } //copy the synapses (this is two-pass so we make sure all neurons exist prior to copying foreach (int nID in neuronsToCopy) { Neuron sourceNeuron = MainWindow.theNeuronArray.GetNeuron(nID); int destId = GetClipboardId(X1o, Y1o, nID); Neuron destNeuron = myClipBoard.GetNeuron(destId); if (sourceNeuron.synapses != null) { foreach (Synapse s in sourceNeuron.synapses) { //only copy synapses with both ends in the selection if (neuronsToCopy.Contains(s.TargetNeuron)) { destNeuron.AddSynapse(GetClipboardId(X1o, Y1o, s.TargetNeuron), s.Weight); } } } } //copy modules foreach (ModuleView mv in MainWindow.theNeuronArray.modules) { if (theSelection.NeuronInSelection(mv.FirstNeuron) > 0 && theSelection.NeuronInSelection(mv.LastNeuron) > 0) { ModuleView newMV = new ModuleView() { FirstNeuron = GetClipboardId(X1o, Y1o, mv.FirstNeuron), TheModule = mv.TheModule, Color = mv.Color, Height = mv.Height, Width = mv.Width, Label = mv.Label, CommandLine = mv.CommandLine, }; myClipBoard.modules.Add(newMV); } } }