Option Explicit
Private Sub cmdCancel_Click()
Unload Me
End
End Sub
Private Sub cmdOK_Click()
Dim rLen As Double, rHgt As Double
Dim CadApp As AcadApplication
Dim CadDwg As AcadDocument
Dim lineObj As AcadLine
Dim textObj As AcadText
Dim pt1(0 To 2) As Double, pt2(0 To 2) As Double, pt3(0 To 2) As Double
Dim pt4(0 To 2) As Double, pt5(0 To 2) As Double
Dim x As Integer, y As Integer, z As Integer
x = 0
y = 1
z = 2
'Get the textbox values
If IsNumeric(txtLength.Text) = True Then
rLen = CDbl(txtLength.Text)
Else
MsgBox "Invalid Length"
Exit Sub
End If
If IsNumeric(txtHeight.Text) = True Then
rHgt = CDbl(txtHeight.Text)
Else
MsgBox "Invalid Height"
Exit Sub
End If
'<><><><><><><><><><><><><><><><><><><><><>
' Connect to AutoCAD
'<><><><><><><><><><><><><><><><><><><><><>
On Error Resume Next
TryAgain:
Set CadApp = GetObject(, "AutoCAD.Application")
If Err.Number <> 0 Then 'Not Running
Set CadApp = CreateObject("AutoCAD.Application")
Err.Clear
'AutoCAD may open a new drawing upon opening - close it
CadApp.ActiveDocument.Close False
If Err.Number <> 0 Then
Err.Clear 'No Drawings are Open
End If
End If
On Error GoTo 0
'Add a new drawing
CadApp.Documents.Add
Set CadDwg = CadApp.ActiveDocument
'Define the points - start at 0,0
pt1(x) = 0#: pt1(y) = 0#: pt1(z) = 0#
pt2(x) = pt1(x) + rLen
pt2(y) = pt1(y)
pt2(z) = 0#
pt3(x) = pt2(x)
pt3(y) = pt2(y) + rHgt
pt3(z) = 0#
pt4(x) = pt1(x)
pt4(y) = pt3(y)
pt4(z) = 0#
'Draw the lines
Set lineObj = CadDwg.ModelSpace.AddLine(pt1, pt2)
Set lineObj = CadDwg.ModelSpace.AddLine(pt2, pt3)
Set lineObj = CadDwg.ModelSpace.AddLine(pt3, pt4)
Set lineObj = CadDwg.ModelSpace.AddLine(pt4, pt1)
'Add some text
pt5(x) = (pt1(x) + pt2(x)) / 2
pt5(y) = (pt1(y) + pt4(y)) / 2
pt5(z) = 0#
Set textObj = CadDwg.ModelSpace.AddText("HERE YOU GO!", pt5, 0.125)
CadApp.ZoomExtents
Set lineObj = Nothing
Set CadDwg = Nothing
Set CadApp = Nothing
Unload Me
End
End Sub