Split and sort a pointlist

Split and sort a pointlist

EnM4st3r
Advocate Advocate
1,803 Views
24 Replies
Message 1 of 25

Split and sort a pointlist

EnM4st3r
Advocate
Advocate

Hi, I made a lisp with the help of others here to extend a line to the boundarys of a closed polyline.
I tried to make a little extra change, so it creates a new line for every 2 points.
The reason of that is, so if it finds like 4,6 or more intersection points so a Line gets created between those.
Like that example:

EnM4st3r_0-1702019904411.png


To do that i take the points returned from LM:intersections and try to split them into 2 lists.
However this pointlist is not really sorted so the created lines get messed up sometimes.

Do you have tips for a better split-list function and how to sort that depending on the direction of the starting line?

(defun c:test123 ()
  (create-intersecting-lines (car (entsel "\nLine: ")) (car (entsel "\nPolyline: ")))
  (princ)
)

(defun create-intersecting-lines (line poly / LM:intersections split-list makeline linex splitted-lst line-list)
  
  (defun LM:intersections (ob1 ob2 mod / lst rtn)
    (if (and (vlax-method-applicable-p ob1 'intersectwith)
              (vlax-method-applicable-p ob2 'intersectwith)
              (setq lst (vlax-invoke ob1 'intersectwith ob2 mod))
        )
        (repeat (/ (length lst) 3)
            (setq rtn (cons (list (car lst) (cadr lst) (caddr lst)) rtn)
                  lst (cdddr lst)
            )
        )
    )
    (reverse rtn)
  );defun
  
  (defun split-list (lst / i list1 list2)
    (setq i 1)
    (foreach x lst
      (if (= i 1) 
        (setq list1 (cons x list1) 
              i 2
        ) 
        (setq list2 (cons x list2)
              i 1
        )
      )
    )
    (list list1 list2)
  );defun
  
  (defun makeline (p1 p2 / v1 v2 lineobj)
    (setq v1 (vlax-3d-point p1))
    (setq v2 (vlax-3d-point p2))
    (setq lineobj (vla-addline (vla-get-modelspace (vla-get-activedocument (vlax-get-acad-object))) v1 v2))
    (vlax-vla-object->ename lineobj)
  );defun
  
  (setq linex (entget line)
        lineobj (vlax-ename->vla-object line)
        polyobj (vlax-ename->vla-object poly)
  )
  (setq splitted-lst (split-list (LM:intersections lineobj polyobj acExtendThisEntity)))
  (setq line-list (mapcar 'makeline (car splitted-lst)(cadr splitted-lst))) ;; makeline needs osmode 0?
  (entdel line)
  line-list
)
0 Likes
Accepted solutions (2)
1,804 Views
24 Replies
Replies (24)
Message 21 of 25

ВeekeeCZ
Consultant
Consultant

@komondormrex wrote:

hey there,

check the shorter one


checked. it seems too short to me. I would add at least two d's...

0 Likes
Message 22 of 25

EnM4st3r
Advocate
Advocate
thanks a lot. Also the copy instead of vla-addline is indeed what i would want
0 Likes
Message 23 of 25

ВeekeeCZ
Consultant
Consultant

@EnM4st3r wrote:


d


Why did you drop the distance sorting example? 


 tbh i didnt know where to implement it in my code and also did not completely understand the logic..

 

Gave it another thought, i think the idea was to use the sorting on the Pointlist I get from LM:intersections right?
so i sort the list before splitting it?

 

But i think i dont need (car e1) and (car e2) for that as i can use it without the car. so in my original code it would work like that.

  (setq linex (entget line)
        p10 (cdr (assoc 10 linex))
        lineobj (vlax-ename->vla-object line)
        polyobj (vlax-ename->vla-object poly)
  )
  (setq pointlist (LM:intersections lineobj polyobj acExtendThisEntity))
  (setq sorted-pointlist (vl-sort pointlist '(lambda (e1 e2) (< (distance p10 e1) (distance p10 e2)))))
  (setq splitted-lst (split-list sorted-pointlist))
 
  (setq line-list (mapcar 'makeline (car splitted-lst)(cadr splitted-lst)))
  (entdel line)
  line-list
);defun

 


 

Right. But what I didn't realize is that p10 is not always a good one. It needs to be a real extreme, like the most lower-left point - therefore my pmin expression.

 

Or another way is to sort the points by x coord, then by y...

Message 24 of 25

komondormrex
Mentor
Mentor

@ВeekeeCZ wrote:

checked. it seems too short to me. I would add at least two d's...


sorry to have not done it 😁

and you are not the one to measure)

0 Likes
Message 25 of 25

ВeekeeCZ
Consultant
Consultant

@komondormrex wrote:

@ВeekeeCZ wrote:

checked. it seems too short to me. I would add at least two d's...


sorry to have not done it 😁

and you are not the one to measure)


 

Ok, sure. I see. You have used a different method than I thought. Sorting by x, then by y would be probably even shorter.

0 Likes