Jump to content

closest vertex to a point an polyline


HARRY-PLOTTER

Recommended Posts

Build a list of vertexes and distances from said point and sort it ascending; retain first element.

 

Another:

 

(defun c:test ()
   (setq a (vlax-ename->vla-object (car (entsel "\nSelect Polyline"))))
   (setq b (vlax-curve-getClosestPointTo a (trans (cadr (grread T)) 1 0)))
   (setq c (vlax-curve-getParamAtPoint a b))
   (or (< (- c (setq c (fix c))) 0.50000000001) (setq c (1+ c)))
   (setq v (vlax-curve-getPointAtParam a c))
   (princ (strcat "\nVertex n. " (itoa (1+ c)) " (" (rtos (car v)) " " (rtos (cadr v)) ")" ))
   (princ)
)
   

Link to comment
Share on other sites

Another to add a green point at the nearest param .

 

(defun c:Test (/ s e pr p pt a b)
 ;;    Tharwat 2014. cadtutor        ;;
 (if (and (setq s (entsel)) (eq (cdr (assoc 0 (entget (setq e (car s))))) "LWPOLYLINE"))
   (progn (setq pr (vlax-curve-getparamatpoint (car s) (setq p (vlax-curve-getclosestpointto (car s) (cadr s)))))
          (if (< (distance p (setq a (vlax-curve-getpointatparam (car s) (fix pr))))
                 (distance p (setq b (vlax-curve-getpointatparam (car s) (1+ (fix pr)))))
              )
            (setq pt a)
            (setq pt b)
          )
          (entmakex (list '(0 . "POINT") (cons 10 pt) '(62 . 3)))
   )
   (princ "\n Nothing selected or not a Polyline <!>")
 )
 (princ)
)
(vl-load-com)

Link to comment
Share on other sites

There is no need to convert the object to with vlax-*** functions ;)

The type of the argument for VLAX-CURVE-* functions family is listed in help as

The VLA-object to be measured
so a good programming practice will be to comply, even if entity name is supported. This will ensure a running code if one day Autodesk decide to enforce the rule (it has happened in the past, although I don't recall the function in case).
Link to comment
Share on other sites

(defun c:Test ( / s e pr p pt a b ) (vl-load-com)
 ;;    Tharwat 2014. cadtutor - mods by M.R.    ;;
 (if (and (setq s (entsel)) (wcmatch (cdr (assoc 0 (entget (setq e (car s))))) "*POLYLINE"))
   (progn (setq pr (vlax-curve-getparamatpoint (car s) (setq p (vlax-curve-getclosestpointto (car s) (cadr s)))))
          (setq a (vlax-curve-getpointatparam (car s) (fix pr)))
          (setq b (vlax-curve-getpointatparam (car s) (1+ (fix pr))))
          (entmakex (list '(0 . "POINT") (cons 10 a) '(62 . 3)))
          (entmakex (list '(0 . "POINT") (cons 10 b) '(62 . 3)))
   )
   (princ "\n Nothing selected or not a Polyline <!>")
 )
 (princ)
)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...