WPF 插件HelixToolkit库实现3D显示

文章目录

前言

一、HelixToolkit安装

二、使用步骤

1.引入库

2.xmal代码设置

        3.后端程序

        4.3D显示

总结



前言

3D显示、旋转、部件移动(位置获取)

开发一个可导入.stl文件,可视化3D显示模型,部件之间鼠标移动、键盘设置位置等。


WPF开发库HelixToolkit

一、HelixToolkit安装

二、使用步骤

1.引入库

引入库命名空间

  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">

2.xmal代码设置

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>

3.后端程序

模型导入。

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);

        }
    }
}

4.3D显示

鼠标滚轮实现模型放大与缩小;按住鼠标右键拖动实现模型方位调节。


总结

helixToolkit工具实现3D模型加载到WPF,开发流程效率大大提高。后期实现模型部件运动控制。。。

  • 8
    点赞
  • 44
    收藏
    觉得还不错? 一键收藏
  • 8
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值