2016-06-09 28 views
1

我對wpf和c#還很新,我正在做一個關於旋轉,網格平移的項目。將旋轉/平移到按鈕meshgeometry3d wpf c#

我在XAML中創建了網格,看起來一切正常。這是當我嘗試編程旋轉/平移按鈕,我有問題。由於大部分代碼都在XAML中,我猜想旋轉也應該在XAML中,但我無法找到將旋轉附加到按鈕的方式。我剛剛找到了一些示例,展示如何旋轉並非我想要的按鈕。 所以我想知道是否有辦法在XAML中做到這一點,或者我需要在* .cs文件中傳輸網格的所有位置和索引。

這裏是我的XAML代碼:

<Window x:Class="WpfApplication16.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:local="clr-namespace:WpfApplication16" 
    mc:Ignorable="d" 
    Title="MainWindow" Height="979" Width="1319"> 
<Grid Margin="0,0,102,6"> 
    <Viewport3D Margin="0,0,600,-100" d:IsLocked="True" > 
     <ModelVisual3D> 
      <ModelVisual3D.Content> 
       <Model3DGroup> 
        <GeometryModel3D> 
         <GeometryModel3D.Geometry> 

          <!-- Left: Corner Normal is (-1, 1, 2) --> 
          <MeshGeometry3D Positions="2,1,1 
              1.7,1.7,1 
              1,2,1 
              0.3,1.7,1 
              0 1 1 
              0.3 0.3 1 
              1 0 1 
              1.7 0.3 1 
              1.5 1 1 
              1.35 1.35 1 
              1 1.5 1 

              0.65 1.35 1 
              0.5 1 1 
              0.65 0.65 1 
              1 0.5 1 
              1.35 0.65 1 
              1.5 1 0.7 
              1.35 1.35 0.7 
              1 1.5 0.7 
              0.65 1.35 0.7 
              0.5 1 0.7 
              0.65 0.65 0.7 
              1 0.5 0.7 
              1.35 0.65 0.7" 

             TriangleIndices="0 1 9, 0 9 8, 
                 1 2 10, 
                 1 10 9, 2 3 11, 
                 2 11 10, 3 4 12, 
                 3 12 11, 4 5 13, 
                 4 13 12, 5 6 14, 
                 5 14 13, 6 7 15, 
                 6 15 14, 7 0 8, 
                 7 8 15, 8 9 15, 
                 9 14 15, 9 10 14, 
                 10 13 14, 10 11 13, 
                 11 12 13 

                 0 1 17, 0 17 16, 
                 1 2 18, 1 18 17, 
                 2 3 19, 2 19 18, 
                 3 4 20, 3 20 19, 
                 4 5 21, 4 21 20, 
                 5 6 22, 5 22 21, 
                 6 7 23, 6 23 22, 
                 7 0 16, 7 16 23, 
                 16 17 24, 17 18 24, 
                 18 19 24, 19 20 24, 
                 20 21 24, 21 22 24, 
                 22 23 24, 23 16 24 

                 16 17 23, 17 22 23, 
                 17 18 22, 18 21 22, 
                 18 19 21, 19 20 21" /> 
         </GeometryModel3D.Geometry> 

         <GeometryModel3D.Material> 
          <DiffuseMaterial Brush="Red" /> 
         </GeometryModel3D.Material> 

         <GeometryModel3D.Transform> 
          <RotateTransform3D> 
           <RotateTransform3D.Rotation> 
            <AxisAngleRotation3D x:Name="rotate" Axis="1 0 0" Angle="10"/> 
           </RotateTransform3D.Rotation> 
          </RotateTransform3D> 
         </GeometryModel3D.Transform> 
        </GeometryModel3D> 

        <GeometryModel3D> 
         <GeometryModel3D.Geometry> 
          <!-- Right: Corner Normal is (-1, 2, 1) --> 
          <MeshGeometry3D Positions=" 1.5 1 0.7 
                1.35 1.35 0.7 
                1 1.5 0.7 
                0.65 1.35 0.7 
                0.5 1 0.7 
                0.65 0.65 0.7 
                1 0.5 0.7 
                1.35 0.65 0.7 
                1 1 0.5" 

             TriangleIndices="0 1 8 
                  1 2 8 
                  2 3 8 
                  3 4 8 
                  4 5 8 
                  5 6 8 
                  6 7 8 
                  7 0 8" /> 
         </GeometryModel3D.Geometry> 

         <GeometryModel3D.Material> 
          <DiffuseMaterial Brush="Blue" /> 
         </GeometryModel3D.Material> 

         <GeometryModel3D.Transform> 
          <RotateTransform3D> 
           <RotateTransform3D.Rotation> 
            <AxisAngleRotation3D x:Name="rotate1" Axis="1 0 0" Angle="10"/> 
           </RotateTransform3D.Rotation> 
          </RotateTransform3D> 
         </GeometryModel3D.Transform> 
        </GeometryModel3D> 

        <!-- Light source --> 
        <DirectionalLight Color="White" Direction="-5,-5,-5" /> 

       </Model3DGroup> 
      </ModelVisual3D.Content> 
     </ModelVisual3D> 


     <!-- Camera --> 
     <Viewport3D.Camera> 
      <OrthographicCamera Position="0 0 10" 
          LookDirection="0 0 -1" 
          UpDirection="0 1 0" 
          Width="6" /> 
     </Viewport3D.Camera> 

    </Viewport3D> 
    <Button x:Name="button" Content="Rotation de 10 degrées en X" HorizontalAlignment="Right" Height="81" Margin="0,68.5,0,0" VerticalAlignment="Top" Width="316"/> 
    <Button x:Name="button1" Content="Rotation 5 degrées en Y" HorizontalAlignment="Right" Height="81" Margin="0,223.667,0,0" VerticalAlignment="Top" Width="316"/> 
    <Button x:Name="button2" Content="Translation X+" Margin="0,353,0,0" Height="84" VerticalAlignment="Top" HorizontalAlignment="Right" Width="307"/> 
    <Button x:Name="button3" Content="Translation Y+" HorizontalAlignment="Right" Height="81" Margin="0,503.5,0,0" VerticalAlignment="Top" Width="316"/> 
    <Button x:Name="button4" Content="Translation Z+" HorizontalAlignment="Right" Height="81" Margin="0,0,0,217" VerticalAlignment="Bottom" Width="316" d:LayoutOverrides="VerticalAlignment"/> 

