Jump to content

How to read polyline vert to file with name?


iztok13

Recommended Posts

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?

Link to comment
Share on other sites

  • Replies 27
  • Created
  • Last Reply

Top Posters In This Topic

  • Lee Mac

    17

  • iztok13

    10

  • CarlB

    1

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


Link to comment
Share on other sites

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))

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

  • 2 weeks later...

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

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]polylinesso4.th.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:

Link to comment
Share on other sites

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??

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...