예제 #1
0
        /// <summary>
        /// Process an ordinary sketch by first creating a feature sketch for it.
        /// This method calls process using the created feature sketch, then
        /// returns the feature sketch, in case you want to use it. The given
        /// ordinary sketch is modified in the process.
        /// </summary>
        /// <param name="project">the sketch project to use</param>
        /// <returns>the featureSketch used during processing</returns>
        public FeatureSketch process(Sketch.Project project)
        {
            FeatureSketch featureSketch = FeatureSketch.MakeFeatureSketch(project);

            process(featureSketch);
            return(featureSketch);
        }
예제 #2
0
        /// <summary>
        /// Main constructor of the subcircuit window. Takes in a project that it will wrap into an inkcanvas sketch
        /// in order to display.
        /// </summary>
        /// <param name="subSketch"></param>
        public MainWindow(ref Sketch.Project subSketch)
        {
            try
            {
                InitializeComponent();
            }
            catch (Exception ex)
            {
                // Log error
                System.Console.WriteLine(ex.InnerException.Message);
                //System.Console.WriteLine(ex.ListTrace);
            }

            // Set up notes panel
            this.subSketch        = subSketch;
            WrapperSketch         = new InkToSketchWPF.InkCanvasSketch(new InkCanvas());
            WrapperSketch.project = subSketch;
            this.WrapperSketch.ClearButNotSketch();
            // Makes the ink to display
            WrapperSketch.CreateInkStrokesFromSketch();

            //Set up the ink holder
            this.inkCanvas        = WrapperSketch.InkCanvas;
            this.inkCanvas.Height = dockPanel.Height;
            this.inkCanvas.Width  = dockPanel.Width;

            //The dockPanel is in a view box, which means that it will scale (and scale its children)
            //to fit the screen.
            TextBlock helpText = new TextBlock();

            helpText.Text     = "You can click a sub-circuit if one exists to view it";
            helpText.FontSize = 16;

            dockPanel.Children.Clear();
            dockPanel.Children.Add(this.inkCanvas);
            this.inkCanvas.Children.Add(helpText);

            //Actually color the strokes on the inkcanvas according to the recognition
            //This is valid because only simulatable circuits can be viewed here
            foreach (System.Windows.Ink.Stroke inkStroke in this.inkCanvas.Strokes)
            {
                Sketch.Substroke substroke = (Sketch.Substroke)WrapperSketch.GetSketchSubstrokeByInk(inkStroke);
                Domain.ShapeType label     = substroke.Type;

                Color color = label.Color;

                inkStroke.DrawingAttributes.Color = color;
            }

            //Draws the inkcanvas with the new colors
            this.WrapperSketch.InkCanvas.InvalidateVisual();
            this.inkCanvas.UpdateLayout();



            // Set Editing Modes
            inkCanvas.EditingMode         = InkCanvasEditingMode.None;
            inkCanvas.EditingModeInverted = InkCanvasEditingMode.None;
            inkCanvas.StylusDown         += new StylusDownEventHandler(inkCanvas_StylusDown);
        }
예제 #3
0
        public void TestXMLConversion()
        {
            const string      filename = "tmp.xml";
            RecognitionResult type     = new RecognitionResult(LogicDomain.AND, 0.9, 0.33);

            Sketch.Project project1 = new Sketch.Project(Sketches.newValidSketch());;

            foreach (Sketch.Shape shape in project1.sketch.Shapes)
            {
                type.ApplyToShape(shape);
            }

            ConverterXML.SaveToXML writer = new ConverterXML.SaveToXML(project1);
            writer.WriteXML(filename);

            ConverterXML.ReadXML reader  = new ConverterXML.ReadXML(filename);
            Sketch.Sketch        sketch2 = reader.Sketch;

            sketch2.CheckConsistency();

            Assert.IsTrue(project1.sketch.Equals(sketch2), "Original sketch is not equal to the loaded sketch");
            Assert.IsTrue(sketch2.Equals(project1.sketch), "Loaded sketch is not equal to the original");

            foreach (Sketch.Shape shape in sketch2.Shapes)
            {
                Assert.AreEqual(type.Type, shape.Type, "Shape types are not preserved across save/load");
                Assert.IsTrue(Math.Abs(type.Confidence - shape.Probability) < 0.0001, "Shape confidences are not preserved across save/load");
                Assert.IsTrue(Math.Abs(type.Orientation - shape.Orientation) < 0.0001, "Shape orientations are not preserved across save/load");
            }
        }
