之前寫的VBA代碼,如果全部用.NET改寫,比較勞命傷财,沒什麼興趣改寫。是以用了一種很偷懶的方法。
通過com方式調用AutoCAD 200x Type Library,和AutoCAD/ObjectDBX Common xx.x Type Library,定義VBA中的ThisDrawing對象,書寫比較規範的VBA代碼基本上不用做什麼修改就能運作了。
對于VBA中的窗體,可以先導出為VB窗體,然後用更新向導更新到.NET,做些必要的修改就可以了。
AutoDesk官方有相關的視訊教程,還有個導出VBA到VB的工具。VBA→VB→VB.NET
<b>[VB.NET] </b>
<b></b>

代碼
1 Imports Autodesk.AutoCAD.Interop
2
3 Public Class Class1
4
5 ReadOnly Property ThisDrawing() As Autodesk.AutoCAD.Interop.AcadDocument
6 Get
7 Return Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.AcadDocument
8 End Get
9 End Property
10
11 <Autodesk.AutoCAD.Runtime.CommandMethod("TEST")> _
12 Sub test()
13 ThisDrawing.Utility.Prompt("Hello World!")
14 End Sub
15
16 End Class
<b> </b>
<b>[C#] </b>

1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using Autodesk.AutoCAD.Interop;
5
6 namespace CSTest
7 {
8 public class Class1
9 {
10 public static AcadDocument ThisDrawing
11 {
12 get
13 {
14 return (AcadDocument)Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.AcadDocument;
15 }
16 }
17
18 [Autodesk.AutoCAD.Runtime.CommandMethod("TEST")]
19 public void test()
20 {
21 ThisDrawing.Utility.Prompt("Hello World!");
22 }
23
24 }
25 }
26
27