Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cutting holes in PathGeometry

I have a PathGeometry defining some path like this one:

original path http://devblog.ailon.org/devblog/_stuff/wpfpathgeoquestion/original.gif

This is a simplified example. In reality it can have segments of any type (Line, Arc, Bezier).

Now I need to cut a hole of some sort and size (square, circle, etc.) in segment joint points so the final result looks something like this:

with holes http://devblog.ailon.org/devblog/_stuff/wpfpathgeoquestion/with_holes.gif

My initial idea was to combine the original PathGeometry with other Geometry containing squares (or whatever) in the joint points using GeometryCombineMode.Exclude. Unfortunately this produces empty geometry if path is not filled. And if it is filled it produces incorrect result.

So I'm stuck. In simple example with line segments I could've calculated the border points upfront and made my line segments end there, but with arcs and beziers this would be sort of overkill.

Does anyone have an idea how to accomplish this?

Note: I need these holes erased (transparent) so placing a white square above it wont do.

like image 921
Alan Mendelevich Avatar asked Feb 27 '09 09:02

Alan Mendelevich


1 Answers

Oh. Did it. The idea is to create an OpacityMask out of your holes using DrawingBrush and then push it on the drawing context before drawing the PathGeometry. Something like this.

RectangleGeometry r = new RectangleGeometry(graphVisual.Bounds);
GeometryDrawing dr = new GeometryDrawing(Brushes.Black, null, Geometry.Combine(r, bulletHoles, GeometryCombineMode.Exclude, null));
DrawingBrush br = new DrawingBrush(dr);
drawingContext.PushOpacityMask(br);

drawingContext.DrawGeometry(null, new Pen(Brushes.Green, 2), graphVisual);

drawingContext.Pop();
like image 63
Alan Mendelevich Avatar answered Nov 12 '22 13:11

Alan Mendelevich