この演習では、AutoCAD 図面を新規に作成し、その図面に 1 行の文字を追加し、図面を保存します。これらをすべて VBA から行います。
[コード]の順に選択して、ThisDrawing クラス モジュールのコード ウィンドウを開きます。
[プロシージャ]の順に選択して、プロジェクトに新しいプロシージャを作成します。
' Create a new drawing
ThisDrawing.Application.Documents.Add
Dim insPoint(0 To 2) As Double 'Declare insertion point
Dim textHeight As Double 'Declare text height
Dim textStr As String 'Declare text string
Dim textObj As AcadText 'Declare text object
insPoint(0) = 2 'Set insertion point X coordinate
insPoint(1) = 4 'Set insertion point Y coordinate
insPoint(2) = 0 'Set insertion point Z coordinate
textHeight = 1 'Set text height to 1.0
textStr = "Hello World!" 'Set the text string
'Create the Text object
Set textObj = ThisDrawing.ModelSpace.AddText _
(textStr, insPoint, textHeight)
ThisDrawing.SaveAs("Hello.dwg")
[サブ/ユーザー フォームを実行]の順に選択して、HelloWorld サブル チンを実行します。
サブルーチンの実行が終了したら、AutoCAD のアプリケーション ウィンドウに切り替えます。Hello World! という文字が図面に表示されているはずです。図面名は Hello.dwg となります。