Author Topic: Error Handler Help  (Read 1730 times)

0 Members and 1 Guest are viewing this topic.

KHoughton

  • Guest
Error Handler Help
« on: February 23, 2017, 04:26:48 PM »
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: [Select]
(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)
)

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: Error Handler Help
« Reply #1 on: February 24, 2017, 08:11:53 AM »
... 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.
So why do you have this in your code?:
Code: [Select]
    (if (= msg "quit / exit abort")
        (prompt "\nCommand Cancelled\n")
    )

ronjonp

  • Needs a day job
  • Posts: 7527
Re: Error Handler Help
« Reply #2 on: February 24, 2017, 09:15:18 AM »
Maybe this will give you some ideas. Also, remember to localize your variables.

Code - Auto/Visual Lisp: [Select]
  1. (defun c:tocell (/ *error* elev p1 vars x)
  2.   (defun *error* (msg)
  3.     ;; Reset vars
  4.     (mapcar '(lambda (x) (setvar (car x) (cdr x))) vars)
  5.     (and (/= 1 (getvar 'cvport)) (command-s "_.pspace"))
  6.     (or (wcmatch (strcase msg) "*BREAK,*CANCEL*,*EXIT*") (princ (strcat "\n** Error: " msg " **")))
  7.     (princ)
  8.   )
  9.   (if (= 1 (getvar 'cvport))
  10.     (progn ;; Store all variable values before you change them
  11.            (setq vars (mapcar '(lambda (x) (cons x (getvar x))) '(clayer cmdecho attdia osmode)))
  12.            ;; Change them
  13.            (setvar 'cmdecho 0)
  14.            (setvar 'osmode 1)
  15.            (setvar 'attdia 0)
  16.            (if (null (tblobjname "layer" "G-Text"))
  17.              (command "layer"         "new"           "G-Text"        "color"
  18.                       "7"             "G-Text"        "d"
  19.                       "Standard Text, Leaders, Notes" "G-Text"        "lw"
  20.                       ".18"           "G-Text"        ""
  21.                      )
  22.            )
  23.            (setvar 'clayer "G-Text")
  24.            (if (and (null (command-s "_.mspace"))
  25.                     (setq elev (rtos (cadr (getpoint "\nPick Point for Elevation: "))))
  26.                     (null (command-s "_.pspace"))
  27.                     (setq p1 (getpoint "\n Pick Insertion Point:"))
  28.                )
  29.              (progn (command "insert"
  30.                              "S:/CAD_Standards/_SE_Custom/structural/symbols/blocks/Toc-Elev_Left"
  31.                              "_scale"                        "1"
  32.                              "_rotate"                       "0"
  33.                              "_non"                          p1
  34.                              elev                            ""
  35.                              ""
  36.                             )
  37.              )
  38.            )
  39.            ;; Put vars back to original value
  40.            (mapcar '(lambda (x) (setvar (car x) (cdr x))) vars)
  41.     )
  42.     (alert "NOT in paperspace!")
  43.   )
  44.   (princ)
  45. )
« Last Edit: February 24, 2017, 12:04:46 PM by ronjonp »

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

KHoughton

  • Guest
Re: Error Handler Help
« Reply #3 on: February 24, 2017, 11:28:06 AM »
So why do you have this in your code?:
Code: [Select]
    (if (= msg "quit / exit abort")
        (prompt "\nCommand Cancelled\n")
    )

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.
« Last Edit: February 24, 2017, 11:37:04 AM by Keith H. »

ronjonp

  • Needs a day job
  • Posts: 7527
Re: Error Handler Help
« Reply #4 on: February 24, 2017, 12:05:33 PM »
Code modified above to return to paperspace on error and will set osmode too.

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

KHoughton

  • Guest
Re: Error Handler Help
« Reply #5 on: February 24, 2017, 01:00:48 PM »
Works great! Thanks for your help! I really appreciate it.

ronjonp

  • Needs a day job
  • Posts: 7527
Re: Error Handler Help
« Reply #6 on: February 24, 2017, 01:26:45 PM »
Works great! Thanks for your help! I really appreciate it.
Glad to help :)

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC