Sunday, December 28, 2014

AutoLISP Tutorial for AutoCAD Users: Lesson 2

Hello, world is very nice. But what you really wanted to do was write a custom AutoCAD command. So let's do that.



Your first custom AutoCAD command


Let's make a shortcut command to set the current layer to zero.

Type the following at the AutoCAD command line:

(defun c:l0 () (command "._layer" "_thaw" "0" "_set" "0" "_unlock" "0" "_on" "0" ""))
Now your first command is ready. Now set the current layer to "0". Type:

l0
Here is what we did:

  • defun means DEfine a FUNction
  • c: in front of the function name means it's a Command function
  • () means there are no arguments or local variables in the function
  • command means we are going to use AutoCAD commands. (The (command) function invokes the AutoCAD command interpreter in dialog-box-off ("filedia=0", "cmddia=0", "attdia=0") mode.
  • "._layer" means use the layer command even if it has been UNDEFINE'd (.) or if the local language calls it something like "nivel" (_). We also used "_" before the command options in case the local language calls them something else.
  • Every time we ended a string ("a string") in the (command) function, it told AutoLISP to issue a carriage return (enter key) to the AutoCAD command interpreter
  • We added a "" at the end of the command function to add the extra carriage return (enter key) that is required to leave the "layer" command
  • We defined a function called the command l0 with no arguments that runs the layer command

Your first custom AutoCAD command shortened to an evil minimum


You may want to save space or keystrokes. This is not against the AutoLISP rules, but it is against the Golden Rule. Please consider leaving your programs verbose instead of cleverly shortened. You will likely read a program many more times than you will write it, and you may not feel so clever in 6 months when you are trying to modify or debug your program. But just so you know what is legal, here is your first program shortened:

(defun c:l0()(command"layer""t""0""s""0""u""""on"""""))
Here is what we did:

  • The layer command doesn't need full reponses, so we shortened "thaw" to "t" and so forth
  • AutoLISP doesn't need spaces to keep parentheses and quotation marks separate, so we eliminated all non-essential spaces.
  • We are not service-minded, so we eliminated the ability for our command to work internationally and when the "layer" command is UNDEFINE'd.


I strongly advise against the practices above. They are all against the Golden Rule, which is all that really matters in life here and there. Keep the Golden Rule, children.

But it goes away so easily!

If you typed in the shortened, evil version of the "l0" command, you may have noticed that AutoCAD didn't complain that you were overwriting the previous definition of the "l0" command. AutoLISP variables and functions are very ethereal that way. They disappear without warning when you close a drawing (unless you told AutoCAD to keep them using LISPINIT 0), when AutoCAD closes, or when they are overwritten.

Type the following to zap the (c:l0) command function:

(setq c:l0 nil)
Here is what we did:
  • (setq) is the function we use to set the value of a variable (or symbol as they are called in AutoLISP)
  • c:l0 is a function, but really it's just a special kind of symbol, and we can set it to nil, which makes the function go away
  • nil is the AutoLISP value that means FALSE or NULL. It isn't quite "empty", but it's close. 0 isn't nil and "" (an empty string) isn't nil.
  • We setq'd the value of the l0 command function to nil.

So let's see how to save our work.

Lesson 3

No comments: