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

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

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

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

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

Sunday's Tip

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.

Saturday's Tip

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.

Friday's Tip

Default Values

In many AutoCAD commands, AutoCAD remembers the last entry and puts that entry between angle brackets (<>). Then all you have to do is press Enter, and that value is used. You should use that technique whenever you're creating your own defaults in AutoLISP. First make sure that you haven't declared as local the variable that will hold the value. Then follow a three-step process :

  1. Require input from the user and assign that input to a second variable.
  2. Test to see if the input is nil, and if it is nil, assign a global variable to the real variable.
  3. Finally, assign the real variable to the global variable.

You're then ready for the next sequence.

Here's an example :

(defun c:prog1 (/ a)
	(if (= gv nil)
		(setq gv 1.0)
	);end if
 
	(princ "\nEnter Distance <")
	(princ gv)
	(princ "> ")
	(setq a (getdist))
	(if (= a nil)
		(setq a gv)
	);end if
 
	(setq gv a)
	(princ "\nThe Distance is ")
	(princ a)
	(princ)
);defun

gv is the global variable. No matter how often you run this routine while in the same drawing, it will maintain the last entry as the default. The first time through the routine, gv is tested to see if it has a value. If it doesn't, the routine assigns 1.0 as the default value.