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.
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.
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 :
- Require input from the user and assign that input to a second variable.
- Test to see if the input is nil, and if it is nil, assign a global variable to the real variable.
- 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.
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.
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) ) )
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>
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)