/// <summary>
        /// Converts a Circuit instance into seraliazable CircuitData object
        /// </summary>
        /// <param name="circuit">the circuit to convert</param>
        /// <returns>converted circuitdata</returns>
        public CircuitData ConvertFromCircuit(Circuit circuit)
        {
            CircuitData circuitData = new CircuitData(circuit.Name);

            CreateElementData(circuitData, circuit);
            return(circuitData);
        }
        private void CreateElements(Circuit circuit, CircuitData circuitData, SignalList signals, bool createGraphics)
        {
            Dictionary <BaseElement, BaseElementData> elemDict = new Dictionary <BaseElement, BaseElementData>();

            foreach (BaseElementData elemData in circuitData.Elements)
            {
                InputOutputElement element = null;
                element = ConvertElementData(elemData, createGraphics, elemDict, signals);
                if (element != null)
                {
                    circuit.AddElement(element);
                }
            }
            foreach (ConnectionData connectionData in circuitData.Connections)
            {
                Connection connection = new Connection();
                connection.Name = connectionData.Name;
                ConnectTerminals(connection, circuit, elemDict);
                if (connection.Terminals.Count > 0)
                {
                    if (createGraphics)
                    {
                        GraphicConnection graphicConnection =
                            GraphicObjectFactory.CreateInstance(typeof(Connection), connection) as GraphicConnection;
                        CreateConnectionLines(circuit, graphicConnection, connectionData);
                    }
                    circuit.AddElement(connection);
                }
            }
        }
        /// <summary>
        /// Converts (restores) deserialized CircuitData object into a Circuit instance.
        /// Creation of graphical objects for GUI is determined by parameter.
        /// </summary>
        /// <param name="circuitData">deserialized CircuitData object</param>
        /// <param name="signals">the corresponding Signals for the circuit</param>
        /// <param name="createGraphics">states whether to create graphical objects</param>
        /// <returns>restored Circuit instance</returns>
        public Circuit ConvertToCircuit(CircuitData circuitData, SignalList signals, bool createGraphics)
        {
            Circuit circuit = new Circuit();

            CreateElements(circuit, circuitData, signals, createGraphics);
            return(circuit);
        }
        /// <summary>
        /// Serializes a Circuits converted to CircuitData objects to a XML file
        /// </summary>
        /// <param name="fileName">The filename of the file</param>
        /// <param name="circuitData">The CircuitData object to serialize</param>
        public static void SerializeCircuit(string fileName, CircuitData circuitData)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(CircuitData));
            FileStream    fs         = new FileStream(fileName, FileMode.Create);

            serializer.Serialize(fs, circuitData);
            fs.Close();
        }
        /// <summary>
        /// Deserializes a XML file to a CircuitData object, which can be converted to Circuits.
        /// </summary>
        /// <param name="fileName">The filename of the file</param>
        /// <returns>The deserialized CircuitData object</returns>
        public static CircuitData DeserializeCircuit(string fileName)
        {
            if (File.Exists(fileName) == false)
            {
                return(new CircuitData());
            }
            XmlSerializer serializer  = new XmlSerializer(typeof(CircuitData));
            FileStream    fs          = new FileStream(fileName, FileMode.Open, FileAccess.Read);
            CircuitData   circuitData = (CircuitData)serializer.Deserialize(fs);

            fs.Close();
            return(circuitData);
        }
        private void CreateElementData(CircuitData circuitData, Circuit circuit)
        {
            List <BaseElementData> elements    = new List <BaseElementData>();
            List <ConnectionData>  connections = new List <ConnectionData>();

            foreach (BaseElement be in circuit)
            {
                if (be is Connection)
                {
                    ConnectionData connData = ConvertConnection(be);
                    connections.Add(connData);
                }
                else if (be is InputOutputElement)
                {
                    BaseElementData elemData = ConvertIOElement(be);
                    if (elemData != null)
                    {
                        elements.Add(elemData);
                    }
                }
            }
            circuitData.Elements    = elements.ToArray();
            circuitData.Connections = connections.ToArray();
        }
 /// <summary>
 /// Converts (restores) deserialized CircuitData object into a Circuit instance.
 /// Creation of graphical objects for GUI is determined by parameter.
 /// </summary>
 /// <param name="circuitData">deserialized CircuitData object</param>
 /// <param name="createGraphics">states whether to create graphical objects</param>
 /// <returns>restored Circuit instance</returns>
 public Circuit ConvertToCircuit(CircuitData circuitData, bool createGraphics)
 {
     return(ConvertToCircuit(circuitData, null, createGraphics));
 }
 /// <summary>
 /// Converts (restores) deserialized CircuitData object into a Circuit instance.
 /// Graphical objects for GUI are not created.
 /// </summary>
 /// <param name="circuitData">deserialized CircuitData object</param>
 /// <returns>restored Circuit instance</returns>
 public Circuit ConvertToCircuit(CircuitData circuitData)
 {
     return(ConvertToCircuit(circuitData, null, false));
 }