日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > C# >内容正文

C#

c 语言cad 二次开发,c#对AutoCAD二次开发

發(fā)布時間:2025/3/21 C# 52 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c 语言cad 二次开发,c#对AutoCAD二次开发 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

對AutoCAD進(jìn)行二次開發(fā)可以使用:ObjectArx,VBA,VLisp。但在這里不借用它們,而是直接使用C#開發(fā)。

有類庫和應(yīng)用程序兩種方式:

方法1:vs2010 開發(fā)AutoCAD 2008 類庫

建立動態(tài)庫,從AutoCAD命令行使用NETLOAD調(diào)入,然后執(zhí)行其方法

一 創(chuàng)建項(xiàng)目

1,建一個wxindows窗體程序“項(xiàng)目”,設(shè)置輸出為“類庫”

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!");

}

}

}

二?工程的目標(biāo)框架框架版本

在 vs2010 中 開發(fā),默認(rèn)的版本是.NET Framework 4.0版本高,對于引用AutoCAD

2008,講無法編譯

在工程的屬性中,目標(biāo)框架是 改為 .NET Framework 2.0 或者.NET Framework 3.5 就可以了

三 調(diào)試的方法設(shè)置

vs2010生成 AutoCAD2008 類庫調(diào)試方法

屬性中 --調(diào)試-- 外部啟動程序:

C:\Program Files\Autodesk\MDT 2008\acad.exe

四?啟動AutoCAD 2008

1 在vs2010的開發(fā)環(huán)境,編譯鏈接完成后,按F5鍵啟動調(diào)試,等待自動啟動AutoCAD 2008完成后,

2 在命令行輸入:NETLOAD,彈出裝入類庫的對話框,

瀏覽找到剛編譯形成的類庫,ClassLibrary2\ClassLibrary2\bin\Debug\ClassLibrary2.dll

3?在命令行輸入: HelloWorld,

將會提示是:未知的命令,原因是acdbmgd.dll acmgd.dll版本過高所致

五?重新引入較低版本的動態(tài)庫

AutoCAD 2008?自帶的動態(tài)庫 acdbmgd.dll acmgd.dll

版本 17.1.0.0,運(yùn)行時版本 v2.0.50727

版本高?,在vs2010工程中引用后,雖可以生成的動態(tài)庫,但在AutoCAD中NETLOAD可以裝入,但執(zhí)行其中的方法,提示是未知的命令,有兩種方法:

1 引用AutoCAD的安裝目錄下的acdbmgd.dll 和acmgd.dll的版本是 17.1.0.0,從其屬性中

將“復(fù)制本地” 改為 “False”,這樣形成的dll 的方法,在AutoCAD中就可以認(rèn)識,不再是未知的命令

2 可以到網(wǎng)上下載較低的版本,比如我下載如下的版本,添加引用它們就可以了

版本 16.2.54.0

運(yùn)行時版本 v1.0.3705

===================================

方法2:c#建立一個操AutoCAD2008的應(yīng)用程序

一 首先建立一個 基于WIndowsFormApplicaton的項(xiàng)目

二 類型庫的添加引用

右擊項(xiàng)目的“引用”--“添加引用”--從“COM”頁,找到以下兩個類型庫

1 AutoCAD 2008 Type Library

引用名稱?----- 對應(yīng)的動態(tài)庫

AutoCAD?-----

Autodesk.AutoCAD.Interop.dll?嵌入互操作類型 False

2 Autodesk AutoCAD Mechanical 1.0 Type Library

引用名稱?----- 對應(yīng)的動態(tài)庫

AcadmAuto ----- Interop.AcadmAuto.dll,嵌入互操作類型 False

AXDBLib?-----

Autodesk.AutoCAD.Interop.Common.dll

GEAuto?-----

Interop.GEAuto.dll

三 主要的操作函數(shù)

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);//刪除不需要的實(shí)體

acd.Dispose();

}

}

}

3 使用的改造自網(wǎng)上的類

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 類初始化及析構(gòu)操作

///

/// 類初始化,試圖獲取一個正在運(yùn)行的AutoCAD實(shí)例,

/// 如果沒有則新起動一個實(shí)例。

///

public AutoCADConnector()

{

try

{

//取得一個正在運(yùn)行的AUTOCAD實(shí)例

this._Application =

(AutoCAD.AcadApplication)Marshal.GetActiveObject("AutoCAD.Application.17");

}//end of try

catch

{

try

{

//建立一個新的AUTOCAD實(shí)例,并標(biāo)識已經(jīng)建立成功。

_Application = new AutoCAD.AcadApplicationClass();

_Initialized = true;

}

總結(jié)

以上是生活随笔為你收集整理的c 语言cad 二次开发,c#对AutoCAD二次开发的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。