AutoCAD二次开发AutoCAD.NET API开发环境搭建
AutoCAD二次开发——AutoCAD.NET API开发环境搭建
AutoCAD二次开发——AutoCAD.NET API开发环境搭建
AutoCAD二次开发工具:1986年AutoLisp,1989年ADS,1990年DCL,1993年ADS-RX,1995年ObjectARX,1996年Active X Automation(COM),1997年VBA,1998年Visual Lisp,2006年.net API(DLL).
趋势和方向:AutoCAD.net API(AutoCAD2006出现,形式是用netload调用DLL插件)
ACAD、ARX、VS、.NetFramework版本对应关系。
AutoCAD VS .NetFramework
2015 2012 4.5
2012-2014 2010/2012 4.0 也有用vs10开发在08cad运行 .net4.0有DLR
2010-2011 2008 3.5 cad10-12用户28%第二多 推荐 也有用vs08在cad08运行 .net3.0之后有LINQ,wpf,wf,wcf
下面没有wpf,LINQ,WCF,WF,所以不建议使用。
2007-2009 2005 2 cad用户37%最多 64位的有07和15,这个版本是能兼容最多的,这是最低的要求版本了。 VC从vs2005才有vc8
2004-2006 2002 用户27% 06年才开发出.netAPI,所以不能用低于06
由于ARX和AutoCAD都是Autodesk公司出品的,所以版本号一致。
选择版本对应最关键的是VS引用的DLL必须要和目标环境AutoCAD一致。其次是FrameWork版本。上面只是最佳组合,但不是硬性要求。
CAD官方:
VS
CAD ARX Wiz
CAD.NET Wiz
08
12
10
12/13
10-12/13
12
14/15/16
14/15/16
13
16
office
vs2010 office 07 10
vs2012 office 10 13
vs2015 office 10 13 16
综合:
vs2010 cad12/13 office 07/10
vs2012 cad 14/15/16 office 10/13
首选:vs10(C#,C++,VSTO) cad13 office 10
VS2015(C#) office 10
最低标准: vs2010 framework3.5 cad2007 arx2007 sql 2008R2
经典环境: win7 vs2010 cad2013/2 arx2013
时尚环境: win10 vs2012 cad2016
最新版本: framework 2015 cad2016 sql 2014
因考虑到CAD二次开发是为广大用户使用,而且必须基于一定的CAD版本和framework版本,为尽量保证开发的软件能在大多数平台使用,而且开发环境还比较人性化,且具有大多数功能,采用经典环境开发。如果个人或指定环境使用,则建议采用时尚环境,可以在win10这样通用平台下运行,还可以加入应用商店。
搭建环境步骤:
1、 安装 ObjectARX SDK (Arx_All_2007) (里面有Interop的两个lib,因为CAD默认位置不确定不好找。)
2、 安装 autocad_2010-2012_dotnet_wizards (开发向导,免去手动操作的麻烦)
方式有两种:①手动建立类文件dll,②用dotNet Wizard向导自动完成。建议向导方便快捷
方法一:手动建立类库
1、 vs中新建类库。
2、 添加引用:
D:\Program Files (x86)\AutoCAD 2007\ (经对比与ObjectARX 2007里面的一样,所以用cad目录的)
AcCui.dll AcDbMgd.dll AcMgd.dll 完成之后显示:AcCui acdbmgd acmgd
D:\Program Files (x86)\AutoCAD2007\ObjectARX 2007\inc
acax17ENU.tlb axdb17enu.tlb
完成之后显示: AutoCAD, AXDBLib
(注:AutoCAD 2014拆出一个AcCoreMgd.dll,当处理编辑器、发布与打印、定义AutoLISP命令和函数时引用。)
3、 把引用的文件属性,复制本地改为False
4、 在项目文件上右键属性—应用程序——目标框架.NETFramework3.5 输出类型:类库,
生成————目标平台:Any CPU
调试————启动外部程序:D:\Program Files (x86)\AutoCAD 2007\acad.exe
5、 添加using,写代码。
//************** using的内容如下 **************//
// Visula Studio2010 //
//*******************************************//
using System;
using System.Collections.Generic;
using System.Text;
//*******************************************//
// Type Library //
//*******************************************//
using Autodesk.AutoCAD.Interop; // AutoCAD 2008 TypeLibrary
using Autodesk.AutoCAD.Interop.Common; //AutoCAD/ObjectDBXCommon 17.0 Type Library
using Autodesk.AutoCAD.Customization; //accui.dll
//*******************************************//
// acdbmgd.dll //
//*******************************************//
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.LayerManager;
using Autodesk.AutoCAD.GraphicsSystem;
using Autodesk.AutoCAD.GraphicsInterface;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.DatabaseServices.Filters;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Colors;
//******************************************//
//--------------------------------------------//
// acmgd.dll //
//------------------------------------------*//
using Autodesk.AutoCAD.Windows.ToolPalette;
using Autodesk.AutoCAD.Windows;
using Autodesk.AutoCAD.Publishing;
using Autodesk.AutoCAD.PlottingServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.ApplicationServices;
方法二:用wizards直接建立,能自动引用指定dll,设置调试cad路径,还能写两个cs文件,一个命令的,一个启动关闭的。
1、安装autocad_2010-2012_dotnet_wizards。
2、运行vs2010——新建c#--Autodesk—AutoCADplug-in,选择.NET Framework 3.5
3、ObjectARX路径:D:\ProgramFiles (x86)\AutoCAD 2007
CAD路径:D:\Program Files(x86)\AutoCAD 2007
选择 Interop Common , Interop , AcCui
完成之后显示:AcCui acdbmgd acmgd
Interop Interop.common (应该与acax17ENU.tlb,axdb17enu.tlb一样,与其它二次开发方式交互时用,如VBA、LISP、ARX)
Autodesk.AutoCAD.Interop: C:\windows\assembly\GAC_MSIL\Autodesk.AutoCAD.Interop\17.0.54.0__eed84259d7cbf30b\Autodesk.AutoCAD.Interop.dll
Autodesk.AutoCAD.Interop.Common:C:\windows\assembly\GAC_MSIL\Autodesk.AutoCAD.Interop.Common\17.0.54.0__eed84259d7cbf30b\Autodesk.AutoCAD.Interop.Common.dll
4、调试路径,引用复制本地false,Framework3.5, x86,64选Any CPU都已经默认设置好,using必备的也写了,不需操作。
5、默认两个类,一个myCommands.cs myPlugin.cs
myCommands.cs:用于命令方式
myPlugin.cs:软件开启或关闭时执行
helloworld操作代码:
[CommandMethod("hello")]
public static void hello()
{
Editor ed =Application.DocumentManager.MdiActiveDocument.Editor;
ed.WriteMessage("Hello World");
}
以下是helloworld示例代码:
using System;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
//加速加载,加载程序时直接注册类中命令,否则搜索每个类中命令。
[assembly: CommandClass(typeof(WizardDotNetAPI.MyCommands))]
namespace WizardDotNetAPI
{
public class MyCommands
{
[CommandMethod("hello",CommandFlags.Session)]
public void hello()
{
//****在这里写我们的插件功能***//
Editor ed =Application.DocumentManager.MdiActiveDocument.Editor;
ed.WriteMessage("HelloWorld");
//这里我们写的功能就要用到各命名空间里的类、接口、方法,这是学习的重点//
}
}
}
自动运行代码:
using System;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
[assembly:ExtensionApplication(typeof(WizardDotNetAPI.MyPlugin))]
namespace WizardDotNetAPI
{
publicclass MyPlugin : IExtensionApplication
{
voidIExtensionApplication.Initialize()
{
//Initialize your plug-in application here
}
voidIExtensionApplication.Terminate()
{
//Do plug-in application clean up here
}
}
}
第二部分:编译和调试
(1)手动加载
点击生成>生成解决方案,或者 调试——启动调试。
启动AutoCAD——netload——选择D:\Documents\VisualStudio 2010\Projects\FirstCADdll\FirstCADdll下的类文件.dll
Netload加载的程序不能卸载,要想再调试只能退出Autocad,然后重新编译、加载。
(2)自动加载
菜单 项目>属性>调试>启动操作,选择启动外部程序, AutoCAD.exe 。
温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/63206.html