Example #1
0
        /// <summary>
        /// Creates a Substroke from Microsoft point data
        /// </summary>
        /// <param name="stroke"></param>
        /// <returns></returns>
        static public Sketch.Substroke MakeSubstroke(Microsoft.Ink.Stroke stroke)
        {
            Sketch.Point[] SketchPoints = stripStroke(stroke);
            Sketch.XmlStructs.XmlShapeAttrs XmlAttrs = new Sketch.XmlStructs.XmlShapeAttrs(true);
            XmlAttrs.Time      = SketchPoints[0].Time;
            XmlAttrs.Source    = "InkOverlay";
            XmlAttrs.PenHeight = stroke.DrawingAttributes.Height;
            XmlAttrs.PenTip    = stroke.DrawingAttributes.PenTip.ToString();
            XmlAttrs.PenWidth  = stroke.DrawingAttributes.Width;
            XmlAttrs.Color     = stroke.DrawingAttributes.Color.ToArgb();
            Sketch.Substroke s = new Sketch.Substroke(SketchPoints, XmlAttrs);

            return(s);
        }
Example #2
0
        /// <summary>
        /// Reads the Shape XML attributes
        /// </summary>
        /// <param name="reader">XML text reader for the file</param>
        private void ReadShape(XmlTextReader reader)
        {
            Sketch.XmlStructs.XmlShapeAttrs shapeAttrs = new Sketch.XmlStructs.XmlShapeAttrs();

            // Required MIT XML attributes
            string type = reader.GetAttribute("type");
            string name = reader.GetAttribute("name");
            string id   = reader.GetAttribute("id");
            string time = reader.GetAttribute("time");

            if ((type != null) && (name != null) && (id != null) && (time != null))
            {
                shapeAttrs.Type = type;
                shapeAttrs.Name = name;
                shapeAttrs.Id   = new Guid(id);
                shapeAttrs.Time = Convert.ToUInt64(time);
            }
            else
            {
                throw new ApplicationException("Invalid Format: Shapes must contain a Type, Name, Id, and Time");
            }

            // Get the rest of the attributes, if they are there
            // We use temporary strings here so that we can check if the attribute is <null>
            string author      = reader.GetAttribute("author");
            string color       = reader.GetAttribute("color");
            string height      = reader.GetAttribute("height");
            string width       = reader.GetAttribute("width");
            string area        = reader.GetAttribute("area");
            string laysInk     = reader.GetAttribute("laysInk");
            string orientation = reader.GetAttribute("orientation");
            string penTip      = reader.GetAttribute("penTip");
            string raster      = reader.GetAttribute("raster");
            string substrokeOf = reader.GetAttribute("substrokeOf");
            string p1          = reader.GetAttribute("p1");
            string p2          = reader.GetAttribute("p2");
            string x           = reader.GetAttribute("x");
            string y           = reader.GetAttribute("y");
            string text        = reader.GetAttribute("text");
            string leftx       = reader.GetAttribute("leftx");
            string topy        = reader.GetAttribute("topy");
            string control1    = reader.GetAttribute("control1");
            string control2    = reader.GetAttribute("control2");
            string start       = reader.GetAttribute("start");
            string end         = reader.GetAttribute("end");
            string source      = reader.GetAttribute("source");

            if (author != null)
            {
                shapeAttrs.Author = new Guid(author);
            }
            if (color != null)
            {
                shapeAttrs.Color = Convert.ToInt32(color);
            }
            if (height != null)
            {
                shapeAttrs.Height = Convert.ToSingle(height);
            }
            if (width != null)
            {
                shapeAttrs.Width = Convert.ToSingle(width);
            }
            if (area != null)
            {
                shapeAttrs.Area = Convert.ToDouble(area);
            }
            if (laysInk != null)
            {
                shapeAttrs.LaysInk = Convert.ToBoolean(laysInk);
            }
            if (orientation != null)
            {
                shapeAttrs.Orientation = Convert.ToSingle(orientation);
            }
            if (penTip != null)
            {
                shapeAttrs.PenTip = penTip;
            }
            if (raster != null)
            {
                shapeAttrs.Raster = raster;
            }
            if (substrokeOf != null)
            {
                shapeAttrs.SubstrokeOf = new Guid(substrokeOf);
            }
            if (p1 != null)
            {
                shapeAttrs.P1 = p1;
            }
            if (p2 != null)
            {
                shapeAttrs.P2 = p2;
            }
            if (x != null)
            {
                shapeAttrs.X = Convert.ToSingle(x);
            }
            if (y != null)
            {
                shapeAttrs.Y = Convert.ToSingle(y);
            }
            if (text != null)
            {
                shapeAttrs.Text = text;
            }
            if (leftx != null)
            {
                shapeAttrs.LeftX = Convert.ToSingle(leftx);
            }
            if (topy != null)
            {
                shapeAttrs.TopY = Convert.ToSingle(topy);
            }
            if (control1 != null)
            {
                shapeAttrs.Control1 = Convert.ToDouble(control1);
            }
            if (control2 != null)
            {
                shapeAttrs.Control2 = Convert.ToDouble(control2);
            }
            if (start != null)
            {
                shapeAttrs.Start = new Guid(start);
            }
            if (end != null)
            {
                shapeAttrs.End = new Guid(end);
            }
            if (source != null)
            {
                shapeAttrs.Source = source;
            }

            // Some local variables concerning the arguments
            ArrayList args           = new ArrayList();
            string    uniformArgtype = null;

            bool      isShape       = false;
            ArrayList shapeArgs     = new ArrayList();
            ArrayList strokeArgs    = new ArrayList();
            ArrayList substrokeArgs = new ArrayList();

            // Boolean to check if the current shape element is actually a Sketch.Shape
            if (!((string)shapeAttrs.Type).ToLower().Equals("stroke") &&
                !((string)shapeAttrs.Type).ToLower().Equals("substroke"))
            {
                isShape = true;
            }

            reader.Read();
            string argname = reader.Name;

            // Now read the subelements, until we get to the end of the shape
            while (reader.NodeType != XmlNodeType.EndElement)
            {
                if (argname.ToLower().Equals("arg"))
                {
                    string argtype = reader.GetAttribute("type");
                    string argid   = reader.ReadElementString();

                    // Set the argtype (must be the same for all regular shapes, such as Strokes and Substrokes)
                    if (uniformArgtype == null)
                    {
                        uniformArgtype = argtype;
                    }

                    // If the current element is not a Sketch.Shape
                    if (!isShape)
                    {
                        // Check to make sure that the argument is the same
                        if (uniformArgtype == argtype)
                        {
                            args.Add(new Guid(argid));
                        }
                        else
                        {
                            throw new ApplicationException("Invalid Format: Only abnormal Shapes are allowed to contain multiple arg types");
                        }
                    }
                    // If the current shape is a Sketch.Shape
                    else
                    {
                        // Add the argument to either a Substroke ArrayList or a Shape ArrayList
                        if (argtype.ToLower().Equals("stroke"))
                        {
                            strokeArgs.Add(new Guid(argid));
                        }
                        else if (argtype.ToLower().Equals("substroke"))
                        {
                            substrokeArgs.Add(new Guid(argid));
                        }
                        else
                        {
                            shapeArgs.Add(new Guid(argid));
                        }
                    }
                }

                // TODO: Handle aliases
                else if (argname.ToLower().Equals("alias"))
                {
                    string aliastype = reader.GetAttribute("type");
                    string aliasname = reader.GetAttribute("name");
                    string aliasid   = reader.ReadElementString();

                    //s.Aliases.Add(new Shape.Alias(aliastype, aliasname, aliasid));
                }
            }

            // If the shape is a Substroke
            if (((string)shapeAttrs.Type).ToLower().Equals("substroke"))
            {
                if (uniformArgtype.ToLower().Equals("point"))
                {
                    args.Insert(0, shapeAttrs);
                    this.substrokeToPointsHT.Add(shapeAttrs.Id, args);
                }
                else
                {
                    throw new ApplicationException("Invalid Format: Substrokes must be made of points");
                }
            }

            // If the shape is a Stroke
            else if (((string)shapeAttrs.Type).ToLower().Equals("stroke"))
            {
                // If the arguments are Points we need to create a corresponding Substroke
                if (uniformArgtype.ToLower().Equals("point"))
                {
                    Sketch.XmlStructs.XmlShapeAttrs substrokeAttrs = shapeAttrs.Clone();

                    // Create a new Substroke and link it to the Points in the hashtable
                    substrokeAttrs.Id   = System.Guid.NewGuid();
                    substrokeAttrs.Name = "substroke";
                    substrokeAttrs.Type = "Substroke";

                    args.Insert(0, substrokeAttrs);
                    this.substrokeToPointsHT.Add(substrokeAttrs.Id, args);

                    // Create a new ArrayList allowing the Stroke to point to the Substroke
                    ArrayList substrokeArray = new ArrayList();
                    substrokeArray.Add(shapeAttrs);
                    substrokeArray.Add(substrokeAttrs.Id);

                    this.strokeToSubstrokesHT.Add(shapeAttrs.Id, substrokeArray);
                }
                else if (uniformArgtype.ToLower().Equals("substroke"))
                {
                    args.Insert(0, shapeAttrs);
                    this.strokeToSubstrokesHT.Add(shapeAttrs.Id, args);
                }
                else
                {
                    throw new ApplicationException("Invalid Format: Strokes are required to have "
                                                   + "Substroke or Point arguments");
                }
            }

            // If the shape is a Sketch.Shape
            else
            {
                // Have the args ArrayList be of the format:
                args.Add(shapeAttrs);                           // ArrayList[0] = XML Attributes
                args.Add(shapeArgs);                            // ArrayList[1] = shape Ids
                args.Add(strokeArgs);                           // ArrayList[2] = Stroke Ids
                args.Add(substrokeArgs);                        // ArrayList[3] = Substroke Ids

                this.shapeToArgsHT.Add(shapeAttrs.Id, args);
            }

            // And consume the </shape>
            reader.ReadEndElement();
        }
