Jump to content

What is the correct way of calling a function in a command c: ?


Recommended Posts

Posted (edited)

I have learnt Autolisp on my own for several weeks but I am confused with some basics.

 

I just came up with this weird and clearly redundant piece of code, and when I load it and type "cane" in the console, it shows "nil". When I type "cane" in AutoCAD command prompt, it asks for my radius and then stop. What is the reason for this?

 

Btw is this a correct way of calling a function in the c: command?

 

(defun c:cane ()
 (setq radius (getreal "\nEnter radius: "))
 (setq jas (circarea radius))
 (princ jas)
)

(defun circarea (rad)
  (setq n (* pi rad rad))
)

Edited by SLW210
Code Tags!!!!
Posted

Nothing is wrong with that result .

 

You can not run the routine from a console since that it is built as a standalone program that it starts with "c:" so that's why it did return nil.

 

But if it is started without "c:" it would run as if it is running in the command line as standalone program .

 

To explain the fore-said with modification on the codes , just remove the c: and load the function again and call it from console to see the difference like this (cane) .

 

Hope this helps .

Posted
I just came up with this weird and clearly redundant piece of code, and when I load it and type "cane" in the console, it shows "nil". When I type "cane" in AutoCAD command prompt, it asks for my radius and then stop. What is the reason for this?

 

The function is defined with the symbol c:cane, therefore the symbol cane will hold no value when evaluated through the console unless you have assigned a value to this symbol elsewhere.

 

On evaluating the symbol c:cane at the console, you will see a pointer to the memory address of the function definition:

_$ c:cane
#<USUBR @000000002e6fb930 C:CANE>

You can then evaluate this function at the console as you might any other AutoLISP function:

_$ (c:cane)
4.523894.52389

(The result is duplicated because princ will print the value to the command-line and will then return the supplied value, which is in turn returned by the calling function c:cane since it is the last evaluated expression - you can suppress this duplication by including a (princ) or (prin1) as the last evaluated expression, which will cause a null symbol to be returned).

 

Prefixing the function symbol with c: has no effect when evaluating the function through AutoLISP, however, this prefix enables the function to be evaluated as a command at the AutoCAD command-line.

Posted

You will often see in examples here different ways of defining defuns like (AH:dothis) its the author leaving his/her signature on the code and as Lee said previously type dothis will not work, you have to use the full version (AH:dothis)

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...