1、在笔记6中创建了一个绘制直线的命令,若其他基本绘图命令都按那个写法会麻烦得要死,所以要总结其中的公共复用部分包封到一个统一的类中,这样在以后使用起来,特别是移植VBA代码时更方便,代码如下:
Public Class AcModelSpace
Public Shared Function Addline(Starpoint As Point3d, Endpoint As Point3d) As ObjectId
Dim Ent As New Line(Starpoint, Endpoint)
Dim EntID As ObjectId = AppendEnt(Ent)
Return EntID
End Function
Public Shared Function AppendEnt(Ent As Entity) As ObjectId
Dim db As Database = HostApplicationServices.WorkingDatabase
Dim EntID As ObjectId
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)
EntID = Btr.AppendEntity(Ent)
Trans.AddNewlyCreatedDBObject(Ent, True)
Trans.Commit()
End Using
Return EntID
End Function
End Class
2、myline命令中的代码也要做出对应的改变,是不是比之前简洁多了?
<CommandMethod("MyLineNew")>
Public Sub MyLineNew()
Dim db As Database = HostApplicationServices.WorkingDatabase
Dim Pt1 As New Point3d(0, 0, 0)
Dim Pt2 As New Point3d(100, 100, 0)
AcModelSpace.Addline(Pt1, Pt2)
End Sub
3、按此思路框架,扩展类AcModelSpace,添加一系列的AddXXX方法,便可轻松的清晰的扩展绘图命令的内容