赞
踩
3D显示、旋转、部件移动(位置获取)
开发一个可导入.stl文件,可视化3D显示模型,部件之间鼠标移动、键盘设置位置等。
WPF开发库HelixToolkit
引入库命名空间
- 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:lab2._3D"
- xmlns:hv="http://helix-toolkit.org/wpf" <!-- 添加helixToolkit命名空间-->
- mc:Ignorable="d"
- Title="Window3D" Height="450" Width="800">
UI设置3D光源、相机、网格等信息
- <Window x:Class="lab2._3D.Window3D"
- 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:lab2._3D"
- xmlns:hv="http://helix-toolkit.org/wpf"
- mc:Ignorable="d"
- Title="Window3D" Height="450" Width="800">
- <Grid>
-
- <!--加载模型:设置光源、相机、坐标网格-->
- <hv:HelixViewport3D 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"
- >
- <!-- Remember to add light to the scene -->
- <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>
- <hv:SunLight/>
-
- <hv:GridLinesVisual3D Width="1000" Length="1000" MinorDistance="50" MajorDistance="50" Thickness="1" Fill="#E5CFCECE"/>
- </hv:HelixViewport3D>
-
- </Grid>
- </Window>
模型导入。
- using HelixToolkit.Wpf;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Media.Media3D;
- using System.Windows.Shapes;
-
- namespace lab2._3D
- {
- /// <summary>
- /// Window3D.xaml 的交互逻辑
- /// </summary>
- public partial class Window3D : Window
- {
- public Window3D()
- {
- InitializeComponent();
-
- //模型加载
- string modelPath1 = AppDomain.CurrentDomain.BaseDirectory + "Corvus_Building_Lower_NO_Windows.stl"; //模型地址
-
- ModelImporter import = new ModelImporter();
- var initGroup1 = import.Load(modelPath1);
-
- string modelPath2 = AppDomain.CurrentDomain.BaseDirectory + "Corvus_Building_Roof_V2.stl";
-
- var initGroup2 = import.Load(modelPath2);
- 材质
- //GeometryModel3D geometryModel3D = initGroup.Children[0] as GeometryModel3D;
- //DiffuseMaterial diffMat = new DiffuseMaterial(new SolidColorBrush(Colors.White));
- //geometryModel3D.Material = diffMat;
- //ViewPort3D进行显示
- ModelVisual3D modelVisual3d1 = new ModelVisual3D();
- modelVisual3d1.Content = initGroup1;
- viewPort3D.Children.Add(modelVisual3d1);
- ModelVisual3D modelVisual3d2= new ModelVisual3D();
- modelVisual3d2.Content = initGroup2;
- viewPort3D.Children.Add(modelVisual3d2);
-
- }
- }
- }
鼠标滚轮实现模型放大与缩小;按住鼠标右键拖动实现模型方位调节。
helixToolkit工具实现3D模型加载到WPF,开发流程效率大大提高。后期实现模型部件运动控制。。。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。