예제 #4
0
        public static Featurefy.FeatureSketch newValidFeatureSketch(Sketch.Project project)
        {
            Dictionary <string, string> files = new Dictionary <string, string>();

            files["FeaturesSingle"] = "FeatureListSingle.txt";
            files["FeaturesGroup"]  = "FeatureListGroup.txt";
            return(Featurefy.FeatureSketch.MakeFeatureSketch(project, files));
        }
예제 #5
0
        /// <summary>
        /// precondition: circuit is not null
        /// </summary>
        /// <param name="circuit"></param>
        /// <param name="xmlDocument"></param>
        private static void WriteCircuit(Sketch.Project project, XmlTextWriter xmlDocument)
        {
            xmlDocument.WriteStartElement("circuit");

            SaveToXML.WriteInputOutput(project.inputs, project.outputs, xmlDocument);
            SaveToXML.WriteBehavior(project.behavior, xmlDocument);
            SaveToXML.WriteLogisim(project.saveToCircDoc, xmlDocument);
            xmlDocument.WriteEndElement();
        }
예제 #6
0
        /// <summary>
        /// Import the circuit and project into our instance of SaveToCirc.
        /// </summary>
        private SaveToCirc(Sketch.Project project, Circuit circuit)
        {
            this.circuit = circuit;
            this.project = project; // Need Project to get CircuitElement -> Subproject correspondence.

            this.stringWriter = new StringWriter();
            this.writer       = new XmlTextWriter(stringWriter);
            writer.Formatting = System.Xml.Formatting.Indented;

            writeCircuit();
        }
예제 #7
0
        private static string writeSubProjects(Sketch.Project project)
        {
            System.Text.StringBuilder builder        = new System.Text.StringBuilder();
            List <Sketch.Project>     allSubprojects = project.AllSubprojects;

            foreach (Sketch.Project sub in allSubprojects)
            {
                builder.Append(sub.saveToCircDoc);
            }
            return(builder.ToString());
        }
예제 #8
0
        /// <summary>
        /// Write the lookup information for subcircuits
        /// </summary>
        /// <param name="xmlDocument"></param>
        private static void WriteSubCircuitInfo(XmlTextWriter xmlDocument, Sketch.Project project)
        {
            xmlDocument.WriteStartElement("SubCircuitLookup");
            foreach (int indexUsed in project.subProjectsused.Keys)
            {
                if (project.subProjectsused[indexUsed] > 0)
                {
                    xmlDocument.WriteStartElement("Number");
                    xmlDocument.WriteAttributeString("lookup", indexUsed.ToString());
                    WriteSketch(project.subProjectLookup[indexUsed], xmlDocument);
                    xmlDocument.WriteEndElement();
                }
            }

            xmlDocument.WriteEndElement();
        }
예제 #9
0
        /// <summary>
        /// Default constructor (not used)
        /// </summary>
        public MainWindow()
        {
            try
            {
                InitializeComponent();
            }
            catch (Exception ex)
            {
                // Log error
                System.Console.WriteLine(ex.InnerException.Message);
            }

            this.subSketch = null;
            this.inkCanvas = new InkCanvas();

            // Set Editing Modes
            inkCanvas.EditingMode         = InkCanvasEditingMode.None;
            inkCanvas.EditingModeInverted = InkCanvasEditingMode.None;
        }
