The points specified by a user can be used to obtain the area of a closed region.
You can measure an arbitrary closed region defined by the 2D or 3D points specified by the user. The points must be coplanar.
To obtain the area specified by points from the user
- Use the GetPoint method in a loop to obtain the points from the user.
- Create a lightweight polyline from the points provided by the user. Use the AddLightweightPolyline method to create the polyline.
- Use the Area property to obtain the area of the newly created polyline.
- Erase the polyline using the Erase method.
Calculate the area defined by points entered from the user
This example prompts the user to enter five points. A polyline is then created out of the points entered. The polyline is closed, and the area of the polyline is displayed in a message box.
- AutoLISP
-
(vl-load-com) (defun c:Ch3_CalculateDefinedArea() (setq acadObj (vlax-get-acad-object) doc (vla-get-ActiveDocument acadObj) moSpace (vla-get-ModelSpace doc) utilityObj (vla-get-Utility doc)) ;; Get the points from the user (setq p1 (vla-GetPoint utilityObj nil "\nFirst point: ") p2 (vla-GetPoint utilityObj p1 "\nSecond point: ") p3 (vla-GetPoint utilityObj p2 "\Third point: ") p4 (vla-GetPoint utilityObj p3 "\Fourth point: ") p5 (vla-GetPoint utilityObj p4 "\Fifth point: ")) (setq p1 (vlax-safearray->list (vlax-variant-value p1)) p2 (vlax-safearray->list (vlax-variant-value p2)) p3 (vlax-safearray->list (vlax-variant-value p3)) p4 (vlax-safearray->list (vlax-variant-value p4)) p5 (vlax-safearray->list (vlax-variant-value p5))) ;; Create the 2D polyline from the points (setq vertices (vlax-make-safearray vlax-vbDouble '(0 . 9))) (vlax-safearray-fill vertices (list (nth 0 p1) (nth 1 p1) (nth 0 p2) (nth 1 p2) (nth 0 p3) (nth 1 p3) (nth 0 p4) (nth 1 p4) (nth 0 p5) (nth 1 p5) )) (setq polyObj (vla-AddLightWeightPolyline moSpace vertices)) (vla-put-Closed polyObj :vlax-true) (vla-ZoomAll acadObj) ;; Display the area for the polyline (alert (strcat "The area defined by the points is " (rtos (vla-get-Area polyObj) 2))) )
- VBA (AutoCAD Only)
-
Sub Ch3_CalculateDefinedArea() Dim p1 As Variant Dim p2 As Variant Dim p3 As Variant Dim p4 As Variant Dim p5 As Variant ' Get the points from the user p1 = ThisDrawing.Utility.GetPoint(, vbCrLf & "First point: ") p2 = ThisDrawing.Utility.GetPoint(p1, vbCrLf & "Second point: ") p3 = ThisDrawing.Utility.GetPoint(p2, vbCrLf & "Third point: ") p4 = ThisDrawing.Utility.GetPoint(p3, vbCrLf & "Fourth point: ") p5 = ThisDrawing.Utility.GetPoint(p4, vbCrLf & "Fifth point: ") ' Create the 2D polyline from the points Dim polyObj As AcadLWPolyline Dim vertices(0 To 9) As Double vertices(0) = p1(0): vertices(1) = p1(1) vertices(2) = p2(0): vertices(3) = p2(1) vertices(4) = p3(0): vertices(5) = p3(1) vertices(6) = p4(0): vertices(7) = p4(1) vertices(8) = p5(0): vertices(9) = p5(1) Set polyObj = ThisDrawing.ModelSpace.AddLightWeightPolyline(vertices) polyObj.Closed = True ThisDrawing.Application.ZoomAll ' Display the area for the polyline MsgBox "The area defined by the points is " & _ polyObj.Area, , "Calculate Defined Area" End Sub