AfraLISP - Learn AutoLISP for AutoCAD productivity

Tips'n'Tricks

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

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)
	(while 
		(setq d (tblnext s (null d)))
		(setq r (cons (cdr (assoc 2 d)) r))
	);while
);defun
 
;;;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)

Yesterday'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.

Sunday'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.

Saturday's Tip

Entity Length

This will display the length of most entities :

;Coding starts here
(defun c:lg ( / x_object x_length)
(vl-load-com)
(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)))
(princ)
);defun
(princ)
;Coding ends here

Friday's Tip

ON/OFF or THAW/FREEZE?

Both the off and frozen states make layers invisible. AutoCAD introduced the frozen and thawed layer states to reduce regeneration time - and that's the main difference between On/Off and Thawed/Frozen layer visibility options. However, today's computers are faster, and AutoCAD has since introduced several ways to avoid regeneration while panning and zooming - such as Aerial View, Zoom Dynamic, and real-time zooming and panning. Also, remember that thawing a layer causes a regeneration, whereas turning a layer back on only causes a redraw. As a result, you might actually save a regeneration by using On/Off instead of Thawed/Frozen.

Thursday's Tip

Filleting Lines

If you have lines within your drawing that you'd like to fillet that you built without the POLYLINE command, you can quickly convert your series of lines to a polyline using the PEDIT command and then use the polyline option during your FILLET command to put fillets on each corner of the polyline - all at one time.

To convert your series of lines to a polyline, type PEDIT at the command line and when prompted to select objects, select one of the objects on your screen. AutoCAD will tell you that the line isn't a polyline and then will ask you if you want to turn that line into one. Reply Y for yes and press [Enter]. At the next prompt, Enter an option…

[Close/Join/Width/Edit vertex/Fit/Spline/Decurve/Ltype gen/Undo]:

Type J for join and press [Enter]. You're then prompted to select the objects onscreen to join. Draw a window around the objects you want to join together as a polyline. Press [Enter] again to exit the command. Your objects have been joined as a polyline and you're now ready to use the FILLET command.

First specify a radius for your fillet by typing FILLET at the command line and then entering R for radius. Then, type a value for the radius and press [Enter]. Press [Enter] again to re-enter the FILLET command and this time type P for polyline in response to the prompt. Select your newly created polyline on your screen and all the corners are filleted onscreen.

Wednesday's Tip

Restoring a Circle

This routine will join 2 arcs back into a circle. It will also complete a circle from an Arc.

(defun c:rcirc (/ os pt2 a pt1 ra pt3)
	(setq os (getvar "osmode"))
	(setvar "osmode" 512)
	(setq pt2 (getpoint "\nPick one of the Arcs : "))
	(setq a (entget (ssname (ssget pt2) 0)))
	(setq pt1 (cdr (assoc 10 a)))
	(setq ra (cdr (assoc 40 a)))
	(command "erase" pt2 "")
	(setq pt3 (getpoint "\nPick other Arc : "))
	(setvar "osmode" os)
	(command "erase" pt3 "")
	(command "circle" pt1 ra)
	(princ)
)
(princ)

Begin this routine with one or two arcs. Pick either arc. That arc will disappear. Pick the remaining arc if you started with two. If you started with only one arc, pick any blank space on the screen or press Enter. Now the circle will be restored or the single arc is turned into a complete circle.