AfraLISP - Learn AutoLISP for AutoCAD productivity


Below are listed all the tips of the day from the last seven days. If you'd like to see the next tip of the day, visit again tomorrow.

Today's Tip

Extent Points

by Roger Farley

These four lines will give you the extreme four values in a points list, where PointList is a list of 2D or 3D points (like a list of pline vertex points) :

(setq X1 (apply 'min (mapcar 'car PointList)))
; The smallest 'X' value
(setq Y1 (apply 'min (mapcar 'cadr PointList)))
; The smallest 'Y' value
(setq X2 (apply 'max (mapcar 'car PointList)))
; The largest 'X' value
(setq Y2 (apply 'max (mapcar 'cadr PointList)))
; The largest 'Y' value

Then assemble like :

(setq LowerLeft (List X1 Y1))
(setq LowerRight (List X2 Y1))
(setq UpperRight (List X2 Y2))
(setq UpperLeft (List X1 Y2))


(setq CPWin (list (List X1 Y1) (list X2 Y1)
	    (list X2 Y2) (list X1 Y2)))
;A list of 4 points, can be passed to a 'ssget' function

I use the trick in finding an extents boundary around the objects I am working with, or performing a zoom window around the work area.

Yesterday's Tip

Random Number Generator

Random number generation function - based on the linear congruential method as presented in Doug Cooper's book Condensed Pascal, pp. 116-117.

Returns a random number between 0 and 1 :

(defun randnum (/ modulus multiplier increment random)
  (if (not seed)
    (setq seed (getvar "DATE"))
  (setq modulus    65536
        multiplier 25173
        increment  13849
        seed  (rem (+ (* multiplier seed) increment) modulus)
        random     (/ seed modulus)

Tuesday's Tip

Command Line VBA

Did you know that you can run VBA statements from the command line?

Try this :

From AutoCAD's Command line

VBASTMT <enter>
ThisDrawing.PurgeAll <enter>

Monday's Tip

Listing Symbols

The following will give you a list of all entries in a symbol table. This is great for creating a list to populate a list box in DCL.

;;;Start Coding Here
(defun tablelist (s / d r)
		(setq d (tblnext s (null d)))
		(setq r (cons (cdr (assoc 2 d)) r))
;;;End Coding Here

For example, if you would like a list of all layers in a specific drawing, use this :

(setq all_layers (tablelist "LAYER"))

AutoLisp should return something like this :

("7" "6" "5" "4" "3" "2" "0")

To populate a list box with the key of "selections," use this :

(start_list "selections")
(mapcar 'add_list all_layers)

Sunday's Tip

Drawing Path

To get the full path, you append the DWGPREFIX system variable (which stores the path) to the DWGNAME system variable (which stores the file name). Use code such as the following example to retrieve and assign the values of DWGNAME and DWGPREFIX to variables in AutoLISP :

(setq DN (getvar "DWGNAME"))
(setq DP (getvar "DWGPREFIX"))

Use the AutoLISP function (strcat) to concatenate the results and assign them to a variable, for example :

(setq TM (strcat DP DN))

In this example, the variable TM contains the full path including the file name.

Saturday's Tip

AutoLisp Comments

Did you know that you can write block comments in your AutoLisp files like this :

;| This is the start of the comments.
   You can carry your comments to multiple lines.
   This will end your comments |;

That little line is the pipe character or vertical bar.

Friday's Tip

Entity Length

This will display the length of most entities :

;Coding starts here
(defun c:lg ( / x_object x_length)
(setq x_object (entsel))
(setq x_object (vlax-Ename->Vla-Object (car x_object)))
(setq x_length (vlax-curve-getdistatparam x_object 
               (vlax-curve-getendparam x_object )))
(alert (strcat "Length = " (rtos x_length)))
;Coding ends here