See the top rated post in this thread. Click here

Results 1 to 1 of 1

Thread: AutoLISP 101 - Command Expressions (basic error handling and local functions)

  1. #1
    Past Vice President / AUGI Volunteer peter's Avatar
    Join Date
    2000-09
    Location
    Honolulu HI
    Posts
    1,109
    Login to Give a bone
    1

    Default AutoLISP 101 - Command Expressions (basic error handling and local functions)

    Before I dig into Entity Lists...

    I thought I might talk a little about the command expression (and command-s and vl-cmdf too)

    They allow the user to run AutoCAD commands, which sometimes can do things that LISP (or VisualLISP) cannot do.

    To me it is the LAST choice to do something.

    I prefer to use ActiveX, then entity methods and finally the command pipe (or throat).


    So how can we use the command expression.

    Code:
    (command "line" pause pause "")
    The pause argument waits for user input or selection.

    If you do not like to see the prompts you can set the system variable "cmdecho" to 0 (or 1 to see)

    That way you can add your own prompts.

    Code:
    (defun C:L1 (/ intCMDEcho)
     (setq intCMDEcho (getvar "cmdecho"))
     (setvar "cmdecho" 0)
     (command "line" pause pause "")
     (setvar "cmdecho" intCMDEcho)
    )
    The only problem with this is escaping the command expression will cause the cmdecho to remain off.

    So the following code includes an error function that runs if you escape the command expression and
    changes the system variable back to the original value. This is also a good example of creating a local
    function inside another function.

    Code:
    (defun C:L1 (/ intCMDEcho *Error*)
     (defun *Error* (X)
      (setvar "cmdecho" intCMDEcho)
     )
     (setq intCMDEcho (getvar "cmdecho"))
     (setvar "cmdecho" 0)
     (command "line" pause pause "")
     (setvar "cmdecho" intCMDEcho)
    )
    In there are both the Command-S and VL-Cmdf expressions that are similar syntax.

    VL-cmdf is nice because it check to make sure the expression works before running it so I use it.

    You need to run the (vl-load-com) expression to load VisualLISP.

    I just make it the very last line of code in my files like.

    Code:
    (defun C:L1 (/ intCMDEcho *Error*)
     (defun *Error* (X)
      (setvar "cmdecho" intCMDEcho)
     )
     (setq intCMDEcho (getvar "cmdecho"))
     (setvar "cmdecho" 0)
     (vl-cmdf "line" pause pause "")
     (setvar "cmdecho" intCMDEcho)
    )
    (vl-load-com)
    Last edited by peter; 2017-09-14 at 06:07 AM.
    AutomateCAD

Similar Threads

  1. Diesel Expressions vs Field expressions in Trueview 2014
    By jayvee in forum DWG TrueView - General
    Replies: 1
    Last Post: 2018-07-13, 11:09 AM
  2. Error Handling
    By ticad02 in forum AutoLISP
    Replies: 16
    Last Post: 2009-12-21, 03:39 PM
  3. Error Handling
    By whattaz13 in forum AutoLISP
    Replies: 2
    Last Post: 2008-07-16, 01:03 PM
  4. Replies: 7
    Last Post: 2006-11-17, 12:56 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •