• Industries
  • Products
  • Buy
  • Services & Support
  • Communities
  • Discussion Groups

    Visual LISP, AutoLISP and General Customization

    Reply
    Active Contributor
    Posts: 27
    Registered: ‎01-28-2008

    Return to Calling Function after Trapping Error in Subroutine

    52 Views, 2 Replies
    03-13-2012 09:33 AM

    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))
    Centroid
    )

    (defun trap1 (errmsg)
    (prompt "\nUnable to determine centroid of highlighted polygon")
    (setq centroid nil)
    (command "pedit" pe "w" 10 "")
    (setq *error* temperr)
    )

    Please use plain text.
    *Expert Elite*
    dgorsman
    Posts: 3,278
    Registered: ‎10-12-2006

    Re: Return to Calling Function after Trapping Error in Subroutine

    03-13-2012 09:55 AM in reply to: ghlad

    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.


    Please use plain text.
    Active Contributor
    Posts: 27
    Registered: ‎01-28-2008

    Re: Return to Calling Function after Trapping Error in Subroutine

    03-13-2012 10:14 AM in reply to: dgorsman

    Thanks.  Looks like I'll have to add the vl-catch-all functions.

    Please use plain text.