Reputation: 456
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...
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.
Upvotes: 6
Views: 16682
Reputation: 11
# This is my code which works fine #
<Window x:Class="Helix_Car_Demo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Helix_Car_Demo"
xmlns:helix="http://helix-toolkit.org/wpf"
mc:Ignorable="d"
Background="Black"
WindowState="Maximized" WindowStyle="None"
Title="MainWindow" Height="350" Width="525">
<Grid>
<helix:HelixViewport3D Name="viewport3D" ZoomExtentsWhenLoaded="True" MouseDoubleClick="viewport3D_MouseDoubleClick">
<helix:SunLight/>
</helix:HelixViewport3D>
</Grid>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
ModelVisual3D device = new ModelVisual3D();
device.Content = getModel("hearse.3ds");
viewport3D.Children.Add(device);
}
public Model3D getModel(string path)
{
Model3D device = null;
try
{
viewport3D.RotateGesture = new MouseGesture(MouseAction.LeftClick);
ModelImporter import = new ModelImporter();
device = import.Load(path);
}
catch (Exception e)
{ }
return device;
}
} }
Upvotes: -2
Reputation: 456
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 ;)
Upvotes: 7