Jump to content

Sort selected entities by shortest distance between start coordinates of each entity


Recommended Posts

Posted

Hello.

Can someone suggest a way to sort a list of selected entities by the shortest distance between end coordinate of one entity and start coordinate of the next?

 

Here is an example:

attachment.php?attachmentid=56763&cid=1&stc=1

yellow points represent the start of entity, and the numbers is the order they should end up.

#1 line is the closest to 0,0; it's end coordinate is closest to arch #2, then polyline #3 and so on.

 

Thank you.

 

P.S.

What I'm trying to achieve here is optimize how my plotter draws. It's driver fails to optimize the way it draws, which seems to obey the sequence in which the entities were created in the drawing. Right now I have to manually copy each entity one by one in proper sequence the way I want it to plot...

acad_entity_sort.PNG

Posted

The problem is essentially the Travelling Salesman Problem (i.e. visiting all nodes following the shortest path). Below is a solution implementing a nearest-neighbour greedy algorithm - note that this approach will not yield the optimal result:

(defun sortentlist ( lst / foo rtn )
   (setq foo
       (lambda ( x l / a d r )
           (setq d (distance (caddr x) (cadar l))
                 r (car l)
           )
           (foreach y (cdr l)
               (if (< (setq a (distance (cadr y) (caddr x))) d)
                   (setq d a r y)
               )
           )
           r
       )
   )
   (setq lst (mapcar '(lambda ( x ) (list x (vlax-curve-getstartpoint x) (vlax-curve-getendpoint x))) lst)
         rtn (list (car lst))
         lst (cdr lst)
   )
   (while lst
       (setq rtn (cons (foo (car rtn) lst) rtn)
             lst (vl-remove (car rtn) lst)
       )
   )
   (reverse (mapcar 'car rtn))
)

 

The algorithm could also be implemented using sorting, but this will be less efficient:

(defun sortentlist ( lst / rtn )
   (setq lst (mapcar '(lambda ( x ) (list x (vlax-curve-getstartpoint x) (vlax-curve-getendpoint x))) lst)
         rtn (list (car lst))
         lst (cdr lst)
   )
   (while lst
       (setq lst (vl-sort lst '(lambda ( a b ) (< (distance (caddar rtn) (cadr a)) (distance (caddar rtn) (cadr b)))))
             rtn (cons (car lst) rtn)
             lst (vl-remove (car rtn) lst)
       )
   )
   (reverse (mapcar 'car rtn))
)

Posted

Lee was this not another post problem how to make the laser cutter do the next closest item saving head movement time.

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