WPF项目中不显示VtkActor

时间:2014-12-04 10:56:00

标签: c# wpf xaml vtk

我正在尝试使用ActiViz.NET和Visual Studio 2013从我的WPF项目中的vtk库加载RenderWindowControl。该库工作正常,因为我做了一个新项目只是为了练习但是当我尝试将它集成到我的工作,演员没有显示。这是我的代码:

MainWindow.xaml:

<Window x:Class="myProject.Views.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:VtkTab="clr-namespace:myProject.Views.UITabs.VtkTab"
    x:Name="Mainwindow"
    MinHeight="600"
    MinWidth="800"
    Title="{Binding Title}"
    Height="720"
    Width="1280"
    Icon="{StaticResource ApplicationIcon}"
    Loaded="OnLoaded"
    DataContext="{Binding Main, Source={StaticResource ViewModelLocator}}"
    Style="{StaticResource WindowStyle}"
    mc:Ignorable="d">

    <DockPanel>
        <TabControl>
        ....
        ....
        <VtkTab:VtkTabView />
        ....
        .... 
        </TabControl>
    </DockPanel>
</Window>

VtkTabView.xaml:

<UserControl x:Class="myProject.Views.UITabs.VtkTab.VtkTabView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:vtk="clr-namespace:Kitware.VTK;assembly=Kitware.VTK"
        Height="480" Width="640">
        <WindowsFormsHost Name="Wfh">
            <vtk:RenderWindowControl x:Name="RenderControl" />
        </WindowsFormsHost>
</UserControl>

VtkTabView.xaml.cs:

public partial class UITabView
{

    protected static Random _random = new Random();
    vtkActor actor = vtkActor.New();

    public VtkTabView()
    {
        InitializeComponent();

        var sphere = vtkSphereSource.New();
        sphere.SetThetaResolution(8);
        sphere.SetPhiResolution(16);

        var shrink = vtkShrinkPolyData.New();
        shrink.SetInputConnection(sphere.GetOutputPort());
        shrink.SetShrinkFactor(0.9);

        var move = vtkTransform.New();
        move.Translate(_random.NextDouble(), _random.NextDouble(), _random.NextDouble());
        var moveFilter = vtkTransformPolyDataFilter.New();
        moveFilter.SetTransform(move);

        moveFilter.SetInputConnection(shrink.GetOutputPort());

        var mapper = vtkPolyDataMapper.New();
        mapper.SetInputConnection(moveFilter.GetOutputPort());

        // The actor links the data pipeline to the rendering subsystem 
        actor.SetMapper(mapper);
        actor.GetProperty().SetColor(1, 0, 0);

        RenderControl.Load += RenderWindowControlOnLoad;
    }

    void RenderWindowControlOnLoad(object sender, EventArgs e)
    {
        var renderer = RenderControl.RenderWindow.GetRenderers().GetFirstRenderer();
        renderer.AddActor(actor);
        renderer.SetBackground(0, 1, 0);
        renderer.Render();
    }
}

正如你所看到的,在方法RenderWindowControlOnLoad(VtkTabView.xaml.cs)中我接受了actor(一个Sphere)并将它添加到声明为WPF的渲染中,但是它没有显示出来。我试图改变背景颜色,并调用渲染以防万一,但它仍然无法正常工作:(

1 个答案:

答案 0 :(得分:0)

呼叫

RenderControl.RenderWindow.Render();

而不是

renderer.Render();