Пример #1
0
        /// <summary>
        /// Makes sure that every shape in the sketch has a unique name.
        /// </summary>
        public virtual void process(Sketch.Sketch sketch)
        {
            Recognizers.TextRecognizer textRecognizer = new Recognizers.TextRecognizer();
            HashSet <string>           alreadySeen    = new HashSet <string>();
            int count = 0;

            foreach (Sketch.Shape shape in sketch.Shapes)
            {
                if (shape.Type == LogicDomain.TEXT && shape.Name == null)
                {
                    textRecognizer.recognize(shape);
                }

                if (alreadySeen.Contains(shape.Name) || shape.Name == null)
                {
                    while (alreadySeen.Contains(shape.Type + "_" + count))
                    {
                        count++;
                    }
                    shape.Name = (shape.Type + "_" + count);
                }

                alreadySeen.Add(shape.Name);
            }

            if (DEBUG)
            {
                // For debugging purposes:
                Console.WriteLine("Sketch contains");
                foreach (Sketch.Shape shape in sketch.Shapes)
                {
                    Console.WriteLine(" - " + shape.Name + ", a " + shape.Type);
                }
            }
        }
Пример #2
0
        /// <summary>
        /// Applies a label to a group of substrokes.
        /// </summary>
        public override bool Execute()
        {
            // Get the sketch we are working with
            Sketch.Sketch sketch = sketchPanel.InkSketch.Sketch;

            // Accumulate the list of substrokes that are about to be relabeled
            List <Sketch.Substroke> substrokes = new List <Sketch.Substroke>();

            foreach (Stroke stroke in inkStrokes)
            {
                // Find the corresponding substroke in the sketch
                Sketch.Substroke sub = sketchPanel.InkSketch.GetSketchSubstrokeByInk(stroke);

                // Add it to the growing list
                substrokes.Add(sub);
            }

            // Make a new shape out of these substrokes
            Domain.ShapeType    theType = Domain.LogicDomain.getType(label);
            List <Sketch.Shape> shapesToRegroup;

            labeledShape = sketch.MakeNewShapeFromSubstrokes(out shapesToRegroup, substrokes, theType, 1.0);

            if (userSpecifiedLabel)
            {
                // Updates the orientation for the sake of the ghost gate
                if (labeledShape.Type.Classification == LogicDomain.GATE_CLASS)
                {
                    RecognitionInterfaces.Orienter orienter = RecognitionManager.RecognitionPipeline.createDefaultOrienter();
                    orienter.orient(labeledShape, sketchPanel.InkSketch.FeatureSketch);
                }

                // Update the shape name for text
                else if (labeledShape.Type.Classification == LogicDomain.TEXT_CLASS)
                {
                    RecognitionInterfaces.Recognizer recognizer = new Recognizers.TextRecognizer();
                    recognizer.recognize(labeledShape, sketchPanel.InkSketch.FeatureSketch);
                }
            }

            // Record the fact that user specified this grouping or label,
            // so we don't accidentally change it in the future.
            labeledShape.AlreadyGrouped = userSpecifiedGroup;
            if (label != new ShapeType().Name)
            {
                labeledShape.UserLabeled = userSpecifiedLabel;


                // Also, update the recognition to take this into account
                if (userSpecifiedLabel && ErrorCorrected != null)
                {
                    ErrorCorrected(labeledShape);
                }
            }

            // Fun Fix
            // Problem description:
            //   Suppose you draw wire -> notgate -> wire
            //                      --------|>o--------
            //   Then you label the notgate as a wire, so the
            //   whole ensamble becomes a single wire. Then you
            //   relabel it as a notgate again. The two wires
            //   which *were* distinct are still part of the same
            //   wire mesh.
            // Problem solution:
            //   Here, when you apply a label to a group of
            //   substrokes, we will "explode" all the wire shapes
            //   that were changed (break them into single-substroke
            //   shapes). We expect that they will be reconnected
            //   later.

            List <Sketch.Shape> newShapesToRegroup = new List <Sketch.Shape>(shapesToRegroup);

            foreach (Sketch.Shape modifiedShape in shapesToRegroup)
            {
                if (Domain.LogicDomain.IsWire(modifiedShape.Type))
                {
                    List <Sketch.Shape> newShapes = sketch.ExplodeShape(modifiedShape);
                    newShapesToRegroup.Remove(modifiedShape);
                    newShapesToRegroup.AddRange(newShapes);
                }
            }
            shapesToRegroup = newShapesToRegroup;

            // Make sure the old connected shapes are updated with their
            // relationships to the newly labeled shape
            if (Regroup != null)
            {
                // Regroup everything so highlighting/labels are correctly updated
                // Ensures that the newly labeled shape's relationship to its
                // connected shapes are updated as well
                shapesToRegroup.Add(labeledShape);
                Regroup(new List <Sketch.Shape>(shapesToRegroup));
            }

            sketchPanel.EnableDrawing();
            return(true);
        }