예제 #10
0
 /// <summary>
 /// Test to see if you pen downed on a subcircuit in the picture, and display it
 /// </summary>
 private void inkCanvas_StylusDown(object sender, StylusEventArgs e)
 {
     //gather all strokes within 50 pixels of the pen down
     System.Windows.Ink.StrokeCollection strokesHit = inkCanvas.Strokes.HitTest(e.GetPosition(inkCanvas), 50);
     foreach (System.Windows.Ink.Stroke stroke in strokesHit)
     {
         Sketch.Substroke substroke = (Sketch.Substroke)WrapperSketch.GetSketchSubstrokeByInk(stroke);
         // Check if you landed on a subcircuit
         if (substroke.ParentShape.Type == Domain.LogicDomain.SUBCIRCUIT)
         {
             //Bring up a new window and truth table window for the new project
             Sketch.Project subProject       = subSketch.subProjectLookup[substroke.ParentShape.SubCircuitNumber];
             MainWindow     subCircuitWindow = new SubCircuitWindow.MainWindow(ref subProject);
             SimulationManager.TruthTableWindow subTruthWindow = new SimulationManager.TruthTableWindow(subProject, substroke.ParentShape.Name);
             subCircuitWindow.Show();
             subTruthWindow.Show();
             break;
         }
     }
 }
예제 #11
0
        /// <summary>
        /// Writes the complete LogiSim representation to the given filepath.
        /// </summary>
        public static void LogiSimExport(Sketch.Project project, Circuit circuit,
                                         string filepath)
        {
            XmlTextWriter textWriter = new XmlTextWriter(filepath, System.Text.Encoding.UTF8);

            textWriter.Formatting = System.Xml.Formatting.Indented;
            textWriter.WriteStartElement("project");

            // Specify LogiSim configuration and main project.
            SpecifyLibraries(textWriter);
            textWriter.WriteStartElement("main");
            textWriter.WriteAttributeString("name", project.UniqueIdentifier);
            textWriter.WriteEndElement();
            SpecifyTools(textWriter);

            // Write project information.
            textWriter.WriteRaw(writeSubProjects(project));
            if (circuit != null)
            {
                textWriter.WriteRaw(SubcircuitInfo(project, circuit));
            }
            textWriter.WriteEndElement();
            textWriter.Close();
        }
예제 #12
0
        /// <summary>
        /// writes the sketch and its points, substrokes and shapes to the given xml document.
        /// also writes its circuit.
        /// </summary>
        /// <param name="sketch"></param>
        /// <param name="circuit"></param>
        /// <param name="xmlDocument"></param>
        private static void WriteSketch(Sketch.Project project, XmlTextWriter xmlDocument)

        {
            xmlDocument.WriteStartElement("sketch");

            string[] sketchAttributeNames  = project.sketch.XmlAttrs.getAttributeNames();
            object[] sketchAttributeValues = project.sketch.XmlAttrs.getAttributeValues();

            Sketch.Point[]     points     = project.sketch.Points;
            Sketch.Shape[]     shapes     = project.sketch.Shapes;
            Sketch.Stroke[]    strokes    = project.sketch.Strokes;
            Sketch.Substroke[] substrokes = project.sketch.Substrokes;


            int length;
            int i;


            // Write all the attributes
            length = sketchAttributeNames.Length;
            for (i = 0; i < length; ++i)
            {
                if (sketchAttributeValues[i] != null)
                {
                    xmlDocument.WriteAttributeString(sketchAttributeNames[i], sketchAttributeValues[i].ToString());
                }
            }

            // Write all the points
            length = points.Length;
            for (i = 0; i < length; ++i)
            {
                SaveToXML.WritePoint(points[i], xmlDocument);
            }

            // Write all the substrokes
            length = substrokes.Length;
            for (i = 0; i < length; ++i)
            {
                SaveToXML.WriteSubstroke(substrokes[i], xmlDocument);
            }

            // Write all the strokes
            length = strokes.Length;
            for (i = 0; i < length; ++i)
            {
                SaveToXML.WriteStroke(strokes[i], xmlDocument);
            }

            // Write all the shapes
            length = shapes.Length;
            for (i = 0; i < length; ++i)
            {
                SaveToXML.WriteShape(shapes[i], xmlDocument);
            }

            WriteCircuit(project, xmlDocument);

            WriteSubCircuitInfo(xmlDocument, project);
            xmlDocument.WriteEndElement();
        }
