Results 1 to 5 of 5

Thread: Writing to a text file

  1. #1
    Member
    Join Date
    2006-12
    Posts
    7
    Login to Give a bone
    0

    Default Writing to a text file

    I have a question on how to format the data going into a text file.

    the code that I have so far.......

    Code:
    (defun bo_add_space (a b c)
     (if (= c 1) ;;add spaces after string
      (repeat (- b (strlen a)) 
       (setq a (strcat a " "))
      )
      (repeat (- b (strlen a)) 
       (setq a (strcat " " a))
      )
     )
    a
    )
    
         ;;buyout list created, now format and write to file
         (setq bo1 (strcat (getvar "dwgprefix")(getvar "dwgname"))
               bo2 (get_date) ;;current system date
               bo3 (dos_file bo1) ;;file information
               bo4 (get_project_info) ;;project information
               bo5 (strcat (getvar "dwgprefix") "BUYOUT" (substr bo2 1 2)(substr bo2 4 2)(substr bo2 7 2) ".txt") ;;output file
         )
         (dos_delete bo5) ;;if file already exists, delete it
         (if (setq f1 (open bo5 "w"))
          (progn
           (write-line "Buyout Generator Results" f1)
           (write-line (strcat "Generated: " bo2) f1)
           (write-line (strcat "Drawing File: " bo1) f1)
           (write-line (strcat "Buyout File Name: " bo5) f1)
           (write-line (strcat (nth 0 bo4) " " (nth 1 bo4)) f1) ;;project name
           (write-line (nth 2 bo4) f1) ;;location
           (write-line "" f1)
           (write-line (strcat "CAD Engineer: " (nth 7 bo4)) f1)
           (write-line "" f1)
           (write-line sta f1)
           (write-line "" f1)
           (write-line "QTY      Part#      Description" f1)
           (foreach x a1
            (if (= (type (cadr x)) 'INT)
             (write-line (strcat (bo_add_space (itoa (cadr x)) 7 1) "  " (car x) "    " (nth 2 x)) f1)
             (write-line (strcat (bo_add_space (rtos (cadr x) 2 1) 7 1) "  " (car x) "    " (nth 2 x)) f1)
            )
           )
           (write-line "" f1)
           (write-line "Cabinets With Assumed Buyouts That Do Not Have Part Numbers Assigned:" f1)
           (write-line "" f1)
           (write-line "Cabinet:         Cabinet Number(s):" f1)
           (foreach x no_bo
            (write-line (strcat (car x) "   " (cadr x)) f1)
           )
           (write-line "" f1)
           (write-line "< END OF REPORT >" f1)
           (setq f1 (close f1))
           (princ "Done.")
           (dos_shellexe "notepad.exe" bo5)
          )
          (alert "Cannot Open Output File!")
         )
    The result that I am getting:
    STATUS: FINAL

    QTY Part# Description
    30 2-02027 WHT/2-02035 BLK 12 3/4 MUSIC CABINET SHELF LINER 31 DEEP
    30 2-05323 12 1/8W X 15 5/8H WIRE GRILL DR
    30 2-05457 MUSIC DOOR NEW WIDE BODY HASP PLATE

    Cabinets With Assumed Buyouts That Do Not Have Part Numbers Assigned:

    Cabinet: Cabinet Number(s):

    < END OF REPORT >

    My part numbers vary from 7-23 characters. How do I get each line of the description to line up eventhough the part number length varies?

    Thanks for any help you might have!
    Ed
    Last edited by Opie; 2007-02-19 at 08:32 PM. Reason: [CODE] tags added

  2. #2
    AUGI Addict kennet.sjoberg's Avatar
    Join Date
    2002-05
    Posts
    1,707
    Login to Give a bone
    0

    Default Re: Writing to a text file

    Quote Originally Posted by ewuensch
    . . .My part numbers vary from 7-23 characters. How do I get each line of the description to line up eventhough the part number length varies?
    Assign say 30 chr to one column, and if the item is only 13 chrs then add 17 spaces in_front
    Code:
    (setq Item "13defgjyfsert" )
    (setq ColW 30 )
    (setq in_front "" )
    (setq Item (strcat (repeat (- ColW (strlen Item )) (setq in_front (strcat in_front " " ))) Item ))
    
    = "                 13defgjyfsert"

    : ) Happy Computing !

    kennet
    Last edited by kennet.sjoberg; 2007-02-19 at 11:00 PM.

  3. #3
    Active Member David.Hoole's Avatar
    Join Date
    2000-12
    Location
    Yorkshire
    Posts
    84
    Login to Give a bone
    0

    Default Re: Writing to a text file

    Ed

    Another option you might want to consider is to output your data in comma delimited format. Just place a comma between each item on each line.
    The resulting file can be opened directly in Excel.

  4. #4
    I could stop if I wanted to
    Join Date
    2015-08
    Posts
    263
    Login to Give a bone
    0

    Default Re: Writing to a text file

    Quote Originally Posted by ewuensch
    Code:
    (write-line "QTY Part# Description" f1)
    (foreach x a1
    (if (= (type (cadr x)) 'INT)
    (write-line (strcat (bo_add_space (itoa (cadr x)) 7 1) " " (car x) " " (nth 2 x)) f1)
    (write-line (strcat (bo_add_space (rtos (cadr x) 2 1) 7 1) " " (car x) " " (nth 2 x)) f1)
    )
    Ed,

    I would use tabs instead of spaces. So when I open the text file in Excel, it will be splitted into columns and easy to manipulate.

    Try this:

    Code:
    (write-line  (strcat "QTY" "\t" "Part#" "\t" "Description")  f1)
    (foreach x a1
    (if (= (type (cadr x)) 'INT)
      (write-line (strcat (itoa (cadr x)) "\t" (car x) "\t" (nth 2 x)) f1  )
      (write-line (strcat (rtos (cadr x) 2 1) "\t" (car x) "\t" (nth 2 x)) f1  )
    )
    Regards,

    Abdul Huck

  5. #5
    Member
    Join Date
    2006-12
    Posts
    7
    Login to Give a bone
    0

    Default Re: Writing to a text file

    Thanks for all the help everyone. I never thought about using tabs, and that was the easiest solution!! This is a great place to ask questions, and to get answers.

Similar Threads

  1. 2013: Over writing the level with text
    By bwilliams133 in forum Revit Architecture - General
    Replies: 2
    Last Post: 2013-08-13, 07:13 AM
  2. Writing dwg's to a zip file.
    By kdayman in forum VBA/COM Interop
    Replies: 14
    Last Post: 2008-08-12, 06:13 PM
  3. Writing a DXF file
    By meyekj in forum AutoLISP
    Replies: 5
    Last Post: 2008-06-13, 05:34 AM
  4. Replies: 23
    Last Post: 2007-02-06, 11:30 PM
  5. Replies: 1
    Last Post: 2005-08-13, 04:37 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •