AutoCAD.NET API开发环境搭建

来源:互联网 发布:逃生2剧情解析知乎 编辑:程序博客网 时间:2024/05/29 03:38
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 WizCAD.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 。


命令行参数设置:/nologo /b"D:\Documents\Visual Studio 2010\Projects\我的一个向导生成程序dotnet_wizards_helloworld\dotnet_wizards_helloworld\dotnet_wizards_helloworld\bin\Debug\start.scr"


start.scr 文件是自己编写的autocad运行脚本文件,该文件是文本文件,添加一行文本:netload "D:\Documents\Visual Studio2010\Projects\FirstCADdll\FirstCADdll\bin\Debug\FirstCADdll.dll"


这样我们就可以直接运行调试了。


(3)调试


按照以上步骤运行后,不支持断点调试,我们还应修改acad.exe.config文件,该文件在D:\Program Files (x86)\AutoCAD 2007下,增加一行<supportedRuntime version="v2.0.50727"/>内容。修改后的acad.exe.config.xml的内容如下:


<configuration>


   <startup>


<supportedRuntime version="v2.0.50727"/>


   </startup>


<!--All assemblies in AutoCAD are fully trusted so there's nopoint generating publisher evidence-->


   <runtime>       


<generatePublisherEvidence enabled="false"/>   


   </runtime>


</configuration>


小技巧:不关闭CAD调试。


Netload加载的程序不能卸载,要想再调试只能退出Autocad,每次编译都需重启AutoCAD,很麻烦,到网上搜遍了,但没有解决方法,都说是微软没开发这个功能,(看来就等着我正太君研究解决方案呢,O(∩_∩)O~)。经过我不断尝试找到一种可以几乎和Edit and Continue匹配的代替方法如下:


1、按上面步骤进行调试,任意写一个命令如Hello,在这个命令里设上断点,然后在AutoCAD运行时调用这个命令,就会进入VS的调试页面,在这里就可以进行修改了。每次需要修改时用这个hello命令。


第三部分:程序部署


AutoCAD2012之前,用注册表部署,之后用插件自动加载器。


①注册表部署:在注册表相应启动加载程序,可以用reg文件,也可以在程序里写(要引用Microsoft.Win32),如2010的位置为


[HKEY_LOCAL_MACHINE\Software\Autodesk\AutoCAD\R18.0\ACAD-8001:804\Applications\xxx]


description(描述)、loadctrls(加载控制加载方式)、loader(程序路径)、managed(程序形式Ox01托管程序)


loadctrls:0x02:CAD启动时加载,0x04命令加载,0x10从不加载,0x20:显式加载


(CAD2007_64位在注册表中没有固定位置,像Interop一样难找,用搜到F3不停搜,结果还是没找到,所以以下代码未测试成功。)


reg文件内容如下:


[HKEY_LOCAL_MACHINE/SOFTWARE/Autodesk/AutoCAD/R17.0/ACAD-5001:804/Applications/yds]


"DESCRIPTION"="加载自定义dll文件"


"LOADCTRLS"=dword:0000000c


"MANAGED"=dword:00000001


"LOADER"=" D:\\Documents\\testdll\\hello.dll"


[HKEY_LOCAL_MACHINE/SOFTWARE/Autodesk/AutoCAD/R17.0/ACAD-5001:804/Applications/yds/Commands]


"ShowPalette"="ShowPalette"


5001表示autocad2007;LOADCTRLS表示描述装载的方式,值为12表示只有当发出命令时dll才被加载,当为2时启时就被加载(不过我改成2,启动时还是没有自动加载);MANAGED的值只能为1,表示它是个托管程序。


再合并此注册表,下次启动autocad2007就可自动加载相关的dll了,并且执行哪此事命令了。


②:配置文件部署:插件自动加载器,ApplicationPlugins里放软件包xxx.bundle(包含contents\dll和PackageContents.xml),CAD启动时,自动将PackageContents.xml中数据注册加载。


