Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NullReferenceException loading RenderWindowControl for vtk in WPF

I am trying to load a RenderWindowControl from vtk libraries on my WPF proyect using ActiViz.NET and Visual Studio 2013. The library works fine since I did a new project just to practice on itbut when I tried to integrate it into my work, I got a null RenderWindowControl this time. This is my code:

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"
        Loaded="WindowLoaded"
        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);
    }

    private void WindowLoaded(object sender, RoutedEventArgs e)
    {
        var renderer = RenderControl.RenderWindow.GetRenderers().GetFirstRenderer();
        renderer.AddActor(actor);
    }
}

RenderControl.RenderWindow is null on WindowLoaded (VtkTabView.xaml.cs) and I do not know why. Might it be because I load UITabView from a second xamp and I lose the content of RenderControl?, it is the only difference I see compare to the example I did.

like image 822
Sierra Avatar asked Dec 04 '14 09:12

Sierra


1 Answers

Access the RenderWindow on Load event of the RenderWindowControl.

e.g.

public VtkTabView()
{
    InitializeComponent();
    // initialize your sphrere and actor

    RenderControl.Load += MyRenderWindowControlOnLoad;
}

private void MyRenderWindowControlOnLoad(object sender_in, EventArgs eventArgs_in){

//access the RenderWindow here

}
like image 89
JohnnyQ Avatar answered Sep 24 '22 15:09

JohnnyQ