.NET 6 十行代码搭建三维应用程序

前言

本文介绍在.NET 6环境下如何集成Rapid SDK三维控件,请首先确保已经安装了Vistual Studio 2022,社区版就够用了。

一、创建项目

选择创建Windows窗体应用

e741b9924a0951218aa4467c5152a259.png

给程序起一个酷酷的名字,选一个酷酷的位置:90443174f5ac4d342aa6c73c83e383b7.png

选一下.NET 6da8b89defa8d00fd4d3f07e8fa424129.png

二、配置项目

从nuget.org上或者本地安装AnyCAD Rapid SDK 2022。

下载链接: https://pan.baidu.com/s/1FQQpwjImo-T3thUk5E1sQw 提取码: 9d86

94a5e6891243c129e35de03b9d8db335.png

三、设计界面

给三维界面留个位置,采用经典的所有窗口。右边用来显示三维内容。

5c7fcbe2849124027c1744a9f12adc16.png

四、创建三维控件

using AnyCAD.Forms;
namespace WinFormsStarter
{
    public partial class Form1 : Form
    {
        RenderControl mRenderView; 
        public Form1()
        {
            InitializeComponent();
            mRenderView = new RenderControl(this.splitContainer1.Panel2);
        }
    }
}

运行一下:0b4767e2d15247cf54a967c8d3534384.png

五、显示模型

using AnyCAD.Forms;
using AnyCAD.Foundation;

namespace WinFormsStarter
{
    public partial class Form1 : Form
    {
        RenderControl mRenderView; 
        public Form1()
        {
            InitializeComponent();

            mRenderView = new RenderControl(this.splitContainer1.Panel2);
            //构造函数里不能在使用Rapid SDK的其他的API,需要放在Load里面
        }

        // 窗口加载后,创建个球
        private void Form1_Load(object sender, EventArgs e)
        {
            var shape = ShapeBuilder.MakeSphere(new GPnt(0, 0, 0), 10);
            mRenderView.ShowShape(shape, ColorTable.PaleVioletRed);
        }
    }
}

再运行一下:004b601b987440480a64eecac1335b8f.png

六、资源释放

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

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

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

namespace WinFormsStarter
{
    internal static class Program
    {
        /// <summary>
        ///  The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            AnyCAD.Foundation.GlobalInstance.Initialize();
            ApplicationConfiguration.Initialize();
            Application.Run(new Form1());
            AnyCAD.Foundation.GlobalInstance.Destroy();
        }
    }
}

七、总结

.NET6为开发者带来了高效的开发体验,而AnyCAD Rapid SDK也一样,通过简单几步即可为应用添加三维能力,让程序显得高大上!

AnyCAD Rapid SDK的包括三维显示、造型、STEP/IGES读取等场景的三维功能,能够满足大部分的三维工业软件应用场景,比如CAD设计、CAE仿真、CAM加工,机器人模拟等,可以应用在建筑、机械、电力、化工、自动化等多个领域。

源码:https://gitee.com/anycad/rapid.net.starter

转自:AnyCAD

链接:cnblogs.com/anycad/p/anycad-winforms-net6-starter.html

- EOF -

技术群:添加小编微信dotnet999

公众号:dotnet讲堂

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值