예제 #1
0
        ////////////////////////////////////////////////////////////////////////////////////////////
        private bool LoadAnnotationsFile(string commentsFilePath, List <SvgElement> elements, out List <AnnotationElement> annotations)
        {
            // Load the XML document contents from the data
            byte[]      schematicFileData = File.ReadAllBytes(commentsFilePath);
            XmlDocument document          = new XmlDocument();

            using (MemoryStream memoryStream = new MemoryStream(schematicFileData))
            {
                using (XmlReader xmlReader = XmlReader.Create(memoryStream))
                {
                    document.Load(xmlReader);
                }
            }

            // Obtain and validate the root node of the document
            XmlElement root = document.DocumentElement;

            if (!String.Equals(root.Name, "SchematicAnnotations", StringComparison.Ordinal))
            {
                annotations = null;
                return(false);
            }

            // Load the comments
            XmlElement commentsGroupNode = root.ChildNodes.OfType <XmlElement>().FirstOrDefault((x) => String.Equals(x.Name, "Comments", StringComparison.OrdinalIgnoreCase));

            if (commentsGroupNode != null)
            {
                foreach (XmlElement childElement in commentsGroupNode.ChildNodes.OfType <XmlElement>().Where((x) => String.Equals(x.Name, "Comment", StringComparison.OrdinalIgnoreCase)))
                {
                    string nodeID  = childElement.GetAttribute("NodeID");
                    string comment = childElement.InnerText;

                    SvgElement element = elements.FirstOrDefault((x) => String.Equals(x.ID, nodeID, StringComparison.OrdinalIgnoreCase));
                    if (element != null)
                    {
                        element.Comments = comment;
                    }
                }
            }

            // Load the annotations
            annotations = new List <AnnotationElement>();
            XmlElement annotationsGroupNode = root.ChildNodes.OfType <XmlElement>().FirstOrDefault((x) => String.Equals(x.Name, "Annotations", StringComparison.OrdinalIgnoreCase));

            if (annotationsGroupNode != null)
            {
                foreach (XmlElement childElement in annotationsGroupNode.ChildNodes.OfType <XmlElement>().Where((x) => String.Equals(x.Name, "Annotation", StringComparison.OrdinalIgnoreCase)))
                {
                    string name     = childElement.GetAttribute("Name");
                    string comment  = childElement.InnerText;
                    double width    = Convert.ToDouble(childElement.GetAttribute("Width"));
                    double height   = Convert.ToDouble(childElement.GetAttribute("Height"));
                    double posX     = Convert.ToDouble(childElement.GetAttribute("PosX"));
                    double posY     = Convert.ToDouble(childElement.GetAttribute("PosY"));
                    double rotation = Convert.ToDouble(childElement.GetAttribute("Rotation"));
                    int    layer    = Convert.ToInt32(childElement.GetAttribute("Layer"));

                    AnnotationElement element = new AnnotationElement()
                    {
                        Name = name, Description = comment, Width = width, Height = height, PosX = posX, PosY = posY, Angle = rotation, Layer = layer
                    };
                    element.Description = comment;
                    annotations.Add(element);
                }
            }
            return(true);
        }
