Author Topic: Error handling in lisp  (Read 910 times)

0 Members and 1 Guest are viewing this topic.

Amsterdammed

  • Swamp Rat
  • Posts: 507
  • Revit MEP 2016 Acad2013, OPEN DCL
Error handling in lisp
« on: August 10, 2005, 05:49:00 PM »
Hello  everybody

Is there  any way to tell lisp what to do on error, like in VBA?

Thanks in Advance

Bernd
Bernd

ronjonp

  • Needs a day job
  • Posts: 7071
Error handling in lisp
« Reply #1 on: August 10, 2005, 06:11:50 PM »
Take a look HERE

Windows 10 x64 - AutoCAD /C3D 2020

Custom Build PC

Amsterdammed

  • Swamp Rat
  • Posts: 507
  • Revit MEP 2016 Acad2013, OPEN DCL
Error handling in lisp
« Reply #2 on: August 10, 2005, 07:46:41 PM »
Interesting

Got to understand this, I see.

Thanks, Ronjonp
Bernd

Jürg Menzi

  • Swamp Rat
  • Posts: 597
  • Oberegg, Switzerland
Error handling in lisp
« Reply #3 on: August 11, 2005, 04:30:30 AM »
Code: [Select]
; == For single modules:
(defun C:MyFunc ( / OldCmd *Error*)
 (or Me:Aco (setq Me:Aco (vlax-get-acad-object)))
 (or Me:Acd (setq Me:Acd (vla-get-ActiveDocument Me:Aco)))
 (vla-StartUndoMark Me:Acd)
 (setq OldCmd (getvar "CMDECHO"))
 (defun *Error* (Msg)
  (setvar "CMDECHO" OldCmd)
  (vla-EndUndoMark Me:Acd)
  (if (and Msg (not (eq Msg "quit / exit abort")))
   (princ Msg)
  )
  (princ)
 )
 ;
 ;...code...
 ;
 (*Error* nil)
)

; == For the common function library:
(defun C:MyFunc ( / )
 (MeStartFunc '("CMDECHO" "OSMODE"))
 ;
 ;...code...
 ;
 (MeEndFunc)
)
;
; -- Function MeStartFunc
; Start function to LISP programs.
; Arguments [Type]:
;   Lst = Systemvars list [LIST]
; Return [Type]:
;   > Null
; Notes:
;   None
;
(defun MeStartFunc (Lst)
 (or Me:Aco (setq Me:Aco (vlax-get-acad-object)))
 (or Me:Acd (setq Me:Acd (vla-get-ActiveDocument Me:Aco)))
 (vla-StartUndoMark Me:Acd)
 (setq Me:Oer *Error*
      *Error* MeUserError
 )
 (mapcar
 '(lambda (l)
   (if (not (assoc l Me:Var))
    (setq Me:Var (append Me:Var (list (cons l (getvar l)))))
   )
  ) Lst
 )
 (princ)
)
;
; -- Function MeEndFunc
; End function to LISP programs.
; Arguments [Type]:
;   --- =
; Return [Type]:
;   > Null
; Notes:
;   None
;
(defun MeEndFunc ()
 (if Me:Var
  (mapcar '(lambda (l) (setvar (car l) (cdr l))) Me:Var)
 )
 (setq *Error* Me:Oer
       Me:Oer  nil
       Me:Var  nil
 )
 (vla-EndUndoMark Me:Acd)
 (princ)
)
;
; -- Function MeUserError
; User error handler.
; Arguments [Type]:
;   Msg = AutoLISP message [STR]
; Return [Type]:
;   > Null
; Notes:
;   None
;
(defun MeUserError (Msg)
 (if (and Msg (not (eq Msg "quit / exit abort")))
  (princ Msg)
 )
 (MeEndFunc)
 (princ)
)
A computer's human touch is its unscrupulousness!
MENZI ENGINEERING GmbH
Current A2k14... A2k18 - Start R2.18

hudster

  • Gator
  • Posts: 2846
Error handling in lisp
« Reply #4 on: August 11, 2005, 05:06:34 AM »
this is the error checking routine included on the old afralisp website.

just add (LOAD "ERROR.LSP") to your lisp routine and save the error.lsp in a pathed directory and away you go.

Code: [Select]
;;;
;;; TITLE: error.lsp
;;;
;;; Copyright (C) 2005 by www.afralisp.com
;;;
;;; Permission to use, copy, modify, and distribute this
;;; software and its documentation for any purpose and without
;;; fee is hereby granted
;;;
;;; THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT EXPRESS OR
;;; IMPLIED WARRANTY. ALL IMPLIED WARRANTIES OF FITNESS FOR ANY
;;; PARTICULAR PURPOSE AND OF MERCHANTABILITY ARE HEREBY
;;; DISCLAIMED.
;;;
;;; www.afralisp.com
;;; April 2005
;;;
;;;-------------------------------------------------------------
;;; Description:
;;; Error trap, console break function to be called from
;;; within other lisp routines in event of error or user
;;; console break.
;;;
;;;-------------------------------------------------------------
;;; COMMAND LINE: none
;;;-------------------------------------------------------------
(defun error () ;load function
  (prompt "\nGlobal Error Trap Loaded") ;inform user
  (princ)
) ;defun
;;;*==========================================================
(defun initerr () ;init error
  (setq oldlayer (getvar "clayer") ;save settings
osm (getvar "osmode")
oldpick (getvar "pickbox")
blip (getvar "blipmode")
tlm (getvar "tilemode")
cmd (getvar "cmdecho")
mnec (getvar "menuecho")
hilt (getvar "highlight")
  )
  (setq temperr *error*) ;save *error*
  (setq *error* trap) ;reassign *error*
  (princ)
) ;defun
;;;*===========================================================
(defun trap (errmsg) ;define trap
  (command nil nil nil)
  (if (not (member errmsg '("console break" "Function Cancelled"))
      )
    (princ (strcat "\nError: " errmsg)) ;print message
  )
  (command "undo" "b") ;undo back
  (setvar "clayer" oldlayer) ;reset settings
  (setvar "blipmode" blip)
  (setvar "menuecho" mnec)
  (setvar "highlight" hilt)
  (setvar "osmode" osm)
  (setvar "pickbox" oldpick)
  (setvar "tilemode" tlm)
  (princ "\nError Resetting Enviroment ") ;inform user
  (terpri)
  (setq *error* temperr) ;restore *error*
  (princ)
) ;defun
;;;*===========================================================
(defun reset () ;define reset
  (setq *error* temperr) ;restore *error*
  (setvar "clayer" oldlayer) ;reset settings
  (setvar "blipmode" blip)
  (setvar "menuecho" mnec)
  (setvar "highlight" hilt)
  (setvar "osmode" osm)
  (setvar "cmdecho" cmd)
  (setvar "pickbox" oldpick)
  (setvar "tilemode" tlm)
  (princ)
) ;defun
;;;*======================================================
(princ)
Revit BDS 2017, 2016, 2015, 2014, AutoCAD 2017, 2016, Navisworks 2017, 2016, BIM360 Glue

Amsterdammed

  • Swamp Rat
  • Posts: 507
  • Revit MEP 2016 Acad2013, OPEN DCL
Error handling in lisp
« Reply #5 on: August 11, 2005, 06:38:52 AM »
:D
Thanks, Gentlemen
Bernd