Code Red > AutoLISP (Vanilla / Visual)

Error Handler Help

(1/2) > >>

KHoughton:
I am trying to add an error handler to a lisp routine to restore previous settings/variables in the event the user hits enter or esc. The attached routine seems to work as desired but if enter or esc is pressed there is no error message. I would like for a message to display if the command is interrupted.

The purpose of the routine is to add a text label (attached block) in paper space by selecting a point in model space and automatically filling in the elevation value as the block is inserted.

Any suggestions to streamline the routine are definitely welcome and appreciated.

Thanks!


--- Code: ---(defun C:tocell (/ olderror)

(setvar "CMDECHO" 0)
(setq olderror *error* *error* inserror)
(setq OM (getvar "osmode"))
(setvar "OSMODE" 1)
(setq attdia_org (getvar "attdia"))
(setvar "ATTDIA" 0)
(setq LYR (getvar "clayer"))
(setq LR10 (tblsearch "layer" "G-Text"))
    (if (= LR10 nil)
        (command "layer" "new" "G-Text" "color" "7" "G-Text" "d" "Standard Text, Leaders, Notes" "G-Text" "lw" ".18" "G-Text" "")
    )
(setvar "clayer" "G-Text")

(command "mspace")

(setq Elev (rtos (cadr(getpoint "\nPick Point for Elevation: "))))

(command "pspace")

(setq p1 (getpoint "\n Pick Insertion Point:"))
(command "insert" "S:/CAD_Standards/_SE_Custom/structural/symbols/blocks/Toc-Elev_Left" "_scale" "1" "_rotate" "0" "_non" p1 Elev "" "")
(setvar "clayer" LYR)
(setvar "ATTDIA" attdia_org)
(setvar "osmode" OM)
(prompt "\n:: done ::")
)
;
(defun inserror (msg)
    (setvar "OSMODE" OM)
(setvar "ATTDIA" attdia_org)
(command-s "layer" "set" LYR "")
(command-s "pspace")
    (setq *error* OLDERROR)
    (setq msg1 msg)
    (if (= msg "quit / exit abort")
        (prompt "\nCommand Cancelled\n")
    )
    (princ)
)

--- End code ---

roy_043:

--- Quote from: Keith H. on February 23, 2017, 04:26:48 PM ---... but if enter or esc is pressed there is no error message. I would like for a message to display if the command is interrupted.
--- End quote ---
So why do you have this in your code?:

--- Code: ---    (if (= msg "quit / exit abort")
        (prompt "\nCommand Cancelled\n")
    )
--- End code ---

ronjonp:
Maybe this will give you some ideas. Also, remember to localize your variables.


--- Code - Auto/Visual Lisp: ---(defun c:tocell (/ *error* elev p1 vars x)  (vl-load-com)  (defun *error* (msg)    ;; Reset vars    (mapcar '(lambda (x) (setvar (car x) (cdr x))) vars)    (and (/= 1 (getvar 'cvport)) (command-s "_.pspace"))    (or (wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*") (princ (strcat "\n** Error: " msg " **")))    (princ)  )  (if (= 1 (getvar 'cvport))    (progn ;; Store all variable values before you change them           (setq vars (mapcar '(lambda (x) (cons x (getvar x))) '(clayer cmdecho attdia osmode)))           ;; Change them           (setvar 'cmdecho 0)           (setvar 'osmode 1)           (setvar 'attdia 0)           (if (null (tblobjname "layer" "G-Text"))             (command "layer"         "new"           "G-Text"        "color"                      "7"             "G-Text"        "d"                      "Standard Text, Leaders, Notes" "G-Text"        "lw"                      ".18"           "G-Text"        ""                     )           )           (setvar 'clayer "G-Text")           (if (and (null (command-s "_.mspace"))                    (setq elev (rtos (cadr (getpoint "\nPick Point for Elevation: "))))                    (null (command-s "_.pspace"))                    (setq p1 (getpoint "\n Pick Insertion Point:"))               )             (progn (command "insert"                             "S:/CAD_Standards/_SE_Custom/structural/symbols/blocks/Toc-Elev_Left"                             "_scale"                        "1"                             "_rotate"                       "0"                             "_non"                          p1                             elev                            ""                             ""                            )             )           )           ;; Put vars back to original value           (mapcar '(lambda (x) (setvar (car x) (cdr x))) vars)    )    (alert "NOT in paperspace!")  )  (princ))

KHoughton:

--- Quote from: roy_043 on February 24, 2017, 08:11:53 AM ---So why do you have this in your code?:

--- Code: ---    (if (= msg "quit / exit abort")
        (prompt "\nCommand Cancelled\n")
    )
--- End code ---

--- End quote ---

Because I am trying to get it to show on the command line if the command is cancelled.


Ronjonp ~ Thank you for your input. I'm new to lisp so I'm working through your code trying to figure out what everything is doing. Your code does not return the osmode back to the original settings but I was able to fix that. Also if the command is cancelled it stays in model space rather than switching back to paper space as it did in the original routine. I'm trying to figure out how to accomplish this with your new code.

ronjonp:
Code modified above to return to paperspace on error and will set osmode too.

Navigation

[0] Message Index

[#] Next page

Go to full version