View Full Version : How to read polyline vert to file with name?
iztok13
12th Jan 2009, 08:15 am
Hello,
I have problem to make lsp file, that will read vertices of polilyine x,y,z coordinates to txt file with named polyline.
I have a bunch of polyline and i need to mark them with name and attach x,y,z coordinates of them.
So basicly i need to pick the polilyine, then must ask me the name of polilyine then record in to the txt file with the name and coordinates x,y,z. I have a lot of them so i need to be asked for another polyline and again and again and put to same txt file.
any suggestions?
Lee Mac
12th Jan 2009, 06:18 pm
Perhaps this?
(defun c:plco (/ File oFile pLin pStr nlist pLen wLine)
(if (setq File (getfiled "Create a Text File" "C:\\" "txt" 9))
(progn
(setq oFile (open file "W"))
(while (setq pLin (ssget ":S"
(list (cons 0 "LWPOLYLINE,POLYLINE")
(cons 410 (getvar "CTAB"))
) ;_ end list
) ;_ end ssget
) ;_ end setq
(sssetfirst nil pLin)
(if (/= (setq pStr (getstring t "\nSpecify Name for Selected Polyline >> ")) "")
(progn (foreach x (entget (ssname pLin 0))
(if (eq 10 (car x))
(setq nlist (cons (cdr x) nlist))
) ;_ end if
) ;_ end foreach
(setq pLen (length nlist))
(while (not (minusp (setq pLen (1- pLen))))
(setq wLine (strcat (rtos (car (nth pLen nlist)) 2 2)
","
(rtos (cadr (nth pLen nlist)) 2 2)
) ;_ end strcat
) ;_ end setq
(if (caddr (nth pLen nlist))
(strcat wLine "," (rtos (caddr (nth pLen nlist)) 2 2))
) ;_ end if
) ;_ end while
(write-line (strcat pStr "\t" wLine) oFile)
) ;_ end progn
(princ "\n<!> No Line Name Specified. <!>")
) ;_ end if
(sssetfirst nil)
) ;_ end while
(close oFile)
) ;_ end progn
(princ "\n<!> No File Selected. <!> ")
) ;_ end if
(princ)
) ;_ end defun
Lee Mac
12th Jan 2009, 07:25 pm
Sorry, my mistake, initial post would only read first vertex.
(defun c:plco (/ File oFile pLin pStr nlist pLen wLine wfLine)
(if (setq File (getfiled "Create a Text File" "C:\\" "txt" 9))
(progn
(setq oFile (open file "W"))
(while (setq pLin (ssget ":S" (list (cons 0 "LWPOLYLINE") (cons 410 (getvar "CTAB")))))
(sssetfirst nil pLin)
(if (/= (setq pStr (getstring t "\nSpecify Name for Selected Polyline >> ")) "")
(progn (foreach x (entget (ssname pLin 0))
(if (eq 10 (car x))
(setq nlist (cons (cdr x) nlist))))
(setq nlist (reverse nlist) pLen (length nlist) wfLine "")
(while (not (minusp (setq pLen (1- pLen))))
(setq wLine (strcat (rtos (car (nth pLen nlist)) 2 2) ","
(rtos (cadr (nth pLen nlist)) 2 2)))
(if (caddr (nth pLen nlist))
(strcat wLine "," (rtos (caddr (nth pLen nlist)) 2 2)))
(setq wfLine (strcat wLine "\t" wfLine))
) ;_ end while
(write-line (strcat pStr "\t" wfLine) oFile)
) ;_ end progn
(princ "\n<!> No Line Name Specified. <!>")
) ;_ end if
(sssetfirst nil)
) ;_ end while
(close oFile)
) ;_ end progn
(princ "\n<!> No File Selected. <!> ")
) ;_ end if
(princ))
CarlB
12th Jan 2009, 07:39 pm
Lee,
It doesn't appear this code would work for "polylines"? I see they can be selected, but code seems to use just group code 10 for vertices (so works for 'lwpolylinwes'). To get vertices of a regular (heavyweight) polyline you'll need a different method, such as stepping through with 'entnext'. Seeing that the OP asked for z values as well, he might even want it to work for 3d polylines.
Lee Mac
12th Jan 2009, 07:49 pm
Sorry Carl, will sort it.
Lee Mac
12th Jan 2009, 08:15 pm
Ok, try this:
(defun c:plco (/ File oFile pLin pStr pEnt nlist pLen wLine wfLine vPt wvLine)
(if (setq File (getfiled "Create a Text File" "C:\\" "txt" 9))
(progn
(setq oFile (open file "W"))
(while (setq pLin (ssget ":S" (list (cons 0 "LWPOLYLINE,POLYLINE") (cons 410 (getvar "CTAB")))))
(sssetfirst nil pLin)
(if (/= (setq pStr (getstring t "\nSpecify Name for Selected Polyline >> ")) "")
(progn
(setq pEnt (ssname pLin 0))
(cond ((= "LWPOLYLINE" (cdr (assoc 0 (entget pEnt))))
(foreach x (entget pEnt)
(if (eq 10 (car x))
(setq nlist (cons (cdr x) nlist))
) ;_ end if
) ;_ end foreach
(setq nlist (reverse nlist)
pLen (length nlist)
wfLine ""
) ;_ end setq
(while (not (minusp (setq pLen (1- pLen))))
(setq wLine (strcat (rtos (car (nth pLen nlist)) 2 2)
","
(rtos (cadr (nth pLen nlist)) 2 2)
) ;_ end strcat
) ;_ end setq
(setq wfLine (strcat wLine "\t" wfLine))
) ;_ end while
(write-line (strcat pStr "\t" wfLine) oFile)
)
((= "POLYLINE" (cdr (assoc 0 (entget pEnt))))
(setq wvLine ""
pEnt (entnext pEnt)
) ;_ end setq
(while (/= (cdr (assoc 0 (entget pEnt))) "SEQEND")
(setq vPt (cdr (assoc 10 (entget pEnt)))
wvLine (strcat (rtos (car vPt) 2 2)
","
(rtos (cadr vPt) 2 2)
","
(rtos (caddr vPt) 2 2)
"\t"
wvLine
) ;_ end strcat
pEnt (entnext pEnt)
) ;_ end setq
) ;_ end while
(write-line (strcat pStr "\t" wvLine) oFile)
)
) ;_ end cond
) ;_ end progn
(princ "\n<!> No Line Name Specified. <!>")
) ;_ end if
(sssetfirst nil)
) ;_ end while
(close oFile)
) ;_ end progn
(princ "\n<!> No File Selected. <!> ")
) ;_ end if
(princ)
) ;_ end defun
iztok13
13th Jan 2009, 08:38 am
thx Lee Mac, works perfect. This is what i need. thx again
Lee Mac
13th Jan 2009, 12:33 pm
Excellent, glad it worked OK for you.
I had to mess around with it a bit to work with 3DPolylines, as I normally only work in 2D - but got it working in the end :P
iztok13
27th Jan 2009, 02:56 pm
Lee mac:
sorry it doesnt work like a charm, i notice that if i had a lot of polilys , coordinates get in mess, basicly the first poly is OK then the second poly had 2 times more vertex, i try to corect this but i didnt succses.
any idea. & also if can be in front of coordinates the name of poly?
thx
Lee Mac
27th Jan 2009, 02:57 pm
I'll have a look at it :)
iztok13
27th Jan 2009, 03:02 pm
o that was fast replay, thx in advance so i have one more wish, if can be every vertex in one line.
if i have name of poly : vertex_name and x, y, coordinates
vertex_name x y
vertex_name x y
vertex_name x y
vertex_name x y
i hope u understand, & again thx in advance
Lee Mac
27th Jan 2009, 03:05 pm
This is a quick fix for the multiple coordinate issue:
(defun c:plco (/ File oFile pLin pStr pEnt nlist pLen wLine wfLine vPt wvLine)
(if (setq File (getfiled "Create a Text File" "C:\\" "txt" 9))
(progn
(setq oFile (open file "W"))
(while (setq pLin (ssget "_:S" (list (cons 0 "LWPOLYLINE,POLYLINE") (cons 410 (getvar "CTAB")))))
(sssetfirst nil pLin)
(if (/= (setq pStr (getstring t "\nSpecify Name for Selected Polyline >> ")) "")
(progn
(setq pEnt (ssname pLin 0))
(cond ((= "LWPOLYLINE" (cdr (assoc 0 (entget pEnt))))
(foreach x (entget pEnt)
(if (eq 10 (car x))
(setq nlist (cons (cdr x) nlist))
) ;_ end if
) ;_ end foreach
(setq nlist (reverse nlist)
pLen (length nlist)
wfLine ""
) ;_ end setq
(while (not (minusp (setq pLen (1- pLen))))
(setq wLine (strcat (rtos (car (nth pLen nlist)) 2 2)
","
(rtos (cadr (nth pLen nlist)) 2 2)
) ;_ end strcat
) ;_ end setq
(setq wfLine (strcat wLine "\t" wfLine))
) ;_ end while
(write-line (strcat pStr "\t" wfLine) oFile)
(setq nlist nil
wfLine nil
)
)
((= "POLYLINE" (cdr (assoc 0 (entget pEnt))))
(setq wvLine ""
pEnt (entnext pEnt)
) ;_ end setq
(while (/= (cdr (assoc 0 (entget pEnt))) "SEQEND")
(setq vPt (cdr (assoc 10 (entget pEnt)))
wvLine (strcat (rtos (car vPt) 2 2)
","
(rtos (cadr vPt) 2 2)
","
(rtos (caddr vPt) 2 2)
"\t"
wvLine
) ;_ end strcat
pEnt (entnext pEnt)
) ;_ end setq
) ;_ end while
(write-line (strcat pStr "\t" wvLine) oFile)
(setq wvLine nil)
)
) ;_ end cond
) ;_ end progn
(princ "\n<!> No Line Name Specified. <!>")
) ;_ end if
(sssetfirst nil)
) ;_ end while
(close oFile)
) ;_ end progn
(princ "\n<!> No File Selected. <!> ")
) ;_ end if
(princ)
) ;_ end defun
But I may consider re-writing this LISP as I am not happy with it.
As for the other request - would you like the name of the polyline after all the coordinates?
Lee Mac
27th Jan 2009, 03:07 pm
One more question -->
Will you only be working with POLYLINES (3D) or will I have to encompass LWPOLYLINES (2d) also?
Lee Mac
27th Jan 2009, 04:16 pm
Ok, this is better I think:
;|
Polyline Coordinate Writer
by Lee McDonnell
27.01.2009
|;
(defun c:plco2 (/ File oFile ss eLst Selss pName aEnt vLst i vNme)
(vl-load-com)
(if (setq File (getfiled "Create a Text File" "C:\\" "txt" 9))
(progn
(setq oFile (open File "W"))
(if (setq ss (ssget (list (cons 0 "POLYLINE")
(if (getvar "CTAB") (cons 410 (getvar "CTAB"))(cons 67 (- 1 (getvar "TILEMODE")))))))
(progn
(setq eLst (vl-remove-if 'listp (mapcar 'cadr (ssnamex ss)))
Selss (ssadd))
(foreach ent eLst
(ssadd ent Selss)
(sssetfirst nil Selss)
(if (not (setq pName (getstring t "\nSpecify Name for Selected Polyline > ")))
(setq pName (vl-princ-to-string ent)))
(write-line (strcat "Polyline: " pName "\n\n") oFile)
(setq aEnt (entnext ent))
(while (/= "SEQEND" (cdr (assoc 0 (entget aEnt))))
(setq vLst (cons (cdr (assoc 10 (entget aEnt))) vLst)
aEnt (entnext aEnt)))
(setq i (length vLst) vNme "Vertex No: " vt 1)
(while (not (minusp (setq i (1- i))))
(write-line
(strcat vNme (rtos vt) "\t"
(rtos (car (nth i vLst)) 2 2) ","
(rtos (cadr (nth i vLst)) 2 2) ","
(rtos (caddr (nth i vLst)) 2 2) "\n") oFile)
(setq vt (1+ vt)))
(write-line "\n" oFile)
(setq vLst nil)
(ssdel ent Selss)
(sssetfirst nil nil)))
(princ "\n<!> No Polyline Selected <!>")))
(princ "\n<!> No File Selected <!>"))
(close oFile)
(princ "\n...Vertices Written to File...")
(princ))
Only works on POLYLINES for the minute, but if you need it expanded I shall write another.
Select as many POLYLINES as you like at a time :)
iztok13
28th Jan 2009, 08:56 am
A big thx for the effort.
But this is complitly different from the first one.
I will explain:
I have normal Polyline not 2d or 3d polys. All polylines are closed. Every single polyiline have a different name. So I need to be ask for each poly what is a name of it.
Basicly is somthing like the first lisp you wrote. It s workin only coordinates of the second, thrd ,.., are wrong or they are repeat. I dont now why?
Example, if I have four closed normal poly (not 2D or 3D) with name A,B,C and D. A have 4 vertices, B have 4 vertices, C have 7 vertices and D have 3 vertices, the TXT file should looks like that:
A x,y
A x,y
A x,y
A x,y
B x,y
B x,y
B x,y
B x,y
C x,y
C x,y
C x,y
C x,y
C x,y
C x,y
C x,y
D x,y
D x,y
D x,y
Here is a preview of example drawing:
[/URL][URL="http://img90.imageshack.us/my.php?image=polylinesso4.jpg"]http://img90.imageshack.us/img90/8498/polylinesso4.th.jpg (http://img90.imageshack.us/my.php?image=polylinesso4.jpg)
A,B,C,D are the name of polys, x and y are coordinates of them.
I hope i dont make a lot of trouble for this..
Thx in advance..:oops:
Lee Mac
28th Jan 2009, 01:31 pm
Did my other posted LISP not fix the multiple co-ordinate issue?
iztok13
28th Jan 2009, 01:34 pm
I didnt try on 3d or 2d polyline, only on normal polyline, when i select the polyline it say nothing selected????
Mybe because is not 2 or 3D poly??
Lee Mac
28th Jan 2009, 01:37 pm
Is can only be a 2D or 3D poly - unless you are working in 4D which I highly doubt... :wink:
I am surprised it says nothing selected... could you post a sample drawing in 2000 format that I could work from?
iztok13
28th Jan 2009, 01:56 pm
ok, thx
here is an example of my drawing of polylines:
http://www.2shared.com/file/4743843/d6e1e7a4/polylines.html
thx
Name of polyline is like this :"SEM-S3-10"......
Lee Mac
28th Jan 2009, 02:39 pm
Ok,
Please look at the documents attached and verify that the notepad doc "desired" is what you wish:
Lee Mac
28th Jan 2009, 02:42 pm
If the notepad file: "Desired" is indeed what you would like:
;|
Polyline Coordinate Writer
by Lee McDonnell
27.01.2009
|;
; Version 2 ~ Polyline Writing Format Altered.
(defun c:plco2 (/ File oFile ss eLst Selss pName aEnt vLst i vNme)
(vl-load-com)
(if (setq File (getfiled "Create a Text File" "C:\\" "txt" 9))
(progn
(setq oFile (open File "W"))
(if (setq ss (ssget (list (cons 0 "POLYLINE")
(if (getvar "CTAB") (cons 410 (getvar "CTAB"))(cons 67 (- 1 (getvar "TILEMODE")))))))
(progn
(setq eLst (vl-remove-if 'listp (mapcar 'cadr (ssnamex ss)))
Selss (ssadd))
(foreach ent eLst
(ssadd ent Selss)
(sssetfirst nil Selss)
(if (not (setq pName (getstring t "\nSpecify Name for Selected Polyline > ")))
(setq pName (vl-princ-to-string ent)))
(setq aEnt (entnext ent))
(while (/= "SEQEND" (cdr (assoc 0 (entget aEnt))))
(setq vLst (cons (cdr (assoc 10 (entget aEnt))) vLst)
aEnt (entnext aEnt)))
(setq i (length vLst))
(while (not (minusp (setq i (1- i))))
(write-line
(strcat pName "\t"
(rtos (car (nth i vLst)) 2 2) ","
(rtos (cadr (nth i vLst)) 2 2) ","
(rtos (caddr (nth i vLst)) 2 2) "\n") oFile))
(setq vLst nil)
(ssdel ent Selss)
(sssetfirst nil nil)))
(princ "\n<!> No Polyline Selected <!>")))
(princ "\n<!> No File Selected <!>"))
(close oFile)
(princ "\n...Vertices Written to File...")
(princ))
Then the above should do your bidding. :)
iztok13
28th Jan 2009, 03:04 pm
ok the the desire file is ok, But when i run plco2 lisp writen above i cant select poly, it say nothing selected????
please try the file wich i upload and try if you have the same problem?
Lee Mac
28th Jan 2009, 03:41 pm
I can't get to the file you uploaded...
Can you not attach the file?
iztok13
28th Jan 2009, 05:05 pm
Ok here is the same file;
9811
:)
Lee Mac
28th Jan 2009, 05:57 pm
Ahh, you are using (2D) LWPolylines - I shall alter the LISP accordingly :)
Lee Mac
28th Jan 2009, 06:04 pm
Here you are - just window the whole drawing to select polylines (as in your example).
;|
Polyline Coordinate Writer
by Lee McDonnell
27.01.2009
|;
; Modified for use with LWPOLYLINES
(defun c:plco2 (/ File oFile ss eLst Selss pName aEnt vLst i)
(vl-load-com)
(if (setq File (getfiled "Create a Text File" "C:\\" "txt" 9))
(progn
(setq oFile (open File "W"))
(if (setq ss (ssget (list (cons 0 "LWPOLYLINE")
(if (getvar "CTAB") (cons 410 (getvar "CTAB"))(cons 67 (- 1 (getvar "TILEMODE")))))))
(progn
(setq eLst (vl-remove-if 'listp (mapcar 'cadr (ssnamex ss)))
Selss (ssadd))
(foreach ent eLst
(ssadd ent Selss)
(sssetfirst nil Selss)
(if (not (setq pName (getstring t "\nSpecify Name for Selected Polyline > ")))
(setq pName (vl-princ-to-string ent)))
(setq vLst (mapcar 'cdr (vl-remove-if '(lambda (x) (/= 10 (car x))) (entget ent))))
(setq i (length vLst))
(while (not (minusp (setq i (1- i))))
(write-line
(strcat pName "\t"
(rtos (car (nth i vLst)) 2 2) ","
(rtos (cadr (nth i vLst)) 2 2) "\n") oFile))
(setq vLst nil)
(ssdel ent Selss)
(sssetfirst nil nil)))
(princ "\n<!> No Polyline Selected <!>")))
(princ "\n<!> No File Selected <!>"))
(close oFile)
(princ "\n...Vertices Written to File...")
(princ))
iztok13
29th Jan 2009, 08:46 am
Ok I tested, This time I think it really works for me. im really glad that you help me. This routin will make my work much easier than time before.
Thank you again.
:)
Lee Mac
29th Jan 2009, 01:18 pm
No Problem - glad we finally got it sorted :)
Powered by vBulletin™ Version 4.1.2 Copyright © 2013 vBulletin Solutions, Inc. All rights reserved.