3

I'm looking for conversion between path markup and Geometry. I found a good post showing how to get the Geometry from path markup:

Path Markup Syntax to Geometry

string pathMarkup = "M 100,200 C 100,25 400,350 400,175 H 280";
Geometry myGeometry = Geometry.Parse(pathMarkup);

Geometry to Path Markup Syntax

Now what if I want to get the path markup from the existing geometry?

Geometry myGeometry = //some geometry
string pathMarkup = ??

Any idea how to convert a geometry to its equivalent path markup?

Community
  • 1
  • 1
Hossein Narimani Rad
  • 31,361
  • 18
  • 86
  • 116

2 Answers2

4

What about

Geometry myGeometry = //some geometry
string pathMarkup = myGeometry.ToString();
Hamlet Hakobyan
  • 32,965
  • 6
  • 52
  • 68
  • This doesn't appear to work for `CombinedGeometry` or `GeometryGroup` objects, both which are `Geometry` subclasses. Anyone know how to achieve that? – Mark A. Donohoe Nov 16 '20 at 04:05
3

Expanding on Hamlet Hakobyan's answer, unfortunately the ToString() approach only works with Path Geometries. So in order to apply this generically to all Geometry types:

Geometry myGeometry = PathGeometry.Parse("M 8, 0 L 2,25 16,25 Z");
string pathString = myGeometry.ToString(); // Works only for PathGeometry

GeometryGroup geomGroup = new GeometryGroup();
geomGroup.Children.Add(myGeometry);
geomGroup.Transform = myTransform;

string pathString = PathGeometry.CreateFromGeometry(geomGroup).ToString();
Community
  • 1
  • 1
Ben
  • 3,241
  • 4
  • 35
  • 49