TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: hudster on February 16, 2004, 05:16:24 AM

Title: My First Lisp...
Post by: hudster on February 16, 2004, 05:16:24 AM
Hurrah.

I've just written my first lisp and It works, it's very basic but it's a beginning.

All it does is sets a few variables I like in AutoCAD, but up until now i've used scripts to set them, now I have a lisp that does it when I open a drawing.
Code: [Select]

(defun c:var ()
(command "_BLIPMODE" "off")
(command "_CELTSCALE" "1")
(command "_CMDDIA" "1")
(command "_COLOR" "bylayer")
(command "_DIMASSOC" "1")
(command "_DIMSHO" "1")
(command "_DRAGMODE" "AUTO")
(command "_FACETRES" "2" )
(command "_FILEDIA" "1")
(princ))
Title: My First Lisp...
Post by: SMadsen on February 16, 2004, 07:13:53 AM
Excellent, Hudster. Keep it up!

If you wanna give yourself another assignment, you might want to look into other ways to write the same stuff. For example:
Code: [Select]
(defun c:varNext ()
  (setvar "BLIPMODE" 0)
  (setvar "CELTSCALE" 1.0)
  (setvar "CMDDIA" 1)
  (setvar "CECOLOR" "BYLAYER")
  (setvar "DIMASSOC" 1)
  (setvar "DIMSHO" 1)
  (setvar "DRAGMODE" 1)
  (setvar "FACETRES" 2.0)
  (setvar "FILEDIA" 1)
  (princ))


.. and don't stop until you can write it like this:
Code: [Select]
(defun c:varFinal ()
  (mapcar (function (lambda (var) (setvar (car var) (cdr var))))
          '(("BLIPMODE" . 0)("CELTSCALE" . 1.0)("CMDDIA" . 1)
            ("CECOLOR" . "BYLAYER")("DIMASSOC" . 1)("DIMSHO" . 1)
            ("DRAGMODE" . 1)("FACETRES" . 2.0)("FILEDIA" . 1)
           )
  )
  (princ))
Title: My First Lisp...
Post by: hudster on February 16, 2004, 08:46:49 AM
I've not got to grips with why lines are indented.  is there a reason for this?
Title: My First Lisp...
Post by: Mark on February 16, 2004, 09:09:52 AM
"improves text appearance and readability"
Title: My First Lisp...
Post by: CADaver on February 16, 2004, 09:10:12 AM
Quote from: Hudster
I've not got to grips with why lines are indented.  is there a reason for this?


Readability
Each step in is indented in a liitle more than the previous step so you can "see" the function's steps.
Title: My First Lisp...
Post by: Mark on February 16, 2004, 09:12:34 AM
Here's a reference
http://xarch.tu-graz.ac.at/autocad/docs/acg.txt
Title: My First Lisp...
Post by: CAB on February 16, 2004, 12:34:57 PM
The VLIDE will format for you.

Pull down menu 'Tools'
  Select "Format Code in Editor"

Your code formatted.

Code: [Select]
(defun c:var ()
  (command "_BLIPMODE" "off")
  (command "_CELTSCALE" "1")
  (command "_CMDDIA" "1")
  (command "_COLOR" "bylayer")
  (command "_DIMASSOC" "1")
  (command "_DIMSHO" "1")
  (command "_DRAGMODE" "AUTO")
  (command "_FACETRES" "2")
  (command "_FILEDIA" "1")
  (princ)
)


Also when posting code on the forum use the CODE button as shown above.
Click the button, paste your code, click the CODE button again.
This will display the formatting and will keep the parenthesizes
from being miss interpreted buy the forum formatter.

Welcome aboard. :)

CAB
Title: My First Lisp...
Post by: daron on February 16, 2004, 12:41:47 PM
Like that?^

Also another reason for using the code tag is the sometimes you have to use the number 8 in your code and put a ) after the 8 and the code ends up looking something like
(getfiled "etc" "etc" "etc" 8) <that and you end up with smilies all embedded throughout your code.
Title: My First Lisp...
Post by: JohnK on February 16, 2004, 01:34:10 PM
Formatting is important for lispers cause it helps us see what function we are in the middle of. Because there is a open paren and a close paren for each AutoLisp function, you indent when you nest other functions in functions. For instance:
 
Code: [Select]
(one
    (two
      (three)
     );_ end two
   );_ end one


Nesting occures when an AutoLisp function takes an argument you can then use another function for that argument. -i.e. (+ (* 2 1) 5)

Although you see it as weird now, when you start getting more involved in more complex lisps you will see the benifit of indentation.