天天看點

十行代碼搭建基于.NET6三維應用程式

本文介紹在.NET6環境下如何內建Rapid SDK三維控件,請首先確定已經安裝了Vistual Studio 2022,社群版就夠用了。

1 建立項目

選擇建立Windows窗體應用

十行代碼搭建基于.NET6三維應用程式

給程式起一個酷酷的名字,選一個酷酷的位置:

十行代碼搭建基于.NET6三維應用程式

選一下.NET6

十行代碼搭建基于.NET6三維應用程式

2 配置項目

從nuget.org上或者本地安裝AnyCAD Rapid SDK 2022。

下載下傳連結: ​​https://pan.baidu.com/s/1FQQpwjImo-T3thUk5E1sQw​​

提取碼: 9d86

十行代碼搭建基于.NET6三維應用程式

3 設計界面

給三維界面留個位置,采用經典的所有視窗。右邊用來顯示三維内容。

十行代碼搭建基于.NET6三維應用程式

4 建立三維控件

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

運作一下:

十行代碼搭建基于.NET6三維應用程式

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

再運作一下:

十行代碼搭建基于.NET6三維應用程式

6 資源釋放

在調試模式下,程式退出的時候在輸出視窗中,你可能會發現這樣的錯誤:

程式“[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();
        }
    }
}      

7 總結

.NET6為開發者帶來了高效的開發體驗,而AnyCAD Rapid SDK也一樣,通過簡單幾步即可為應用添加三維能力,讓程式顯得高大上!