# .Net 环境搭建
# 1 准备工作
在Windows下,AnyCAD Rapid使用基于VC++编译,运行时候依赖Vistual C++ 运行时库。因此,64位版本需要在客户机上安装vc_redist.x64.exe,32位版本需要安装vc_redist.x86.exe。
# 1.1 vc_resit下载:
百度盘下载地址:
链接: 百度云
提取码: d8u4
微软官方下载(推荐):
- x86: vc_redist.x86.exe
- x64: vc_redist.x64.exe
# 1.2 SDK下载
推荐使用nuget管理AnyCAD .Net SDK,方便升级和环境配置。
TIP
快速通道:直接从github下载sample示例:anycad/anycad.rapid.net
# 2 添加程序集引用
使用nuget自动添加.Net程序集引用
TIP
若访问nuget很慢,可以关注anycad微信公众号,下载最新版本的本地包。
# 3 工程项目属性设置
WinForms .Net项目默认为AnyCPU,为满足三维应用程序的高性能要求,建议设置项目首选64位环境,即禁用首选32位选项,如下图所示:
# 4 SDK初始化
假如在窗体中增加了一个splitContainer1控件,其右边部分用于显示三维内容。
只需要在窗体的构造函数InitializeComponent()后添加几行代码,就能把三维控件添加到窗体容器中:
# 方法一
public partial class MainForm : Form
{
RenderControl mRenderView;
public MainForm()
{
InitializeComponent();
mRenderView = new RenderControl();
this.splitContainer1.Panel2.Controls.Add(mRenderView); // 这里根据需要指定三维控件的容器
mRenderView.Dock = DockStyle.Fill;
}
...
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
# 方法二
一行代码初始化
public partial class MainForm : Form
{
RenderControl mRenderView;
public MainForm()
{
InitializeComponent();
mRenderView = new RenderControl(this.splitContainer1.Panel2);
}
...
}
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
TIP
目前只支持使用代码动态添加控件到窗体中,不支持直接从toolbox中添加。
至此,可以编译和运行程序,此时应该可以看到AnyCAD的三维渲染界面:
# 5 导入STEP模型验证
在File菜单上增加Open菜单项,增加响应函数:
private void stepToolStripMenuItem1_Click(object sender, EventArgs e)
{
OpenFileDialog dialog = new OpenFileDialog();
dialog.Filter = "STEP (*.stp;*.step)|*.stp;*.step";
if (dialog.ShowDialog() != DialogResult.OK)
return;
var shape = StepIO.Open(dialog.FileName);
if (shape == null)
return;
mRenderView.ShowShape(shape, Vector3.LightGray); // 用灰白色来显示模型
mRenderView.ZoomAll(); // 把模型缩放到合适位置
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
打开一个模型测试一下:
# 6 小结
通过引入AnyCAD程序集、项目属性设置、控件初始化三步,即可完成WinForms与AnyCAD三维控件集成。 再结合StepIO、IgesIO就可打造一个功能丰富的CAD Viewer了。