Jump to content

Write a LIST into a file


marc578

Recommended Posts

Hi can anybody give an advice in order to write a LIST into a *.txt file?

 

(defun c:prueba4()

(setq a (entsel))

(setq b (entget(car a)))

(setq pt1 (cdr(assoc 10 b)))

(setq f(open "ppr.txt" "w"))

(write-line pt1 f)

(close f)

)

 

With the previous code I got the following message:

"bad argument type: stringp (39.198 28.5504 0.0)"

Link to comment
Share on other sites

The probelm lays in the incorrect way of using (write-line).

 

it should be with strings to write the data to file . Like :

 

(write-line [color="red"]"[/color]Welcome to CADTutor.[color="red"]"[/color] f)

 

Try to replace your PT1 with the previous line and it will work.

 

Maybe someone else would have something to make it. I hope so.

 

Regards

Link to comment
Share on other sites

Unfortunately this isn't the answer the OP was looking for - his goal was to record the coordinates of a point into a file. To do that the list must be converted into a string. Can do that using a nice function by Gile:

 

;;; lST2STR
;;; Returns a string which is the concatenation of a list and a  separator
;;; Author: Gile
;;; Arguments
;;; str = the string
;;; sep = the separator pattern
(defun lst2str (lst sep)
 (if (cadr lst)
   (strcat (vl-princ-to-string (car lst))
       sep
       (lst2str (cdr lst) sep)
   )
   (vl-princ-to-string (car lst))
 )
)

 

Use it like:

(LST2STR PT1 "\t")

 

 

Regards,

Link to comment
Share on other sites

I hope this is the easiest way posted by Great Mr.Lee Mac. This is most useful for me to convert the string into list as well as list to string.

 

 
(defun L->Ptr (lst) (princ) (vl-string-trim "()" (vl-princ-to-string lst)))
(defun Ptr->L (ptr) (read (strcat "(" ptr ")")))

Link to comment
Share on other sites

I hope this is the easiest way posted by Great Mr.Lee Mac. This is most useful for me to convert the string into list as well as list to string.

 

 
(defun L->Ptr (lst) (princ) (vl-string-trim "()" (vl-princ-to-string lst)))
(defun Ptr->L (ptr) (read (strcat "(" ptr ")")))

 

Muthu, as I have explained to you in the past - I never intended those functions to be used for that purpose... they were constructed to be used for manipulating DCL tile values.

Link to comment
Share on other sites

Muthu, as I have explained to you in the past - I never intended those functions to be used for that purpose... they were constructed to be used for manipulating DCL tile values.

 

DearLee,

I agreed. But it can be useful for that purpose too and not for all the cases.

If you can fine any other wat for that particular purposes (List to string & string to List) , of course it will be so much helpful to me as well as others.

 

Regards,

Muthu.

Link to comment
Share on other sites

Unfortunately this isn't the answer the OP was looking for - his goal was to record the coordinates of a point into a file. To do that the list must be converted into a string. Can do that using a nice function by Gile:

 

;;; lST2STR
;;; Returns a string which is the concatenation of a list and a  separator
;;; Author: Gile
;;; Arguments
;;; str = the string
;;; sep = the separator pattern
(defun lst2str (lst sep)
 (if (cadr lst)
   (strcat (vl-princ-to-string (car lst))
       sep
       (lst2str (cdr lst) sep)
   )
   (vl-princ-to-string (car lst))
 )
)

 

Use it like:

(LST2STR PT1 "\t")

 

Regards,

 

 

Dear msasu,

Good one.

do u have anyother routine to conver the string into list?

 

Regards,

Muthu

Link to comment
Share on other sites

Hi friends, I wrote the following that work so good for me, maybe can help you too:

 

****************************************************************

(setq a (entsel))

(setq b (entget(car a)))

(setq pt1 (cdr(assoc 10 b)))

(setq pt2 (cdr(assoc 11 b)))

(setq f(open "C:\\Documents and Settings\\Marco\\Mis documentos\\AAA.txt" "w"))

