Author Topic: 3dpolyline export  (Read 2755 times)

0 Members and 1 Guest are viewing this topic.

Robert98

  • Guest
3dpolyline export
« on: November 27, 2010, 03:02:46 PM »
Hi all dear members
I practiced on nested entities with 3dpolyline sample , I want export total distances and Z values of 3dpolyline to a  text file in the tab delimited format , such as :

total        z
0            123.12
20           124.52
35           122.74
.            .
.            .
.            .
1265.23      174.65

but my codes return this error :

; error: bad argument type: 2D/3D point: nil

I don't understand why 2D/3D point ? my sample is in 3d format !
please tell me why distance function returns this message ? :?

Code: [Select]
;;;;;Get vertex of a polyline by Alireza Edalatipour / www.autolisp.blogfa.com
(defun poly-lst (polyline / ename enameg polylist)
  (setq ename (entnext polyline))
  (if (/= ename nil)
    (progn
      (setq enameg (entget ename))
      (while (/= (cdr (assoc 0 enameg)) "SEQEND")
(if (/= (cdr (assoc 70 enameg)) 16)
  (setq polylist (cons (cdr (assoc 10 enameg)) polylist))
  )
(setq ename (entnext ename))
(setq enameg (entget ename))
)
      )
    )
   (reverse polylist)
  )  ;defun

;;***********************************

(defun c:xpor  (/ en ptlist dis total i file temp total z)

  (setq en (car (entsel "\n Select a 3D PolyLine : ")))
  (setq ptlist (poly-lst en)
total  0
i      0)
  (setq file (findfile "ptlist.txt"))
  (if (not file)
    (setq temp (open "c:\ptlist.txt" "w"))
    (setq temp (open file "w"))
    ) ;end if
  (while (< i (length ptlist))
    (setq dis (distance (nth i ptlist) (nth (1+ i) ptlist))
  total (+ total dis)
  ) ;setq dis
    (write-line (rtos total 2 2) "     " (rtos  z 2 2) "     " temp) ;write-line
    (setq i (1+ i))
    ) ;end while
  (close temp)
  (princ)
  )  ;defun

Lee Mac

  • Seagull
  • Posts: 12912
  • London, England
Re: 3dpolyline export
« Reply #1 on: November 27, 2010, 03:10:02 PM »
Your error will occur within the last evaluation of the WHILE expression, as:

Code: [Select]
(nth (1+ i) ptlist)
Will be nil.

Also, your write-line statement needs a 'strcat' :-)

Also, the variable 'z' is not defined.
« Last Edit: November 27, 2010, 03:17:16 PM by Lee Mac »

Robert98

  • Guest
Re: 3dpolyline export
« Reply #2 on: November 27, 2010, 03:21:13 PM »
Your error will occur within the last evaluation of the WHILE expression, as:

Code: [Select]
(nth (1+ i) ptlist)
Will be nil.

Also, your write-line statement needs a 'strcat' :-)

Also, the variable 'z' is not defined.
Hi dear Lee
Thanks for your quick answer and reply , I 'll challenge with it and come back again .

Lee Mac

  • Seagull
  • Posts: 12912
  • London, England
Re: 3dpolyline export
« Reply #3 on: November 27, 2010, 03:23:17 PM »
This is how I might approach it:

Code: [Select]
;; <-- Scroll down after you have a working solution yourself --> ;;
























(defun c:xpor ( / e f )

  (if (and (setq e (car (entsel)))
           (wcmatch (cdr (assoc 0 (entget e))) "*POLYLINE")
           (setq f (getfiled "Output File" "" "txt" 1))
           (setq f (open f "w"))
      )
    (
      (lambda ( par end ) (write-line "Total     Z-Value" f)
        (while (<= (setq par (1+ par)) end)
          (write-line
            (strcat
              (rtos (vlax-curve-getDistatParam e par) 2 2) "     "
              (rtos (caddr (vlax-curve-getPointatParam e par)) 2 2)
            )
            f
          )
        )
        (close f)
      )
      (1- (vlax-curve-getStartParam e))
      (vlax-curve-getEndParam e)
    )
  )

  (princ)
)

