Advertisement
GeneralGDA

SVG export example

Jul 4th, 2019
358
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.83 KB | None | 0 0
  1. private static readonly XNamespace SvgNamespace = "http://www.w3.org/2000/svg";
  2.  
  3. [Pure]
  4. private static string Svgify(double value)
  5. {
  6.     return value.ToString("0.##", CultureInfo.InvariantCulture);
  7. }
  8.  
  9. [Pure]
  10. private static XElement Tag(string name, params (string Name, string Value)[] attributes)
  11. {
  12.     return new XElement(SvgNamespace + name, ToXmlAttributes(attributes));
  13. }
  14.  
  15. [Pure]
  16. private static XElement Tag(string name, string content, params (string Name, string Value)[] attributes)
  17. {
  18.     return new XElement(SvgNamespace + name, content, ToXmlAttributes(attributes));
  19. }
  20.  
  21. [Pure]
  22. private static IEnumerable<XAttribute> ToXmlAttributes([NotNull] (string Name, string Value)[] attributes)
  23. {
  24.     return from attribute in attributes select new XAttribute(attribute.Name, attribute.Value);
  25. }
  26.  
  27. [UsedForDebug]
  28. internal static void SaveToSvg([NotNull] this Skeleton self, [NotNull] string path)
  29. {
  30.     ThrowIf.Argument.IsNull(self, nameof(self));
  31.     ThrowIf.Argument.IsNull(path, nameof(path));
  32.  
  33.     var svg = Tag("svg", ("viewBox", "0 0 3000 3000"));
  34.  
  35.     foreach (var bone in self.Bones)
  36.     {
  37.         var points = new StringBuilder();
  38.        
  39.         foreach (var vertex in bone.Vertices)
  40.         {
  41.             points.Append($"{Svgify(vertex.Position.X)},{Svgify(vertex.Position.Y)} ");
  42.         }
  43.  
  44.         svg.Add
  45.         (
  46.             Tag("polyline", ("points", points.ToString()), ("fill", "none"), ("stroke", "black"))
  47.         );
  48.     }
  49.  
  50.     var sign = +1;
  51.     var boneCounter = 0;
  52.     foreach (var bone in self.Bones)
  53.     {
  54.         var vertexCounter = 0;
  55.         foreach (var vertex in bone.Vertices)
  56.         {
  57.             var positionX = vertex.Position.X;
  58.             var positionY = vertex.Position.Y;
  59.  
  60.             {
  61.                 var x = Svgify(positionX);
  62.                 var y = Svgify(positionY);
  63.  
  64.                 svg.Add
  65.                 (
  66.                     Tag("circle", ("cx", x), ("cy", y), ("r", "5"), ("stroke", "red"))
  67.                 );
  68.             }
  69.  
  70.             {
  71.                 var x = Svgify(positionX + sign * 10);
  72.                 var y = Svgify(positionY + sign * 10);
  73.  
  74.                 svg.Add
  75.                 (
  76.                     Tag("text", vertexCounter.ToString(), ("x", x), ("y", y), ("style", "fill:green; font-size:10px;"))
  77.                 );
  78.                 vertexCounter++;
  79.             }
  80.         }
  81.  
  82.         var boneStartX = Svgify(bone.Vertices[0].Position.X - sign * 20);
  83.         var boneStartY = Svgify(bone.Vertices[0].Position.Y - sign * 20);
  84.  
  85.         svg.Add
  86.         (
  87.             Tag("text", boneCounter.ToString(), ("x", boneStartX), ("y", boneStartY), ("style", "fill:blue; font-size:10px;"))
  88.         );
  89.         boneCounter++;
  90.  
  91.         sign *= -1;
  92.     }
  93.  
  94.     using (var xmlWriter = XmlWriter.Create(path))
  95.     {
  96.         svg.Save(xmlWriter);
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement