WPF 三维应用搭建(基础)

1 创建项目

选择创建WPF应用
 

给程序起一个酷酷的名字,选一个酷酷的位置:
 

选一下.NET6
 

2 配置项目

从nuget.org上安装AnyCAD Rapid SDK 2022。
 

3 设计界面

  • 首先引入程序集:
  • xmlns:anycad="clr-namespace:AnyCAD.WPF;assembly=AnyCAD.WPF.NET6"

  • 设计布局

给三维界面留个位置,采用经典的左右窗口。右边用来显示三维内容。完整的xaml如下:

<Window x:Class="WpfApp1.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:WpfApp1"
        xmlns:anycad="clr-namespace:AnyCAD.WPF;assembly=AnyCAD.WPF.NET6"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition MinWidth="100" Width="0.3*"/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <anycad:RenderControl Grid.Column="1" x:Name="mRenderCtrl"/>
    </Grid>
</Window>

运行一下:
 

5 显示模型

  • 增加一个ViewerReady事件

 

 
<anycad:RenderControl Grid.Column="1" x:Name="mRenderCtrl" ViewerReady="mRenderCtrl_ViewerReady"/>
  • 在mRenderCtrl_ViewerReady中创建一个球
 private void mRenderCtrl_ViewerReady()
        {
            var shape = ShapeBuilder.MakeSphere(new GPnt(0, 0, 0), 100);
            mRenderCtrl.ShowShape(shape, ColorTable.AliceBlue);
        }

在三维控件初始化完成之前是不能进行对控件进行操作的

再运行一下:
 

6 资源释放

在调试模式下,程序退出的时候在输出窗口中,你可能会发现这样的错误:

 
  1. 程序“[57196] WpfApp1.exe”已退出,返回值为 3221225477 (0xc0000005) 'Access violation'。

这是因为AnyCAD Rapid SDK没有正确的释放资源。为保证三维控件资源能够正确释放,程序能够得到正常的返回值,只需要这样加一下Startup和Exit消息:

 
<Application x:Class="WpfApp1.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:WpfApp1"
             StartupUri="MainWindow.xaml" Exit="Application_Exit" Startup="Application_Startup">
    <Application.Resources>
    </Application.Resources>
</Application>

代码修改:

  public partial class App : Application
    {
        private void Application_Startup(object sender, StartupEventArgs e)
        {
            AnyCAD.Foundation.GlobalInstance.Initialize();
        }
        private void Application_Exit(object sender, ExitEventArgs e)
        {
            AnyCAD.Foundation.GlobalInstance.Destroy();
        }
    }
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值