예제 #2
0
        ////////////////////////////////////////////////////////////////////////////////////////////
        private bool LoadSchematicFile(string schematicFilePath, out List <SvgElement> elements, out double schematicWidth, out double schematicHeight)
        {
            elements        = new List <SvgElement>();
            schematicWidth  = 0;
            schematicHeight = 0;

            string[] colorNames =
            {
                "aqua",        "aquamarine",   "blue",          "blueviolet",     "brown",           "burlywood",         "cadetblue",       "chartreuse",      "chocolate",     "coral",         "cornflowerblue",   "crimson",   "cyan",
                "darkblue",    "darkcyan",     "darkgoldenrod", "darkgray",       "darkgreen",       "darkkhaki",         "darkmagenta",     "darkolivegreen",  "darkorange",    "darkorchid",    "darkred",
                "darksalmon",  "darkseagreen", "darkslateblue", "darkslategray",  "darkturquoise",   "darkviolet",        "deeppink",        "deepskyblue",     "dimgray",       "dodgerblue",    "firebrick",
                "forestgreen", "fuchsia",      "gold",          "goldenrod",      "gray",            "green",             "greenyellow",     "hotpink",         "indianred",     "indigo",        "lawngreen",        "lightblue", "lightcoral",
                "lightgreen",  "lightpink",    "lightsalmon",   "lightseagreen",  "lightskyblue",    "lightsteelblue",    "lime",            "limegreen",       "magenta",       "maroon",        "mediumaquamarine",
                "mediumblue",  "mediumorchid", "mediumpurple",  "mediumseagreen", "mediumslateblue", "mediumspringgreen", "mediumturquoise", "mediumvioletred", "midnightblue",
                "navy",        "olive",        "olivedrab",     "orange",         "orangered",       "orchid",            "palegoldenrod",   "palegreen",       "paleturquoise", "palevioletred", "peachpuff",        "peru",      "pink",
                "plum",        "powderblue",   "purple",        "red",            "rosybrown",       "royalblue",         "saddlebrown",     "salmon",          "sandybrown",    "seagreen",      "sienna",           "silver",    "skyblue",   "slateblue",
                "slategray",   "springgreen",  "steelblue",     "tan",            "teal",            "thistle",           "tomato",          "turquoise",       "violet",        "wheat",         "yellowgreen"
            };
            Type         brushesTypeInfo       = typeof(Brushes);
            var          brushesTypeProperties = brushesTypeInfo.GetProperties();
            List <Brush> brushes = new List <Brush>();

            foreach (string colorName in colorNames)
            {
                PropertyInfo brushPropertyInfo = brushesTypeProperties.FirstOrDefault((x) => String.Equals(x.Name, colorName, StringComparison.OrdinalIgnoreCase));
                if (brushPropertyInfo == null)
                {
                    continue;
                }
                Brush brush = (Brush)brushPropertyInfo.GetValue(null);
                brushes.Add(brush);
            }
            Random rand = new Random(42);

            // Load the XML document contents from the data
            byte[]      schematicFileData = File.ReadAllBytes(schematicFilePath);
            XmlDocument document          = new XmlDocument();

            using (MemoryStream memoryStream = new MemoryStream(schematicFileData))
            {
                using (XmlReader xmlReader = XmlReader.Create(memoryStream))
                {
                    document.Load(xmlReader);
                }
            }

            // Obtain and validate the root node of the document
            XmlElement root = document.DocumentElement;

            if (!String.Equals(root.Name, "svg", StringComparison.Ordinal))
            {
                return(false);
            }

            schematicWidth  = Convert.ToDouble(root.GetAttribute("width"));
            schematicHeight = Convert.ToDouble(root.GetAttribute("height"));

            // If we have an entry for the target project in the project cache, populate the full
            // info now, otherwise populate a new object.
            List <SvgElement> svgData = new List <SvgElement>();

            foreach (XmlElement childElement in root.ChildNodes.OfType <XmlElement>())
            {
                // Ensure this child element is a supported type
                if (!String.Equals(childElement.Name, "g", StringComparison.Ordinal))
                {
                    return(false);
                }

                SvgElement element = new SvgElement();
                element.ID = childElement.GetAttribute("id");

                Brush randomColor = brushes[rand.Next() % brushes.Count];

                string lastPathData = String.Empty;
                Dictionary <string, string> combinedPaths = new Dictionary <string, string>();
                foreach (XmlElement groupNodeChildElement in childElement.ChildNodes.OfType <XmlElement>())
                {
                    if (String.Equals(groupNodeChildElement.Name, "desc", StringComparison.Ordinal))
                    {
                        element.Description = groupNodeChildElement.InnerText;
                    }
                    else if (String.Equals(groupNodeChildElement.Name, "rect", StringComparison.Ordinal))
                    {
                        RectangleElement rectangleElement = new RectangleElement();
                        rectangleElement.Width  = Double.Parse(groupNodeChildElement.GetAttribute("width"));
                        rectangleElement.Height = Double.Parse(groupNodeChildElement.GetAttribute("height"));
                        rectangleElement.PosX   = Double.Parse(groupNodeChildElement.GetAttribute("x"));
                        rectangleElement.PosY   = Double.Parse(groupNodeChildElement.GetAttribute("y"));
                        string style = groupNodeChildElement.GetAttribute("style");
                        if (!style.Contains("stroke:none"))
                        {
                            rectangleElement.Stroke = randomColor;
                        }
                        if (style.Contains("fill:"))
                        {
                            rectangleElement.Fill = randomColor;
                        }
                        element.RectangleElements.Add(rectangleElement);
                    }
                    else if (String.Equals(groupNodeChildElement.Name, "text", StringComparison.Ordinal))
                    {
                        XmlElement textChildElement = groupNodeChildElement.ChildNodes.OfType <XmlElement>().FirstOrDefault((x) => String.Equals(x.Name, "tspan", StringComparison.OrdinalIgnoreCase));
                        if (textChildElement != null)
                        {
                            element.TextData = textChildElement.InnerText;
                            element.TextPos  = new Point(Convert.ToDouble(groupNodeChildElement.GetAttribute("x")), Convert.ToDouble(groupNodeChildElement.GetAttribute("y")));
                        }
                    }
                    else if (String.Equals(groupNodeChildElement.Name, "path", StringComparison.Ordinal))
                    {
                        string style    = groupNodeChildElement.GetAttribute("style");
                        string pathData = groupNodeChildElement.GetAttribute("d");
                        // The generated schematic sometimes contains duplicate connection markers listed directly
                        // after each other. When we combine the path elements together, the overlapping areas
                        // cancel out, causing these markers to become invisible. We strip the duplicate markers
                        // here to resolve the issue.
                        if (!String.Equals(pathData, lastPathData, StringComparison.Ordinal))
                        {
                            lastPathData = pathData;
                            string combinedPath;
                            if (!combinedPaths.TryGetValue(style, out combinedPath))
                            {
                                combinedPaths.Add(style, pathData);
                            }
                            else
                            {
                                if ((combinedPath.Length > 0) && (pathData.StartsWith("m ")))
                                {
                                    var pathDataSplit = pathData.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                                    if (!pathDataSplit[1].Contains(','))
                                    {
                                        pathData = " M " + pathDataSplit[1] + " " + pathDataSplit[2] + " m 0 0 " + String.Join(" ", pathDataSplit.Skip(3));
                                    }
                                    else
                                    {
                                        pathData = " M " + pathDataSplit[1] + " m 0 0 " + String.Join(" ", pathDataSplit.Skip(2));
                                    }
                                }
                                combinedPath        += pathData;
                                combinedPaths[style] = combinedPath;
                            }
                        }
                    }
                    else
                    {
                        return(false);
                    }
                }

                foreach (var combinedPathEntry in combinedPaths)
                {
                    string      style          = combinedPathEntry.Key;
                    string      combinedPath   = combinedPathEntry.Value;
                    PathElement pathElement    = new PathElement();
                    Geometry    geometryObject = StreamGeometry.Parse(combinedPath);

                    pathElement.PathData = geometryObject;
                    if (!style.Contains("stroke:none"))
                    {
                        pathElement.Stroke = randomColor;
                    }
                    if (style.Contains("fill:"))
                    {
                        pathElement.Fill = Brushes.Black;
                    }
                    element.PathElements.Add(pathElement);
                }

                elements.Add(element);
            }

            return(true);
        }