TheSwamp
Code Red => AutoLISP (Vanilla / Visual) => Topic started by: Mark on February 05, 2004, 09:13:12 AM
-
If you had to teach a newbie one thing about autolisp programming what would it be?
-
Learn how to use sub-routines, they can make your life a whole lot better. This is a simple example but in a complex program they are a must:
;;; converts meters to US Survey Feet
;;; put all of this in one file
(defun conv (m / ft)
(setq ft (/ (* m 39.37) 12))
)
(defun c:m2f (/ m f)
(prompt "\nConvert Meters to US Survey Feet")
(if (setq m (getreal "\nEnter Meters: "))
(setq f (conv m))
)
(prompt (strcat (rtos m)" converted to "(rtos f)))
(princ)
)
-
OOOoo I got an example too. (I just did this one today.)
brb
-
Sorry, i did this on the command line and i had to cut and paste a bit. :P
;;;===================================================================;
;;; Set the curent layer based on a selected object. ;
;;;===================================================================;
(defun entse7 (/ x)
(while (not (setq x (car (entsel))))
(princ "\nYou missed, try again.")) x)
(defun GetLyr ()
(cdr (assoc 8 (entget (entse7)))))
(defun PutLyr (lyr) (setvar "clayer" lyr))
(defun c:as () (PutLyr (GetLyr)))
Yeah, use sub's they help alot.
-
Here are some simple but useful ones.
;;; Convert Radians to Degrees
(defun rtod (r) (* 180.0 (/ r pi)))
;;; Convert Degrees TO Radians
(defun dtor (d) (/ (* d pi) 180.0))
;Make 2D point from 3D point
(defun 3dP->2dP (3dpt)(list (car 3dpt) (cadr 3dpt)))
-
Remember to give names to your variables and commands that give adequate description to what they are. For example:
Use elist instead of e
Also comment your code with as much information as possible, this will allow you to revisit your code at a later date and understand what is going on, particularly in a complex program.
-
Remember to give names to your variables and commands that give adequate description to what they are. For example:
Use elist instead of e
What's an elist ?
-
remember to test each line or segment as you write it ~ it'll save you heaps of time later !
learn to get, store, change and reset variables
learn to undo begin and end etc
oh..
and learn Autocad as well !
-
That would be an entity list Mark. Here's something all newbies should know: Test, alter, Test, alter, ReTest and most of all write code. If it don't work ask someone.
-
That would be an entity list Mark.
Exactly .... see, I didn't even have to tell Daron what the variable was, only it's name and he knew what it was.
Of course I could have specified ...
EntityDataList
or
CurrentEntityDataList
or
EntityListThatIsCurrentlyBeingModified
but then that becomes just too much typing ....
:twisted:
-
If you had to teach a newbie one thing about autolisp programming what would it be?
It would be: what's the difference between CAR and CDR?
PS: you didn't say he/she knew nothing about programming in general
-
Oh .. rephrasing .. it would be: look here (http://theswamp.org/phpBB2/viewtopic.php?p=10703#10703)
-
Learn to use the Vlisp console, I still need to....
It probably will turn you into a "SMadsen" overnight :lol:
-
When I started trying to read the code others had written
the code seemed very cryptic.
Here are a few variable names you can expect to see:
Note: lower case L often looks like a one (l1)
point: p pt p1 p2 pt1 pt2
center point: cp cpt
entity name: e e1 e2 en en1 ent ent1
entity list: e eL elist
angle: a a1 ang ang1
counter: c cnt x
Box positions: LL lower left
ur Upper Right
lr lower right
ul upper left
Selection set: ss ss1 s1
Center: cent c c1
Center point: cpt
Distance: dist d
Radius: rad rad1
Coordinates: coor x y xy
Object: obj
These are a few I could think of.
One trick I use for those of us who are typing impaired is to use
a short variable name and when the routine is complete do a
"Search & Replace" to make the names more readable.
And please avoid using a b x y
CAB
-
Learn to use the Vlisp console, I still need to....
It probably will turn you into a "SMadsen" overnight :lol:
Not likely, but it will help.
-
My biggest is comment your code. Besides helping you, it can help others that follow behind you. When I started at this company, all the code that I was having to rework to be compatible with 2002 had no comments. So what does that mean? Well, on some of the larger programs I had to test line by line to see what was happening and it is very time consuming. One program took me over a week to get it straightened out. I probably could have written a smaller program faster than going through all that. BTW, I just rewrote any other programs that were bombing out. Here is an example of what I did as far as rewriting the programs:
This is way it was written when I got here:
***********************
(setq del_item (entget(car(entsel "\nSelect item you want to delete: "))))
(setq txt1 del_item)
(setq txt2 (assoc 0 txt1))
(setq txt2a (cdr txt2))
(setq txt3 (assoc 8 txt1))
(setq txt3a (cdr txt2))
(setq sel_item (ssget "X" (list (cons 0 txt2) (cons 8 txt3))))
(command ".erase" sel_item "")
************************
This is what I did in rewriting it...much shorter and sweeter from 8 lines to 3 lines. Imagine that on a much larger scale or more complex program.
************************
(setq del_item (entget(car(entsel "\nSelect item you want to delete: "))));;selects single entity and creates list
(setq sel_item (ssget "X" (list (cons 0 (cdr (assoc 0 del_item))) (cons 8 (cdr (assoc 8 del_item))))));;extracts entity name and layer it's on and selects all entities that match this criteria
(command ".erase" sel_item "");;deletes selected item
************************
-
Mine would be: Make sure your code takes into account what users may do when they use your routine.
As an example, take this into account:
(setq thing
(entget
(car
(entsel "\nSelect item you want to delete: ")
)
)
)
This will completely bomb out when the user doesn't select something - which will happen in the course of normal events.
It's better to wrap it into an (if..then..) expression:
(if (setq e (car (entsel "\nSelect the item you wish to delete:")))
(setq e (entget e))
)
If the user doesn't select anything, (car nil) still returns nil, because nil is actually a list data type.