Author Topic: Need Help on LISP that extracts X,Y,Z info from a 3Dpoly  (Read 5319 times)

0 Members and 1 Guest are viewing this topic.

grdruck

  • Guest
Need Help on LISP that extracts X,Y,Z info from a 3Dpoly
« on: March 10, 2005, 02:53:02 PM »
Hi all - I wrote some code to do the following:

here is my logic:

User Selects objects to export X,Y,Z data
Loop through each entity selected - no more entities exit program
Check if entity is a line, polyline, or 3Dployline
if not continue loop
if yes extract X,Y,Z info to external file

the Code I have so far:

Code: [Select]

;;   ExtXYZ_v2.LSP
;; Written by Garth Druckenmiller on March 10, 2005
;;       Updated on  -
;;      
;;     with help from various sources.   Thanks to those on TheSwamp.org

(defun C:EXTXYZ ()

  (Prompt "\nExtXYZ - Extracts X,Y,Z data from selected 3Dpolyline(s)")
  (Prompt "\n")

  (setq USEROS (getvar "osmode"))
  (setvar "osmode" 0)
  (setq OLD_CMDECHO (getvar "CMDECHO"))
  (setvar "CMDECHO" 0)

  (setq SELECTION_SET (ssget))
  (setq NO_OF_ITEMS (sslength SELECTION_SET))

  (setq SSPOSITION 0)

  (repeat NO_OF_ITEMS

     (setq ENTITY_NAME (ssname SELECTION_SET SSPOSITION))  ;; retrieve entity name
     (setq ENTITY_LIST (entget ENTITY_NAME))               ;; retrieve info about entity
     (setq ENTITY_TYPE (cdr (assoc 0 ENTITY_LIST)))        ;; retrieve entity type


     (cond ((equal ENTITY_TYPE "LINE")
               (prompt "\n Its a LINE")
           )

           ((equal ENTITY_TYPE "LWPOLYLINE")
               (prompt "\nIts a POLYLINE")
           )

;; when selected entity is a 3D polyline... do the following...

           ((equal ENTITY_TYPE "POLYLINE")
               (prompt "\nIts a 3DPOLYLINE")

      ;;(setq e (entget (car (entsel))))
      ;get the parent entity list

      (setq r 1)
      ;set loop control number to 1

      (while r
      ;while loop control is not nil, carry on looping

         (setq ENTITY_LIST (entget (entnext (cdr (car ENTITY_LIST)))))
         ;get the vertex entity list

         (if (/= (cdr (assoc 0 ENTITY_LIST)) "SEQEND")
         ;if it is not "end-of-sequence

              (progn
       ;do the following

         (terpri)
         ;new line

         (princ (cdr (assoc 10 ENTITY_LIST)))
         ;print the co-ordinates
 
       );progn

               (setq r nil)
       ;if end of sequence, stop looping

         );if

        );while

           )

     )

     (setq SSPOSITION (1+ SSPOSITION))

  )

   
    (setvar "CMDECHO" OLD_CMDECHO)
    (setvar "osmode" USEROS)

    (prompt "\nProgram complete.")
    (princ)
)


OK,  I don't have the part where it writes to an external txt file.
It runs ok and extracts info only for 3Dpolyline for now but I'm getting a *Cancel* at end of list of vertex's.  Please comment on code and if there is something really wrong.  I'm very new to LISP programing so please go easy on me.  Thx.

whdjr

  • Guest
Re: Need Help on LISP that extracts X,Y,Z info from a 3Dpoly
« Reply #1 on: March 10, 2005, 03:25:21 PM »
Change this
Code: [Select]
(setq SELECTION_SET (ssget))
to
Code: [Select]
(setq SELECTION_SET (ssget '((0 . "POLYLINE"))))
then you don't need this:
Code: [Select]
(cond ((equal ENTITY_TYPE "LINE")
               (prompt "\n Its a LINE")
           )

           ((equal ENTITY_TYPE "LWPOLYLINE")
               (prompt "\nIts a POLYLINE")
           )

;; when selected entity is a 3D polyline... do the following...

           ((equal ENTITY_TYPE "POLYLINE")
               (prompt "\nIts a 3DPOLYLINE")

      ;;(setq e (entget (car (entsel))))
      ;get the parent entity list

or the paren after );while
Code: [Select]
)

I did not test this any more than what I commented on.  A good rule is to use local variables.  You have set up globals and that may be your problem. :)

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Need Help on LISP that extracts X,Y,Z info from a 3Dpoly
« Reply #2 on: March 10, 2005, 05:53:15 PM »
Give this a try.
See if you can comment each line of code to figure out what is taking place.
Code: [Select]
;;   CAB  03/08/2005
;;  Write vertex points of 3d pline to text file
(defun c:3dpolyout (/ ss i ename vlst ent fn coords)
  (setq fn "3d Pline Points.txt")
  (prompt "\nSelect 3D polylines to export.")
  ;; select only 3D plines
  (setq ss (ssget '((0 . "POLYLINE") (-4 . "&") (70 . 8))))
  (if (and (and ss (> (sslength ss) 0))
  (setq fn (open fn "w"))
      )
    (progn
      (princ "***  Point list  ***" fn)
      (repeat (setq i (sslength ss))
(setq ent (ssname ss (setq i (1- i))))
(print ent fn)
(while (and (setq ent (entnext ent))
   (setq vlst (entget ent))
   (= (cdr (assoc 0 vlst)) "VERTEX")
      )
 (setq coords (cons (cdr (assoc 10 vlst)) coords))
)
(foreach x coords
 (print x fn)
)
      )
      (print fn)
      (close fn)
    )
  )
  (princ)
)
(prompt "\n***  Lisp 3d Poly Out loaded, Enter 3dpolyout to run.")
(princ)
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.

Jeff_M

  • King Gator
  • Posts: 4096
  • C3D user & customizer
Need Help on LISP that extracts X,Y,Z info from a 3Dpoly
« Reply #3 on: March 10, 2005, 06:14:32 PM »
Here's a slightly dofferent way of obtaining the coordinates at each vertex.
Code: [Select]

(defun poly-coords (poly / end param)
  (setq end (vlax-curve-getendparam poly)
param -1)
  (while (<= (setq param (1+ param)) end)
    (print (vlax-curve-getpointatparam poly param))
    )
  (princ)
  )
;;;*****EXAMPLE USE******
(defun c:prntpoly (/ poly)
  (and (setq poly (car (entsel)))
       (poly-coords poly)
       )
  (princ)
  )

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Need Help on LISP that extracts X,Y,Z info from a 3Dpoly
« Reply #4 on: March 10, 2005, 06:25:57 PM »
Neat, Thanks Jeff. :)
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.

Jeff_M

  • King Gator
  • Posts: 4096
  • C3D user & customizer
Need Help on LISP that extracts X,Y,Z info from a 3Dpoly
« Reply #5 on: March 10, 2005, 07:25:22 PM »
I should point out that this method works with Oldstyle "heavy" polylines, LWpolylines and 3dpolylines.

grdruck

  • Guest
Need Help on LISP that extracts X,Y,Z info from a 3Dpoly
« Reply #6 on: March 22, 2005, 12:04:46 PM »
Thanks for your help guys - some of it is still over my head while other stuff I can understand. - grdruck