Trying to draw simple rectangle

Trying to draw simple rectangle

Anonymous
Not applicable
7,418 Views
4 Replies
Message 1 of 5

Trying to draw simple rectangle

Anonymous
Not applicable

Hello all I am trying to draw a simple square 10x10x10 from point 0,0 and am getting an error saying not a valid argument in .polarpoint. I am fairly familiar with excel VBA but am new to autocad. Below is my code. 

 

Option Explicit
Sub drect()
    Dim pi As Double
    Dim pt1(0 To 2) As Variant
    Dim pt2 As Variant
    Dim pt3 As Variant
    Dim pt4 As Variant
    
    pi = 3.14159265358979
       
    pt1(0) = 0#: pt1(1) = 0#: pt1(2) = 0#

    With ThisDrawing.Utility
        pt2 = .PolarPoint(pt1, 0, 10)
        pt3 = .PolarPoint(pt2, pi / 2, 10)
        pt4 = .PolarPoint(pt3, pi / 2, 10)
    End With
    
    With ThisDrawing.ModelSpace
       .AddLine pt1, pt2
       .AddLine pt2, pt3
       .AddLine pt3, pt4
       .AddLine pt4, pt1
    End With
       
        
End Sub

 Thanks for any help.

0 Likes
Accepted solutions (1)
7,419 Views
4 Replies
Replies (4)
Message 2 of 5

Hallex
Advisor
Advisor
Accepted solution

Just change angles to make sure you run

in the right order:

Option Explicit
Sub drect()

Dim pi As Double
Dim pt1(0 To 2) As Double
Dim pt2 As Variant
Dim pt3 As Variant
Dim pt4 As Variant
pi = 3.14159265358979
pt1(0) = 0#: pt1(1) = 0#: pt1(2) = 0#
With ThisDrawing.Utility
pt2 = .PolarPoint(pt1, 0, 10)
pt3 = .PolarPoint(pt2, pi / 2, 10)
pt4 = .PolarPoint(pt3, pi, 10)
End With
With ThisDrawing.ModelSpace
.AddLine pt1, pt2
.AddLine pt2, pt3
.AddLine pt3, pt4
.AddLine pt4, pt1
End With
End Sub

 

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
0 Likes
Message 3 of 5

Hallex
Advisor
Advisor

If you already know coordinates then you have use this way:

    Sub RectangleTest()
    Dim polyObj As AcadLWPolyline
    
    ' Even though there is array, this is declared as variant
    Dim polyPoints As Variant
    
    Dim utilObj As Object   ' Late bound object
    Set utilObj = ThisDrawing.Utility
      Dim color As AcadAcCmColor
        Dim acVer As String
          acVer = Left(ThisDrawing.GetVariable("acadver"), 2)
    Set color = AcadApplication.GetInterfaceObject("AutoCAD.AcCmColor." & acVer)
      color.SetRGB 255, 0, 0

    ' Define the lwpolyline.
    ' Pairs of coordinates in counter clockwise order:
    utilObj.CreateTypedArray polyPoints, vbDouble, 0, 0, 10, 0, 10, 10, 0, 10
    
    ' Create the spline
    Set polyObj = ThisDrawing.ModelSpace.AddLightWeightPolyline(polyPoints)
   With polyObj
     .Closed = True
       .ConstantWidth = 0.3
        .Layer = "0"
         .TrueColor = color
    End With
    
    Dim minExt As Variant
    Dim maxExt As Variant
    
    ' Return the bounding box for the rectangle and return the minimum
    ' and maximum extents of the box in the minExt and maxExt variables.
    polyObj.GetBoundingBox minExt, maxExt
      ZoomWindow minExt, maxExt
End Sub

 

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
Message 4 of 5

RalphBrown99
Advocate
Advocate

 

 

Hi:

 

