user275587
user275587

Reputation: 690

WPF, convert Path.DataProperty to Segment objects

I was wondering if there was a tool to convert a path data like "M 0 0 l 10 10" to it's equivalent line/curve segment code.

Currently I'm using:

string pathXaml = "<Path xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\" Data=\"M 0 0 l 10 10\"/>";
Path path = (Path)System.Windows.Markup.XamlReader.Load(pathXaml);

It appears to me that calling XamlParser is much slower than explicitly creating the line segments. However converting a lot of paths by hand is very tedious.

Upvotes: 4

Views: 1964

Answers (2)

user275587
user275587

Reputation: 690

This program will do the conversion: http://stringtopathgeometry.codeplex.com/

Upvotes: 4

itowlson
itowlson

Reputation: 74842

There's nothing built-in to generate C# or VB code from the geometry minilanguage, but you could create one as follows:

  • Emit C# or VB code for new-ing up a PathGeometry.
  • Call PathFigureCollection.Parse on your path string. This will return a PathFigureCollection instance.
  • Iterate over the PathFigureCollection. For each figure:
    • Write out C# or VB code for new-ing a PathFigure object and adding it to the PathGeometry.Figures collection.
    • Iterate over the figure's Segments collection. For each segment, analyse its type and emit type-dependent code for new-ing up the appropriate kind of PathSegment, setting its properties and adding it to the current PathFigure.

Whether this is more or less tedious than converting the paths by hand is something only you can decide, though... it probably depends on how many different kinds of segment you need to handle (i.e. how many different kinds of segment appear in your path strings), since you will have to write separate code for LineSegments, ArcSegments, etc.

EDIT: Thanks to Anvaka in comments for simplifying the original answer by drawing my attention to PathFigureCollection.Parse.

Upvotes: 2

Related Questions