/// <summary> /// Writes the appropriate LogiSim XML to the given file /// </summary> /// <param name="textWriter"></param> public void CircuitToXML(XmlTextWriter textWriter) { textWriter.WriteStartElement("circuit"); textWriter.WriteAttributeString("name", "main"); foreach (var nameAndElement in circuit.CircuitElementGraph) { CircuitSimLib.CircuitElement element = nameAndElement.Value; if (element is CircuitSimLib.INPUT) { InputToXML(textWriter, (CircuitSimLib.INPUT)element); } else if (element is CircuitSimLib.OUTPUT) { OutputToXML(textWriter, (CircuitSimLib.OUTPUT)element); } else if (element is CircuitSimLib.Gate) { GateToXML(textWriter, (CircuitSimLib.Gate)element); } else { Console.Write("invalid type"); } WiresOutOfElementToXML(textWriter, element); } }
/// <summary> /// Get the location of the input from the element and the index of the input /// </summary> /// <param name="element">The element with inputs</param> /// <param name="inputNo">Index of the input (0-indexed)</param> /// <returns></returns> private Point getInpoint(CircuitSimLib.CircuitElement element, int inputNo) { Point location = ConvertPointToLogisimPoint(element.OutPoint()); if (element is CircuitSimLib.INPUT || element is CircuitSimLib.OUTPUT) { return(location); } else if (element is CircuitSimLib.NOT) { return(new Point(location.X - 30, location.Y)); } else { int locX = (int)location.X - 50; int locY = (int)location.Y + (inputNo - 2) * 10; return(new Point(locX, locY)); } }
/// <summary> /// Writes all the outgoing wires of the given element /// </summary> /// <param name="textWriter"></param> /// <param name="element"></param> private void WiresOutOfElementToXML(XmlTextWriter textWriter, CircuitSimLib.CircuitElement element) { foreach (var outputIndexAndNeighbors in element.Outputs) { int outputNo = outputIndexAndNeighbors.Key; Point start = getOutpoint(element, outputNo); System.Collections.Generic.List <CircuitSimLib.CircuitElement> rightNeighbors = outputIndexAndNeighbors.Value; foreach (CircuitSimLib.CircuitElement rightNeighbor in rightNeighbors) { // Generate the inpoint based on which input we're connected to System.Collections.Generic.List <int> inputNo; rightNeighbor.Inputs.TryGetValue(element, out inputNo); foreach (int index in inputNo) { Point end = getInpoint(rightNeighbor, index); // Create the wires ConnectWires(textWriter, start, end); } } } }
/// <summary> /// Get the location of the output from the element and the index of the output /// </summary> /// <param name="element">The element which has outputs</param> /// <param name="outputNo">The index of the output (0-indexed) </param> /// <returns></returns> private Point getOutpoint(CircuitSimLib.CircuitElement element, int outputNo) { // Currently just returns the location of the output return(element.OutPoint(outputNo)); }