kasra Posted April 10, 2010 Posted April 10, 2010 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. Quote
alanjt Posted April 11, 2010 Posted April 11, 2010 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 Quote
JohnM Posted April 11, 2010 Posted April 11, 2010 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 Quote
Lee Mac Posted April 11, 2010 Posted April 11, 2010 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)) Quote
alanjt Posted April 11, 2010 Posted April 11, 2010 Nice way to apply mine to a selectionset. 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)) Quote
Lee Mac Posted April 11, 2010 Posted April 11, 2010 Nice way to apply mine to a selectionset. However, you forgot trans on the picked point. Thanks Alan, but I'm not sure that the trans was missed... :wink: Quote
alanjt Posted April 11, 2010 Posted April 11, 2010 Thanks Alan, but I'm not sure that the trans was missed... :wink: Huhh, you are correct, odd. I take it back. Quote
Lee Mac Posted April 11, 2010 Posted April 11, 2010 Huhh, you are correct, odd. I take it back. No worries Quote
alanjt Posted April 11, 2010 Posted April 11, 2010 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. Quote
Lee Mac Posted April 11, 2010 Posted April 11, 2010 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 ( ) ? Quote
alanjt Posted April 11, 2010 Posted April 11, 2010 But TRANS is still needed as ENTSEL returns ( ) ? Then I'm lost. Doesn't (getpoint) return the point in UCS? Quote
Lee Mac Posted April 11, 2010 Posted April 11, 2010 Yes, the point returns of GETPOINT and ENTSEL are both expressed in UCS, hence TRANS is needed. Quote
alanjt Posted April 11, 2010 Posted April 11, 2010 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. Quote
Lee Mac Posted April 11, 2010 Posted April 11, 2010 What are you talking about? They both need TRANS, that's my point. Quote
alanjt Posted April 11, 2010 Posted April 11, 2010 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. Quote
Lee Mac Posted April 11, 2010 Posted April 11, 2010 OK, I said but I'm not sure that the trans was missed.. because I had included the trans. Quote
alanjt Posted April 11, 2010 Posted April 11, 2010 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'. Quote
kasra Posted April 15, 2010 Author Posted April 15, 2010 Hi dear friends. I'm sorry for late checking your comments. Thanks a lot Alanjt,JohnM,Lee Mac for helping. Quote
Recommended Posts
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.