Someone years ago was kind enough to provide the following lisp that returns the radius of an arc segment in a polyline
(defun c:ArcRad () (setq osm (getvar "osmode")) (prompt "\nPick point on pline arc") (setvar "osmode" 512) ; near (setq pta (getpoint)) (setq ptb (osnap pta "cen")) ; setq ptb (setq rad1 (distance pta ptb)) (setvar "osmode" osm) (princ (strcat "\n" (rtos rad1 2 4))) ; decimal result to 8 places ; change last number to suit (princ) )
The problem is that it doesn't have any error handling. If the users hits Esc or accidentally clicks on a straight line segment the routine fails with an *error* and the user's osmode setting is lost.
I am not a lisp coder so could some please help with error handling to reinstate the correct osmode?
Solved! Go to Solution.
Solved by hmsilva. Go to Solution.
(defun c:ArcRad (/ *error* pta ptb rad1) (defun *error* (msg) (if osm (setvar "OSMODE" osm) ) (cond ((not msg)) ((member msg '("Function cancelled" "quit / exit abort"))) ((princ (strcat "\n** Error: " msg " ** "))) ) (princ) ) ;; *error* (setq osm (getvar "osmode")) (prompt "\nPick point on pline arc") (setvar "osmode" 512); near (if (and (setq pta (getpoint)) (setq ptb (osnap pta "cen")) ; setq ptb );;and (progn (setq rad1 (distance pta ptb)) (setvar "osmode" osm) (princ (strcat "\n" (rtos rad1 2 4))) );; progn (prompt "\nYou did not pick an Arc...") );; if (*error* nil) (princ) )
HTH
Henrique
NBawden-
The error handler Henrique provided to you is a standard , but good , error handler.
I'd suggest remembering where you saved that routine or even to copy the new portion out into another text file called "error" that way you can copy and paste into new routines.
For reference (provided by HmSilva)/Henrique:
(defun *error* (msg) (if osm (setvar "OSMODE" osm) ) (cond ((not msg)) ((member msg '("Function cancelled" "quit / exit abort"))) ((princ (strcat "\n** Error: " msg " ** "))) ) (princ) ) ;; *error*
and don't forget the
(*error* nil)
at the end of routine
If you don't mind requiring the User to pick the Polyline within pickbox range rather than within Object-Snap Aperture range, and to know enough to pick at a location in which nothing else with a center is within Osnap Aperture range, then you can do this without any need to change OSMODE, and therefore no need to save or reset its value, and therefore no need for error handling. Here's a routine I've used for many years:
(defun C:RAD (/ pt); report RADius of curve
; [e.g. Polyline Arc segment, whose radius does not appear in Properties box or List result]
(while
(not (setq pt (cadr (entsel "Select curve: "))))
(prompt "\nNothing selected -- ")
); end while
(if (osnap pt "cen")
(prompt (strcat "\nRadius is " (rtos (distance (osnap pt "nea") (osnap pt "cen"))) "."))
(prompt "\nNo radius for that object.")
); end if
(princ)
)
Add mode and precision arguments to the (rtos) function if you want the radius reported in other than current Linear Units settings.
In order to copy into other routines, where in the routine does the quoted error handling text need to go? I am trying to add the same error code to one of my routines. Thank you.
(defun c:commandname (/ *error* your local variables) (defun *error* (msg) (if variable (setvar 'SYSTEMVARIABLE variable) ) (cond ((not msg)) ((member msg '("Function cancelled" "quit / exit abort"))) ((princ (strcat "\n** Error: " msg " ** "))) ) (princ) );; *error* ;; Your code (setq variable (getvar 'SYSTEMVARIABLE)) ;; Your code (setvar 'SYSTEMVARIABLE newvalue) ;; Your code ;; to restore the variable value (*error* nil) (princ) )
Hope this helps,
Henrique