Add Vertex to 2D / 3D Plines (depending on a specific distance) ?

Add Vertex to 2D / 3D Plines (depending on a specific distance) ?

braudpat
Mentor Mentor
3,963 Views
30 Replies
Message 1 of 31

Add Vertex to 2D / 3D Plines (depending on a specific distance) ?

braudpat
Mentor
Mentor

Hello

 

SORRY I don't find my dream with Google but my keywords were maybe not the right ones !?

 

I am looking for a Lisp routine concerning 2D or 3D PLines (Closed or NOT)

Off course 2D/3D PLines must not be splined !

I have to complexify PLines ! Depending on a distance (DIST)

 

1 - Question : Distance (Default value = 10.00) : ?

2 - Classic ACAD selection ...

So retaining only 2D or 3D PLines (Closed or NOT) on unlocked layers ...

3 - Analyse each segment ... If a segment is longer than DIST create a new Vertex

and if necessary on the same segment create AGAIN a new vertex ... And so one ...

So if a segment is 35.00 long, I will get 3 new vertex on THIS segment

If a segment is < 10.00 : NO new vertex

Process next segment then process next Pline

 

It's only a question of "Add a Vertex" on existing 2D/3D PLines !

 

Please don't explode because I have XDATAs and ODs (from MAP / CIVIL) on these Plines !

 

I hope, I am clear !

 

---- I know that 2D Plines can have ARCs! Maybe it's too complex to add some new vertex on Arc !

---- So please ignore the arcs on 2D Plines and go directly to the next segment !!

 

Thanks in advance, THE HEALTH (Stay Safe), Regards, Patrice

 

 

Patrice ( Supporting Troops ) - Autodesk Expert Elite
If you are happy with my answer please mark "Accept as Solution" and if very happy please give me a Kudos (Felicitations) - Thanks

Patrice BRAUD

EESignature


0 Likes
Accepted solutions (3)
3,964 Views
30 Replies
Replies (30)
Message 21 of 31

braudpat
Mentor
Mentor

Hello @CADaSchtroumpf 

 

YES ... Beautiful routine for 2D PLines with ARCs !

 

THANKS, Regards, Patrice

 

Patrice ( Supporting Troops ) - Autodesk Expert Elite
If you are happy with my answer please mark "Accept as Solution" and if very happy please give me a Kudos (Felicitations) - Thanks

Patrice BRAUD

EESignature


0 Likes
Message 22 of 31

Kent1Cooper
Consultant
Consultant
Accepted solution

For line-segment-only ones, we can do something a lot shorter if we take advantage of the fact that the PEDIT command will deal with a lot of the "stuff" involved -- we can just add vertices along the way, with no need to pull out a list of vertices and add more into the list and impose the altered list back onto the Polyline, etc.  Lightly tested:

 

(vl-load-com)
(defun C:PLMS10  (/ ptpar plent hvy ver seglen ratio delta); = PolyLine Maximum Segment 10
  (defun ptpar (par) (vlax-curve-getPointAtParam plent par))
  (if
    (and
      (setq plent (car (entsel "\nSelect Polyline for maximum-10 segment lengths: ")))
      (wcmatch (cdr (assoc 0 (entget plent))) "*POLYLINE")
    ); and
    (progn ; then
      (setq
        hvy (= (cdr (assoc 0 (entget plent))) "POLYLINE"); "heavy" 2D, or 3D
        ver 0
      ); setq
      (command "_.pedit" plent "_edit")
      (while ; haven't reached end
        (< (vlax-curve-getDistAtParam plent ver) (vlax-curve-getDistAtParam plent (vlax-curve-getEndParam plent)))
        (if (> (setq seglen (distance (ptpar ver) (ptpar (1+ ver)))) 10); next segment longer than 10
          (progn ; then
            (setq
              ratio (/ 10 seglen)
              delta (mapcar '- (ptpar (1+ ver)) (ptpar ver))
            ); setq
            (command "_Insert" "_none" (mapcar '+ (ptpar ver) (mapcar '* delta (list ratio ratio ratio))))
            (if hvy (command "_Next")); move to next vertex [does it for you in LWPolyline]
          ); progn
          (command "_Next"); else
        ); if
        (setq ver (1+ ver))
      ); while
      (command "_eXit" ""); finish out of PEDIT
    ); progn
  ); if
); defun

 

[If 3D Polylines were not in the mix, it could be simpler still, using a mere (polar) function without the ratio and delta variables and the calculations that use them.]

Kent Cooper, AIA
Message 23 of 31

braudpat
Mentor
Mentor

Hello @Kent1Cooper 

 

YES Excellent ! Nice idea to use PEDIT !!

 

Please is it possible to have a small enhancement : select many 2D/3D PLines !?

 

Thanks, THE HEALTH (Stay Safe), Regards, Patrice

 

Patrice ( Supporting Troops ) - Autodesk Expert Elite
If you are happy with my answer please mark "Accept as Solution" and if very happy please give me a Kudos (Felicitations) - Thanks

Patrice BRAUD

EESignature


0 Likes
Message 24 of 31

Kent1Cooper
Consultant
Consultant

@braudpat wrote:

....

Please is it possible to have a small enhancement : select many 2D/3D PLines !?

....


Try this [really minimally  tested]:

