wpf 将模型/内容添加到 Helix 3D Toolkit 中的 HelixViewport3D

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20386519/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 10:10:04  来源:igfitidea点击:

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

c#wpfhelix-3d-toolkit

提问by Rog

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

我正在尝试在 HelixViewport3D 中加载和显示 3d 模型。

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

我可以加载模型(OBJ),但我无法理解如何将模型放入视口。

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

这是我的 WPF 表单的屏幕截图...主要形式

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.

viewprot 被命名为“myView”——我想我可以连接到它来添加我的模型,但我没有看到任何明显可以使用的东西。

Here's my XAML of the form :

这是我的 XAML 形式:

 <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.

你可以看到我卡在哪里。有人可以帮助我走上正轨吗?

回答by Rog

After some experimenting I found the solution.

经过一些实验,我找到了解决方案。

I added the following to my XAML :

我在 XAML 中添加了以下内容:

 <ModelVisual3D x:Name="foo"/>

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

诀窍是给它一个名字,例如'foo'。XAML 现在看起来像这样:

 <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 ;)

当你知道怎么做时很容易;)

回答by Sumit Baperkar

# 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>

Here is my .cs File

这是我的 .cs 文件

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;
    }

} }

} }