在 .NET C# 应用程序 (WinForms) 中使用 ActiViz 时如何定位 x64(64 位)平台

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

我正在创建这个问题并自己回答它,以分享一种简单的方法来 run/debug 和构建应用程序(Windows 表单),在 .NET Framework (C#) 中使用 ActiViz。 It’s OK to Ask and Answer Your Own Questions.


如果我创建一个新的 Windows Forms 应用程序(.NET Framework、C#)并面向 x64 平台:

然后我可以转到项目 > 管理 NuGet 包和 search/install Activiz.NET.x64 (v5.8.0).

但是,在安装 Activiz.NET.x64 后,如果我尝试将 RenderWindowControl 拖到 Form,则会显示以下错误(无法加载工具箱项 'RenderWindowControl'.):

这个问题有解决方法吗?

我知道这个问题已经得到解答 and here (answer not accepted);但是,这些答案包含在 designing/debugging 具有 Activiz.NET.x86(32 位)的应用程序中,并且仅安装 Activiz.NET.x64(64 位)作为应用程序的发行版本。在两个ActiViz包之间来回切换显然很麻烦

Kitware ActiViz website 的常见问题解答中介绍了以下内容:

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.

但是,此问题的解决方法是:

  1. 创建新的 Windows Forms 应用程序 (.NET C#) 并立即面向 x64(64 位)平台;

  2. 通过项目 > 管理 NuGet 包安装 Activiz.NET.x64(撰写此答案时为 v5.8.0);

  3. Panel(例如,名为 viewportPanel)添加到您的 Form。这个 Panel 将是 ParentRenderWindowControl;

  4. 在表单的构造函数中创建一个 RenderWindowControl 实例,如下所示:

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

您现在可以 run/debug 并在使用 Activiz.NET.x64:

的同时构建应用程序

但是,还有一个可能的问题:

假设我希望背景为红色。

public Form1()
{
    InitializeComponent();

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

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

添加上面显示的新代码行将引发 System.NullReferenceException。这是因为 renWinControl.RenderWindowForm1 初始化之前是 null

因此,我们需要在 RenderWindow 上进行的任何设置都应该在表单的构造函数之后完成,例如,我们可以创建一个 Load 事件。在我们这样做的同时,我们还可以创建一些字段以便更轻松地访问 RenderWindowRenderer.

完整代码:

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

最后,在 x64 中调试:

编辑

不要使用Form_Load事件,最好使用RenderWindowControl_Load事件。请记住,在加载控件之前 RenderWindowControl.RenderWindownull