Author Topic: Need lisp convert polyline to 3D polyline  (Read 5543 times)

0 Members and 1 Guest are viewing this topic.

sanju2323

  • Newt
  • Posts: 68
Need lisp convert polyline to 3D polyline
« on: January 03, 2015, 12:34:26 AM »
Need lisp to convert polyline to 3d polyline. please see sample copy
« Last Edit: January 03, 2015, 07:23:53 AM by sanju2323 »

David Bethel

  • Swamp Rat
  • Posts: 656
Re: Need lisp convert polyline to 3D polyline
« Reply #1 on: January 03, 2015, 08:54:46 AM »
With some basic error checking :

Code - Auto/Visual Lisp: [Select]
  1. (defun c:lwp23dp (/ ss i en ed z f e pl sl el bl)
  2.  
  3. (defun massoc (key lst / nl)
  4.   (foreach x lst
  5.     (if (eq key (car x))
  6.         (setq nl (cons (cdr x) nl))))
  7.   (reverse nl))
  8.  
  9.   (and (setq ss (ssget '((0 . "LWPOLYLINE"))))
  10.        (setq i 0)
  11.        (while (setq en (ssname ss i))
  12.               (setq ed (entget en)
  13.                      z (cdr (assoc 38 ed))
  14.                      f (cdr (assoc 70 ed))
  15.                      e (cdr (assoc 210 ed))
  16.                     pl (massoc 10 ed)
  17.                     sl (massoc 40 ed)
  18.                     el (massoc 41 ed)
  19.                     bl (massoc 42 ed))
  20.               (cond ((assoc 6 ed)
  21.                      (alert "3DPOLY Cannot Show LTypes")))
  22.               (cond ((or (/= (car sl) 0)
  23.                          (not (apply '= sl)))
  24.                      (alert "LWPoly Must Be Equal Width"))
  25.                     ((or (/= (car el) 0)
  26.                          (not (apply '= el)))
  27.                      (alert "LWPoly Must Be Equal Width"))
  28.                     ((not (apply '= bl))
  29.                      (alert "LWPoly Must Be Not Contain Arc Segments"))
  30.                     ((not (zerop (cdr (assoc 39 ed))))
  31.                      (alert "LWPoly Cannot Have Thickness"))
  32.                     (T
  33.                      (entdel en)
  34.                      (entmake (list (cons 0 "POLYLINE")
  35.                                     (cons 6 "BYLAYER")
  36.                                     (assoc 8 ed)
  37.                                     (cons 10 (list 0 0 0))
  38.                                     (cons 39 0)
  39.                                     (cons 62 (if (assoc 62 ed) (cdr (assoc 62 ed)) 256))
  40.                                     (cons 66 1)
  41.                                     (cons 70 (+ 8 (cdr (assoc 70 ed))))
  42.                                     (assoc 210 ed)))
  43.                      (foreach v pl
  44.                        (entmake (list (cons 0 "VERTEX")
  45.                                       (cons 6 "BYLAYER")
  46.                                       (assoc 8 ed)
  47.                                       (cons 10 (trans (list (car v) (cadr v) z) e 0))
  48.                                       (cons 39 0)
  49.                                       (cons 62 (if (assoc 62 ed) (cdr (assoc 62 ed)) 256))
  50.                                       (cons 70 32))))
  51.                      (entmake (list (cons 0 "SEQEND")
  52.                                     (assoc 8 ed)
  53.                                     (cons 39 0)
  54.                                     (cons 62 (if (assoc 62 ed) (cdr (assoc 62 ed)) 256))))))
  55.               (setq i (1+ i))))
  56.   (prin1))

-David
« Last Edit: January 03, 2015, 09:05:57 AM by David Bethel »
R12 Dos - A2K

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Need lisp convert polyline to 3D polyline
« Reply #2 on: January 03, 2015, 09:13:47 AM »
My guess is that the OP was looking for much more.  :)
Code: [Select]
Pseudo Code
Have the user select the pline & the text objects.
get the start of the pline
sort the text objects using distance along the pline
  Use the vlax-curve-getclosestpointto function to get a point on the pline from the text coordinates
  Use the vlax-curve-getdistatpoint function to gt the distance along the pline
  Then sort
Create the 3D pline using the sorted list & the Z value in the text coordinate
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Tharwat

  • Swamp Rat
  • Posts: 710
  • Hypersensitive
Re: Need lisp convert polyline to 3D polyline
« Reply #3 on: January 03, 2015, 12:52:28 PM »
Here is my approach .

Code - Auto/Visual Lisp: [Select]
  1. (defun c:Test (/ s p ss i e l pt q lst)
  2.   ;;    Tharwat 03.01.2014      ;;
  3.   (if
  4.     (and (setq s (car (entsel "\n Select Lwpolyline :")))
  5.          (eq (cdr (assoc 0 (entget s))) "LWPOLYLINE")
  6.          (setq p (cdr (assoc 10 (entget s))))
  7.          (princ "Select Texts that represents coordinate points of the 3Dpoly")
  8.          (setq
  9.            ss (ssget (list '(0 . "TEXT") (cons 410 (getvar 'CTAB))))
  10.          )
  11.     )
  12.      (progn
  13.        (repeat (setq i (sslength ss))
  14.          (setq l (cons (list (distance p
  15.                                        (setq pt
  16.                                               (cdr
  17.                                                 (assoc 10
  18.                                                        (setq e (entget (ssname ss (setq i (1- i)))))
  19.                                                 )
  20.                                               )
  21.                                        )
  22.                              )
  23.                              pt
  24.                        )
  25.                        l
  26.                  )
  27.          )
  28.        )
  29.        (setq lst (vl-sort l '(lambda (j k) (< (car j) (car k))))
  30.              q   (cadr (car lst))
  31.        )
  32.                               (list (car q) (cadr q))
  33.                     )
  34.                     0.0
  35.                 )
  36.            )
  37.          (setq
  38.            lst (append lst (list (list 0. (vlax-curve-getendpoint s))))
  39.          )
  40.        )
  41.        (vlax-invoke
  42.                      (vla-get-ActiveDocument (vlax-get-acad-object))
  43.                    )
  44.                    'Block
  45.          )
  46.          'add3dpoly
  47.          (apply 'append (mapcar 'cadr lst))
  48.        )
  49.      )
  50.   )
  51.   (princ)
  52.  

David Bethel

  • Swamp Rat
  • Posts: 656
Re: Need lisp convert polyline to 3D polyline
« Reply #4 on: January 03, 2015, 03:12:21 PM »
My guess is that the OP was looking for much more.  :)

Quite possible.

He will have to some major overhaul of the TEXT entities in order to be accurate.
R12 Dos - A2K

Lee Mac

  • Seagull
  • Posts: 12915
  • London, England
Re: Need lisp convert polyline to 3D polyline
« Reply #5 on: January 03, 2015, 07:35:43 PM »
Here is my interpretation:

Code - Auto/Visual Lisp: [Select]
  1. (defun c:2d23d ( / e i l p s x z )
  2.     (cond
  3.         (   (null (setq s (ssget '((0 . "LWPOLYLINE,TEXT"))))))
  4.         (   (progn
  5.                 (repeat (setq i (sslength s))
  6.                     (setq e (ssname s (setq i (1- i)))
  7.                           x (entget e)
  8.                     )
  9.                     (if (= "TEXT" (cdr (assoc 0 x)))
  10.                         (if (setq z (distof (cdr (assoc 1 x))))
  11.                             (setq l (cons (cons (trans (cdr (assoc 10 x)) e 0) z) l))
  12.                         )
  13.                         (setq p e)
  14.                     )
  15.                 )
  16.                 (null p)
  17.             )
  18.             (princ "\nNo polyline selected.")
  19.         )
  20.         (   (null (setq l (mapcar '(lambda ( x ) (cons (vlax-curve-getclosestpointto p (car x)) (cdr x))) l)))
  21.             (princ "\nSelected text is not numerical.")
  22.         )
  23.         (   (setq x (entget p)
  24.                   z (cdr (assoc 38 x))
  25.             )
  26.             (foreach g x
  27.                 (if (and (= 10 (car g)) (setq g (trans (cdr g) p 0)) (not (vl-some '(lambda ( x ) (equal (car x) g 1e-8)) l)))
  28.                     (setq l (cons (cons g z) l))
  29.                 )
  30.             )
  31.             (entmake '((0 . "POLYLINE") (70 . 8)))
  32.             (foreach v (vl-sort l '(lambda ( a b ) (< (vlax-curve-getparamatpoint p (car a)) (vlax-curve-getparamatpoint p (car b)))))
  33.                 (entmake
  34.                     (list
  35.                        '(00 . "VERTEX")
  36.                        '(70 . 32)
  37.                         (list 10 (caar v) (cadar v) (cdr v))
  38.                     )
  39.                 )
  40.             )
  41.             (entmake '((0 . "SEQEND")))
  42.         )
  43.     )
  44.     (princ)
  45. )

Lee Mac

  • Seagull
  • Posts: 12915
  • London, England
Re: Need lisp convert polyline to 3D polyline
« Reply #6 on: January 03, 2015, 07:44:03 PM »
Here is my approach .

Be careful with scenarios such as the attached example  :wink:

sanju2323

  • Newt
  • Posts: 68
Re: Need lisp convert polyline to 3D polyline
« Reply #7 on: January 03, 2015, 10:11:52 PM »
Excellent job. Thanks All Reply

Tharwat

  • Swamp Rat
  • Posts: 710
  • Hypersensitive
Re: Need lisp convert polyline to 3D polyline
« Reply #8 on: January 03, 2015, 11:21:12 PM »
Here is my approach .

Be careful with scenarios such as the attached example  :wink:

Correct , I did not consider the text value since that it DOES represent the Z coordinate with its coordinates ( the text object ) as shown in OP's attached drawing .  :-D
Though that I admit it should account for every eventuality   :-(

Thank you .

Lee Mac

  • Seagull
  • Posts: 12915
  • London, England
Re: Need lisp convert polyline to 3D polyline
« Reply #9 on: January 04, 2015, 07:43:10 AM »
Here is my approach .

Be careful with scenarios such as the attached example  :wink:

Correct , I did not consider the text value since that it DOES represent the Z coordinate with its coordinates ( the text object ) as shown in OP's attached drawing .  :-D
Though that I admit it should account for every eventuality   :-(

Thank you .

Oh, I hadn't actually noticed that the text had elevation of its own - usually such tasks involve using the text content as the elevation value...

My sample drawing was more illustrating the shortfalls of sorting the text objects by their distance from the polyline start point, which will produce undesired results for polylines which loop back on themselves.

Lee