Jump to content

Export polyline vertex (in order), into a text file


Mehran

Recommended Posts

Hello,

I'm trying to export a single polyline into a specific text form for inserting to survey camera. like this:

 

number,Y,X,100.000,p

 

example:

 

101,4136625.685,424685.802,100.000,p

 

 

1. number always starts from 101,102,103...

2. Z is always 100.000

3. p is always fixed

 

can you kindly help me ?

Link to comment
Share on other sites

mAssoc here from Lee Mac in the link below will give you the vertices (coordinates) of a PolyLine. The second link below shows how to write to a text file. Mash the 2 together and off you go!

 

And very quickly something like this:

 

(defun c:test ( / )
  (defun mAssoc ( key lst ) ;; Sub function to get the coordinates
    (foreach x lst
      (if (= key (car x))
        (setq l (cons (cdr x) l))
      )
    )
    (reverse l)
  ) ; end sub function

  (setq MyPLine (entget (car (entsel "Select Polyline")))) ;; select an entity. No error checking that  polyline is selected though
  (setq Coords (mAssoc 10 MyPline) ) ;; sun the sub funciotn above, selected line and description '10' for coordinates

  (setq file (open [filePath and Name] "w")) ;; open text file, you put in the file name and file path
  (foreach n Coords  ; a foreach loop each coordiante
    (write-line "101" file) ;; change this if you need different numbers ;; writing to the text file
    (write-line (car n) file) 
    (write-line (cadr n) file)
    (write-line "100.000" file)
    (write-line "p" file)
  ) ; end foreach
  (close file) ;; cloe the text file
  (princ) ;; exit silently
)

 

(though it is the weekend here, I turned CAD off a couple of hours ago now, so unchecked, there might be an odd typo in there, but you should get the idea)

 

 

https://www.afralisp.net/autolisp/tutorials/file-handling.php

 

 

 

  • Like 1
Link to comment
Share on other sites

Thank you sir, I really appreciate your help.

I just tested your code, don't know why it throws too many argument error.

Link to comment
Share on other sites

Another get pline co-ords, plus a little bit more good learning exercise.

 

(setq num (getint "\nEnter start number "))
(while (setq plent (entsel "\nPick pline enter to stop"))
(if plent 
(progn 
(setq co-ord (mapcar 'cdr (vl-remove-if-not '(lambda (x) (= (car x) 10)) (entget (car plent)))))
(foreach pt co-ords
; do your write points to file
(setq num (1+ num))
)
)
)
)

 

  • Like 1
Link to comment
Share on other sites

14 hours ago, Mehran said:

Thank you sir, I really appreciate your help.

I just tested your code, don't know why it throws too many argument error.

 

Read through the code above (not follow it blindly), there is some homework for you to enter the file path and file name for the file.

 

Updated code below - you still need to make the same changes though but what I had I think will write each part to a separate line. Also taken a note from BigAl, "enter start number"  which I have put to increment on each line: Should work, but if not will test it after the weekend.

 