예제 #13
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="project">Create SaveToXML from sketch</param>
 public SaveToXML(Sketch.Project project)
 {
     this.project = project;
 }
예제 #14
0
        /// <summary>
        /// Creating a truth table window from a project. This doesn't allow the simulation options
        /// that the table usually provides, so no events are registered
        /// This is for the viewing of subcircuits
        /// </summary>
        /// <param name="project">Project to display the truth table of</param>
        public TruthTableWindow(Sketch.Project project, string title)
        {
            InitializeComponent();
            this.Title += " - " + title;
            // Set table properties
            this.Rows            = SortedLists(project.behavior);
            this.numInputs       = project.inputs.Count;
            this.numOutputs      = project.outputs.Count;
            InputDock.Visibility = System.Windows.Visibility.Hidden;
            CheckBox.Visibility  = System.Windows.Visibility.Hidden;

            // Create truth table and hover timer
            TruthTable.FontFamily  = new FontFamily("Verdana");
            TruthTable.CellSpacing = 0;
            TableRowGroup tableRowGroup = new TableRowGroup();

            TruthTable.RowGroups.Add(tableRowGroup);

            int index = 0;

            // Create the Header Row
            TableRow      HeaderRow = new TableRow();
            List <string> header    = new List <string>();

            header.AddRange(project.inputs.Concat(project.outputs));
            foreach (string head in header)
            {
                index++;
                // Add a column with the name of the input
                TableColumn Column = new TableColumn();
                Column.Width = new GridLength((FlowDocReader.ActualWidth) / header.Count);
                if (FlowDocReader.ActualWidth > 50)
                {
                    Column.Width = new GridLength((FlowDocReader.ActualWidth - 50) / header.Count);
                }
                Column.Name = head;
                TruthTable.Columns.Add(Column);
                TableCell headerCell = new TableCell(new Paragraph(new Run(head)));
                headerCell.TextAlignment = TextAlignment.Center;

                // Color input and output objects differently to disinguish
                if (index > numInputs)
                {
                    headerCell.Foreground = System.Windows.Media.Brushes.IndianRed;
                }
                else
                {
                    headerCell.Foreground = System.Windows.Media.Brushes.Navy;
                }
                headerCell.BorderThickness = new Thickness(0, 0, 0, 1);
                headerCell.BorderBrush     = System.Windows.Media.Brushes.Black;
                HeaderRow.Cells.Add(headerCell);
            }

            TruthTable.RowGroups[0].Rows.Add(HeaderRow);

            // Create the rest of the table
            foreach (List <int> row in Rows)
            {
                TableRow tableRow = new TableRow();
                int      count    = 1;
                foreach (int i in row)
                {
                    TableCell intCell = new TableCell(new Paragraph(new Run(i.ToString())));
                    intCell.TextAlignment = TextAlignment.Center;

                    // Create a border between inputs and outputs
                    intCell.BorderThickness = new Thickness(0, 0, 0, 0);
                    if (count == numInputs)
                    {
                        intCell.BorderThickness = new Thickness(0, 0, 1, 0);
                    }
                    intCell.BorderBrush = System.Windows.Media.Brushes.Black;

                    tableRow.Cells.Add(intCell);
                    count++;
                }
                TruthTable.RowGroups[0].Rows.Add(tableRow);
            }
        }
예제 #15
0
        /// <summary>
        /// Returns a LogiSim string representing the project's top-level circuit.
        /// </summary>
        public static string SubcircuitInfo(Sketch.Project project, Circuit circuit)
        {
            SaveToCirc saver = new SaveToCirc(project, circuit);

            return(saver.stringWriter.ToString());
        }