Can you get the path markup syntax for a CombinedGeometry or a GeometryGroup object?

Viewed 143

When dealing with a PathGeometry object, if you call ToString() you get back the 'path markup syntax' representing the paths. However, the same is not true for GeometryGroup and CombinedGeometry.

Consider the following code:

var hBar  = Geometry.Parse("F1M10,20L40,20L40,30L10,30z");
var vBar  = Geometry.Parse("F1M20,10L30,10L30,40L20,40z");
var cross = Geometry.Parse("F1M20,10L20,10L30,10L30,20L40,20L40,30L30,30L30,40L20,40L20,30L10,30L10,20L20,20z");

var combined = new GeometryGroup(){ FillRule = FillRule.Nonzero };
    combined.Children.Add(hBar);
    combined.Children.Add(vBar);

var union = new CombinedGeometry(GeometryCombineMode.Union, HBar.Geometry, VBar.Geometry);

Console.WriteLine($"hBar     : {hBar}");
Console.WriteLine($"vBar     : {vBar}");
Console.WriteLine($"cross    : {cross}");
Console.WriteLine($"combined : {combined}");
Console.WriteLine($"union    : {union}");

Here are the images: enter image description here

The output from the code is as follows:

hBar     : F1M10,20L40,20 40,30 10,30z
vBar     : F1M20,10L30,10 30,40 20,40z
cross    : F1M20,10L20,10 30,10 30,20 40,20 40,30 30,30 30,40 20,40 20,30 10,30 10,20 20,20z
combined : System.Windows.Media.GeometryGroup
union    : System.Windows.Media.CombinedGeometry

What I'm hoping is to get the resulting path markup syntax for the CombinedGeometry and GeometryGroup objects. For instance, specifically in the case of 'union', I'm hoping to get back something similar to the cross entry.

(* Note: I don't care about the order of the vertices or the drawing direction as long as it shows as an identical shape/path.)

Here's the Microsoft page all about Path Markup Syntax. This is the format I'm trying to get.

So is this possible?

2 Answers

Yes, just flatten the geometry. Note however that this returns a polygon-representation of your geometry.

union.GetFlattenedPathGeometry().ToString()

To maintain the original curves, there seem to be many built-in variations for for this and I will leave them here for future readers.

PathGeometry.CreateFromGeometry(combined).ToString()
PathGeometry.CreateFromGeometry(union).ToString()
Geometry.Combine(hBar, vBar, GeometryCombineMode.Union, null).ToString()

Perhaps I am missing the point but getting the paths back is quite simple...

foreach(var child in combined.Children)
{
    Console.WriteLine($" child of combined = {child.ToString()}");
}
Console.WriteLine($" union.Geometry1 = {union.Geometry1}");
Console.WriteLine($" union.Geometry2 = {union.Geometry2}");

Which produces...

child of combined = F1M10,20L40,20 40,30 10,30z
child of combined = F1M20,10L30,10 30,40 20,40z
union.Geometry1 = F1M10,20L40,20 40,30 10,30z
union.Geometry2 = F1M20,10L30,10 30,40 20,40z
Related