(vl-load-com)
(defun C:PLMS10  (/ ptpar plss n plent hvy ver seglen); = PolyLine Maximum Segment 10
  (defun ptpar (par) (vlax-curve-getPointAtParam plent par))
  (prompt "\nTo apply maximum segment length of 10 units to Polylines,")
  (if (setq plss (ssget "_:L" '((0 . "*POLYLINE"))))
    (repeat (setq n (sslength plss))
      (setq
        plent (ssname plss (setq n (1- n)))
        hvy (= (cdr (assoc 0 (entget plent))) "POLYLINE"); "heavy" 2D, or 3D
        ver 0
      ); setq
      (command "_.pedit" plent "_edit")
      (while
        (< (vlax-curve-getDistAtParam plent ver) (vlax-curve-getDistAtParam plent (vlax-curve-getEndParam plent)))
        (if (> (setq seglen (distance (ptpar ver) (ptpar (1+ ver)))) 10); next segment longer than 10
          (progn ; then
            (setq
              ratio (/ 10 seglen)
              delta (mapcar '- (ptpar (1+ ver)) (ptpar ver))
            ); setq
            (command "_Insert" "_none" (mapcar '+ (ptpar ver) (mapcar '* delta (list ratio ratio ratio))))
            (if hvy (command "_Next")); move to next vertex [does it for you in LWPolyline]
          ); progn
          (command "_Next"); else
        ); if
        (setq ver (1+ ver))
      ); while
      (command "_eXit" ""); finish out of PEDIT
    ); repeat
  ); if
); defun

 

Kent Cooper, AIA
Message 25 of 31

braudpat
Mentor
Mentor

Hello @Kent1Cooper 

 

YES BRAVO ! Felicitations !!

 

Thanks, THE HEALTH (Stay Safe), Regards, Patrice

Patrice ( Supporting Troops ) - Autodesk Expert Elite
If you are happy with my answer please mark "Accept as Solution" and if very happy please give me a Kudos (Felicitations) - Thanks

Patrice BRAUD

EESignature


0 Likes
Message 26 of 31

Hamza.itani
Contributor
Contributor

Sorry for posting in an old thread, but I just need a minor adjustment to this lisp if possible:

Can it be modified so it adds vertex to segments (larger than user specified max length) at the midpoint of segments, or to distribute at equal distances if more than one vertex is required to achieve the specified max length, as shown below: (whithout exploding or changing the orientation/direction or properties of the polyline)

maxsegmentlength.jpg

0 Likes
Message 27 of 31

CADaSchtroumpf
Advisor
Advisor

See my answer HERE

Message 28 of 31

Hamza.itani
Contributor
Contributor

Thanks a lot.

0 Likes
Message 29 of 31

hamza_itani
Enthusiast
Enthusiast

When running the lisp you provided on a poyline with segment length exactly = 10 and specifying a max length between vertex of 1, it's adding vertex every 0.9091 instead of adding every 1 (attached screenshot + DWG). Is there an easy fix? Thanks!
Screenshot 2025-03-19 081111.png

0 Likes
Message 30 of 31

hamza_itani
Enthusiast
Enthusiast

This is the Lisp I'm using, if anyone can help:

(vl-load-com)
(defun add_vtx (obj add_pt ent_name / bulg)
  (vla-addVertex
    obj
    (1+ (fix add_pt))
    (vlax-make-variant
      (vlax-safearray-fill
        (vlax-make-safearray vlax-vbdouble (cons 0 1))
          (list
            (car (trans (vlax-curve-getpointatparam obj add_pt) 0 ent_name))
            (cadr (trans (vlax-curve-getpointatparam obj add_pt) 0 ent_name))
          )
      )
    )
  )
  (setq bulg (vla-GetBulge obj (fix add_pt)))
  (vla-SetBulge obj
    (fix add_pt)
    (/
      (sin (/ (* 4 (atan bulg) (- add_pt (fix add_pt))) 4))
      (cos (/ (* 4 (atan bulg) (- add_pt (fix add_pt))) 4))
    )
  )
  (vla-SetBulge obj
    (1+ (fix add_pt))
    (/
      (sin (/ (* 4 (atan bulg) (- (1+ (fix add_pt)) add_pt)) 4))
      (cos (/ (* 4 (atan bulg) (- (1+ (fix add_pt)) add_pt)) 4))
    )
  )
  (vla-update obj)
)
(defun c:div-vertex_po ( / ss max_l n ent obj_vla pr dist_end dist_start seg_len div l_div l)
  (princ "\nSelect polylines.")
  (while (null (setq ss (ssget '((0 . "LWPOLYLINE")))))
    (princ "\nSelect is empty, or isn't POLYLINE!")
  )
  (initget 7)
  (setq max_l (getdist "\nMax length between vertex: "))
  (repeat (setq n (sslength ss))
    (setq
      ent (ssname ss (setq n (1- n)))
      obj_vla (vlax-ename->vla-object ent)
      pr (fix (vlax-curve-getEndParam ent))
    )
    (repeat (fix (vlax-curve-getEndParam ent))
      (setq
        dist_end (vlax-curve-GetDistAtParam ent pr)
        dist_start (vlax-curve-GetDistAtParam ent (setq pr (1- pr)))
        seg_len (- dist_end dist_start)
        div (1+ (fix (/ seg_len max_l)))
        l_div (/ seg_len div)
        l l_div
      )
      (while (< l seg_len)
        (add_vtx obj_vla (vlax-curve-getparamatdist ent (- dist_end l)) ent)
        (setq l (+ l l_div))
      )
    )
  )
  (prin1)
)

 

0 Likes
Message 31 of 31

hamza_itani
Enthusiast
Enthusiast

Never mind, I got it fixed at CADTUTOR .

Thanks!

0 Likes