Author Topic: Text to a File  (Read 3003 times)

0 Members and 1 Guest are viewing this topic.

vinnyg

  • Guest
Text to a File
« on: December 05, 2005, 12:01:24 PM »
Hi all,

How can I pick text in a drawing to write to a file? I tried "txtout" (see code
below). It works fine except it won't select text that is a block.

; WRITE TEXT FROM A DRAWING TO AN ASCII FILE IN ORDER OF SELECTION
;;****************************************************************************
;                       TXTOUT.LSP V1.0 by Zoltan Toth
;    ZOTO Technologies,
;    23 Greenhills Dve,
;    Melton 3337.
;    E-MAIL: zoltan.toth@ains.net.au
;       WWW: http://www.ains.net.au/zoto/
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;   This program will write text in a drawing to a nominated ASCII file.
;   The lines will be written in order of selection and it is best to pick
;   each line of text individually as selection by WINDOW, CROSSING etc.
;   may result in lines being selected in an undesirable order. In the
;   event that the nominated file already exists, the user has the option
;   to overwrite or append to the existing file, or cancel the operation
;   and start again. The ASCII file created by TXTOUT can then be ported
;   to a text editor or word processor for editing and/or printing. It can
;   also be read back into the drawing as MTEXT.
;;****************************************************************************
;define program name and localize variables
(defun C:TXTOUT (/ FN2 FD2 KWD2 SS2 CTR2 SSQTY2 OBJD2 OBTXT2)
 (setq FN2 (getstring "\nText export file name: "))               ;get file name
 (if (setq FD2 (open FN2 "r"))                             ;if file can be found
  (progn
   (close FD2)                                                       ;close file
   (initget "Overwrite Append Cancel")            ;initialize allowable keywords
   (setq KWD2 (getkword "\nFile exists! Overwrite, Append or Cancel <O/a/c>: "))
  )
 )
 (if (= "Cancel" KWD2)                              ;if user response was Cancel
  (prompt "\nOperation cancelled at user's request.")         ;prompt to confirm
  (progn
   (if (= "Append" KWD2)                            ;if user response was Append
    (progn
     (setq FD2 (open FN2 "a"))                          ;open file for appending
     (prompt (strcat "\nFile \"" FN2 "\" now ready for appending."));and confirm
    )
    (progn
     (setq FD2 (open FN2 "w"))                 ;else open file for (over)writing
     (prompt (strcat "\nFile \"" FN2 "\" now open."))               ;and confirm
    )
   )
   (prompt "\nPick text objects to export: ")             ;prompt to select text
;select only TEXT & MTEXT
   (setq SS2 (ssget '((-4 . "<OR")(0 . "TEXT")(0 . "MTEXT")(-4 . "OR>")))
         CTR2 0                                 ;initialize counter CTR2 to zero
         SSQTY2 (sslength SS2)                       ;find size of selection set
   )
   (repeat SSQTY2                                        ;repeat for each object
    (setq OBJD2 (entget (ssname SS2 CTR2))                  ;extract object data
          OBTXT2 (cdr (assoc 1 OBJD2))                     ;extract string value
          CTR2 (1+ CTR2)                                      ;increment counter
    )
    (write-line OBTXT2 FD2)                                ;write string to file
   )                                                                 ;end REPEAT
   (close FD2)                                                       ;close file
   (if (= "Append" KWD2)                                           ;\inform user
    (prompt (strcat "\n" (itoa SSQTY2) " lines appended to file.")); >of number
    (prompt (strcat "\n" (itoa SSQTY2) " lines written to file.")) ;/ of lines
   )
  )                                                                   ;end PROGN
 )                                                   ;close "(if (= "Append"..."
 (princ)                                                           ;exit quietly
)


Any ideas folks?

TIA

vinnyg

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Text to a File
« Reply #1 on: December 05, 2005, 12:06:31 PM »
If you want text that is in a block, are you talking about attributes, or text objects defined within the block definition?
If text in the block definition, then you can grab it through the block reference in the block collection.
If you want the attributes, there are two methods that I know of:
1. Pick each attribute with nentsel
2. Grab the block, and cycle through the block and get all the attributes

Once you have chosen the way you want to go, just write them to the file the same way the code you provided does.

Hope that helps.
Tim
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

vinnyg

  • Guest
Re: Text to a File
« Reply #2 on: December 05, 2005, 01:28:35 PM »
I want to pick text in a block definition. So exactly what steps do I take
to "grab it through the block reference in the block collection"?
Revise the code?  How?

vinnyg

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Text to a File
« Reply #3 on: December 05, 2005, 01:35:04 PM »
You will have to change the ssget line so you can select blocks.  Then as you step through the selection set, you will want to check if it is a block, and if it is, get the name of the block, then get the block object from the block collection.  Then you will step through the block definition to find the text/mtext, and then you can write that to you text file.

Do you need help codeing? or is this enough to get you started?

Tim
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

vinnyg

  • Guest
Re: Text to a File
« Reply #4 on: December 05, 2005, 03:05:22 PM »
Well, since I'm not very experienced at coding, I'll probably need help with it
if and when you have time. Thanks!  Don't put off more important things
you have to attend to first.

vinnyg

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Text to a File
« Reply #5 on: December 05, 2005, 03:30:09 PM »
This worked for me.
Code: [Select]
; WRITE TEXT FROM A DRAWING TO AN ASCII FILE IN ORDER OF SELECTION
;;****************************************************************************
;                       TXTOUT.LSP V1.0 by Zoltan Toth
;    ZOTO Technologies,
;    23 Greenhills Dve,
;    Melton 3337.
;    E-MAIL: zoltan.toth@ains.net.au
;       WWW: http://www.ains.net.au/zoto/
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;   This program will write text in a drawing to a nominated ASCII file.
;   The lines will be written in order of selection and it is best to pick
;   each line of text individually as selection by WINDOW, CROSSING etc.
;   may result in lines being selected in an undesirable order. In the
;   event that the nominated file already exists, the user has the option
;   to overwrite or append to the existing file, or cancel the operation
;   and start again. The ASCII file created by TXTOUT can then be ported
;   to a text editor or word processor for editing and/or printing. It can
;   also be read back into the drawing as MTEXT.
;;****************************************************************************
;define program name and localize variables
; Modified by Tim Willey to allow block selection, and add text/mtext that exist
;  in the block defination will be added to the text file.
(defun C:TXTOUT (/ FN2 FD2 KWD2 SS2 CTR2 SSQTY2 OBJD2 OBTXT2 BlkCol)
 (vl-load-com)  ; added by TMW
 (setq BlkCol (vla-get-Blocks (vla-get-ActiveDocument (vlax-get-Acad-Object))))  ; added by TMW
 (setq FN2 (getstring "\nText export file name: "))               ;get file name
 (if (setq FD2 (open FN2 "r"))                             ;if file can be found
  (progn
   (close FD2)                                                       ;close file
   (initget "Overwrite Append Cancel")            ;initialize allowable keywords
   (setq KWD2 (getkword "\nFile exists! Overwrite, Append or Cancel <O/a/c>: "))
  )
 )
 (if (= "Cancel" KWD2)                              ;if user response was Cancel
  (prompt "\nOperation cancelled at user's request.")         ;prompt to confirm
  (progn
   (if (= "Append" KWD2)                            ;if user response was Append
    (progn
     (setq FD2 (open FN2 "a"))                          ;open file for appending
     (prompt (strcat "\nFile \"" FN2 "\" now ready for appending."));and confirm
    )
    (progn
     (setq FD2 (open FN2 "w"))                 ;else open file for (over)writing
     (prompt (strcat "\nFile \"" FN2 "\" now open."))               ;and confirm
    )
   )
   (prompt "\nPick text objects to export: ")             ;prompt to select text
;select only TEXT & MTEXT
   (setq SS2 (ssget '((0 . "TEXT,MTEXT,INSERT")))  ; added by TMW
         CTR2 0                                 ;initialize counter CTR2 to zero
         SSQTY2 (sslength SS2)                       ;find size of selection set
   )
   (repeat SSQTY2                                        ;repeat for each object
    (setq OBJD2 (entget (ssname SS2 CTR2)))                  ;extract object data
    (if (= (cdr (assoc 0 OBJD2)) "INSERT")  ; added by TMW
     (vlax-for i (vla-item BlkCol (cdr (assoc 2 OBJD2)))  ; added by TMW
      (if  ; added by TMW
       (or  ; added by TMW
        (= (vla-get-ObjectName i) "AcDbText")  ; added by TMW
        (= (vla-get-ObjectName i) "AcDbMText")  ; added by TMW
       )  ; added by TMW
       (write-line (vla-get-TextString i) FD2)  ; added by TMW
      )  ; added by TMW
     )  ; added by TMW
     (write-line (vla-get-TextString (vlax-ename->vla-object (cdr (assoc -1 OBJD2)))) FD2)  ; added by TMW
    )  ; added by TMW
    (setq CTR2 (1+ CTR2))                                      ;increment counter
   )                                                                 ;end REPEAT
   (close FD2)                                                       ;close file
   (if (= "Append" KWD2)                                           ;\inform user
    (prompt (strcat "\n" (itoa SSQTY2) " lines appended to file.")); >of number
    (prompt (strcat "\n" (itoa SSQTY2) " lines written to file.")) ;/ of lines
   )
  )                                                                   ;end PROGN
 )                                                   ;close "(if (= "Append"..."
 (princ)                                                           ;exit quietly
)

I maked the lines I changed/added with "   ; added by TMW " at the end.

Tim
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

vinnyg

  • Guest
Re: Text to a File
« Reply #6 on: December 05, 2005, 03:39:31 PM »

Thanks......haven't tried it yet but will let you know how it works
when I do. Thanks again,


vinnyg

vinnyg

  • Guest
Re: Text to a File
« Reply #7 on: December 05, 2005, 05:02:33 PM »
Maybe you should just give up on me! I tried it and it didn't work.....but
that's only because I was asleep at the wheel earlier!  The text I want to grab
is attributes after all. jeeeessss!  Hit me over the head with a rock or??

Anyway, it's not your fault it dosen't work.....mine! Actually it does work
for the text only part, not the attributes.

Thanks,

vinnyg

T.Willey

  • Needs a day job
  • Posts: 5251
Re: Text to a File
« Reply #8 on: December 05, 2005, 05:12:38 PM »
I didn't test this one, but it should work.
Code: [Select]
; WRITE TEXT FROM A DRAWING TO AN ASCII FILE IN ORDER OF SELECTION
;;****************************************************************************
;                       TXTOUT.LSP V1.0 by Zoltan Toth
;    ZOTO Technologies,
;    23 Greenhills Dve,
;    Melton 3337.
;    E-MAIL: zoltan.toth@ains.net.au
;       WWW: http://www.ains.net.au/zoto/
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;   This program will write text in a drawing to a nominated ASCII file.
;   The lines will be written in order of selection and it is best to pick
;   each line of text individually as selection by WINDOW, CROSSING etc.
;   may result in lines being selected in an undesirable order. In the
;   event that the nominated file already exists, the user has the option
;   to overwrite or append to the existing file, or cancel the operation
;   and start again. The ASCII file created by TXTOUT can then be ported
;   to a text editor or word processor for editing and/or printing. It can
;   also be read back into the drawing as MTEXT.
;;****************************************************************************
;define program name and localize variables
; Modified by Tim Willey to allow block selection, and add attributes info to text file.
(defun C:TXTOUT (/ FN2 FD2 KWD2 SS2 CTR2 SSQTY2 OBJD2 OBTXT2 AttList)
 (vl-load-com)
 (setq FN2 (getstring "\nText export file name: "))               ;get file name
 (if (setq FD2 (open FN2 "r"))                             ;if file can be found
  (progn
   (close FD2)                                                       ;close file
   (initget "Overwrite Append Cancel")            ;initialize allowable keywords
   (setq KWD2 (getkword "\nFile exists! Overwrite, Append or Cancel <O/a/c>: "))
  )
 )
 (if (= "Cancel" KWD2)                              ;if user response was Cancel
  (prompt "\nOperation cancelled at user's request.")         ;prompt to confirm
  (progn
   (if (= "Append" KWD2)                            ;if user response was Append
    (progn
     (setq FD2 (open FN2 "a"))                          ;open file for appending
     (prompt (strcat "\nFile \"" FN2 "\" now ready for appending."));and confirm
    )
    (progn
     (setq FD2 (open FN2 "w"))                 ;else open file for (over)writing
     (prompt (strcat "\nFile \"" FN2 "\" now open."))               ;and confirm
    )
   )
   (prompt "\nPick text objects to export: ")             ;prompt to select text
;select only TEXT & MTEXT
   (setq SS2 (ssget '((0 . "TEXT,MTEXT,INSERT")))  ; added by TMW
         CTR2 0                                 ;initialize counter CTR2 to zero
         SSQTY2 (sslength SS2)                       ;find size of selection set
   )
   (repeat SSQTY2                                        ;repeat for each object
    (setq OBJD2 (entget (ssname SS2 CTR2)))                  ;extract object data
    (if (= (cdr (assoc 0 OBJD2)) "INSERT")  ; added by TMW
     (if (setq AttList (vlax-get (vlax-ename->vla-object (cdr (assoc -1 OBJD2))) 'GetAttributes))  ; added by TMW
      (foreach Att AttList  ; added by TMW  ; added by TMW
       (write-line (vla-get-TextString Att) FD2)  ; added by TMW
      )  ; added by TMW
     )  ; added by TMW
     (write-line (vla-get-TextString (vlax-ename->vla-object (cdr (assoc -1 OBJD2)))) FD2)  ; added by TMW
    )  ; added by TMW
    (setq CTR2 (1+ CTR2))                                      ;increment counter
   )                                                                 ;end REPEAT
   (close FD2)                                                       ;close file
   (if (= "Append" KWD2)                                           ;\inform user
    (prompt (strcat "\n" (itoa SSQTY2) " lines appended to file.")); >of number
    (prompt (strcat "\n" (itoa SSQTY2) " lines written to file.")) ;/ of lines
   )
  )                                                                   ;end PROGN
 )                                                   ;close "(if (= "Append"..."
 (princ)                                                           ;exit quietly
)

Tim
Tim

I don't want to ' end-up ', I want to ' become '. - Me

Please think about donating if this post helped you.

vinnyg

  • Guest
Re: Text to a File
« Reply #9 on: December 05, 2005, 05:44:28 PM »
Thanks again! I'll give it a try.

vinnyg