PDA

View Full Version : Why C:?



whdjr
2004-08-13, 12:43 PM
Why do you have to use C: to run a command with arguments?
Example:

_.underline - Runs the underline command.
(c:underline "argument") - The c: is required if I have an argument.

I was just wondering why the command with the argument will not run without the preceeding C:?

Anyone know why?

Ed Jobe
2004-08-13, 02:14 PM
It has nothing to do with arguments, any lisp can have arguments. If a symbol is not registered in the command stack, it needs the c: to run as a command so that you can just type "underline" at the command prompt. If you want to call it via lisp, then you have to use its full symbol name "(c:underline)". The only connection with arguments in this case is that, since you are not programming when you enter it as a command, you have to use the command's user interface and it does not accept arguments. Arguments are a feature of the programming language and so you have to call it using lisp if you want to use arguments.

whdjr
2004-08-13, 02:30 PM
I understand all that, but why couldn't you just use (underline "argument") without the c: ?

RobertB
2004-08-13, 03:01 PM
I often write my main code as a simple function for programmic access with arguments, e.g.:


(defun rrbI:MyCoolApp (arg1 arg2) ...)

but then need to provide a command version for the users and menus (to re-execute via <Enter>) so:


(defun C:MyCoolApp (/ inp1 inp2)
(setq inp1 ...
inp2 ...)
(rrbI:MyCoolApp inp1 inp2))

Ed Jobe
2004-08-13, 04:03 PM
Because the function was defun'ed to symbol "C:underline" and not "underline". See Robert's example. IOW, symbol (underline) does not exist.

RobertB
2004-08-13, 05:50 PM
Does this brief code segment help you understand?


_$ (defun C:Test (arg1 arg2) arg1)
C:TEST
_$ (c:test "hello?")
; error: too few arguments
_1$
; reset after error
_$ (defun rrbI:Test (arg1 arg2) arg1)
RRBI:TEST
_$ (defun C:Test () (rrbI:Test "Hello!" nil))
C:TEST
_$ (c:Test)
"Hello!"
_$ (rrbI:Test "Is there anybody out there?" nil)
"Is there anybody out there?"

sinc
2004-08-13, 09:25 PM
???

That confused ME... :-P

The way the Autocad command line works, you can't enter a command with arguments. The command line assumes that "space" is the same as "return", UNLESS what you just started typing starts with an open-parenthesis. Then it knows you're entering a command, and it waits for you to finish it off and hit return.

When you define your function with a "C:" prefix, you tell Autocad to add it to its list of commands. Your function therefore works like a command. For an Autocad command, the command starts executing as soon as you type that space, and there is no chance to enter an argument. It's the same as calling your function with no arguments.

Ed Jobe
2004-08-13, 09:44 PM
He didn't run (c:test) at the command line, but called it via lisp, which is no different than calling any other function. The whole idea is to have a lisp version of a routine and then create a command line wrapper for it. Robert's first example was clearer at demonstrating that. The "rrbl" function is the underlying version and the "c:" version calls it with some user interface code rather than rewriting the whole thing for command line use.

sinc
2004-08-15, 04:10 PM
Yeah, I was able to figure it out, but it took parsing it a few times. :wink:

It confused me because it didn't seem to address what I thought was Will's fundamental point of confusion. I might have misunderstood, but my impression was that Will was being thrown by the way Autocad's command line works.

Actually, as I understood it he asked two questions: why can't he type arguments from the command line, and why, if he defines a function with a name like "c:test", he can type simply "test" at the command line, but not call the function with "(test)"?

Think of it this way. When you type a command at the command line, Autocad tries to run that command. If it doesn't find a command with that name, it prepends a "c:" to it, and looks for an Autolisp function with that name. If it finds the function, it runs it.

So, for example, if you try typing "c:underline" at the command line, you'll get an error, because Autocad will be looking for a command named "c:underline" or a function named "c:c:underline", neither of which is defined.

And the reason you can't enter arguments at the command line is because Autocad starts executing your function as soon as you hit space. This is the same as calling your function with no arguments, and if your function was defined to expect arguments, you'll get the "too few arguments" error.

Does this answer your questions, Will, or did I misunderstand?

whdjr
2004-08-16, 03:15 PM
WOW! I didn't realize this would cause such a stir.

I think my questions were answered. Its still a little foggy though. Let me give an example to discuss by. We have a menu file that loads specific tools via an mnl file. We have some of those tools that require arguments when running and some that don't. When we make our tool buttons the ones that require arguments have to be preceded with a C: and surrounded with parens and the ones with no arguments can be run with just the command and no C: or parens.

My question was why can the tool button with no arguments be run without parens or c:?

The tools with the arguments have to be run with parens and C:.


Why is the C: needed to run the tools with the arguments?
Why can the tools with the arguments be run without C:?
All the tools in question are preloaded via the mnl file so no loading is happening.

If you guys already answered my question I'm sorry I guess I didn't understand it.

Thanks for all your patience,

Ed Jobe
2004-08-16, 05:19 PM
Why is the C: needed to run the tools with the arguments?
Why can the tools with the arguments be run without C:?The aswer to both is the same. Because commands can't have arguments but lisp functions can. However, I don't think I would agree with Richards explanation. When you enter just the text that represents a command name, you are running the code as a command, from the command stack, which doesn't accept arguments as input. When you enter (c:functionname), you are entering a lisp function, which can have arguments. As soon as the command line interpreter sees the parens, it evaluates it as lisp and you need the "c:" to identify the function's symbol name as it was defined when the code was loaded. For example, you could type in "()" and you will get "nil", not "Unknown command". "When in Rome, do as the Romans do". When in lisp, do as the lispers do. :mrgreen:

whdjr
2004-08-16, 05:39 PM
Thanks Ed.

I got it now.

Ed Jobe
2004-08-16, 05:42 PM
Glad to help. :-)