Author Topic: My First Lisp...  (Read 4222 times)

0 Members and 1 Guest are viewing this topic.

hudster

  • Gator
  • Posts: 2848
My First Lisp...
« 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))
Revit BDS 2017, 2016, 2015, 2014, AutoCAD 2017, 2016, Navisworks 2017, 2016, BIM360 Glue

SMadsen

  • Guest
My First Lisp...
« Reply #1 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))

hudster

  • Gator
  • Posts: 2848
My First Lisp...
« Reply #2 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?
Revit BDS 2017, 2016, 2015, 2014, AutoCAD 2017, 2016, Navisworks 2017, 2016, BIM360 Glue

Mark

  • Custom Title
  • Seagull
  • Posts: 28753
My First Lisp...
« Reply #3 on: February 16, 2004, 09:09:52 AM »
"improves text appearance and readability"
TheSwamp.org  (serving the CAD community since 2003)

CADaver

  • Guest
My First Lisp...
« Reply #4 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.

Mark

  • Custom Title
  • Seagull
  • Posts: 28753
My First Lisp...
« Reply #5 on: February 16, 2004, 09:12:34 AM »
TheSwamp.org  (serving the CAD community since 2003)

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
My First Lisp...
« Reply #6 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
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
My First Lisp...
« Reply #7 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.

JohnK

  • Administrator
  • Seagull
  • Posts: 10603
My First Lisp...
« Reply #8 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.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org