Robert98

  • Guest
Re: 3dpolyline export
« Reply #4 on: November 28, 2010, 09:45:12 AM »
Hi dear Lee and other members
I still have not seen your solution and involved by myself problem solving...!
I almost solved this problem , but now I have an incredible problem :
I've run this codes over 10 different examples and , at all of them , information of first point emitted , namely if I have a 3dpolyline with 5 vertex and 4 segment this codes extract information from the second point onwards !
This story is too strange for me , have you ever had such a problem ?
I thank if you or someone show me my wrongs and errors  :?
Code: [Select]
;; ======================================================
;

(defun C:PT  ( / en enlist ptlist en2 enlist2 pxy pz dis)
  (setq en (car (entsel "\n Select a 3D PolyLine :  ")))
  (setq enlist (entget en))
  (setq ptlist (list))
  (setq en2 (entnext en))
  (setq enlist2 (entget en2))
  (while
    (not (equal (cdr (assoc 0 (entget (entnext en2)))) "SEQEND")
         )
     (setq en2 (entnext en2))
     (setq enlist2 (entget en2))
     (if (/= 16 (cdr (assoc 70 enlist2)))

       (setq
         ptlist
          (append ptlist (list (cdr (assoc 10 enlist2))))
         ) ;setq ptlist
              )  ; end if
     )    ;end while
(foreach e  ptlist
  (setq pxy (cons (list (car e) (cadr e)) pxy)
        pz  (cons (caddr e) pz)
        ) ;setq pxy
  )       ;foreach
(setq n (- (length pxy) 1)
      i 0)
 (while (/= i n)
   (setq dis (cons (distance (nth i pxy) (nth (+ i 1) pxy)) dis)
         i   (1+ i))
   )      ;end while
  (terpri)
    (princ dis)
 
  (princ ptlist)
  (princ (reverse pxy))
  (princ (reverse pz))

  (princ)
  )       ;defun

Lee Mac

  • Seagull
  • Posts: 12912
  • London, England
Re: 3dpolyline export
« Reply #5 on: November 28, 2010, 12:26:21 PM »
Good on you for not looking but instead trying to figure it out  :-)

I have had a quick look over your code, and here are my points:

Code: [Select]
(defun C:PT  ( / en enlist ptlist en2 enlist2 pxy pz dis lastpt )
  
  (setq en (car (entsel "\n Select a 3D PolyLine :  ")))

[color=green]  ;; I would include an IF statement to allow for a user
  ;; not picking an object, perhaps also test that it is
  ;; indeed a 3D Polyline.[/color]
  
  (setq enlist (entget en)) [color=green];; I don't believe you are using this[/color]
  
  ; (setq ptlist (list)) [color=green];; No need for this[/color]
  
  (setq en2 (entnext en))
  (setq enlist2 (entget en2))
  
  (while (not (equal (cdr (assoc 0 (entget (entnext en2)))) "SEQEND"))

    ;(setq en2 (entnext en2)) [color=green];; But now you are already past the first point...[/color]
    ;(setq enlist2 (entget en2))
    
    (if (/= 16 (cdr (assoc 70 enlist2)))
      (setq ptlist (append ptlist (list (cdr (assoc 10 enlist2)))))

[color=green]      ;; I would recommend using 'cons', then reverse the resultant list
      ;; append is very inefficient.[/color]
    )

    (setq en2 (entnext en2)) [color=green];; Put these *after* getting the point.[/color]
    (setq enlist2 (entget en2))
  )

[color=green]  ;|  Try to minimise the number of times you are looping through the
      same information, this will make the program more efficient.
  
  (foreach e ptlist
    (setq pxy (cons (list (car e) (cadr e)) pxy)
          pz  (cons (caddr e) pz)
    )
  )

  (setq n (- (length pxy) 1) i 0)
  
  (while (/= i n)
    (setq dis (cons (distance (nth i pxy) (nth (+ i 1) pxy)) dis)
          i   (1+ i)
    )
  )

  |;

  ;; I would replace the above with just something like:[/color]

  (foreach e ptlist
    (setq pz (cons (caddr e) pz) e (list (car e) (cadr e)))

    (setq dis (cons (if lastpt (distance e lastpt) 0.0) dis))
    (setq lastpt e)
  )

  (terpri)
  (princ dis)
  
  (princ ptlist)
  (princ (reverse pxy))
  (princ (reverse pz))

  (princ)
)

