源码:https://download.csdn.net/download/liugang590/87462000
目录
1. 环境
Visual Studio 2019 + .NET FrameWork 4.8.1
2. 创建项目
3. NuGet安装依赖
HelixToolkit.Wpf
4. 创建3D_models文件夹存放底座模型文件
5. 编辑MainView.xaml代码
<Window x:Class="_3DTransformDemo.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:_3DTransformDemo"
xmlns:hv="http://helix-toolkit.org/wpf"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition Height="10*" />
</Grid.RowDefinitions>
<Button Content="旋转" Height="20" Width="100" Click="Button_Click"/>
<!--加载模型:设置光源、相机、坐标网格-->
<hv:HelixViewport3D Grid.Row="1" Name="viewPort3D" ZoomExtentsWhenLoaded="True"
ShowViewCube="false" ShowCoordinateSystem="True"
CoordinateSystemLabelForeground="White"
CoordinateSystemVerticalPosition="Center"
CoordinateSystemHorizontalPosition="Right"
CoordinateSystemHeight="50"
CoordinateSystemWidth="50"
RenderOptions.EdgeMode="Unspecified"
BorderBrush="White"
BorderThickness="0"
ShowFrameRate="False"
ShowCameraInfo="True"
IsManipulationEnabled="True"
Background="Transparent"
>
<hv:HelixViewport3D.DefaultCamera>
<PerspectiveCamera LookDirection="-587.475,-330.619,-229.365" Position="587.475,330.619,256.278" UpDirection="-0.248,-0.139,0.959" FieldOfView="45" NearPlaneDistance="0.1"/>
</hv:HelixViewport3D.DefaultCamera>
<ModelVisual3D>
<hv:SunLight/>
<hv:GridLinesVisual3D Width="1000" Length="1000" MinorDistance="50" MajorDistance="50" Thickness="1" Fill="#E5CFCECE"/>
</ModelVisual3D>
</hv:HelixViewport3D>
</Grid>
</Window>
6. 编辑MainView.xaml.cs代码
using HelixToolkit.Wpf;
using System;
using System.IO;
using System.Windows;
using System.Windows.Media.Animation;
using System.Windows.Media.Media3D;
namespace _3DTransformDemo
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
Model3D model;
public MainWindow()
{
InitializeComponent();
model = LoadModel("IRB6700-MH3_245-300_IRC5_rev02_LINK01_CAD.stl");
Display();
}
public Model3D LoadModel(string modelName)
{
ModelImporter import = new ModelImporter();
string basePath = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.FullName + "\\3D_Models\\";
Model3D model = import.Load(basePath + modelName);
return model;
}
public void TransformModel(double angle)
{
RotateTransform3D rotateTransform = new RotateTransform3D();
AxisAngleRotation3D rotateAxis = new AxisAngleRotation3D(new Vector3D(0, 0, 1), angle);
Rotation3DAnimation rotateAnimation = new Rotation3DAnimation(rotateAxis, TimeSpan.FromSeconds(5));
rotateAnimation.DecelerationRatio = 0.8;
Transform3DGroup transform3DGroup = new Transform3DGroup();
transform3DGroup.Children.Add(rotateTransform);
transform3DGroup.Children.Add(model.Transform);
model.Transform = transform3DGroup;
rotateTransform.BeginAnimation(RotateTransform3D.RotationProperty, rotateAnimation);
}
public void Display()
{
ModelVisual3D modelVisual3d = new ModelVisual3D();
modelVisual3d.Content = model;
viewPort3D.Children.Add(modelVisual3d);
}
private void Button_Click(object sender, RoutedEventArgs e)
{
TransformModel(180);
Display();
}
}
}