Adding models/content to the HelixViewport3D in Helix 3D Toolkit

Rog picture Rog · Dec 4, 2013 · Viewed 13.3k times · Source

I am trying to load and display a 3d model in the HelixViewport3D.

I can get as far as loading the model (OBJ), but I cannot understand how to get the model into the viewport.

Here's a screenshot of my WPF form...main form

The viewprot is named as 'myView' - I thought I could hook into that to add my model, but I don't see anything obvious to use.

Here's my XAML of the form :

 <Window x:Class="HelixTrial.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:HelixToolkit="clr-namespace:HelixToolkit.Wpf;assembly=HelixToolkit.Wpf"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <Grid HorizontalAlignment="Left" Height="250" Margin="241,37,0,0" VerticalAlignment="Top" Width="250">
        <HelixToolkit:HelixViewport3D x:Name="myView" ZoomExtentsWhenLoaded="True">
            <!-- Remember to add light to the scene -->
            <HelixToolkit:SunLight/>
            <!-- You can also add elements here in the xaml -->
            <HelixToolkit:GridLinesVisual3D Width="8" Length="8" MinorDistance="1" MajorDistance="1" Thickness="0.01"/>
        </HelixToolkit:HelixViewport3D>
    </Grid>
</Grid>

And here is the code for my form.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Media3D;
using HelixToolkit.Wpf;

namespace HelixTrial
{

/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        ObjReader CurrentHelixObjReader = new ObjReader();
        Model3DGroup MyModel = CurrentHelixObjReader.Read("C:/Users/Roger/Desktop/cube/cube.obj");

       // Now how to load it into the viewport... ?
    }    
}

}

You can see where I am stuck. Could someone help get me on track please.

Answer

Rog picture Rog · Dec 5, 2013

After some experimenting I found the solution.

I added the following to my XAML :

 <ModelVisual3D x:Name="foo"/>

The trick was to give it a name, ie 'foo' for example. The XAML will now look like this :

 <Window x:Class="HelixTrial.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:HelixToolkit="clr-namespace:HelixToolkit.Wpf;assembly=HelixToolkit.Wpf"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <Grid HorizontalAlignment="Left" Height="250" Margin="241,37,0,0" VerticalAlignment="Top" Width="250">
        <HelixToolkit:HelixViewport3D x:Name="myView" ZoomExtentsWhenLoaded="True">
            <!-- Remember to add light to the scene -->
            <HelixToolkit:SunLight/>
            <ModelVisual3D x:Name="foo"/>
            <!-- You can also add elements here in the xaml -->
            <HelixToolkit:GridLinesVisual3D Width="8" Length="8" MinorDistance="1" MajorDistance="1" Thickness="0.01"/>
        </HelixToolkit:HelixViewport3D>
    </Grid>
</Grid>

Then in the code (as per what I posted in my original question above) you can do this :

 ObjReader CurrentHelixObjReader = new ObjReader();
        Model3DGroup MyModel = CurrentHelixObjReader.Read("C:/Users/Roger/Desktop/cube/cube.obj");

        // Display the model
        foo.Content = MyModel;

Easy when you find out how ;)