Robert98

  • Guest
Re: 3dpolyline export
« Reply #6 on: November 28, 2010, 12:55:35 PM »
Good on you for not looking but instead trying to figure it out  :-)

I have had a quick look over your code, and here are my points:

Code: [Select]
(defun C:PT  ( / en enlist ptlist en2 enlist2 pxy pz dis lastpt )
  
  (setq en (car (entsel "\n Select a 3D PolyLine :  ")))

[color=green]  ;; I would include an IF statement to allow for a user
  ;; not picking an object, perhaps also test that it is
  ;; indeed a 3D Polyline.[/color]
  
  (setq enlist (entget en)) [color=green];; I don't believe you are using this[/color]
  
  ; (setq ptlist (list)) [color=green];; No need for this[/color]
  
  (setq en2 (entnext en))
  (setq enlist2 (entget en2))
  
  (while (not (equal (cdr (assoc 0 (entget (entnext en2)))) "SEQEND"))

    ;(setq en2 (entnext en2)) [color=green];; But now you are already past the first point...[/color]
    ;(setq enlist2 (entget en2))
    
    (if (/= 16 (cdr (assoc 70 enlist2)))
      (setq ptlist (append ptlist (list (cdr (assoc 10 enlist2)))))

[color=green]      ;; I would recommend using 'cons', then reverse the resultant list
      ;; append is very inefficient.[/color]
    )

    (setq en2 (entnext en2)) [color=green];; Put these *after* getting the point.[/color]
    (setq enlist2 (entget en2))
  )

[color=green]  ;|  Try to minimise the number of times you are looping through the
      same information, this will make the program more efficient.
  
  (foreach e ptlist
    (setq pxy (cons (list (car e) (cadr e)) pxy)
          pz  (cons (caddr e) pz)
    )
  )

  (setq n (- (length pxy) 1) i 0)
  
  (while (/= i n)
    (setq dis (cons (distance (nth i pxy) (nth (+ i 1) pxy)) dis)
          i   (1+ i)
    )
  )

  |;

  ;; I would replace the above with just something like:[/color]

  (foreach e ptlist
    (setq pz (cons (caddr e) pz) e (list (car e) (cadr e)))

    (setq dis (cons (if lastpt (distance e lastpt) 0.0) dis))
    (setq lastpt e)
  )

  (terpri)
  (princ dis)
  
  (princ ptlist)
  (princ (reverse pxy))
  (princ (reverse pz))

  (princ)
)
Hi dear Lee
thanks for your good tips
you're a real genius and very good master , I 'm working on it again .
thanks and have good times

Lee Mac

  • Seagull
  • Posts: 12912
  • London, England
Re: 3dpolyline export
« Reply #7 on: November 28, 2010, 01:10:03 PM »
Hi dear Lee
thanks for your good tips
you're a real genius and very good master , I 'm working on it again .
thanks and have good times

Thanks Robert :-)

I wouldn't go as far as 'genius', but that's for others to judge  :-)

Lee Mac

  • Seagull
  • Posts: 12912
  • London, England
Re: 3dpolyline export
« Reply #8 on: November 28, 2010, 01:22:31 PM »
By the way, I forgot to mention to bear in mind that the 'distance' method you are using will only apply to straight segmented polylines, and also that you are currently only using the 2D distance.
« Last Edit: November 28, 2010, 02:13:31 PM by Lee Mac »