Author Topic: What should all lisp newbies know?  (Read 8150 times)

0 Members and 1 Guest are viewing this topic.

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
What should all lisp newbies know?
« 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:
Code: [Select]

;;; 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)
 )
TheSwamp.org  (serving the CAD community since 2003)

JohnK

  • Administrator
  • Seagull
  • Posts: 10626
What should all lisp newbies know?
« Reply #1 on: February 05, 2004, 09:14:49 AM »
OOOoo I got an example too. (I just did this one today.)

brb
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

JohnK

  • Administrator
  • Seagull
  • Posts: 10626
What should all lisp newbies know?
« Reply #2 on: February 05, 2004, 09:17:36 AM »
Sorry, i did this on the command line and i had to cut and paste a bit. :P

Code: [Select]
;;;===================================================================;
;;; 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.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
What should all lisp newbies know?
« Reply #3 on: February 05, 2004, 09:37:59 AM »
Here are some simple but useful ones.

Code: [Select]

;;;  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)))
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
What should all lisp newbies know?
« Reply #4 on: February 05, 2004, 09:38:05 AM »
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.
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
What should all lisp newbies know?
« Reply #5 on: February 05, 2004, 09:42:30 AM »
Quote from: Keith
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 ?
TheSwamp.org  (serving the CAD community since 2003)

hendie

  • Guest
What should all lisp newbies know?
« Reply #6 on: February 05, 2004, 09:45:28 AM »
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 !

daron

  • Guest
What should all lisp newbies know?
« Reply #7 on: February 05, 2004, 09:51:19 AM »
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.

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
What should all lisp newbies know?
« Reply #8 on: February 05, 2004, 10:14:53 AM »
Quote from: Daron
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:
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

SMadsen

  • Guest
Re: What should all lisp newbies know?
« Reply #9 on: February 05, 2004, 10:44:39 AM »
Quote from: Mark Thomas
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

SMadsen

  • Guest
What should all lisp newbies know?
« Reply #10 on: February 05, 2004, 10:49:23 AM »
Oh .. rephrasing .. it would be: look here

rude dog

  • Guest
What should all lisp newbies know?
« Reply #11 on: February 05, 2004, 10:58:19 AM »
Learn to use the Vlisp console, I still need to....
It probably will turn you into a "SMadsen" overnight :lol:

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
What should all lisp newbies know?
« Reply #12 on: February 05, 2004, 11:20:16 AM »
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)

Code: [Select]
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
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

daron

  • Guest
What should all lisp newbies know?
« Reply #13 on: February 05, 2004, 11:21:21 AM »
Quote from: Rude Dog
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.

Craig

  • Guest
What should all lisp newbies know?
« Reply #14 on: February 05, 2004, 11:43:11 AM »
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:

Quote
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
************************