; *******************************************************************************

(setq ptx1 (car pt1))

(setq pty1 (cadr pt1))

(setq ptz1 (caddr pt1))

; *******************************************************************************

(setq ptx1(rtos ptx1 2 4)) ;convert data into a string

(setq pty1(rtos pty1 2 4))

(setq ptz1(rtos ptz1 2 4))

; *******************************************************************************

(setq xyz1(strcat ptx1 ", " pty1 ", " ptz1))

; *******************************************************************************

(write-line xyz1 f)

 

Regards

Link to comment
Share on other sites

Hi friends, I wrote the following that work so good for me, maybe can help you too:

 

****************************************************************

(setq a (entsel))

(setq b (entget(car a)))

(setq pt1 (cdr(assoc 10 b)))

(setq pt2 (cdr(assoc 11 b)))

(setq f(open "C:\\Documents and Settings\\Marco\\Mis documentos\\AAA.txt" "w"))

; *******************************************************************************

(setq ptx1 (car pt1))

(setq pty1 (cadr pt1))

(setq ptz1 (caddr pt1))

; *******************************************************************************

(setq ptx1(rtos ptx1 2 4)) ;convert data into a string

(setq pty1(rtos pty1 2 4))

(setq ptz1(rtos ptz1 2 4))

; *******************************************************************************

(setq xyz1(strcat ptx1 ", " pty1 ", " ptz1))

; *******************************************************************************

(write-line xyz1 f)

 

Regards

 

Try to make it this way, it is shorter and clearer .

(setq a (entsel))
(setq b (entget(car a)))
(setq pt1 (cdr(assoc 10 b)))
(setq pt2 (cdr(assoc 11 b)))
(setq f (open "C:\\Documents and Settings\\Marco\\Mis documentos\\AAA.txt" "w"))
; ************************************************** *****************************
(setq ptx1 (rtos (car pt1) 2 4)
       pty1 (rtos (cadr pt1) 2 4)
       ptz1 (rtos (caddr pt1) 2 4))
       xyz1 (strcat ptx1 ", " pty1 ", " ptz1)
     )
; ************************************************** *****************************
(write-line xyz1 f)

 

Regards,

 

Tharwat

Link to comment
Share on other sites

Example:

 

