I've got a subroutine that gets the centroid of a closed polygon (lwpoly). It appears to fail if the polygon has any zero length line segments. I've added a trap that catches the error and "highlights" the offending polygon, but it stops there and does not return to the main function that called the subroutine. Is there a way to get back to the main function from the trap?
Here's the subroutine (original function by _gile (Autodesk LISP Forum 9-18-2006):
(defun return-centroid (lwpoly space / obj Region Centroid)
(setq *error* trap1)
(setq obj (vlax-ename->vla-object lwpoly))
(setq Region (vlax-invoke space 'addRegion (list obj)))
(setq Centroid (trans (vlax-get (car Region) 'Centroid) 1 0))
(vla-delete (car Region))
(defun trap1 (errmsg)
(prompt "\nUnable to determine centroid of highlighted polygon")
(setq centroid nil)
(command "pedit" pe "w" 10 "")
(setq *error* temperr)
The LISP (*error*) handler oncly cleans things up, it doesn't return to the program flow. What I've done for invalid centroids is use the (vl-catch-all-apply...) and (vl-catch-all-error-p...) functions when getting the Centroid property, allowing me to check if getting the property threw an error or not while still continuing with the program flow.
If you are going to fly by the seat of your pants, expect friction burns.
Adopt. Adapt. Overcome. Or be overcome.