How to calculate the area of 2D shapes in AutoCAD:
Thread Content
In AutoCAD, it is possible to calculate the area (including perimeter) of 2D closed shapes easily and accurately, but the calculation methods vary depending on the type of shape. 1. For simple shapes, such as rectangles and triangles. Simply execute the AREA command (which can be entered via the command line or by clicking the corresponding command icon). After the prompt \"Specify first corner point or :\\" appears, activate snap mode and select the intersection points of the rectangle or triangle one by one, then press Enter. AutoCAD will automatically calculate the area and perimeter, and display the results on the command line. 2. For simple shapes, such as circles or other two-dimensional closed shapes composed of polylines or splines. Execute the AREA command; after the prompt \"Specify first corner point or :\\", select the Object option, and choose the shape for which you want to calculate the values as prompted. AutoCAD will then automatically calculate the area and perimeter. 3. For complex closed shapes composed of simple straight lines and arcs, the AREA command cannot be used directly to calculate the area of the shape. First, use the Boundary command (its usage is determined by selecting the appropriate option in the dialog box shown below; it is used to create a region from a shape whose area is to be calculated). Create a region or polyline object from the shape for which you want to find the area. Then execute the AREA command. When the command prompt displays “Specify first corner point or :”, select the Object option and choose the region that was just created. AutoCAD will then automatically calculate the area and perimeter.(defun AreaSum (/ boundary+_lst boundary- boundary+ elast enext OS_hold msg olderr no insPt area pt doc mspace bdy-_ss idx )
(vl-load-com)
(setq doc (vla-get-activedocument (vlax-get-acad-object)))
(setq mspace (vla-get-modelspace doc))
(IF (< (ATOI (SUBSTR (GETVAR “ACADVER”) 1 2)) 16)
(PROGN
(PRINC “\nThis program can only run in AutoCAD 2004 or later versions.”)
(e*t)
)
)
(if (> (setq no (ent-number)) 1000)
(if (= (dos_msgbox (strcat “There are” (itoa no) “entities on the screen. Are you sure you want to continue?”) “Alert” 4 3) 3)
(e*t)
)
)
(setq olderr *error* msg „“ *error* AreaSum-error)
(setq boundary+_lst ‘())
(setq OS_hold (getvar “OSMODE”) echo_hold (getvar “CMDECHO”))
(setvar “OSMODE” 0)
(setvar “CMDECHO” 0)
_while (setq pt (getpoint “\nSelect an interior point (try to choose one close to the boundary): ”))
(setq elast (entlast) boundary+ nil boundary- nil)
(command “modemacro” “Generating boundaries, please wait…”))
(if (vl-catch-all-apply ‘bpoly (list pt))
(PROGN
(entdel (entlast))
(command “_.BPoly” “a” “o” “r” „“ pt„”)
(setq enext (entnext elast) bdy-_ss (ssadd))
_while enext
(if (eq enext (entnext elast))
(setq boundary+ enext)
(setq bdy-_ss (ssadd enext bdy-_ss))
)
; if (setq enext (entnext enext))
; while
(cond
((= (sslength bdy-_ss) 1)
(setq boundary- (ssname bdy-_ss 0))
)
((> (sslength bdy-_ss) 1)
(command “union” bdy-_ss „“)
(setq idx 0)
(repeat (sslength bdy-_ss)
(if (not (vlax-erased-p (ssname bdy-_ss idx)))
(setq boundary- (ssname bdy-_ss idx))
)
(setq idx (1+ idx))
)
); repeat
)
; cond
(if boundary-
(command “subtract” boundary+ „“ boundary- „“)
)
(if boundary+
(setq boundary+_lst (cons boundary+ boundary+_lst))
)
); progn
)
; IF (if boundary+_lst (highlight boundary+_lst 3) )
(command “modemacro” “”)
)
; while (setq area 0)
(if boundary+_lst
(PROGN
(foreach bdy boundary+_lst
(setq area (+ area (vla-get-area (vlax-ename->vla-object bdy))))
(entdel bdy)
)
; foreach
(if (setq insPt (getpoint “\nText insertion point: ”))
(vla-addText mspace (rtos area 2 2) (vlax-3d-point insPt) 3)
(PRINC (strcat “Total area: “ (rtos area 2 2)))
)
)
; PROGN
)
;if (setvar “OSMODE” OS_hold)
(setvar “CMDECHO” echo_hold)
(vlax-release-object doc)
(vlax-release-object mspace)
(PRINC)
)
;;
;;
;;
(defun highlight (entlst mode / en)
(foreach en entlst (redraw en mode))
)
;;
(defun ent-number (/ scrsize height width center llpt urpt enum ss)
(setq scrsize (getvar “SCREENSIZE”))
(setq height (getvar “VIEWSIZE”) width (* height (/ (car scrsize) (cadr scrsize))) )
(setq center (getvar “VIEWCTR”))
(setq llpt (polar (polar center pi (/ width 2.0)) (* pi 1.5) (/ height 2.0)) urpt (polar (polar center 0 (/ width 2.0)) (* pi 0.5) (/ height 2.0)) ))
(setq ss (ssget “w” llpt urpt))
(if ss
(setq enum (sslength ss))
(setq enum 0)
)
)
;;
(defun AreaSum-error (msg)
(if OS_hold
(setvar “OSMODE” OS_hold)
)
(if echo_hold
(setvar “CMDECHO” echo_hold)
)
(if (> (length boundary+_lst) 0)
(mapcar ‘entdel boundary+_lst )
)
(if olderr
(setq *error* olderr)
)
After loading, enter (AreaSum) in the command line to get the area of the enclosed region! ~~