(defun c:test ( / ent p1 p2 file  )

 (if (and (setq ent  (car (entsel)))
          (setq p1   (cdr (assoc 10 (entget ent)))
                p2   (cdr (assoc 11 (entget ent))))
          (setq file (open "C:\\Documents and Settings\\Marco\\Mis documentos\\AAA.txt" "w")))
   (progn
     (write-line (LM:lst->str (mapcar '(lambda ( x ) (rtos x 2 4)) p1) ",") file)
     (close file)
   )
 )

 (princ)
)

;;-------------------=={ List to String }==-------------------;;
;;                                                            ;;
;;  Constructs a string from a list of strings separating     ;;
;;  each element by a specified delimiter                     ;;
;;------------------------------------------------------------;;
;;  Author: Lee McDonnell, 2010                               ;;
;;                                                            ;;
;;  Copyright © 2010 by Lee McDonnell, All Rights Reserved.   ;;
;;  Contact: Lee Mac @ TheSwamp.org, CADTutor.net             ;;
;;------------------------------------------------------------;;
;;  Arguments:                                                ;;
;;  lst - a list of strings to process                        ;;
;;  del - delimiter by which to separate each list element    ;;
;;------------------------------------------------------------;;
;;  Returns:  String containing each string in the list       ;;
;;------------------------------------------------------------;;

(defun LM:lst->str ( lst del )
 ;; © Lee Mac 2010
 (if (cdr lst)
   (strcat (car lst) del (LM:lst->str (cdr lst) del))
   (car lst)
 )
)

Link to comment
Share on other sites

Example:

 

(defun c:test ( / ent p1 p2 file  )

 (if (and (setq ent  (car (entsel)))
          (setq p1   (cdr (assoc 10 (entget ent)))
                p2   (cdr (assoc 11 (entget ent))))
          (setq file (open "C:\\Documents and Settings\\Marco\\Mis documentos\\AAA.txt" "w")))
   (progn
     (write-line (LM:lst->str (mapcar '(lambda ( x ) (rtos x 2 4)) p1) ",") file)
     (close file)
   )
 )

 (princ)
)

;;-------------------=={ List to String }==-------------------;;
;;                                                            ;;
;;  Constructs a string from a list of strings separating     ;;
;;  each element by a specified delimiter                     ;;
;;------------------------------------------------------------;;
;;  Author: Lee McDonnell, 2010                               ;;
;;                                                            ;;
;;  Copyright © 2010 by Lee McDonnell, All Rights Reserved.   ;;
;;  Contact: Lee Mac @ TheSwamp.org, CADTutor.net             ;;
;;------------------------------------------------------------;;
;;  Arguments:                                                ;;
;;  lst - a list of strings to process                        ;;
;;  del - delimiter by which to separate each list element    ;;
;;------------------------------------------------------------;;
;;  Returns:  String containing each string in the list       ;;
;;------------------------------------------------------------;;

(defun LM:lst->str ( lst del )
 ;; © Lee Mac 2010
 (if (cdr lst)
   (strcat (car lst) del (LM:lst->str (cdr lst) del))
   (car lst)
 )
)

 

 

Dear Mr.Lee,

 

Thanks again.

Actually my aim is to convert any type of list which may contains numbers,strings,list etc into string and vice versa too. I hope you can solve this. If not explain what are the problems you can expect?

 

Regards,

Muthu.

Link to comment
Share on other sites

In the context of your question

 

Try this

 


(vl-load-com)
(defun c:prueba4 ( / a b pt1 pt1-str f)
 (setq a       (entsel)
       b       (entget (car a))
       pt1     (cdr (assoc 10 b))
       pt1-str (vl-prin1-to-string pt1)
       f       (open "ppr.txt" "w")
 )
 (write-line pt1-str f)
 (close f)
 (princ)
)

Link to comment
Share on other sites

  
;;;; Developed by R.Muthu Kumar ;;;;;;;;
(defun lst-str (#list / fn file_descr a)
 (Setq fn (vl-filename-mktemp))
 (setq file_descr (open fn "w"))
 (prin1 #list file_descr)
 (close file_descr)
 (setq file_descr (open fn "r"))
 (Setq a (read (read-line file_descr)))
 (close file_descr)
 (vl-file-delete fn)
 a
)

Link to comment
Share on other sites

Dear friends,

After long discussion I found a way to convert any list into string and vice versa. I hope it will be useful to the people whoever searching for the same task.

;;;---------------------By R.Muthu Kumar-------------------------
;;;---------------------Date:10-08-2010--------------------------
(Setq pt1 (list (list 0 "10" 0) 5 "SP-1" "splice" (list 5 2)))
;;;To Convert into String
(setq str (vl-prin1-to-string pt1))
;;;To Convert the String into List again
(Setq #list (read str))

 

Regards,

R.Muthu KUmar

Link to comment
Share on other sites

icon1.gif

:)

Refer post #14

 

Dear Mr.kerry,

 

I didn't see the same fuction you used in your post also. Anyhow finaly i hope we got solution what i was looking for long time.

Can you find anyother way to convert a string into list?

 

Regards,

Muthu.

Link to comment
Share on other sites

If you keep in mind that READ will return only the first expression in the string then read will be fine.

 

for example

be carefull of something like this 'cause it does exactly what you tell it to do.

 


(setq a   (entsel)
     b   (entget (car a))
     pt1 (cdr (assoc 10 b))
)

(setq str ( strcat  (vl-prin1-to-string pt1) (vl-prin1-to-string a)))
(read str)

Link to comment
Share on other sites

Dear msasu,

Good one.

do u have anyother routine to conver the string into list?

 

Regards,

Muthu

 

@Muthu123, please check this post. Thank you!

 

Regards,

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