Example #3
0
        /// <summary>
        /// Constructs a sketch from the given stream
        /// </summary>
        /// <param name="reader">The XML reader that will read in XML data</param>
        private void CreateSketch(XmlTextReader reader)
        {
            // Read all the data from the XML file
            this.ReadSketchData(reader);

            ArrayList shapes  = new ArrayList();
            ArrayList strokes = new ArrayList();

            Hashtable allStrokes    = new Hashtable();
            Hashtable allSubstrokes = new Hashtable();

            // Cycle through the Stroke Id's pulled from the file
            foreach (DictionaryEntry stroke in this.strokeToSubstrokesHT)
            {
                ArrayList substrokes = new ArrayList();

                // Retrive the XML Stroke info (first element in the ArrayList)
                ArrayList substrokeIds = (ArrayList)stroke.Value;
                Sketch.XmlStructs.XmlShapeAttrs strokeAttrs = (Sketch.XmlStructs.XmlShapeAttrs)substrokeIds[0];

                // Cycle through the Substroke Id's pulled from the file
                for (int i = 1; i < substrokeIds.Count; i++)
                {
                    // Retrive the XML Substroke info and Points from the corresponding Hashtables
                    ArrayList ptsArray = (ArrayList)this.substrokeToPointsHT[(System.Guid)substrokeIds[i]];
                    ArrayList points   = new ArrayList();

                    Sketch.XmlStructs.XmlShapeAttrs substrokeAttrs = (Sketch.XmlStructs.XmlShapeAttrs)ptsArray[0];

                    // Get all of the Points associated with the Substroke
                    for (int k = 1; k < ptsArray.Count; k++)
                    {
                        points.Add((Point)pointsHT[(System.Guid)ptsArray[k]]);
                    }

                    // Create a new Substroke and add it to the local Hashtable and Stroke ArrayList
                    Substroke newSubstroke = new Substroke(points, substrokeAttrs);

                    allSubstrokes.Add((System.Guid)newSubstroke.XmlAttrs.Id, newSubstroke);
                    substrokes.Add(newSubstroke);
                }

                // Add a new Stroke
                Stroke newStroke = new Stroke(substrokes, strokeAttrs);
                allStrokes.Add(newStroke.XmlAttrs.Id, newStroke);
                strokes.Add(newStroke);
            }

            // Cycle through the Shape Id's pulled from the file
            foreach (DictionaryEntry shape in this.shapeToArgsHT)
            {
                ArrayList shapeValue = (ArrayList)shape.Value;

                // Retrive the XML Stroke info (first element in the ArrayList)
                Sketch.XmlStructs.XmlShapeAttrs shapeAttrs = (Sketch.XmlStructs.XmlShapeAttrs)shapeValue[0];

                ArrayList shapeIds   = (ArrayList)shapeValue[1];
                ArrayList currShapes = new ArrayList();

                ArrayList substrokes = RecShape((System.Guid)shape.Key, allStrokes, allSubstrokes, out currShapes);

                // Add a new Shape
                shapes.Add(new Shape(currShapes, substrokes, shapeAttrs));
            }

            // Add the Shapes and Strokes to the Sketch
            sketch.AddShapes(shapes);
            sketch.AddStrokes(strokes);
        }
