Search code examples
autocadautolisp

Mtext. Autolisp returns "Invalid point" but typing the point in the command windows works


I'm new to autolisp and setting my first macro. I want to create a rectangle, label it with some text centered on it and then convert both entities into a block (this is for optimizing the loading of packaged items into a trailer).

I've succeed building the rectangle but I'm stuck on the mtext part. This is what I have done so far:

(defun c:caja  ()

    ;Switch OFF System Variables

    (setvar "osmode" 0)
    ;Switch OFF snap

    ;(setvar "blipmode" 0)
    ;Switch OFF Blipmode

*******************************************************
    ;User Inputs


    (setq pt1 (getpoint "\nSelect start point: "));lower left corner 
    (setq Long (getdist "\nLength m : "))
    (setq Ancho (getdist "\nWidth  : "))
    ;(setq Alto (getdist "\nHeight  : "))
    ;(setq Peso (getdist "\nWeight  : "))


*******************************************************
(setq pt2 (polar pt1 0 Long )) ;lower right corner
(setq pt3 (polar pt2 (* pi 0.5) Ancho));upper right corner

*******************************************************

(command"rectang" pt1 pt3"")

(command "mtext" "!pt1" "!pt3" "potato")

When executing the last line of the code I get:
Invalid point. ; error: Function cancelled. However autocad lets me keep working on the mtext command and asks me to "specify first corner". If I type !pt1 there it Works.

My understanding is that in autolisp I must write between quotes "" every answer that I would normally type in the command prompt so I don't know what I'm doing wrong.


Solution

  • Using the exclaimation mark prefix allows you to evaluate global AutoLISP variables directly at the AutoCAD command-line, outside of any AutoLISP program.

    However, when used within a program, such variables will be evaluated as part of the evaluation of the AutoLISP program, and therefore the exclaimation mark prefix is not required.

    You have already implemented this successfully when calling the RECTANG command:

    (command "rectang" pt1 pt3 "")
    

    Therefore, you can use the same logic for the MTEXT command:

    (command "mtext" pt1 pt3 "potato" "")
    

    I would also make the following recommendations:

    • Store the current values of system variables before changing them, so that you may reset them back to their original values (otherwise the user will lose all of their Object Snap settings, for example).

    • Implement a local error handler to automatically reset system variables in the event of an error or the user pressing Esc. Refer to my tutorial here for more information on how to accomplish this.

    • Use an underscore (_) & period (.) to prefix command names, e.g.:

      (command "_.rectang" ... )
      

      The underscore ensures that the command is interpreted in English in non-English versions of AutoCAD. The period ensures that the standard definition of the command is used, not a redefinition.

    • Test for valid user input using an if statement before proceeding.

    • Declare your local variables to ensure that you variables are not inadvertently overwritten by other programs defining symbols in the document namespace. See my tutorial here for more information on this.