.NET 环境搭建 - .NET6 WinForms

请安装Vistual Studio 2022,社区版就够用了。

1 创建项目

选择创建Windows窗体应用

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

选一下.NET6

2 配置项目

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

3 设计界面

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

4 创建三维控件

using AnyCAD.Forms;
namespace WinFormsStarter
{
    public partial class Form1 : Form
    {
        RenderControl mRenderView; 
        public Form1()
        {
            InitializeComponent();
            mRenderView = new RenderControl(this.splitContainer1.Panel2);
        }
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13

运行一下:

5 显示模型

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);
        }
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

再运行一下:

6 资源释放

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

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

这是因为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();
        }
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

7 总结

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

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

8 延申阅读