.net编写绘制直线已完全不同于ActiveX的(VBA)的方式,过程更类似于arx程序,需要通过操作AutoCAD 数据库添加对象!下面的代码是在以前代码基础上添加了一个新myline命令。
AutoCAD 数据库结构
myline命令代码
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.Geometry
Imports Autodesk.AutoCAD.Internal
Imports Autodesk.AutoCAD.Runtime
Public Class WellcomCAD
Implements IExtensionApplication
Dim acapp As AcadApplication
Public Sub Initialize() Implements IExtensionApplication.Initialize
'Dim ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor
'ed.WriteMessage("net程序已经初始化!")
'acapp = Application.AcadApplication
MsgBox("net程序已经初始化!",, "WellcomCAD")
End Sub
Public Sub Terminate() Implements IExtensionApplication.Terminate
Debug.WriteLine("net程序即将销毁")
End Sub
<CommandMethod("Hello")>
Public Sub Hello()
Dim ed As Editor = Application.DocumentManager.MdiActiveDocument.Editor
ed.WriteMessage("欢迎进入cad世界!")
'acapp = Application.AcadApplication
'acapp.ActiveDocument.Utility.Prompt("prompt")
End Sub
<CommandMethod("MyLine")>
Public Sub MyLine()
Dim db As Database = HostApplicationServices.WorkingDatabase
Dim Pt1 As New Point3d(0, 0, 0)
Dim Pt2 As New Point3d(300, 100, 0)
Dim LinEnt As New Line(Pt1, Pt2)
Using Trans As Transaction = db.TransactionManager.StartTransaction()
Dim Bt As BlockTable = trans.GetObject(db.BlockTableId, OpenMode.ForRead)
Dim Btr As BlockTableRecord = trans.GetObject(Bt.Item(BlockTableRecord.ModelSpace), OpenMode.ForWrite)
Btr.AppendEntity(LinEnt)
Trans.AddNewlyCreatedDBObject(LinEnt, True)
Trans.Commit()
End Using
End Sub
End Class
运行结果