Author Topic: write-line  (Read 9925 times)

0 Members and 1 Guest are viewing this topic.

rude dog

  • Guest
write-line
« on: January 30, 2004, 11:56:54 AM »
How would you accomplish writing a setq'd value (A1) to a file
(defun c:WT (\ A1 LOG)
(setq A1 '(HERE IT IS!))
(setq LOG (open "RC-WT.TXT" "w"))
 (write-line A1 LOG)
 (close LOG)
)
(PRINC)

daron

  • Guest
write-line
« Reply #1 on: January 30, 2004, 12:23:00 PM »
Code: [Select]
(defun c:WT (\ A1 a) ;|LOG is a bad variable. It's a function and you will not get the desired results.|;
     (setq A1 "HERE IT IS!");This will create a list of three empty items, HERE, IT & IS
     (setq a (open "RC-WT.TXT" "w"));again, LOG
     (write-line A1 a);Change LOG
     (close a);same
)

rude dog

  • Guest
write-line
« Reply #2 on: January 30, 2004, 12:38:26 PM »
didnt know...again :x ....thank you :D

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
write-line
« Reply #3 on: January 30, 2004, 02:46:18 PM »
Just remember every thing that goes to the open file must be a string.
Example:
Code: [Select]

Command: (setq fo (open "c:/str_test.txt" "w"))
#<file "c:/str_test.txt">

Command: (setq test (list 1 2 3))
(1 2 3)

Command: (write-line test fo) <-bad because 'test' is a list
; error: bad argument type: stringp (1 2 3)

Command: (write-line (vl-princ-to-string test) fo) <- good converted to a string
"(1 2 3)"

Command: (setq test (list "a" "b" "c"))
("a" "b" "c")

Command: (write-line test fo) <- bad it's still a list
; error: bad argument type: stringp ("a" "b" "c")

; write each item in the list to the open file since it is a string
Command: (foreach item test (write-line item fo)) <-good

; testing for a string
Command: (setq test "this is a string")
"this is a string"

(if (= (type test) 'STR)
  (prompt "Yes it's a string")
  (prompt "No it's not")
  )
(princ)
Yes it's a string

Command: (setq test 123)
123

(if (= (type test) 'STR)
  (prompt "Yes it's a string")
  (prompt "No it's not")
  )
(princ)
No it's not
TheSwamp.org  (serving the CAD community since 2003)

Craig

  • Guest
write-line
« Reply #4 on: January 30, 2004, 02:55:55 PM »
Also keep in mind when you use (open "RC-WT.TXT" "w") and want to write to the same file the "w" switch will overwrite the original file. To add to a file use the "a" switch.
Quote
r Open for reading
w Open for writing. If filename does not exist, a new file is created and opened. If filename already exists, its existing data is overwritten. Data passed to an open file is not actually written until the file is closed with the close function.
a Open for appending. If filename does not exist, a new file is created and opened. If filename already exists, it is opened and the pointer is positioned at the end of the existing data, so new data you write to the file is appended to the existing data.


daron

  • Guest
write-line
« Reply #5 on: January 30, 2004, 03:01:04 PM »
I was going to suggest that. If you do use append, you'll notice everything is on the same line. Use "\n" at the end or beginning of your code to move to the next line.

Anonymous

  • Guest
write-line
« Reply #6 on: January 31, 2004, 02:43:15 AM »
Mark

What a hand...excellent example..I really wanted to reply
to your newbie challenge (I think Im the only one) but I have been totally engulfed in a program im trying to put together I feel inclined to post what I have done on it so far because of the knowledge you share....." (vl-princ-to-string)" what tongue is this command...havent seen it in the book


(defun c:KLT (/ ES LG)
(princ "\nPick layer to keep thawed <Enter to exit>:")
(while
(setq ES (car (entsel)))
(setq ES (entget ES))
(setq EL (cdr (assoc 8 ES)))
(setq EL (strcat EL","))
(setq LG (append LG (LIST EL)))
(setq CS (vl-princ-to-string LG))
(princ "\n Layers to remain thawed:")
(prin1 CS)
)
(setq LL CS)
(setq LAY (open "c:/LAY-INFO.TXT" "w"))                      
(write-line LL LAY)
(close LAY)
(princ)
)

ultimate goal is to strip parenthisis and space from string extracted by the routine (ex.) (DIM, CENTER, WRK,) (TO:) DIM,CENTER,WRK,
so I can call these names from txt file back to command prompt for "thawing"......routine will freeze every layer except "0" go back and thaw these "extracted layer names" from txt file. do some collision checking and then thaw "*"

rude dog

  • Guest
write-line
« Reply #7 on: January 31, 2004, 02:45:51 AM »
:oops: @###@**#@##Last post was from Rude Dog ....very tired

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
write-line
« Reply #8 on: January 31, 2004, 11:02:23 AM »
Now that I understand what you are doing. :D Take a look at the following code. You could add a lot more error checking to this but let's take it one step at a time.
Code: [Select]

(defun c:KLT (/ str ent lay_name LAY)

  ;; create empty string
  (setq str "")

  (while
    ;; loop while selection continues
    (setq ent (car (entsel "\nSelect an item <enter to exit>: ")))

    ;; extact layer name from selected entity
    (setq lay_name (cdr (assoc 8 (entget ent))))

    ;; show the user which layer they picked
    (prompt lay_name)

    ;; create one long string of layer names
    (setq str (strcat str (strcat lay_name ",")))

    ) ; end of loop

  ;; we need to make sure 'str' is not empty
  (if (not (= str ""))
    (progn
      ;; remove the last ',' from the string
      (setq str (substr str 1 (1- (strlen str))))
      (setq LAY (open "C:/LAY-INFO.TXT" "w"))
      (write-line str LAY)
      (close LAY)
      )
    )
  (princ)
  )
TheSwamp.org  (serving the CAD community since 2003)

rude dog

  • Guest
write-line
« Reply #9 on: February 01, 2004, 11:10:06 AM »
Mark,
How do you improve on perfection.....its not gonna happin this is beautiful...
 ;; create one long string of layer names
    (setq str (strcat str (strcat lay_name ",")))  :shock:
so small yet so resourceful....the last comma on the last layer name would not have hurt anything but why not take it out and have a flawess routine
I dont know what else to say dude...
Thanks,
Rudy

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
write-line
« Reply #10 on: February 01, 2004, 11:15:06 AM »
Does it all make sense or did I loose you on some of it?
TheSwamp.org  (serving the CAD community since 2003)

rude dog

  • Guest
write-line
« Reply #11 on: February 01, 2004, 11:36:44 AM »
I believe i got it....I'll finish the rest during the week some time..very busy with kids, work, family and of course super bowl  :)

rude dog

  • Guest
write-line
« Reply #12 on: February 01, 2004, 11:40:12 AM »
When I get the rest done I'll post it for critiquing

rude dog

  • Guest
write-line
« Reply #13 on: February 01, 2004, 06:12:51 PM »
Spoke too soon...there is sumthin' I do not understand I didnt understand why the "if" statement and the "progn" statement where necessary in this program....I omitted them and it still seems to work fine...Are they in there because its just "good programming habits" that you have, that I dont understand or does omitting them sacrafice the integrity of the routine?
I hope this is not coming across wrong dude, just curious :wink:



(defun c:LTC (/ str ent lay_name LAY)

  ;; create empty string
  (setq str "")

  (while
    ;; loop while selection continues
    (setq ent (car (entsel "\nSelect an item <enter to exit>: ")))

    ;; extact layer name from selected entity
    (setq lay_name (cdr (assoc 8 (entget ent))))

    ;; show the user which layer they picked
    (prompt lay_name)

    ;; create one long string of layer names
    (setq str (strcat str (strcat lay_name ",")))

    ) ; end of loop

  ;; we need to make sure 'str' is not empty
  ;(if (not (= str ""))
    ;(progn
      ;; remove the last ',' from the string
      (setq str (substr str 1 (1- (strlen str))))
      (setq LAY (open "C:/LAY-INFO.TXT" "w"))
      (write-line str LAY)
      (close LAY)
      ;)
    ;)
  (princ)
  )

Matt Stachoni

  • Guest
write-line
« Reply #14 on: February 01, 2004, 07:06:48 PM »
Can I ask why you want to write layer names to a file in such a manner?