Author Topic: 3d polyline splitting  (Read 639 times)

0 Members and 1 Guest are viewing this topic.

mariolino0099

  • Newt
  • Posts: 27
3d polyline splitting
« on: April 18, 2024, 09:13:29 AM »
Hi, I have a problem with this code, it divides only the first unit of each segment and not all segments with distance equal or less than the assigned value.
The code should subdivide each segment of the 3dpoly with a value such that each subdivision is less than or at most equal to the assigned value. So for each segment of the 3dpoly there is a different value with which to subdivide that specific segment.
Can anyone help me ?


Code: [Select]
(defun c:poly_add_vertex (/ *error*       c_doc  sv_lst sv_vals
  mdst ss cnt    vlst   ent    elst
  obj flg sp     ep     lcnt   dst
  tdst p
)

  (defun *error* (msg)
    (mapcar 'setvar sv_lst sv_vals)
    (if (not (wcmatch (strcase msg) "*BREAK*,*CANCEL*,*EXIT*"))
      (princ (strcat "\nAn Error : " msg " occurred."))
    )
    (princ)
  ) ;_end_*error*_defun

  (setq c_doc (vla-get-activedocument (vlax-get-acad-object))
sv_lst (list 'cmdecho 'osmode)
sv_vals (mapcar 'getvar sv_lst)
  ) ;end_setq

  (mapcar 'setvar sv_lst '(0 0))

  (initget 7)
  (setq
    mdst (getreal "\nEnter Max Distance between Polyline Vertices : "
)
  )

  (setq ss (ssget ":L" '((0 . "*POLYLINE"))))
  (cond
    (ss
     (repeat (setq cnt (sslength ss))
       (setq elst (entget (setq ent (ssname ss (setq cnt (1- cnt))))))
       (cond ((or (= 2 (logand 2 (cdr (assoc 70 elst))))
  (= 4 (logand 4 (cdr (assoc 70 elst))))
      )
     )
     (t
      (setq vlst nil
    obj (vlax-ename->vla-object ent)
    flg (if (= (vlax-get obj 'objectname) "AcDbPolyline")
   T
   nil
)
    sp 0
    ep (vlax-curve-getendparam ent)
    vlst (cons (if flg
(reverse
   (cdr (reverse (vlax-curve-getstartpoint ent)))
)
(vlax-curve-getstartpoint ent)
       )
       vlst
)
    lcnt 0.0
      ) ;end_setq
      (while (< sp ep)
(setq dst  (distance (vlax-curve-getpointatparam ent sp)
     (vlax-curve-getpointatparam ent (1+ sp))
   )
      tdst dst
)
(cond ((> dst mdst)
       (while (> tdst mdst)
(setq vlst (cons
      (if flg
(reverse
  (cdr (reverse
(vlax-curve-getpointatdist
   ent
   (setq lcnt (+ lcnt (/ mdst dst)))
)
       )
  )
)
(vlax-curve-getpointatdist
  ent
  (setq lcnt (+ lcnt (/ mdst dst)))
)
      )
      vlst
    )
       tdst (- tdst mdst)
) ;end_setq
       ) ;end_while
      )
) ;end_cond
(setq sp   (1+ sp)
      vlst (cons
     (if flg
       (reverse
(cdr
   (reverse (vlax-curve-getpointatparam ent sp))
)
       )
       (vlax-curve-getpointatparam ent sp)
     )
     vlst
   )
      lcnt (vlax-curve-getdistatparam ent sp)
) ;end_setq
      ) ;end_while
      (cond (flg
     (setq sp 0)
     (while (< sp ep)
       (vlax-invoke obj 'setbulge sp 0.0)
       (setq sp (1+ sp))
     )
    )
      )
      (vlax-put obj 'coordinates (apply 'append (reverse vlst)))
     )
       ) ;end_cond
     ) ;end_repeat
    )
    (t (alert "Nothing Selected"))
  ) ;end_cond
  (mapcar 'setvar sv_lst sv_vals)
  (princ)
) ;end_defun

;"AcDb2dPolyline" if curved

ribarm

  • Gator
  • Posts: 3296
  • Marko Ribar, architect
Re: 3d polyline splitting
« Reply #1 on: April 19, 2024, 08:16:59 AM »
Code - Auto/Visual Lisp: [Select]
  1. (defun c:splitpolylines ( / gap ss i poly segx d r )
  2.   (initget 6)
  3.   (setq gap (cond ( (getdist "\nPick or specify gap <0.0> : ") ) (0.0)))
  4.   (prompt "\nSelect polylines on unlocked layer(s)...")
  5.   (if (setq ss (ssget "_:L" (list (cons 0 "*POLYLINE"))))
  6.     (repeat (setq i (sslength ss))
  7.       (setq poly (ssname ss (setq i (1- i))))
  8.       (vl-catch-all-apply (if command-s (function command-s) (function vl-cmdf)) (list "_.explode" poly))
  9.       (foreach seg (vl-remove-if (function listp) (mapcar (function cadr) (ssnamex (ssget "_p"))))
  10.         (cond
  11.           ( (= (cdr (assoc 0 (setq segx (entget seg)))) "LINE")
  12.             (setq segx (subst (cons 10 (mapcar (function +) (cdr (assoc 10 segx)) (mapcar (function *) (mapcar (function /) (mapcar (function -) (cdr (assoc 11 segx)) (cdr (assoc 10 segx))) (list (setq d (distance (cdr (assoc 10 segx)) (cdr (assoc 11 segx)))) d d)) (list gap gap gap)))) (assoc 10 segx) segx))
  13.             (setq segx (subst (cons 11 (mapcar (function +) (cdr (assoc 11 segx)) (mapcar (function *) (mapcar (function /) (mapcar (function -) (cdr (assoc 10 segx)) (cdr (assoc 11 segx))) (list d d d)) (list gap gap gap)))) (assoc 11 segx) segx))
  14.             (entupd (cdr (assoc -1 (entmod segx))))
  15.           )
  16.           ( t
  17.             (setq r (cdr (assoc 40 segx)))
  18.             (setq segx (subst (cons 50 (+ (cdr (assoc 50 segx)) (/ gap r))) (assoc 50 segx) segx))
  19.             (setq segx (subst (cons 51 (- (cdr (assoc 51 segx)) (/ gap r))) (assoc 51 segx) segx))
  20.             (entupd (cdr (assoc -1 (entmod segx))))
  21.           )
  22.         )
  23.       )
  24.     )
  25.   )
  26.   (princ)
  27. )
  28.  

HTH.
M.R.
« Last Edit: April 20, 2024, 08:55:10 AM by ribarm »
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

mariolino0099

  • Newt
  • Posts: 27
Re: 3d polyline splitting
« Reply #2 on: April 22, 2024, 02:21:15 AM »
Marko many thanks,
the code should do the splitting as shown in the image. Basically introduced the user length (0.7 in the example), do the splitting of each individual segment of the 3dpoly and then round to the nearest integer and use a different splitting length for each segment
thanks

DEVITG

  • Bull Frog
  • Posts: 481
Re: 3d polyline splitting
« Reply #3 on: April 22, 2024, 03:29:13 PM »
@mariolino099

Please Upload your sample.dwg
Location @ Córdoba Argentina Using ACAD 2019  at Window 10

mariolino0099

  • Newt
  • Posts: 27
Re: 3d polyline splitting
« Reply #4 on: April 23, 2024, 02:27:03 AM »
thanks..

DEVITG

  • Bull Frog
  • Posts: 481
Re: 3d polyline splitting
« Reply #5 on: April 27, 2024, 08:01:50 AM »
@mariolino0099 , i will check .
Location @ Córdoba Argentina Using ACAD 2019  at Window 10

mariolino0099

  • Newt
  • Posts: 27
Re: 3d polyline splitting
« Reply #6 on: April 30, 2024, 11:32:23 AM »
Hi,
this is the result with my code, it correctly performs the subdivision calculation, but it only subdivides the first unit of length for each segment condensing all subdivisions there without making a uniform subdivision.
Thnaks

mariolino0099

  • Newt
  • Posts: 27
Re: 3d polyline splitting
« Reply #7 on: May 07, 2024, 05:56:33 AM »
Please can anyone help me ?
Thanks

ribarm

  • Gator
  • Posts: 3296
  • Marko Ribar, architect
Re: 3d polyline splitting
« Reply #8 on: May 08, 2024, 09:16:36 AM »
Try this code...

Code - Auto/Visual Lisp: [Select]
  1. (defun c:split3dpolysegs ( / *error* getvertices unit d cmd uf s plx pl vl a b dd n sd nvl k )
  2.  
  3.  
  4.   (defun *error* ( m )
  5.     (if uf
  6.       (if command-s
  7.         (command-s "_.UCS" "_P")
  8.         (vl-cmdf "_.UCS" "_P")
  9.       )
  10.     )
  11.     (if cmd
  12.       (setvar (quote cmdecho) cmd)
  13.     )
  14.     (if m
  15.       (prompt m)
  16.     )
  17.     (princ)
  18.   )
  19.  
  20.   (defun getvertices ( c / i p plst )
  21.     (setq i -1)
  22.     (while (<= (setq i (1+ i)) (vlax-curve-getendparam c))
  23.       (setq p (vlax-curve-getpointatparam c i))
  24.       (setq plst (cons p plst))
  25.     )
  26.     (reverse plst)
  27.   )
  28.  
  29.   (defun unit ( v / d )
  30.     (if (not (equal (setq d (distance (list 0.0 0.0 0.0) v)) 0.0 1e-6))
  31.       (mapcar (function (lambda ( x ) (/ x d))) v)
  32.     )
  33.   )
  34.  
  35.   (setq d 0.7) ;;; initial setting - change to suit your needs ;;;
  36.  
  37.   (setq cmd (getvar (quote cmdecho)))
  38.   (setvar (quote cmdecho) 0)
  39.   (if (= 0 (getvar (quote worlducs)))
  40.     (progn
  41.       (if command-s
  42.         (command-s "_.UCS" "_W")
  43.         (vl-cmdf "_.UCS" "_W")
  44.       )
  45.       (setq uf t)
  46.     )
  47.   )
  48.   (prompt "\nPick reference 3D polyline...")
  49.   (if (setq s (ssget "_.+:E:S" (list (cons 0 "POLYLINE") (cons -4 "<or") (cons 70 8) (cons 70 9) (cons -4 "or>"))))
  50.     (progn
  51.       (setq plx (entget (setq pl (ssname s 0))))
  52.       (setq vl (getvertices pl))
  53.       (while (setq b (cadr vl))
  54.         (if (< d (setq dd (distance (setq a (car vl)) b)))
  55.           (progn
  56.             (setq n (fix (/ dd d)))
  57.             (setq sd (/ dd n))
  58.             (setq nvl (cons a nvl))
  59.             (setq k 0)
  60.             (repeat n
  61.               (setq nvl (cons (mapcar (function +) a (mapcar (function *) (unit (mapcar (function -) b a)) (list (* (setq k (1+ k)) sd) (* k sd) (* k sd)))) nvl))
  62.             )
  63.             (setq vl (cdr vl))
  64.           )
  65.         )
  66.       )
  67.       (setq a (car nvl))
  68.       (setq nvl (cdr nvl))
  69.       (if (not (vlax-curve-isclosed pl))
  70.         (setq nvl (cons a nvl))
  71.       )
  72.       (setq nvl (reverse nvl))
  73.       (vl-cmdf "_.3DPOLY")
  74.       (foreach v nvl
  75.         (vl-cmdf "_non" v)
  76.       )
  77.       (if (vlax-curve-isclosed pl)
  78.         (while (< 0 (getvar (quote cmdactive)))
  79.           (vl-cmdf "_C")
  80.         )
  81.         (while (< 0 (getvar (quote cmdactive)))
  82.           (vl-cmdf "")
  83.         )
  84.       )
  85.     )
  86.   )
  87.   (*error* nil)
  88. )
  89.  

Regards, M.R.
« Last Edit: May 08, 2024, 11:28:46 AM by ribarm »
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube

ribarm

  • Gator
  • Posts: 3296
  • Marko Ribar, architect
Re: 3d polyline splitting
« Reply #9 on: May 08, 2024, 09:53:53 AM »
And this code is opposite of splitting by adding vertices... It's called "diet3dp" like my latest with lwpolyline that I coded recently...

Code - Auto/Visual Lisp: [Select]
  1. (defun c:diet3dp ( / *error* unique getvertices collinear-p group_collinear_pts cmd uf s pl plx cf vl nvl gg )
  2.  
  3.  
  4.   (defun *error* ( m )
  5.     (if uf
  6.       (if command-s
  7.         (command-s "_.UCS" "_P")
  8.         (vl-cmdf "_.UCS" "_P")
  9.       )
  10.     )
  11.     (if cmd
  12.       (setvar (quote cmdecho) cmd)
  13.     )
  14.     (if m
  15.       (prompt m)
  16.     )
  17.     (princ)
  18.   )
  19.  
  20.   (defun unique ( lst )
  21.     (if lst
  22.       (cons (car lst)
  23.         (unique
  24.           (vl-remove-if (function (lambda ( x ) (equal x (car lst) 1e-6)))
  25.             (cdr lst)
  26.           )
  27.         )
  28.       )
  29.     )
  30.   )
  31.  
  32.   (defun getvertices ( c / i p ptlst )
  33.     (setq i -1)
  34.     (while (<= (setq i (1+ i)) (vlax-curve-getendparam c))
  35.       (setq p (vlax-curve-getpointatparam c i))
  36.       (setq ptlst (cons p ptlst))
  37.     )
  38.     (reverse ptlst)
  39.   )
  40.  
  41.   (defun collinear-p ( p1 p p2 )
  42.     (and
  43.       (equal (distance p1 p2) (+ (distance p1 p) (distance p p2)) 1e-6)
  44.       (equal (angle p1 p2) (angle p1 p) 1e-6)
  45.       (equal (angle p1 p2) (angle p p2) 1e-6)
  46.     )
  47.   )
  48.  
  49.   (defun group_collinear_pts ( ptlst / a b c g gg )
  50.     (while ptlst
  51.       (setq a (car ptlst) b (cadr ptlst) c (caddr ptlst))
  52.       (while (and c (collinear-p a b c))
  53.         (if (not (vl-position a g))
  54.           (setq g (cons a g))
  55.         )
  56.         (if (not (vl-position b g))
  57.           (setq g (cons b g))
  58.         )
  59.         (if (not (vl-position c g))
  60.           (setq g (cons c g))
  61.         )
  62.         (setq ptlst (cdr ptlst))
  63.         (setq a (car ptlst) b (cadr ptlst) c (caddr ptlst))
  64.       )
  65.       (setq ptlst (cdr ptlst))
  66.       (if g
  67.         (setq gg (cons (reverse g) gg))
  68.       )
  69.       (setq g nil)
  70.     )
  71.     (reverse gg)
  72.   )
  73.  
  74.   (setq cmd (getvar (quote cmdecho)))
  75.   (setvar (quote cmdecho) 0)
  76.   (if (= 0 (getvar (quote worlducs)))
  77.     (progn
  78.       (if command-s
  79.         (command-s "_.UCS" "_W")
  80.         (vl-cmdf "_.UCS" "_W")
  81.       )
  82.       (setq uf t)
  83.     )
  84.   )
  85.   (prompt "\nPick reference 3D polyline...")
  86.   (if (setq s (ssget "_.+:E:S" (list (cons 0 "POLYLINE") (cons -4 "<or") (cons 70 8) (cons 70 9) (cons -4 "or>"))))
  87.     (progn
  88.       (setq plx (entget (setq pl (ssname s 0))))
  89.       (if (= 9 (cdr (assoc 70 plx)))
  90.         (setq cf t)
  91.       )
  92.       (setq vl (getvertices pl))
  93.       (setq nvl vl)
  94.       (setq gg (group_collinear_pts nvl))
  95.       (foreach g gg
  96.         (setq g (cdr g) g (reverse (cdr (reverse g))))
  97.         (foreach p g
  98.           (setq nvl (vl-remove-if (function (lambda ( x ) (equal p x 1e-6))) nvl))
  99.         )
  100.       )
  101.       (if cf
  102.         (setq nvl (reverse (cdr (reverse nvl))))
  103.       )
  104.       (setq nvl (unique nvl))
  105.       (vl-cmdf "_.3DPOLY")
  106.       (foreach v nvl
  107.         (vl-cmdf "_non" v)
  108.       )
  109.       (if cf
  110.         (while (< 0 (getvar (quote cmdactive)))
  111.           (vl-cmdf "_C")
  112.         )
  113.         (while (< 0 (getvar (quote cmdactive)))
  114.           (vl-cmdf "")
  115.         )
  116.       )
  117.     )
  118.   )
  119.   (*error* nil)
  120. )
  121.  

HTH.
M.R.
« Last Edit: May 08, 2024, 01:04:42 PM by ribarm »
Marko Ribar, d.i.a. (graduated engineer of architecture)

:)

M.R. on Youtube