
一辺が100の五角形を描くVB.netプログラム。任意の中心を指定するだけ。
VBのソースコードの部分だけ以下のようにChatGPTに質問して書いてもらった。
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.Geometry
Imports Autodesk.AutoCAD.Runtime
Public Class Class1
<CommandMethod("PENTAGON")>
Public Sub DrawPentagon()
' ダイアログを表示して中心点を取得
Dim dialogResult As PromptPointResult = GetPoint("Specify center point: ")
If dialogResult.Status <> PromptStatus.OK Then
Exit Sub
End If
Dim centerPoint As Point3d = dialogResult.Value
' 一辺が100の五角形を描画
DrawRegularPentagon(centerPoint, 100)
End Sub
Private Function GetPoint(message As String) As PromptPointResult
Dim editor As Editor = Application.DocumentManager.MdiActiveDocument.Editor
Dim promptOptions As PromptPointOptions = New PromptPointOptions(message)
Dim pointResult As PromptPointResult = editor.GetPoint(promptOptions)
Return pointResult
End Function
Private Sub DrawRegularPentagon(centerPoint As Point3d, sideLength As Double)
Dim doc As Document = Application.DocumentManager.MdiActiveDocument
Dim db As Database = doc.Database
Using trans As Transaction = db.TransactionManager.StartTransaction()
Try
' ブロックテーブルとブロックテーブルレコードを開く
Dim bt As BlockTable = trans.GetObject(db.BlockTableId, OpenMode.ForRead)
Dim btr As BlockTableRecord = CType(trans.GetObject(bt(BlockTableRecord.ModelSpace), OpenMode.ForWrite), BlockTableRecord)
' 一辺が100の五角形を作成
Dim pentagonPoints As Point2dCollection = GetRegularPentagonPoints(centerPoint, sideLength)
Dim poly As Polyline = New Polyline()
For Each point As Point2d In pentagonPoints
poly.AddVertexAt(poly.NumberOfVertices, point, 0, 0, 0)
Next
poly.Closed = True
btr.AppendEntity(poly)
trans.AddNewlyCreatedDBObject(poly, True)
trans.Commit()
Catch ex As Exception
Application.ShowAlertDialog("Error: " & ex.Message)
trans.Abort()
End Try
End Using
End Sub
Private Function GetRegularPentagonPoints(centerPoint As Point3d, sideLength As Double) As Point2dCollection
Dim points As Point2dCollection = New Point2dCollection()
For i As Integer = 0 To 4
Dim angle As Double = i * (2 * Math.PI) / 5
Dim x As Double = centerPoint.X + sideLength * Math.Cos(angle)
Dim y As Double = centerPoint.Y + sideLength * Math.Sin(angle)
points.Add(New Point2d(x, y))
Next
Return points
End Function
End Class
AutoCADでコマンド「PENTAGON」と入力し、中心を指定すると・・・
このように一瞬で一辺100の五角形が出現した。マウスでカチャカチャやったら何秒かかるだろうか?
小さなプログラムのブロックをたくさん作って組み合わせていけば、作図スピードが何十倍にも上がるのは言うまでもない。