| 
  • If you are citizen of an European Union member nation, you may not use this service unless you are at least 16 years old.

  • You already know Dokkio is an AI-powered assistant to organize & manage your digital files & messages. Very soon, Dokkio will support Outlook as well as One Drive. Check it out today!

View
 

Lesson 2

Page history last edited by Matthew Haws 11 months, 1 week ago Saved with comment

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 responses, 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.
  • Once we set the layer to 0, it was the default response for Unlock and ON. So we just used empty strings "" instead of "0" for those inputs. 

 

 

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

 

Comments (0)

You don't have permission to comment on this page.