Esempio n. 1
0
        private void LoadSVG(string fileName)
        {
            PathList.Clear();
            var SvgDoc = new XmlDocument();

            SvgDoc.Load(fileName);
            var xmlnsManager = new XmlNamespaceManager(SvgDoc.NameTable);

            xmlnsManager.AddNamespace("global", SvgDoc.DocumentElement.NamespaceURI);
            if (SvgDoc.SelectNodes("/global:svg", xmlnsManager).Count == 0)
            {
                System.Windows.MessageBox.Show("No SVG tag was found");
                return;
            }

            SvgWidth  = GetAttributeValue(SvgDoc.DocumentElement, "width", "512");
            SvgHeight = GetAttributeValue(SvgDoc.DocumentElement, "height", "512");

            var pathes = SvgDoc.SelectNodes("//global:path", xmlnsManager);

            int TagNum = 0;

            foreach (XmlNode pathNode in pathes)
            {
                //========================= Load Path Data=========================//
                string AtrrString = GetAttributeValue(pathNode, "d", null);
                if (string.IsNullOrEmpty(AtrrString))
                {
                    System.Windows.MessageBox.Show("Path node doesnt have a `d` attribute");
                }

                PathModel pathModel = new PathModel
                {
                    Data = AtrrString,
                    Tag  = TagNum,
                };
                ++TagNum;

                //========================= Load Path Style=========================//
                var StyleParser = new StyleParser(GetAttributeValue(pathNode, "style"));
                pathModel.Fill            = StyleParser.String("fill", "Transparent");
                pathModel.Stroke          = StyleParser.String("stroke", "Black");
                pathModel.StrokeThickness = StyleParser.String("stroke-width", "1");
                PathList.Add(new PathViewModel(pathModel));
            }
        }
Esempio n. 2
0
        private void loadSVG(string fileName)
        {
            PathList.Clear();
            XmlDocument SvgDoc = new XmlDocument();

            SvgDoc.Load(fileName);
            XmlNamespaceManager xmlnsManager = new XmlNamespaceManager(SvgDoc.NameTable);

            xmlnsManager.AddNamespace("global", SvgDoc.DocumentElement.NamespaceURI);
            string      GXpath          = "/global:svg/global:g";
            XmlNodeList currentNodeList = SvgDoc.SelectNodes(GXpath, xmlnsManager);

            SvgWidth  = getDoubleVal(Attribute(SvgDoc.DocumentElement, "width", "1"));
            SvgHeight = getDoubleVal(Attribute(SvgDoc.DocumentElement, "height", "1"));

            while (currentNodeList.Count > 0)
            {
                int TagNum = 0;
                foreach (XmlNode GNode in currentNodeList)
                {
                    XmlNodeList pathsNodes = GNode.SelectNodes("./global:path", xmlnsManager);
                    if (pathsNodes.Count > 0)
                    {
                        foreach (XmlNode pathNode in pathsNodes)
                        {
                            try
                            {
                                //========================= Load Path Data=========================//
                                PathViewModel pathModel = new PathViewModel(new PathModel());
                                pathModel.Data = Attribute(pathNode, "d");

                                //========================= Load Path Style=========================//
                                string AtrrString = null;
                                if ((AtrrString = Attribute(pathNode, "style", null)) != null)    // inkescape style
                                {
                                    StyleParser StyleParser = new StyleParser(AtrrString);
                                    pathModel.Fill            = ColorToBrush(StyleParser.Color("fill"));
                                    pathModel.Stroke          = ColorToBrush(StyleParser.Color("stroke"));
                                    pathModel.StrokeThickness = StyleParser.Size("stroke-width");
                                    pathModel.Tag             = TagNum;
                                }
                                else
                                {
                                    pathModel.Fill            = ColorToBrush(getcolor(Attribute(pathNode, "fill", "none")));
                                    pathModel.Stroke          = ColorToBrush(getcolor(Attribute(pathNode, "stroke", "none")));
                                    pathModel.StrokeThickness = getPenSize(Attribute(pathNode, "stroke-width"));
                                    pathModel.Tag             = TagNum;
                                }

                                PathList.Add(pathModel);
                                TagNum++;
                            }
                            catch (Exception ex)
                            {
                                System.Windows.MessageBox.Show("Error: While Loading SVG. Original error: " + ex.Message);
                            }
                        }
                    }
                }
                GXpath          = GXpath + "/global:g";
                currentNodeList = SvgDoc.SelectNodes(GXpath, xmlnsManager);
            }
        }