CAD目录位置:C:\ProgramFiles\Autodesk\ApplicationPlugins


XP我的文档位置:C:\Documentsand Settings\Administor\Application Data\Autodesk\ApplicationPlugins


Win7我的文档位置:c:\Users\Administrator\AppData\Roaming\Autodesk\ApplicationPlugins


③用lisp语言调用“Netload”命令,自动加载dll。


① lsp文件内容如下:


(COMMAND "netload" " D:\\Documents\\testdll\\hello.dll ")


(Command "hello")


第一句表示让autocad加载指定的dll,用\\,表示转义,避免出现\t等特殊符号。


第二句表示执行名为"hello"的命令。如此dll中有hello这个命令,就会立即执行。


启动autocad2007 ,工具->Autocadlisp->加载应用程序 在弹出的窗体的启动组中添加上述lsp文件路经。


②另外可以定义一个命令调用,不直接启动。


(defun c:hello()


(COMMAND "netload" "D:\\Documents\\testdll\\hello.dll")


(Command "hello")


(princ)


)


上边的hello是定义的函数名,下边的是dll里面的命令,第一次执行hello,执行上边的lsp内容,同时里面有个hello是dll的命令,第二次执行就直接执行dll的命令了。


③另一种调用lsp方法(经测试失败,没找到原因):在AutoCAD安装目录找到D:\Program Files (x86)\AutoCAD 2007\Support\acad2007.lsp


用记事本打开,在最后加入(下段代码第二行即可,注意路径)


(command "netload" "D:\Documents\testdll\hello.dll")


完整代码是:


(arxload"vel.arx")


(if (not (=  (substr(ver) 1 11) "Visual LISP")) (load "acad2007doc.lsp"))


(command "netload" "D:\Documents\testdll\hello.dll")


;; Silent load.


(princ)


2. AutoCAD设置(重要,必须设置):工具-选项-文件-支持文件搜索路径-添加-浏览到hello.dll路径


