之前写的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
相关视频及工具下载地址: http://download.autodesk.com/media/adn/VBA_Migration/DevTV_Recording.zip
[VB.NET]
代码
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
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
[C#]
代码
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
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