Getting Started with AutoCAD VBA 3 : Get inputs from user and display output
learn how to get inputs from user via using VBA
Getting Started with AutoCAD VBA 3 : Get inputs from user and display output
Overview
- In this tutorial I’ll show you how to use VBA to get input from user
- I am assuming that
- you’ve already installed AutoCAD VBA Module
- you have basic knowledge of
VBA
and how to create new method or functions - you already know how to draw basic objects , if not please go through this post first : Getting Started with AutoCAD VBA 1 : Line, Polyline, Circle, Arc, Rectangle, Point
Setup on AutoCAD
- Open blank AutoCAD file with default template, open Visual Basic Editor and Add new module
- Add any sample Code from below and just run it, try to change values like text, text Height, coordinates re-run it.
- Sample codes for each basic objects are given below. You can copy paste this code to
VBA
editor to directly run it without any inputs - Current code is very simple, I’ll try to add bit more details into this code in future, like code to modify it’s different properties
- This is very basic code and self-explanatory, if you still need help then use AI tools like ChatGPT to understand this code, only contact me if everything else fail 😅
Display output to user
AutoCAD terminal
1
2
3
4
Sub SendMessage()
'Added vbCrLf for new line
ThisDrawing.Utility.Prompt "Hello World" & vbCrLf
End Sub
1
2
3
4
5
Sub SendMessage()
Dim radius As Double
radius = 5
ThisDrawing.Utility.Prompt "Radius=" & radius & vbCrLf
End Sub
Message box
1
2
3
Sub SendMessage()
MsgBox ("Hello World")
End Sub
Send Command
1
2
3
Sub SendCommand()
ThisDrawing.SendCommand "_Circle" & vbCr & "0,0,0" & vbCr & "10" & vbCr
End Sub
Get Inputs
Get value from AutoCAD terminal
1
2
3
4
5
6
7
8
Sub GetString()
'Get string input
Dim title As String
title = ThisDrawing.Utility.GetString(True, "Enter Drawing Title : ")
'Print title
ThisDrawing.Utility.Prompt "Your New Drawing title is " & title & vbCrLf
End Sub
1
2
3
4
5
6
Sub GetDecimalValue()
Dim radius As Double
radius = ThisDrawing.Utility.GetReal("Enter Circle Radius : ")
ThisDrawing.Utility.Prompt "Your circle radius is " & radius & vbCrLf
End Sub
1
2
3
4
5
6
Sub GetDecimalValue()
Dim count As Integer
count = ThisDrawing.Utility.GetInteger("Enter Circle Count : ")
ThisDrawing.Utility.Prompt "Your circle count is " & count & vbCrLf
End Sub
Get value using input box
1
2
3
4
5
6
7
8
9
10
11
12
13
Sub GetInputUsingInputBox()
'Integer
Dim count As Integer
count = CDbl(InputBox("Enter the number of the circle:", "Circle Count"))
'Double
Dim radius As Double
radius = CDbl(InputBox("Enter the radius of the circle:", "Circle Radius"))
'String
Dim title As String
title = CStr(InputBox("Enter the drawing title:", "Drawing Title"))
End Sub
Get Point from user
1
2
3
4
5
6
Sub GetPointFromUser()
Dim basePoint As Variant
basePoint = ThisDrawing.Utility.GetPoint(, "Pick Base point : ")
ThisDrawing.Utility.Prompt "Selected base point is: " & basePoint(0) & ", " & basePoint(1) & ", " & basePoint(2) & vbCrLf
End Sub
Get Distance from user
1
2
3
4
5
6
Sub GetDistanceFromUser()
Dim distance As Double
distance = ThisDrawing.Utility.GetDistance(, "Specify Distance:")
ThisDrawing.Utility.Prompt distance & vbCrLf
End Sub
Get Angle from user
1
2
3
4
5
6
Sub GetAngleFromUser()
Dim angle As Double
angle = ThisDrawing.Utility.GetAngle(, "Specify Angle")
ThisDrawing.Utility.Prompt "Angle in radian is : " & angle & vbCrLf
End Sub
Get Keywords from user to switch between different options
1
2
3
4
5
6
7
8
9
Sub GetAngleFromUser()
'Set Keyword input
ThisDrawing.Utility.InitializeUserInput 1, "Height Width Depth"
Dim result As String
result = ThisDrawing.Utility.GetKeyword("Enter a keyword [ Height/Width/Depth ]:")
ThisDrawing.Utility.Prompt "You've selected: " & result & vbCrLf
End Sub
If you have any questions or want to discuss something : Join our comment section
This post is licensed under CC BY-NC-ND 4.0 by the author.