Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Improving Lisp to handle *error*

6 REPLIES 6
SOLVED
Reply
Message 1 of 7
nbawden
612 Views, 6 Replies

Improving Lisp to handle *error*

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?

6 REPLIES 6
Message 2 of 7
hmsilva
in reply to: nbawden

(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

EESignature

Message 3 of 7
bhull1985
in reply to: hmsilva

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

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Please use code tags and credit where credit is due. Accept as solution, if solved. Let's keep it trim people!
Message 4 of 7
Kent1Cooper
in reply to: nbawden

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.

Kent Cooper, AIA
Message 5 of 7
nbawden
in reply to: Kent1Cooper

Thanks everyone!

Message 6 of 7
ScottMason
in reply to: bhull1985

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.

 

Message 7 of 7
hmsilva
in reply to: ScottMason

(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

EESignature

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost