Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

AutoCAD VBA - Polygon

Status
Not open for further replies.

aobjr

Mechanical
May 7, 2003
11
0
0
PH
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
 
Replies continue below

Recommended for you

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
 
Status
Not open for further replies.
Back
Top