</Grid> 
</Window> 

以下是我在* .cs文件的嘗試:

namespace WpfApplication16 
{ 
    /// <summary> 
    /// Interaction logic for MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : Window 
{ 
    AxisAngleRotation3D rotx; 

    public MainWindow() 
    { 
     InitializeComponent(); 

     rotx = new AxisAngleRotation3D(new Vector3D(1, 0, 0), 10); 

    } 

    MeshGeometry3D myGeometryModel = new MeshGeometry3D(); 

    private void button_click(object sender, RoutedEventArgs e) 
    { 

     //GeometryModel3D.Transform = new RotateTransform3D(new AxisAngleRotation3D(new Vector3D(1, 0, 0), angle += 10)); 

     rotx.Angle += 10; 
     rotate.Angle += 10; 
     rotate1.Angle += 10; 

     RotateTransform3D myRotateTransform3D = new RotateTransform3D(); 
     AxisAngleRotation3D myAXisAngleRotation3d = new AxisAngleRotation3D(); 
     myAXisAngleRotation3d.Axis = new Vector3D(1, 0, 0); 
     myAXisAngleRotation3d.Angle = 10; 
     myRotateTransform3D.Rotation = myAXisAngleRotation3d; 

     Transform3DGroup myTransform3Dgroup = new Transform3DGroup(); 
     myTransform3Dgroup.Children.Add(myRotateTransform3D); 

     myGeometryModel.Transform = myTransform3Dgroup; 


    } 
} 
} 

這是第一個按鈕一試,我想圍繞X軸做10度的旋轉。它不工作。

結果是在最後一行說錯誤。 Transform is not a method group。其他按鈕將用於繞Y軸旋轉。另外3個用於在3軸方向x,y,z上平移1個單位。

同樣,我可以使用我在XAML中聲明的內容,或者我需要將這些信息輸入到* .cs文件中?

這可能是一個非常簡單的答案,但就像我說的我是新手。所以先謝謝了。

+0

在button_click中,rotate.Angle + = 10(用於紅色)和rotate1.Angle + = 10(用於藍色)就是需要旋轉的所有東西。 myGeometryModel甚至不在窗口中。我有點困惑你的問題。 –

回答

0

更改您的XAML變換到這一點:

<GeometryModel3D.Transform> 
    <Transform3DGroup> 
     <RotateTransform3D> 
      <RotateTransform3D.Rotation> 
       <AxisAngleRotation3D x:Name="rotate" Axis="1 0 0" Angle="10"/> 
      </RotateTransform3D.Rotation> 
     </RotateTransform3D> 
     <RotateTransform3D> 
      <RotateTransform3D.Rotation> 
       <AxisAngleRotation3D x:Name="rotatey" Axis="0 1 0" Angle="0"/> 
      </RotateTransform3D.Rotation> 
     </RotateTransform3D> 
     <TranslateTransform3D x:Name="translate" OffsetX="0" OffsetY="0" OffsetZ="0" /> 
    </Transform3DGroup> 
</GeometryModel3D.Transform> 

同樣地,對於藍網:

<GeometryModel3D.Transform> 
    <Transform3DGroup> 
     <RotateTransform3D> 
      <RotateTransform3D.Rotation> 
       <AxisAngleRotation3D x:Name="rotate1" Axis="1 0 0" Angle="10"/> 
      </RotateTransform3D.Rotation> 
     </RotateTransform3D> 
     <RotateTransform3D> 
      <RotateTransform3D.Rotation> 
       <AxisAngleRotation3D x:Name="rotatey1" Axis="0 1 0" Angle="0"/> 
      </RotateTransform3D.Rotation> 
     </RotateTransform3D> 
     <TranslateTransform3D x:Name="translate1" OffsetX="0" OffsetY="0" OffsetZ="0" /> 
    </Transform3DGroup> 
</GeometryModel3D.Transform> 

再用線向上按鈕點擊此:

private void button_click(object sender, RoutedEventArgs e) 
{ 
    rotate.Angle += 10; 
    rotate1.Angle += 10; 
} 
private void button1_Click(object sender, RoutedEventArgs e) 
{ 
    rotatey.Angle += 5; 
    rotatey1.Angle += 5; 
} 
private void button2_Click(object sender, RoutedEventArgs e) 
{ 
    translate.OffsetX += 1; 
    translate1.OffsetX += 1; 
} 
private void button3_Click(object sender, RoutedEventArgs e) 
{ 
    translate.OffsetY += 1; 
    translate1.OffsetY += 1; 
} 
private void button4_Click(object sender, RoutedEventArgs e) 
{ 
    translate.OffsetZ += 1; 
    translate1.OffsetZ += 1; 
} 

我希望有所幫助。

+0

非常感謝!但我複製了xaml和cs文件,但按鈕沒有響應?沒有什麼東西在動,我嘗試了一個新的繪圖,但沒有任何動作。你有什麼想法可能是什麼? – user3558419

+0

您是否將按鈕連接到事件處理程序? <按鈕...點擊=「button1_Click」/> –

+0

這就是我忘了。我很抱歉,但是非常感謝你的時間。 – user3558419