简体   繁体   中英

How to target x64 (64 bit) platform while using ActiViz in a .NET C# application (WinForms)

I am creating this question and answering it myself to share an easy way to run/debug and build an application (Windows Forms), using ActiViz in the .NET Framework (C#). It's OK to Ask and Answer Your Own Questions .


If I create a new Windows Forms application (.NET Framework, C#) and target x64 platforms:

针对 x64 平台。

I can then go to Project > Manage NuGet Packages and search/install Activiz.NET.x64 (v5.8.0).

However, after installing Activiz.NET.x64, if I try to drag the RenderWindowControl to a Form , the following error is shown (Failed to load toolbox item 'RenderWindowControl'.):

无法加载工具箱项“RenderWindowControl”。

Is there a workaround for this issue?

I know this question has been answered here and here (answer not accepted) ; however, these answers consist in designing/debugging the application with Activiz.NET.x86 (32 bit) and only installing Activiz.NET.x64 (64 bit) for the release version of the application. It is obviously very cumbersome to switch back and forth between both ActiViz packages.

In the FAQ of the Kitware ActiViz website the following is presented:

Does ActiViz 64 work with Visual Studio?

Visual Studio is a 32 bits application, therefore 64 bits control does not work and you need the 32 bits version of ActiViz when using the designer within Visual Studio. Usually, the 32 bits version is used to design and the 64 bits version is used for the final compilation.

However, a workaround for this issue is to:

  1. Create your new Windows Forms application (.NET C#) and immediately target x64 (64 bit) platforms;

  2. Install Activiz.NET.x64 (v5.8.0 at the time of writing this answer) through Project > Manage NuGet Packages;

  3. Add a Panel (named viewportPanel , for example) to your Form . This Panel will be the Parent to a RenderWindowControl ;

  4. Create a RenderWindowControl instance in the form's constructor like so:

     using Kitware.VTK; using System.Windows.Forms; namespace ActiVizTest { public partial class Form1 : Form { public Form1() { InitializeComponent(); RenderWindowControl renderWindowControl = new RenderWindowControl() { Parent = viewportPanel, AddTestActors = true }; } } }

You can now run/debug and build the application while using Activiz.NET.x64:

在此处输入图片说明

However, there is still a possible issue:

Lets say that I want the background to be red.

public Form1()
{
    InitializeComponent();

    RenderWindowControl renderWindowControl = new RenderWindowControl()
    {
        Parent = viewportPanel,
        AddTestActors = true
    };

    renderWindowControl.RenderWindow.GetRenderers().GetFirstRenderer().SetBackground(1, 0, 0);
}

Adding the new line of code shown above will raise a System.NullReferenceException . This is because renWinControl.RenderWindow is null before Form1 is initialized.

So, any setup we need to do on the RenderWindow should be done after the form's constructor, for example, we can create a Load event. And while we are at it, we can also create some fields to have easier access to the RenderWindow and Renderer .

Full code:

using Kitware.VTK;
using System;
using System.Windows.Forms;
namespace ActiVizTest
{
    public partial class Form1 : Form
    {
        private readonly RenderWindowControl renderWindowControl;
        private vtkRenderWindow renderWindow;
        private vtkRenderer renderer;
        public Form1()
        {
            InitializeComponent();

            renderWindowControl = new RenderWindowControl()
            {
                Parent = viewportPanel,
                AddTestActors = true
            };
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            renderWindow = renderWindowControl.RenderWindow;
            renderer = renderWindow.GetRenderers().GetFirstRenderer();

            renderer.SetBackground(1, 0, 0);
        }
    }
}

And finally, debugging in x64:

在此处输入图片说明

EDIT

Instead of using the Form_Load event, it is best to use the RenderWindowControl_Load event. Just remember that RenderWindowControl.RenderWindow is null before the control is loaded.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM