是否可以在HelixToolKit WPF项目中创建可点击的TubeVisual3D?

时间:2015-06-07 20:56:11

标签: wpf clickable helix-3d-toolkit

创建可能具有关联事件的TubeVisual3D的正确方法是什么,尤其是用户交互事件,例如鼠标点击?

在WPF C#项目中使用HelixToolKit。

谢谢

2 个答案:

答案 0 :(得分:1)

下面我概述了一种对我有用的不同方法。 我建议你查看UIElement3D课程。UIElement3D class reference

我们走了。

  • 创建一个扩展UIElement3D的类。
  • 在构造函数中创建tube对象,并将其指定给Visual3DModel成员。
  • 覆盖符合您需求的事件处理程序。

这是一个例子。

using System.Windows.Media;    
using System.Windows.Media.Media3D;

public InteractiveTubeVisual3D : UIElement3D
{
   public InteractiveTubeVisual3D( List<Point3D>paths, double tubeDiameter = 0.55)
   {
        int thetaDiv = 12;
        Material material = MaterialHelper.CreateMaterial( Colors.Crimson );

        MeshBuilder meshBuilder = new MeshBuilder();
        meshBuilder.AddTube(paths, tubeDiameter, thetaDiv, false);

        GeometryModel3D model = new GeometryModel3D( meshBuilder.ToMesh(), material);
        Visual3DModel = model;
   }

   protected override void OnMouseDown( MouseButtonEventArgs Event )
   {
       base.OnMouseDown( Event );
       //change the color of the tube when left mouse clicked, revert back on right mouse clicked
       if ( Event.LeftButton == MouseButtonState.Pressed )
       {
           GeometryModel3D
               tube = Visual3DModel as GeometryModel3D;
               tube.Material = MaterialHelper.CreateMaterial( Colors.CornflowerBlue );
       }
       else if ( Event.RightButton == MouseButtonState.Pressed )
       {
           GeometryModel3D
               tube = Visual3DModel as GeometryModel3D;
               tube.Material = MaterialHelper.CreateMaterial( Colors.Crimson );
       }

       Event.Handled = true;
   }

}

接下来的步骤说明了如何将其添加到您的场景中。

  • 创建一个ContainerUIElemnt3D。ContainerUIElement3D class ref
  • 将孩子的管子作为孩子添加到ContainerUIElement3D。
  • 将ContainerUIElement3D对象添加为Helix视口的子项。

示例程序

using System.Windows.Media.Media3D;
public partial class MainWindow : Window
{
    public MainWindow( )
    {
        InitializeComponent();

        ContainerUIElement3D container = new ContainerUIElement3D();

        List<Point3D> paths = CreatePath(); // pass in you tubes points. 

        InteractiveTubeVisual3D tube = new InteractiveTubeVisual3D(paths);
        container.Children.Add(tube);

        HelixViewPort.Children.Add(container);
    }
}

HelixViewPort是x:来自XAML的名称引用

<h:HelixViewport3D x:Name="HelixViewPort" >
    <h:DefaultLights/>
</h:HelixViewport3D>

希望它有帮助:)。祝你好运。

答案 1 :(得分:0)

是的。您必须使用命中测试来确定您的TubeVisual3D上是否有鼠标点击,一旦您知道这一点,您就可以执行您想要的任何操作。这必须通过鼠标点击事件来完成......