Forum unknown
· AutoLISP
area command
3 messages in this thread
I have several drawings which contain multiple polyline entities, each of
which may have several hundred vertices. I would like to measure the area
of each polyline. Currently I am doing this manually. I have tried to write
an Alisp routine to do this and record the results in a file. I can select
each entity from the data base but cannot figure a way to bind the result
of the area command. Is there a way around this that I have overlooked?
Emmanuel —
Try this. It doesn't write to a file, but you should be able to
modify the code to do so.
; AutoCAD Command function P-AREA for Version 2.6 and Release 9
; Returns total area of all POLYLINES on a user-specified LAYER.
; Does NOT trap for invalid layer name, nor for empty selection set.
; No claims are made as to the validity of the data
; which this function returns.
(defun C:P-AREA (/ LNAME SSET1 INDEX ENAME) ;local variable list
(setvar "cmdecho" 0) ;turn off "Comand:" prompt echo
(setq LNAME (getstring "\nLayer name: ")) ;prompt for layer name
(setq SSET1 ;make selection set SSET1
(ssget "x"
(list ;make filter list
(cons 8 LNAME) ;entity must be on layer LNAME
(cons 0 "POLYLINE") ;and must be a POLYLINE
)
)
)
(setq INDEX 0) ;initialize INDEX to zero
(setq ENAME (ssname SSET1 INDEX)) ;get first entity from SSET1
(command "AREA" "a" "e") ;start AREA command and Add Entities
(while ENAME ;if there's another entity…
(command ENAME) ;add it to total area
(setq INDEX (1+ INDEX))
(setq ENAME (ssname SSET1 INDEX)) ;get next entity from SSET1
)
(command "" "") ;normal end to AREA command
(princ ;print to screen…
(strcat ;concatenate the following strings…
"\nTotal area all POLYLINES on Layer "
(strcase LNAME) ;convert LNAME to caps
" : "
(rtos (getvar "AREA")) ;convert last AREA from real to string
)
)
(princ) ;suppress nil normally returned by defun
)
Emmanuel —
Try this. It doesn't write to a file, but you should be able to
modify the code to do so.
; AutoCAD Command function P-AREA for Version 2.6 and Release 9
; Returns total area of all POLYLINES on a user-specified LAYER.
; Does NOT trap for invalid layer name, nor for empty selection set.
; No claims are made as to the validity of the data
; which this function returns.
(defun C:P-AREA (/ LNAME SSET1 INDEX ENAME) ;local variable list
(setvar "cmdecho" 0) ;turn off "Comand:" prompt echo
(setq LNAME (getstring "\nLayer name: ")) ;prompt for layer name
(setq SSET1 ;make selection set SSET1
(ssget "x"
(list ;make filter list
(cons 8 LNAME) ;entity must be on layer LNAME
(cons 0 "POLYLINE") ;and must be a POLYLINE
)
)
)
(setq INDEX 0) ;initialize INDEX to zero
(setq ENAME (ssname SSET1 INDEX)) ;get first entity from SSET1
(command "AREA" "a" "e") ;start AREA command and Add Entities
(while ENAME ;if there's another entity…
(command ENAME) ;add it to total area
(setq INDEX (1+ INDEX))
(setq ENAME (ssname SSET1 INDEX)) ;get next entity from SSET1
)
(command "" "") ;normal end to AREA command
(princ ;print to screen…
(strcat ;concatenate the following strings…
"\nTotal area all POLYLINES on Layer "
(strcase LNAME) ;convert LNAME to caps
" : "
(rtos (getvar "AREA")) ;convert last AREA from real to string
)
)
(princ) ;suppress nil normally returned by defun
)