2015-06-07 41 views
-1

創建一個TubeVisual3D可能有關聯的事件,尤其是用戶交互事件,如鼠標點擊的正確方法是什麼?是否可以在HelixToolKit WPF項目中創建可點擊的TubeVisual3D?

在WPF C#項目中使用HelixToolKit。

謝謝

+0

不知道它是否是最好的選擇,但我實現了一個透明網格,其中有幾個圓柱體,其路徑等於我可以點擊的TubeVisual3D元素。這意味着用戶將看到一個TubeVisual3D,但他點擊一個透明圓柱體。 –

回答

0

是的。您必須使用命中測試來確定鼠標點擊是否在TubeVisual3D上,一旦您知道這一點,您可以執行任何您想要的操作。這必須通過鼠標點擊事件來完成......

1

下面我概述了一種適用於我的不同方法。 我建議看一下UIElement3D類。 UIElement3D class reference

我們走吧。

  • 創建一個擴展UIElement3D的類。
  • 在構造函數中創建管對象並將其分配給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:名稱REF從XAML

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

希望它能幫助:)。祝你好運。

相關問題