WPF, convert Path.DataProperty objects to a segment

I was wondering if there is a tool to convert the path data, for example "M 0 0 10 10", into its equivalent line / curve segment code.

I am currently 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 seems to me that calling XamlParser is much slower than explicitly creating line segments. However, converting a large number of paths manually is very tedious.

+4
source share
2 answers

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

+4
source

There is nothing built in to generate C # or VB code from a minimal geometry language, but you can create it like this:

  • Modify the C # or VB code to create a PathGeometry.
  • Call PathFigureCollection.Parse in the path string. This will return an instance of PathFigureCollection .
  • Iterate over a PathFigureCollection. For each digit:
    • Write the C # or VB code for the new PathFigure object and add it to the PathGeometry.Figures collection.
    • Iterating over a collection of segments. For each segment, analyze its type and emit type-specific code to create the appropriate PathSegment type, set its properties and add it to the current PathFigure.

Whether this is more or less tedious than converting paths manually, this is something you can decide though ... it probably depends on how many different segments you need to process (i.e. how many different kinds of segments appear in your path lines), since you have to write separate code for LineSegments, ArcSegments, etc.

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

+1
source

Source: https://habr.com/ru/post/1303981/


All Articles