Jump to content

How get the nearest endpoint of a line by click on it??


Recommended Posts

Posted

Hi all.

I need a combination of functions that can get the nearest endpoint of a line which selecting by click on it or crossing a window.

please guide me....

thanks.

  • Replies 21
  • Created
  • Last Reply

Top Posters In This Topic

  • alanjt

    10

  • Lee Mac

    9

  • kasra

    2

  • JohnM

    1

Top Posters In This Topic

Posted

I've used this a few times. :)

 

;;; Retrieve closest end point on object
;;; #EntPnt - List with object and point
;;; Alan J. Thompson, 11.10.09
 (defun AT:ClosestEndPoint (#EntPnt)
   (if (vl-consp #EntPnt)
     (car (vl-sort
            (list (vlax-curve-getstartpoint (car #EntPnt))
                  (vlax-curve-getendpoint (car #EntPnt))
            ) ;_ list
            (function
              (lambda (a b)
                (< (distance (trans (cadr #EntPnt) 1 0) a) (distance (trans (cadr #EntPnt) 1 0) b))
              ) ;_ lambda
            ) ;_ function
          ) ;_ vl-sort
     ) ;_ car
   ) ;_ if
 ) ;_ defun

Posted

I’m sure there are several ways to accomplish this but one way is to use the (entsel) function. This will return the entity name and the coordinates of the point picked.

Use the (entget) function to first verify that it is a line then get the start and end points.

You might want to use a while loop to error trap if the user picks nothing or an object that is not a line.

Then use the (distance) function to check the distance from the pick point to the start point of the line and then the pick point to the end point of the line.

Then use the (min) function to find the shortest distance.

Using a crossing window can be accomplished but it will require a lot more code and it will still have situations where it can go wrong, Like: what if the user windows 2 lines?

How will you know what line to run the program on. So it would be better to stick with the pick a point on the line option.

Start coding and post it if you have any questions or need help

Posted

Another, not dissimilar to Alan's

 

(defun GetClosestEndInSS (ss pt / GetPoints)
 (vl-load-com) ;; Lee Mac  ~  11.04.10
 (setq pt (trans pt 1 0))

 (defun GetPoints (ss / i ent lst)
   (setq i -1)
   (while (setq ent (ssname ss (setq i (1+ i))))
     (setq lst (cons (vlax-curve-getStartPoint ent)
                     (cons (vlax-curve-getEndPoint ent) lst))))
   lst)

 (car (vl-sort (GetPoints ss) (function (lambda (a b) (< (distance pt a)
                                                         (distance pt b)))))))



(defun c:test (/ ss pt)

 (if (and (setq ss (ssget))
          (setq pt (getpoint "\nPoint to Test: ")))
   (entmake (list (cons 0 "POINT") (cons 10 (GetClosestEndInSS ss pt)))))

 (princ))

Posted

Nice way to apply mine to a selectionset. :thumbsup: However, you forgot trans on the picked point.

 

Another, not dissimilar to Alan's

 

(defun GetClosestEndInSS (ss pt / GetPoints)
 (vl-load-com) ;; Lee Mac  ~  11.04.10
 (setq pt (trans pt 1 0))

 (defun GetPoints (ss / i ent lst)
   (setq i -1)
   (while (setq ent (ssname ss (setq i (1+ i))))
     (setq lst (cons (vlax-curve-getStartPoint ent)
                     (cons (vlax-curve-getEndPoint ent) lst))))
   lst)

 (car (vl-sort (GetPoints ss) (function (lambda (a b) (< (distance pt a)
                                                         (distance pt b)))))))



(defun c:test (/ ss pt)

 (if (and (setq ss (ssget))
          (setq pt (getpoint "\nPoint to Test: ")))
   (entmake (list (cons 0 "POINT") (cons 10 (GetClosestEndInSS ss pt)))))

 (princ))

Posted
Nice way to apply mine to a selectionset. :thumbsup: However, you forgot trans on the picked point.

 

Thanks Alan, but I'm not sure that the trans was missed... :wink:

Posted
Thanks Alan, but I'm not sure that the trans was missed... :wink:

Huhh, you are correct, odd. I take it back.

Posted
:) No worries :)

Ok, now I know why I was so confused and have it in mine. When I originally wrote the above sub, I first used the point from entsel to extract the vlax-curve-getClosetPointOnCurve, so I had to translate the point to get the correct closestpoint, but I modified it a while back to remove it (realized it was pointless) and I never considered trans being removed.

Posted
Ok, now I know why I was so confused and have it in mine. When I originally wrote the above sub, I first used the point from entsel to extract the vlax-curve-getClosetPointOnCurve, so I had to translate the point to get the correct closestpoint, but I modified it a while back to remove it (realized it was pointless) and I never considered trans being removed.

 

But TRANS is still needed as ENTSEL returns ( ) ? :huh:

Posted
But TRANS is still needed as ENTSEL returns ( ) ? :huh:

Then I'm lost. Doesn't (getpoint) return the point in UCS?

Posted

Yes, the point returns of GETPOINT and ENTSEL are both expressed in UCS, hence TRANS is needed.

Posted
Thanks Alan, but I'm not sure that the trans was missed... :wink:

 

Yes, the point returns of GETPOINT and ENTSEL are both expressed in UCS, hence TRANS is needed.

 

Explain to me why one requires it while the other doesn't? I check mine and it does need trans. Apparently I've overlooking some pivotal detail.

Posted

What are you talking about? They both need TRANS, that's my point.

Posted
What are you talking about? They both need TRANS, that's my point.

Nevermind. I had overlooked where you trans the point from the start. Your response to me saying it needed trans led me to believe your statement of not being missed meant that it wasn't needed. That's why I was so lost, I couldn't figure out why your's would work without trans.

Posted

OK, I said

 

but I'm not sure that the trans was missed..

 

because I had included the trans.

Posted
OK, I said

 

 

 

because I had included the trans.

I understand, but I interpreted it as it wasn't needed, so it wasn't 'missed'.

Posted
Glad we cleared that up :P

No kidding.

Posted

Hi dear friends.

I'm sorry for late checking your comments.

Thanks a lot Alanjt,JohnM,Lee Mac for helping.

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...