Vahid
Vahid

Reputation: 5454

Get the point coordinates of a CombinedGeometry

I'm using the code below to exclude one geometry from a CombinedGeometry

enter image description here

and get the final geometry as below:

enter image description here

I was wondering is there a way to get the geometry definition of this final result? Something like a point collection or something like that that helps me redraw this with different properties later or even write the point coordinates to a file?

<Window x:Class="Combine.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Canvas Width="300" Height="300">
        <Path Stroke="Black" StrokeThickness="1" Fill="Transparent">
            <Path.Data>

                <CombinedGeometry GeometryCombineMode="Exclude">
                    <CombinedGeometry.Geometry1>
                        <RectangleGeometry Rect="40,40, 200, 50" />
                    </CombinedGeometry.Geometry1>
                    <CombinedGeometry.Geometry2>
                        <RectangleGeometry Rect="100,0, 50, 200" />
                    </CombinedGeometry.Geometry2>
                </CombinedGeometry>
            </Path.Data>
        </Path>
    </Canvas>
</Window>

Upvotes: 1

Views: 1575

Answers (2)

Patrick
Patrick

Reputation: 11

You could also use PathGeometry.Combine() instead of the CombinedGeometry. The resulting PathGeometry has figures and segments. PS: You can also create a PathGeometry from any other geometry. Have a look at the static constructor PathGeometry.CreateFromGeometry(). PathGeometry result = PathGeometry.Combine(g1, g2, GeometryCombineMode.Exclude, null);

Upvotes: 1

Dmitry
Dmitry

Reputation: 2062

You can try to use combinedGeometry.GetFlattenedPathGeometry(); method. This will return a PathGeometry object. You can then either convert it to string and write to file, either run through the figurs that are contained in the PathGeometry and use the coordinates.

PathGeometry geometry = combinedGeometry.GetFlattenedPathGeometry();

Console.WriteLine(geometry.ToString());


foreach (PathFigure figure in geometry.Figures)
{
     Console.WriteLine(figure.StartPoint);
     foreach (PathSegment segment in figure.Segments)
     {
         foreach (Point point in ((PolyLineSegment)segment).Points)
         {
             Console.WriteLine(point);
         }
     }
 }

Upvotes: 2

Related Questions