④、用.NET程序加载.NET程序集,用到了微软的核心类库mscorlib中的System.Reflection命令空间。加载时只要一个函数Assembly.LoadFrom()可以了,速度很快。此法可随时后台加载.NET程序集,但托管的程序集无法卸载,直到关闭AutoCAD为至。方法源自Kean (C#),未测试:


源自:http://www.lubanren.net/weblog/post/209.html


usingAutodesk.AutoCAD.ApplicationServices;


usingAutodesk.AutoCAD.EditorInput;


usingAutodesk.AutoCAD.Runtime;


usingSystem.Reflection;


namespaceLoadModule


{


    public class Commands


    {


        [CommandMethod("MNL")]


         static public void MyNetLoad()


         {


             Document doc =


              Application.DocumentManager.MdiActiveDocument;


             Editor ed = doc.Editor;


             PromptStringOptions pso =


               new PromptStringOptions(


                 "\n输入要加载的程序集全路径: "


               );


             pso.AllowSpaces = true;


             PromptResult pr =ed.GetString(pso);


             if (pr.Status != PromptStatus.OK)


                 return;


             try


             {


                 Assembly.LoadFrom(pr.StringResult);


             }


             catch (System.Exception ex)


             {


                 ed.WriteMessage(


                   "\n无法加载程序集{0}: {1}",


                   pr.StringResult,


                   ex.Message


                 );


             }


         }


     }


}


建议:算法是核心,什么语言,环境,部署,都是外表,关键是学思维和算法。


参考资料:


objectarx.net论坛:http://www.objectarx.net/forum.php?mod=forumdisplay&fid=6


英文手册:


http://www.mjtd.com/helpcenter/netguide/index.html?url=WS73099cc142f48755-5c83e7b1120018de8c0-23fe.htm,topicNumber=d0e79


官方地址:http://usa.autodesk.com/adsk/servlet/index?id=1911627&siteID=123112


官方教程2015开发向导:http://help.autodesk.com/view/ACD/2015/ENU/?guid=GUID-C3F3C736-40CF-44A0-9210-55F6A939B6F2


明经通道:http://www.mjtd.com/caddevelop/info-61-1094.html


鲁班人[Ben'sBlog] 土木工程知识:http://www.lubanren.net/weblog/catalog.asp?cate=6&page=2
明经通道     http://www.mjtd.com/
晓东CAD     http://bbs.xdcad.cn/
版本问题:
http://blog.csdn.net/zx_pisan/article/details/6311472
http://bbs.mjtd.com/forum.php?mod=viewthread&tid=100420 ;


书籍:截止2015.1.19,搜遍所有网络,英文资料很多,但理解困难,中文就以下四本书最符合.net API学习,其余全都看过,不行。只要学好以下四本书就足够了!
C#  AutoCAD.Net API


1.  AutoCAD VBA &VB.NET开发基础与实例教程(第2版)(C#版) 2013-6 88.00元 中国电力出版社 曾洪飞,卢择临,张帆 页 数621 AutoCAD 2012Visual Studio 2010  C#


2.  [深入浅出AutoCAD.NET二次开发].李冠亿 中国工业出版社 2012.2 定价78 带光盘  P485   以.net为主,结合各种二次开发方式。


3.  基于Visual C#的AutoCAD 开发及其在工程中的应用 肖泽云 三峡大学  2010.4 P218网络版无书 用的ActiveX,但内容是土木工程CAD二次开发,主要学习算法思路。


4.  AutoCAD .NET 开发指南2012 版 官网翻译网络版,P629 孙成波2014.2.8  AutoCAD .NET Developer's Guide2012 版翻译 ,官方权威手册,内容太全,太多,不适合学习,遇到问题查找时用。
C++ ObjectARX
AutoCAD ObjectARX 开发基础与实例教程 张帆、朱文俊 2014-06-01 VC++ 6.0和VS 2005 arx2002 cad2002 
AutoCAD2010二次开发实例教程(ObjectARX)  王文波 2013年4月 机械专业用
公路工程CAD:
1. 土木工程CAD技术 清华大学 任爱珠 张建平  2006.5 带光盘     P450    综合介绍CAD,ARX为主,基础知识,主要学习图形学基本算法。
2.公路计算机辅助工程 2000 朱照宏:理论,比较综合概括。
《道路勘测设计软件开发与应用指南》 朱照宏 2003 数模部分好
3.公路计算机辅助设计 符锌砂
4.道路路线CAD 朱照宏 
5.道路工程CAD 杨宏志 于娇 许金良 周艳 
6.道路与桥梁工程计算机绘图 许金良 黄安录
道路CAD及其使用教程、工程实例
公路工程CAD基础教程 郑益民
计算机辅助工程 张叔辉
公路CAD  张郃生


本文由正态分布X~N(μ,σ2 ) QQ2052702900写于2015年1月21日,为近几天所学知识总结。


【注】所有文字,均为个人学习总结,仅当个人留存。若君不幸踏访,切勿讥讽,确有可取之处,不妨随心借取,确有错漏之处,还望不吝赐教。


vs2010旗舰版2.5G,经典,装完10G,实测安装c#,C++和VSTO共4.59G
2012 Ultimate旗舰版1.5G ,少了msdn文档 和 SQLEXPRESS,有在线安装部分。安装完不带文档3G,带文档13G
vs2015大约要占30G。
如果系统带.net framework那就会少占点。不带会自动装上 
开源CAD:http://blog.csdn.net/zxgis/article/details/8222729
pudn下载: http://www.pudn.com/sourcecode/graph/CAD/download210_2.html           ; 
书下载:  http://www.csdn.net/tag/cad%E4%BA%8C%E6%AC%A1%E5%BC%80%E5%8F%91/download