EXPORTING TEXT (txtout.lsp)

Sometimes you need to grab some text in a drawing and dump it into a word processor. The usual Windows cut and paste will work but it looks as it did in the drawing and you cannot edit it in any way. Txtout.lsp is a program that will prompt you for a file name and some lines of text that will then be dumped into the file in ASCII form, easily editable by a word processor for appearance and/or content. You can select either Text or Mtext. You can even drop it back into a drawing using the Mtext command or R12’s Asctext Lisp program.

The listing

After the program description, the program’s name is defined and variable names are localized. The user is then prompted to supply a file name which will be stored in variable FN2. An IF function is then started which uses an attempt to open the file as its test-expression. If the attempt was successful, the IF is passed a non-nil value and its then-expression, grouped by a PROGN, will run. This will close the file and initialize allowable keyword responses for the following prompt which asks the user to overwrite the file, append to it or cancel the entire operation.

This IF is then closed and a new IF started. This one checks to see if the user responded with "Cancel". If so, the program’s then-expression will run, confirming that the user is cancelling the operation. Otherwise the IF’s else-expression will run. Note how the earlier INITGET allowed <ENTER> as a response and the prompt following had the letter "O" in upper case in the options box, indicating that "Overwrite" was the default. By testing for "Cancel", we can allow any other response to invoke the IF’s else-expression. By nesting a further IF inside this else-expression which tests for the "Append" response, we effectively make "Overwrite" the default.

The "Append" IF’s then-expression opens the user specified file for appending and then displays a message on the command line informing the user that the file is ready for appending. The else-expression opens the file for writing and informs the user of this. The file descriptor is stored in variable FD2. File descriptors are a bit like object (or entity) names; they’re just a series of numbers and letters. The OPEN function’s "w" option will either open a new file or overwrite an existing one. If the file did not exist, the program would have jumped straight into this else-expression since the user would not even have been prompted to overwrite, append or cancel, effectively accepting the "Overwrite" default.

The else-expression and the IF are then closed off and we continue the else-expression for the IF which checked for KWD2 being "Cancel" where the user is prompted to pick text objects to export. The next line starts a multiple SETQ. This starts with the SSGET function using a filter list to accept only text & mtext objects which are then stored in variable SS2. It continues by initializing variable CTR2 to zero and finding the number of objects in selection set SS2 and storing this in variable SSQTY2.

The SETQ is closed and we move into a REPEAT loop which will repeat once for each object in selection set SS2. This starts by extracting the object data from the CTR2’th object in SS2 and storing it in variable OBJD2. This uses some nested code, first extracting the object name with the SSNAME function, passing it back to the ENTGET function which extracts the object data which is then passed back to the SETQ function. Note that SSNAME counts from zero. When the counting variable CTR2 is 0, the name of the first object is extracted; when it is 1, the second is extracted and so on. The textual information of the text object is then extracted and stored in variable OBTXT2, and the counter variable CTR2 is incremented by 1.

The text in OBTXT2 is written as one line into the file. If there are any more objects in the selection set, the REPEAT will then loop around and run again.

Once all the lines have been written to the file, the file is closed. As a nice little touch, I have included a few lines of code that will print a message to the command line informing the user of how many lines were written or appended to the file. The PROGN and IF are closed and the PRINC function is used to prevent nil being printed on the screen as the program exits.

©1996-2001 ZOTO Technologies