AutoCAD二次開發——AutoCAD.NET API開發環境搭建

來源:互聯網
上載者:User

標籤:

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 。

命令列參數設定:/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

AutoCAD二次開發——AutoCAD.NET API開發環境搭建

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.