I saw your post and I do have a similar question. I want to draw a simple rectangle but in my case I am using textboxes as a tool to ask the user the dimensions of the rectangle (length and width). The problem is that the textboxes are unresponsive to the user (me) so it is not possible to make the program run. I think that it has to do with the properties of the textboxes but I do not know how to deal with this anyway. I pick up the code from the following youtube video: 

 

http://www.youtube.com/watch?v=GlIJkG5fBKU

 

Here is the code:

 

Private Sub CommandButton1_Click()

pi = 3.14159265358979
UserForm1.Hide

pt1 = ThisDrawing.Utility.GetPoint(, "Pick Lower Left Corner:")
pt2 = ThisDrawing.Utility.PolarPoint(pt1, 0, Val(TextBox1.Text))
pt3 = ThisDrawing.Utility.PolarPoint(pt2, pi / 2, Val(TextBox2.Text))
pt4 = ThisDrawing.Utility.PolarPoint(pt1, pi / 2, Val(TextBox2.Text))


ThisDrawing.ModelSpace.AddLine pt1, pt2
ThisDrawing.ModelSpace.AddLine pt2, pt3
ThisDrawing.ModelSpace.AddLine pt3, pt4
ThisDrawing.ModelSpace.AddLine pt4, pt1

End Sub

 

 

Thanks in advance

 

 

Rafael Moreno

0 Likes
Message 5 of 5

Hallex
Advisor
Advisor

Hi Rafael, try this code in form module, I just added possibility

to create the closed polyline (almost not tested)

Option Explicit
Const pi As Double = 3.14159265358979

'Add CheckBox1 at the right of Draw button on form

Private Sub CommandButton1_Click()
Dim pt1 As Variant
Dim pt2 As Variant
Dim pt3 As Variant
Dim pt4 As Variant
Me.Hide
If Me.CheckBox1.Value = True Then
pt1 = ThisDrawing.Utility.GetPoint(, "Pick Lower Left Corner:")
pt2 = ThisDrawing.Utility.PolarPoint(pt1, 0, Val(TextBox1.Text))
pt3 = ThisDrawing.Utility.PolarPoint(pt2, pi / 2, Val(TextBox2.Text))
pt4 = ThisDrawing.Utility.PolarPoint(pt1, pi / 2, Val(TextBox2.Text))


ThisDrawing.ModelSpace.AddLine pt1, pt2
ThisDrawing.ModelSpace.AddLine pt2, pt3
ThisDrawing.ModelSpace.AddLine pt3, pt4
ThisDrawing.ModelSpace.AddLine pt4, pt1

ElseIf Me.CheckBox1.Value = False Then

 Dim polyObj As AcadLWPolyline
    
    Dim polyPoints(0 To 7) As Double

   pt1 = ThisDrawing.Utility.GetPoint(, "Pick Lower Left Corner:")
   polyPoints(0) = CDbl(pt1(0)): polyPoints(1) = CDbl(pt1(1))
   pt2 = ThisDrawing.Utility.PolarPoint(pt1, 0, Val(TextBox1.Text))
   polyPoints(2) = CDbl(pt2(0)): polyPoints(3) = CDbl(pt2(1))
   pt3 = ThisDrawing.Utility.PolarPoint(pt2, pi / 2, Val(TextBox2.Text))
   polyPoints(4) = CDbl(pt3(0)): polyPoints(5) = CDbl(pt3(1))
   pt4 = ThisDrawing.Utility.PolarPoint(pt1, pi / 2, Val(TextBox2.Text))
   polyPoints(6) = CDbl(pt1(0)): polyPoints(7) = CDbl(pt4(1))

    Set polyObj = ThisDrawing.ModelSpace.AddLightWeightPolyline(polyPoints)
  
    polyObj.Closed = True

End If
Me.Show
End Sub

Private Sub CommandButton2_Click()
End
End Sub


Private Sub UserForm_Initialize()
Me.CheckBox1.Caption = "Draw As Lines"
End Sub

 Cheers

 

_____________________________________
C6309D9E0751D165D0934D0621DFF27919
0 Likes