Exemplo n.º 1
0
        /// <summary>
        /// Tries to parse a sketch into a circuit, and
        /// indicates whether or not it was successful.
        ///
        /// The main functionality in circuit recognition
        /// here is that it both determines what are inputs
        /// and outputs on both the gate and circuit level,
        /// and organizes the results into something the
        /// circuit can be built off of for simulation.
        /// </summary>
        /// <param name="sketch">The sketch to parse</param>
        /// <returns>True if parsing was a success,
        /// false if there were errors</returns>
        public bool SuccessfullyParse(Sketch.Sketch sketch)
        {
            // We should have a cleared circuit model each time
            // we try to parse a sketch.
            resetCircuitModel();

            #region Check circuit domain

            string       shouldNotWorkReason = "There are no shapes.";
            Sketch.Shape shouldNotWorkShape  = null;
            if (sketch.Shapes.Length > 0)
            {
                foreach (Sketch.Shape shape in sketch.Shapes)
                {
                    if (!_domain.IsProperlyConnected(shape))
                    {
                        shouldNotWorkReason = "Shape " + shape.Name + " is not properly connected";
                        shouldNotWorkShape  = shape;
                        break;
                    }
                }
            }
            #endregion

            // Check to make sure the sketch was valid in the first place.
            // If not, do not continue.
            _successfulParse = CheckSketch(sketch);
            if (!_successfulParse)
            {
                return(false);
            }

            loadWiresAndGates(sketch);
            connectGatesToWires();
            loadCircuitIO(sketch);
            connectComponentsToComponents();

            // Make sure that the circuit we just made is valid
            _successfulParse = CheckCircuit();

            if (debug)
            {
                printErrors();
            }

            // This method call is here only to let the SimulationManager
            // have a mapping of shapes to their names.
            mapShapesToNames(sketch);

            // Likewise, this method is here only for mesh highlighting
            // to have a mapping of sketch strokes to circuit parts.
            mapStrokesToCircuitParts(sketch);

            return(_successfulParse);
        }