×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

AutoCAD VBA - Polygon

AutoCAD VBA - Polygon

AutoCAD VBA - Polygon

(OP)
I'm new to VBA. I would like to automate in autocad the generation of either close or open polygon then if it is closed polygon it would also calculate the area. The input data should be the angle and length for each line in the polygon. The input data maybe also extracted automatically from a .txt file. It should be applicable to "n" number of lines. Sample below is a equiangular triangle. . Angle rotation is counterclockwise using the default reference origin of the Autocad.Thanks in advance.

Input data:    angle in decimal degrees    length
  sample               120                   500
                       240                   500
                       0                     500

RE: AutoCAD VBA - Polygon

The following code example draws a closed rectangular polyline (of a size representative of an architectural masonry block), puts it on layer "srw", and gives it a linetype scale of 1/3.  The thing to remember is that even if using a 2D polyline, you need the 3D set of points (polylines have an elevation, or z-value, default = 0).  That means for a PLINE with 4 corners, you need 12 points (3D * 4pts = 12), and you start with the 0-dimension and go to the 11-dimension in your array of points (see "Dim points (0 to 11) As Double").  Then you can write a formula to calculate each point in your array of points.  Simple trig for your problem.  Remember the first point is an x-value, the second a y-value, the third a z-value (default = 0). It can get weird with having the order of points out of place if you generate point formulas using a loop, but not all that difficult.  I actually use a more expanded version of the following code in Excel and draw the PLINEs in ACAD, so I'm not sure how much of it is peculiar to that use.  I think the gist is generally the same.

Keith



Sub AddBlock()
 Dim autocadApp As AcadApplication
 Dim blockObj As AcadPolyline
 Dim dwgObj As AcadDocument
 Dim points(0 To 11) As Double
 Set autocadApp = GetObject(, "AutoCAD.Application")
 Set dwgObj = autocadApp.ActiveDocument
 points(0) = x * 1.5: points(1) = 0: points(2) = 0
 points(3) = x * 1.5 + 1.5: points(4) = 0: points(5) = 0
 points(6) = x * 1.5 + 1.5: points(7) = 61 / 96: points(8)=0
 points(9) = x * 1.5: points(10) = 61 / 96: points(11) = 0
 Set blockObj = dwgObj.ModelSpace.AddPolyline(points)
 With blockObj
   .Closed = True
   .Layer = "srw"
   .LinetypeScale = 1 / 3
 End With
End Sub

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources