【Hello AutoCAD 005】几个常用的绘图函数
1 '创建直线
2 Public Function addLine(ByVal startPt As Point3d, ByVal endPt As Point3d) As ObjectId
3 '在内存中创建一个直线对象
4 Dim LineEnt As New Line(startPt, endPt)
5 '调用AppendEntity函数,将直线加入到模型空间
6 Dim entID As ObjectId = AppendEntity(LineEnt)
7 Return entID
8 End Function
9
10 '创建圆
11 Public Function addCircle(ByVal cenPt As Point3d, ByVal Radius As Double) As ObjectId
12 '在内存中创建一个圆对象
13 Dim Ent As New Circle(cenPt, Vector3d.ZAxis, Radius)
14 Dim entID As ObjectId = AppendEntity(Ent)
15 Return entID
16 End Function
17
18 '创建圆弧【由圆心、半径、起始角度和终止角度创建圆弧】
19 Public Function addArc(ByVal cenPt As Point3d, ByVal Radius As Double, ByVal startAng As Double, ByVal endAng As Double) As ObjectId
20 '在内存中创建一个圆弧对象
21 Dim Ent As New Arc(cenPt, Radius, startAng, endAng)
22 Dim entID As ObjectId = AppendEntity(Ent)
23 Return entID
24 End Function
25 '由二维点集合和线宽创建二维优化多线段函数
26 Public Function addPline(ByVal Pts As Point2dCollection, ByVal Width As Double) As ObjectId
27 Try
28 '得到点集合的数量
29 Dim n As Integer = Pts.Count
30 '在内存中创建一个二维优化多线段对象
31 Dim Ent As New Polyline(n)
32 '向多线段添加顶点
33 For i As Integer = 0 To n - 1
34 Ent.AddVertexAt(i, Pts.Item(i), 0, Width, Width)
35 Next
36 Dim entID As ObjectId = AppendEntity(Ent)
37 Return entid
38 Catch ex As Exception
39 '创建失败,则返回一个空的ObjectId
40 Dim nullId As ObjectId = ObjectId.Null
41 Return nullId
42 End Try
43 End Function
44
45 '将图形对象加入到模型空间的函数
46 Public Function AppendEntity(ByVal Ent As Entity) As ObjectId
47 '得到当前文档的数据库对象
48 Dim Db As Database = HostApplicationServices.WorkingDatabase
49 Dim entID As ObjectId
50 Using Trans As Transaction = Db.TransactionManager.StartTransaction()
51 '以读方式打开块表
52 Dim Bt As BlockTable = CType(Trans.GetObject(Db.BlockTableId, OpenMode.ForRead), BlockTable)
53 '以写方式打开模型空间块表记录
54 Dim Btr As BlockTableRecord = CType(Trans.GetObject(Bt.Item(BlockTableRecord.ModelSpace), OpenMode.ForWrite), BlockTableRecord)
55 '将图形对象的信息加到块表记录中,并返回ObjectId对象
56 entID = Btr.AppendEntity(Ent)
57 '把直线添加到事物处理中
58 Trans.AddNewlyCreatedDBObject(Ent, True)
59 '提交事物处理
60 Trans.Commit()
61 End Using
62 Return entID
63 End Function
64