AfraLISP - Learn AutoLISP for AutoCAD productivity

AutoLISP Tips'n'Tricks

Below are listed the most recent AutoLISP tips'n'tricks.

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.

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

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.

Breaking a Circle

Have you ever tried breaking a Circle into 2 separate parts using the Break command? It doesn't work does it? This routine allows you to separate a circle into two arcs by picking two points on the circle.

(defun c:bcirc (/ os pt1 pt2 a pt3)
	(setq os (getvar "osmode"))
	(setvar "osmode" 512)
	(setq pt1 (getpoint "\nFirst Break in Circle : "))
	(setq pt2 (getpoint "\nSecond Break in Cricle : "))
	(setq a (entget (ssname (ssget pt1) 0)))
	(setq pt3 (cdr (assoc 10 a)))
	(command "break" pt1 pt2)
	(command "arc" pt1 "e" pt2 pt3)
	(setvar "osmode" os)
	(princ)
)
(princ)

Pick any 2 points on the a Circle. The circle will look the same, but it will be broken into two arcs.

Converting Strings to Uppercase

There are many occasions when you'll need to convert a string variable to uppercase, especially when you've requested a response and need to test it. It's much easier to test it against all-uppercase than to try to test for every combination of uppercase and lowercase. The command for the conversion is (strcase) :

(setq uc (strcase lc))

This converts the value of the string variable lc to uppercase and assigns the new value to the variable uc.