(defun c:test ( / MyPline acount Coords MyFileName file n )
  (defun mAssoc ( key lst ) ;; Sub function to get the coordinates
    (foreach x lst
      (if (= key (car x))
        (setq l (cons (cdr x) l))
      )
    )
    (reverse l)
  ) ; end sub function

  (setq MyPLine (entget (car (entsel "\nSelect Polyline: ")))) ;; select an entity. No error checking that polyline is selected
  (setq acount (getint "\nEnter start Number: "))              ;; Enter a number.
  (setq Coords (mAssoc 10 MyPline) )                           ;; Run subfunction mAssoc for '10' in the entity description

  (setq MyFileName [File path and file name here, example "c:\\Folder\\file.txt" using double '\\']  ;;The save path and filename

;;  (if MyFileName(vl-file-delete MyFileName))                  ;; If the file exists, delete it. Take out comment marks (';;') if needed
  (setq file (open MyFileName "w"))                             ;; Open / Create text file for editing.
  (write-line "" file)                                        ;; Blankline before the line data. Comment out (add ';;')if not required
  (foreach n Coords                                             ;; A foreach loop each coordinate
    (setq MyLine (strcat (rtos acount) " " (rtos (car n)) " " (rtos (cadr n)) " 100.000 P" )) ;; Create text line for text file
    (write-line MyLine file)                                    ;; Write text to file
    (setq acount (+ acount 1))                                  ;; Increment reference number by 1
  ) ; end foreach                                               ;; End loop
  (close file)                                                  ;; close the text file
  (princ)                                                       ;; exit silently
)

 

Edited to add comments to the code above.

 

This should give a decent start to what you want. Most other things you might want here are somewhere online - find them out and ask how to integrate them perhaps, or ask for more details

 

Edited by Steven P
Link to comment
Share on other sites

3 hours ago, Steven P said:

 

Read through the code above (not follow it blindly), there is some homework for you to enter the file path and file name for the file.

 

Updated code below - you still need to make the same changes though but what I had I think will write each part to a separate line. Also taken a note from BigAl, "enter start number"  which I have put to increment on each line: Should work, but if not will test it after the weekend.

 

(defun c:test ( / MyPline acount Coords MyFileName file n )
  (defun mAssoc ( key lst ) ;; Sub function to get the coordinates
    (foreach x lst
      (if (= key (car x))
        (setq l (cons (cdr x) l))
      )
    )
    (reverse l)
  ) ; end sub function

  (setq MyPLine (entget (car (entsel "\nSelect Polyline: ")))) ;; select an entity. No error checking that polyline is selected
  (setq acount (getint "\nEnter start Number: "))              ;; Enter a number.
  (setq Coords (mAssoc 10 MyPline) )                           ;; Run subfunction mAssoc for '10' in the entity description

  (setq MyFileName [File path and file name here, example "c:\\Folder\\file.txt" using double '\\']  ;;The save path and filename

;;  (if MyFileName(vl-file-delete MyFileName))                  ;; If the file exists, delete it. Take out comment marks (';;') if needed
  (setq file (open MyFileName "w"))                             ;; Open / Create text file for editing.
  (write-line "" file)                                        ;; Blankline before the line data. Comment out (add ';;')if not required
  (foreach n Coords                                             ;; A foreach loop each coordinate
    (setq MyLine (strcat (rtos acount) " " (rtos (car n)) " " (rtos (cadr n)) " 100.000 P" )) ;; Create text line for text file
    (write-line MyLine file)                                    ;; Write text to file
    (setq acount (+ acount 1))                                  ;; Increment reference number by 1
  ) ; end foreach                                               ;; End loop
  (close file)                                                  ;; close the text file
  (princ)                                                       ;; exit silently
)

 

Edited to add comments to the code above.

 

This should give a decent start to what you want. Most other things you might want here are somewhere online - find them out and ask how to integrate them perhaps, or ask for more details

 

Thank you very much.. 

output is like this:

101.0000 423735.7991 4138278.7044 100.000 P

 

1. number is float  like 101.0000 instead of 101

 

2. how can I make coordinates output with 3 decimal place. like 423735.799 4138278.704

 

 

 

Link to comment
Share on other sites

18 minutes ago, Mehran said:

Thank you very much.. 

output is like this:

101.0000 423735.7991 4138278.7044 100.000 P

 

1. number is float  like 101.0000 instead of 101

 

2. how can I make coordinates output with 3 decimal place. like 423735.799 4138278.704

 

 

 

I fixed it,

Thanks again

  • Like 1
Link to comment
Share on other sites

Not a problem, always good to see someone read what we offer, understand, learn and have a go at it, fixing or altering to suit.

  • Thanks 1
Link to comment
Share on other sites

4 hours ago, Steven P said:

Not a problem, always good to see someone read what we offer, understand, learn and have a go at it, fixing or altering to suit.

is there a way to check if polyline is already selected or not?

(setq MyPLine (entget (car (entsel "\nSelect Polyline: "))))

It doesn't matter if I select polyline before executing command, I still need to select polyline.

Link to comment
Share on other sites

That will break with error :

 

Expected ENAME ... if you miss selection...

 

(if (setq MyPLine (car (entsel "\nSelect Polyline: ")))

  (setq MyPLineDXF (entget MyPLine))

)

 

This is how it should look like...

Link to comment
Share on other sites

16 minutes ago, marko_ribar said:

That will break with error :

 

Expected ENAME ... if you miss selection...

 

(if (setq MyPLine (car (entsel "\nSelect Polyline: ")))

  (setq MyPLineDXF (entget MyPLine))

)

 

This is how it should look like...

Hi, Thanks for your reply.

Still wants me to select polyline. even though I've selected my polyline before executing command

Link to comment
Share on other sites

44 minutes ago, BIGAL said:

Have a look at this, only allows 1 pick a time. Press enter will exit. 

(while (setq plent (entsel "\nPick pline enter to stop"))

 

Hello Sir, thanks.

No, still the same

Link to comment
Share on other sites

I think if you are selecting many lines all at once you will want to use a selection set instead of selecting them using entsel. This is a decent reference guide http://lee-mac.com/ssget.html

 

Quickly and no testing, replace

 

  (setq MyPLine (entget (car (entsel "\nSelect Polyline: ")))) ;; select an entity. No error checking that polyline is selected

 

 

with 

princ "\nSelect polylines: ")
(setq MySS (ssget '(( 0 . "LWPOLYLINE"))) ) ;Selection. Filtered for polylines
(setq SSCount 0)
(while (< SSCount (sslength MySS))
  (setq MyPLine (entget (ssname MySS SSCount)))

;;;;; The rest of the code here

;;;;; Until
  (close file)                                                  ;; close the text file

;;;;; Then add in these 2 lines
  (setq SSCount (+ SSCount 1))
) ; End while                 

 

 

A quick edit and perhaps not the most optimised method, but it should work

 

Edited by Steven P
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...