TheSwamp
Code Red => AutoLISP (Vanilla / Visual) => Topic started by: grdruck 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:
;; 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.
-
Change this
(setq SELECTION_SET (ssget))
to
(setq SELECTION_SET (ssget '((0 . "POLYLINE"))))
then you don't need this:
(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
)
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. :)
-
Give this a try.
See if you can comment each line of code to figure out what is taking place.
;; 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)
-
Here's a slightly dofferent way of obtaining the coordinates at each vertex.
(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)
)
-
Neat, Thanks Jeff. :)
-
I should point out that this method works with Oldstyle "heavy" polylines, LWpolylines and 3dpolylines.
-
Thanks for your help guys - some of it is still over my head while other stuff I can understand. - grdruck