Example #4
0
        /// <summary>
        /// Reads the Shape XML attributes
        /// </summary>
        /// <param name="reader">XML text reader for the file</param>
        private void ReadShape(XmlTextReader reader)
        {
            // Required MIT XML attributes
            string type = reader.GetAttribute("type");
            string name = reader.GetAttribute("name");
            string id   = reader.GetAttribute("id");
            string time = reader.GetAttribute("time");

            XmlStructs.XmlShapeAttrs shapeAttrs = new XmlStructs.XmlShapeAttrs(new Guid(id));

            if ((type != null) && (id != null) && (time != null))
            {
                shapeAttrs.Type = type;
                shapeAttrs.Name = name;
                shapeAttrs.Time = Convert.ToUInt64(time);
            }
            else
            {
                string bad = "";
                if (type == null)
                {
                    bad += "Type ";
                }
                if (name == null)
                {
                    bad += "Name ";
                }
                if (id == null)
                {
                    bad += "ID ";
                }
                if (time == null)
                {
                    bad += "Time ";
                }

                throw new ApplicationException("Invalid Format: Shapes must contain a Type, Name, Id, and Time. Missing " + bad);
            }

            // Get the rest of the attributes, if they are there
            // We use temporary strings here so that we can check if the attribute is <null>
            string author         = reader.GetAttribute("author");
            string color          = reader.GetAttribute("color");
            string height         = reader.GetAttribute("height");
            string width          = reader.GetAttribute("width");
            string area           = reader.GetAttribute("area");
            string laysInk        = reader.GetAttribute("laysInk");
            string orientation    = reader.GetAttribute("orientation");
            string penTip         = reader.GetAttribute("penTip");
            string penWidth       = reader.GetAttribute("penWidth");
            string penHeight      = reader.GetAttribute("penHeight");
            string raster         = reader.GetAttribute("raster");
            string substrokeOf    = reader.GetAttribute("substrokeOf");
            string p1             = reader.GetAttribute("p1");
            string p2             = reader.GetAttribute("p2");
            string x              = reader.GetAttribute("x");
            string y              = reader.GetAttribute("y");
            string probability    = reader.GetAttribute("probability");
            string text           = reader.GetAttribute("text");
            string leftx          = reader.GetAttribute("leftx");
            string topy           = reader.GetAttribute("topy");
            string control1       = reader.GetAttribute("control1");
            string control2       = reader.GetAttribute("control2");
            string start          = reader.GetAttribute("start");
            string end            = reader.GetAttribute("end");
            string source         = reader.GetAttribute("source");
            string classification = reader.GetAttribute("classification");

            if (author != null)
            {
                shapeAttrs.Author = new Guid(author);
            }
            if (color != null)
            {
                shapeAttrs.Color = Convert.ToInt32(color);
            }
            if (height != null)
            {
                shapeAttrs.Height = Convert.ToSingle(height);
            }
            if (width != null)
            {
                shapeAttrs.Width = Convert.ToSingle(width);
            }
            if (area != null)
            {
                shapeAttrs.Area = Convert.ToSingle(area);              // Convert.ToDouble(area);
            }
            if (laysInk != null)
            {
                shapeAttrs.LaysInk = Convert.ToBoolean(laysInk);
            }
            if (orientation != null)
            {
                shapeAttrs.Orientation = Convert.ToSingle(orientation);
            }
            else
            {
                shapeAttrs.Orientation = 0;  // arbitrary set orientation to 0.
            }
            if (penTip != null)
            {
                shapeAttrs.PenTip = penTip;
            }
            if (penWidth != null)
            {
                shapeAttrs.PenWidth = Convert.ToSingle(penWidth);
            }
            if (penHeight != null)
            {
                shapeAttrs.PenHeight = Convert.ToSingle(penHeight);
            }
            if (raster != null)
            {
                shapeAttrs.Raster = raster;
            }
            if (substrokeOf != null)
            {
                shapeAttrs.SubstrokeOf = new Guid(substrokeOf);
            }
            if (p1 != null)
            {
                shapeAttrs.P1 = p1;
            }
            if (p2 != null)
            {
                shapeAttrs.P2 = p2;
            }
            if (x != null)
            {
                shapeAttrs.X = Convert.ToSingle(x);
            }
            if (y != null)
            {
                shapeAttrs.Y = Convert.ToSingle(y);
            }
            if (probability != null)
            {
                shapeAttrs.Probability = Convert.ToSingle(probability);
            }
            if (text != null)
            {
                shapeAttrs.Text = text;
            }
            if (leftx != null)
            {
                shapeAttrs.LeftX = Convert.ToSingle(leftx);
            }
            if (topy != null)
            {
                shapeAttrs.TopY = Convert.ToSingle(topy);
            }
            if (control1 != null)
            {
                shapeAttrs.Control1 = control1;
            }
            if (control2 != null)
            {
                shapeAttrs.Control2 = control2;
            }
            if (start != null)
            {
                shapeAttrs.Start = new Guid(start);
            }
            if (end != null)
            {
                shapeAttrs.End = new Guid(end);
            }
            if (source != null)
            {
                shapeAttrs.Source = source;
            }
            if (classification != null)
            {
                shapeAttrs.Classification = classification;
            }

            // Some local variables concerning the arguments
            List <Guid> args           = new List <Guid>();
            string      uniformArgtype = null;

            bool        isShape       = false;
            List <Guid> shapeArgs     = new List <Guid>();
            List <Guid> strokeArgs    = new List <Guid>();
            List <Guid> substrokeArgs = new List <Guid>();
            List <Guid> neighborArgs  = new List <Guid>();

            // Boolean to check if the current shape element is actually a Sketch.Shape
            if (!shapeAttrs.Type.ToLower().Equals("stroke") &&
                !shapeAttrs.Type.ToLower().Equals("substroke"))
            {
                isShape = true;
            }

            reader.Read();
            string argname   = reader.Name;
            int    tagNumber = int.MinValue;

            // Now read the subelements, until we get to the end of the shape
            while (reader.NodeType != XmlNodeType.EndElement)
            {
                argname = reader.Name;
                if (argname.ToLower().Equals("arg"))
                {
                    string argtype = reader.GetAttribute("type");
                    string argid   = reader.ReadElementString();

                    // Set the argtype (must be the same for all regular shapes, such as Strokes and Substrokes)
                    if (uniformArgtype == null)
                    {
                        uniformArgtype = argtype;
                    }

                    // If the current element is not a Sketch.Shape
                    if (!isShape)
                    {
                        // Check to make sure that the argument is the same
                        if (uniformArgtype == argtype)
                        {
                            args.Add(new Guid(argid));
                        }
                        else
                        {
                            throw new ApplicationException("Invalid Format: Only abnormal Shapes are allowed to contain multiple arg types");
                        }
                    }
                    // If the current shape is a Sketch.Shape
                    else
                    {
                        // Add the argument to either a Substroke ArrayList or a Shape ArrayList
                        if (argtype.ToLower().Equals("stroke"))
                        {
                            strokeArgs.Add(new Guid(argid));
                        }
                        else if (argtype.ToLower().Equals("substroke"))
                        {
                            substrokeArgs.Add(new Guid(argid));
                        }
                        else if (argtype.ToLower().Equals("shape"))
                        {
                            shapeArgs.Add(new Guid(argid));
                        }
                        else
                        {
                            neighborArgs.Add(new Guid(argid));
                        }
                    }
                }

                // TODO: Handle aliases
                else if (argname.ToLower().Equals("alias"))
                {
                    string aliastype = reader.GetAttribute("type");
                    string aliasname = reader.GetAttribute("name");
                    string aliasid   = reader.ReadElementString();

                    //s.Aliases.Add(new Shape.Alias(aliastype, aliasname, aliasid));
                }
                else if (argname.ToLower().Equals("neighbor"))
                {
                    // CODE TO WORK WITH NEIGHBORS
                    string argtype = reader.GetAttribute("type");
                    string argid   = reader.ReadElementString();
                    neighborArgs.Add(new Guid(argid));
                }
                else if (argname.ToLower().Equals("subcircuit"))
                {
                    tagNumber = Convert.ToInt32(reader.GetAttribute("tagNumber"));
                    reader.Read();
                }
            }

            // If the shape is a Substroke
            if (shapeAttrs.Type.ToLower().Equals("substroke"))
            {
                if (uniformArgtype.ToLower().Equals("point"))
                {
                    this.substrokeToAttrs.Add(shapeAttrs.Id, shapeAttrs);
                    this.substrokeToPointIDs.Add(shapeAttrs.Id, args);
                }
                else
                {
                    throw new ApplicationException("Invalid Format: Substrokes must be made of points");
                }
            }

            // If the shape is a Stroke
            else if (shapeAttrs.Type.ToLower().Equals("stroke"))
            {
                // If the arguments are Points we need to create a corresponding Substroke
                if (uniformArgtype.ToLower().Equals("point"))
                {
                    Sketch.XmlStructs.XmlShapeAttrs substrokeAttrs = shapeAttrs.Clone();

                    // Create a new Substroke and link it to the Points in the hashtable
                    substrokeAttrs.Name = "substroke";
                    substrokeAttrs.Type = "Substroke";

                    this.substrokeToAttrs.Add(substrokeAttrs.Id, substrokeAttrs);
                    this.substrokeToPointIDs.Add(substrokeAttrs.Id, args);

                    List <Guid> s = new List <Guid>(1);
                    s.Add(substrokeAttrs.Id);

                    this.strokeToAttrs.Add(shapeAttrs.Id, shapeAttrs);
                    this.strokeToSubstrokeIDs.Add(shapeAttrs.Id, s);
                }
                else if (uniformArgtype.ToLower().Equals("substroke"))
                {
                    this.strokeToAttrs.Add(shapeAttrs.Id, shapeAttrs);
                    this.strokeToSubstrokeIDs.Add(shapeAttrs.Id, args);
                }
                else
                {
                    throw new ApplicationException("Invalid Format: Strokes are required to have "
                                                   + "Substroke or Point arguments");
                }
            }

            // If the shape is a Sketch.Shape
            else
            {
                this.shapeToAttrs.Add(shapeAttrs.Id, shapeAttrs);
                this.shapeToShapeIDs.Add(shapeAttrs.Id, shapeArgs);
                this.shapeToStrokeIDs.Add(shapeAttrs.Id, strokeArgs);
                this.shapeToSubstrokeIDs.Add(shapeAttrs.Id, substrokeArgs);
                this.shapeToNeighborIDs.Add(shapeAttrs.Id, neighborArgs);
                if (tagNumber != int.MinValue)
                {
                    this.shapeToTagNumber.Add(shapeAttrs.Id, tagNumber);
                }
            }

            // And consume the </shape>
            reader.ReadEndElement();
        }