Monday, September 22, 2014

Helix 3D Toolkit - Well Viewer Part 1

The last few years I've been working in the oil industry, programming small custom engineering applications.  One common requirement is to have a 3D plot of the well trajectory.  My first crack at this several years ago was rather crude, basically using a 3D to 2D orthographic transformation.  In fact the first rendition of it was done in an Excel VSTO application and plotted using an Excel chart. After migrating the application to WPF I plotted it using the SciChart chart package.  Though not beautiful, this worked and did enough to get us by while we worked on more technical aspects of our program.

3D well profile plotted with SciChart
I was aware of WPF's native 3D capability, but I never could find the time to dig into it to make a new 3D well survey plot to draw this graph.  I stumbled on Helix 3D Toolkit on NuGet, several months ago and just recently I found the time to dive in and replace our 3D plot with a more attractive and functional tool.

Helix wraps the core WPF 3D functionality, to provide an extra level of sweetness and ease of use.  It recently moved to GitHub and appears to be a project with growing activity.  Documentation is nearly non-existent, however the source code has a great suite of sample applications.  Code samples are worth thousands of words :)

I decided to have a preview well plot and then allow the user to open up a new window which would contain a larger version of the plot and expose more controls to manipulate the 3D view-port.

3D well profile preview plotted with Helix 3D Toolkit

Window giving a larger view-port and more user controls

One of the nice things about the toolkit is that the controls provide a huge amount of functionality.  For example I was able to databind to a collection of 3D points representing my tube path.  The Helix3DViewport class provided panning, zooming, rotation, copy image clipboard etc.  Honestly, after reading through several of the code samples creating this plot was very simple.

It's not all sunshine and roses, I did run into one issue.  For my preview plot I keep one instance of the view instantiated and when a user clicks on a new well, a new WellSurveyPlotViewModel is created and the data context of the preview view is updated.  When the user clicked the Open Well Viewer button a window service creates a new window passing off the WellSurveyPlotViewModel as its data context.  I could not get the view to zoom extents with the new camera information.  After reading the source code I arrived at the idea of using an attached property to reset the camera and call for a re-zoom.  The following two lines, in an attached property hooked into a ReZoom boolean property in the preview view model solved the issue.

  viewport.Camera = viewport.DefaultCamera;

Overall this toolkit is great, using the MVVM pattern I was able to recreate our 3D plot using Helix in a weekend of several short coding sessions.

No comments:

Post a Comment