Vertices from Path Data in Silverlight

Topics: User Forum
Apr 4, 2010 at 2:57 AM
Edited Apr 4, 2010 at 5:18 AM

I have a series of paths in a canvas. I want to create a GeomFactory.CreatePolygonGeom(... from each one. How can I translate the Path Data into Vertices? I am playing with the Physics Helper, but what I am trying to do is slightly different (or so I think).

Note: This is currently a Silverlight 3 app, until 4 gets released in two weeks.

Thanks,

Apr 4, 2010 at 7:16 AM

Well, how are you getting that path data?

Do you know the corner point locations? If so, that's simple from there.

Be more specific please.

Apr 4, 2010 at 2:42 PM

Ok.

Say I went into Blend, and made a path:

 <Path Fill="White" Stretch="Fill" Stroke="Black" Margin="149.5,129.5,186.5,150.5" UseLayoutRounding="False" Data="M227,150 L150,204 L250,315 L453,329 L445,227 L417,130 L324,201 z"/>

Thanks

 

Apr 5, 2010 at 1:58 AM

Andy to the rescue... again 

http://www.andybeaulieu.com/Home/tabid/67/EntryID/111/Default.aspx
Apr 5, 2010 at 2:39 AM

Ok, well maybe I am not quite there. I have not yet made this work.

Basically, I need a list of vertices from a polygon that I can feed to BodyFactory.Instance.CreatePolygonBody()

How can I do this from something like:

<Path Fill="White" Stretch="Fill" Stroke="Black" Margin="149.5,129.5,186.5,150.5" UseLayoutRounding="False" Data="M227,150 L150,204 L250,315 L453,329 L445,227 L417,130 L324,201 z"/>

Thanks

Coordinator
Apr 5, 2010 at 9:55 PM

This is much trickier than you would expect.

1st problem:

When you create a path in Expression Blend, it writes the path to XAML in the form of the "MiniLanguage" (Data="m227, 150... yada).  When a path is represented in this mini language format it's not possible to programattically access the "figures" within the PathGeometry.  This means you can't get at things like line segments and bezier curves.

To get past this, in your code, you need to get hold of the mini language string : Path.Data.ToString()... I think, then convert it to a PathGeometry.

You can find some code for doing thin in the project from this blog posting: http://74.125.93.132/search?q=cache:F-6lE1PUemoJ:blog.dobaginski.com/josh/%3Fp%3D55+Path+mini-language+parser&cd=1&hl=en&ct=clnk&gl=us

2nd problem:

Ok, you now have PathGeometry... now you the heck do you convert it to verts?   Well, you go hunting for some more code to do this.   Look at this project: http://www.codeproject.com/KB/silverlight/PathAnimation.aspx

In particular, look at the 'PathGeometryHelper' code.   This will take a PathGeometry and convert it to verts like you want.

You many need to tweak the verts a bit so that the physics geom aligns with the path, but this should get you close. 

-Jeff