c 语言cad 二次开发,c#对AutoCAD二次开发
對AutoCAD進行二次開發可以使用:ObjectArx,VBA,VLisp。但在這里不借用它們,而是直接使用C#開發。
有類庫和應用程序兩種方式:
方法1:vs2010 開發AutoCAD 2008 類庫
建立動態庫,從AutoCAD命令行使用NETLOAD調入,然后執行其方法
一 創建項目
1,建一個wxindows窗體程序“項目”,設置輸出為“類庫”
2,添加引用--瀏覽--從AutoCAD2008的安裝目錄C:\Program Files\Autodesk\MDT
2008下,找到引用acdbmgd.dll和acmgd.dll
3,引用如下命名空間
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Runtime;
4,方法名前,加特性 CommandMethod
5,完整代碼如下:
using System;
using System.Collections.Generic;
using System.Text;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Runtime;
namespace ClassLibrary2
{
public class
Class1
{
[CommandMethod("HelloWorld")]
public void HelloWorld()
{
Editor ed =
Application.DocumentManager.MdiActiveDocument.Editor;
ed.WriteMessage("HelloWorld CAD!");
}
}
}
二?工程的目標框架框架版本
在 vs2010 中 開發,默認的版本是.NET Framework 4.0版本高,對于引用AutoCAD
2008,講無法編譯
在工程的屬性中,目標框架是 改為 .NET Framework 2.0 或者.NET Framework 3.5 就可以了
三 調試的方法設置
vs2010生成 AutoCAD2008 類庫調試方法
屬性中 --調試-- 外部啟動程序:
C:\Program Files\Autodesk\MDT 2008\acad.exe
四?啟動AutoCAD 2008
1 在vs2010的開發環境,編譯鏈接完成后,按F5鍵啟動調試,等待自動啟動AutoCAD 2008完成后,
2 在命令行輸入:NETLOAD,彈出裝入類庫的對話框,
瀏覽找到剛編譯形成的類庫,ClassLibrary2\ClassLibrary2\bin\Debug\ClassLibrary2.dll
3?在命令行輸入: HelloWorld,
將會提示是:未知的命令,原因是acdbmgd.dll acmgd.dll版本過高所致
五?重新引入較低版本的動態庫
AutoCAD 2008?自帶的動態庫 acdbmgd.dll acmgd.dll
版本 17.1.0.0,運行時版本 v2.0.50727
版本高?,在vs2010工程中引用后,雖可以生成的動態庫,但在AutoCAD中NETLOAD可以裝入,但執行其中的方法,提示是未知的命令,有兩種方法:
1 引用AutoCAD的安裝目錄下的acdbmgd.dll 和acmgd.dll的版本是 17.1.0.0,從其屬性中
將“復制本地” 改為 “False”,這樣形成的dll 的方法,在AutoCAD中就可以認識,不再是未知的命令
2 可以到網上下載較低的版本,比如我下載如下的版本,添加引用它們就可以了
版本 16.2.54.0
運行時版本 v1.0.3705
===================================
方法2:c#建立一個操AutoCAD2008的應用程序
一 首先建立一個 基于WIndowsFormApplicaton的項目
二 類型庫的添加引用
右擊項目的“引用”--“添加引用”--從“COM”頁,找到以下兩個類型庫
1 AutoCAD 2008 Type Library
引用名稱?----- 對應的動態庫
AutoCAD?-----
Autodesk.AutoCAD.Interop.dll?嵌入互操作類型 False
2 Autodesk AutoCAD Mechanical 1.0 Type Library
引用名稱?----- 對應的動態庫
AcadmAuto ----- Interop.AcadmAuto.dll,嵌入互操作類型 False
AXDBLib?-----
Autodesk.AutoCAD.Interop.Common.dll
GEAuto?-----
Interop.GEAuto.dll
三 主要的操作函數
1 使用的文件中加入語句
using AutoCAD = Autodesk.AutoCAD.Interop;
using System.Runtime.InteropServices;
using dbx = Autodesk.AutoCAD.Interop.Common;
2 注操作代碼
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms;
using AutoCAD = Autodesk.AutoCAD.Interop;
using dbx = Autodesk.AutoCAD.Interop.Common;
using SmartSoft.ACAD;
namespace aotuCADwinFrm
{
public
partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
AutoCADConnector acd=new AutoCADConnector();//生成操作類對象
dbx.AxDbDocument doc_as = acd.GetThisDrawing("c:\\doc_as.dwg",
"");
dbx.AxDbDocument acddoc = acd.GetThisDrawing("c:\\D1.dwg",
"");//打開圖形文件
//?dbx.AcadBlockReference brf =
acd.GetBlockReference(acddoc,"pp");
acd.GetEntityReference(acddoc, doc_as);//刪除不需要的實體
acd.Dispose();
}
}
}
3 使用的改造自網上的類
using System;
using AutoCAD = Autodesk.AutoCAD.Interop;
using System.Runtime.InteropServices;
using dbx = Autodesk.AutoCAD.Interop.Common;
namespace SmartSoft.ACAD
{
///
///
讀取AutoCAD屬性信息
///
public class
AutoCADConnector : IDisposable
{
private AutoCAD.AcadApplication _Application;
private bool _Initialized;
private bool _Disposed;
private dbx.AxDbDocument doc_as;
#region 類初始化及析構操作
///
/// 類初始化,試圖獲取一個正在運行的AutoCAD實例,
/// 如果沒有則新起動一個實例。
///
public AutoCADConnector()
{
try
{
//取得一個正在運行的AUTOCAD實例
this._Application =
(AutoCAD.AcadApplication)Marshal.GetActiveObject("AutoCAD.Application.17");
}//end of try
catch
{
try
{
//建立一個新的AUTOCAD實例,并標識已經建立成功。
_Application = new AutoCAD.AcadApplicationClass();
_Initialized = true;
}
總結
以上是生活随笔為你收集整理的c 语言cad 二次开发,c#对AutoCAD二次开发的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C语言程序模拟银行输入密码,模拟银行输入
- 下一篇: 约瑟夫环c语言计蒜客链表,约瑟夫环的故事